Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 17a7451488 | |||
| 416bfc76fa |
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
Serialization helpers for the Gajumaru.
|
Serialization helpers for the Gajumaru.
|
||||||
|
|
||||||
For an overview of the static serializer, see [this document](doc/static.md).
|
|
||||||
|
|
||||||
## Build
|
## Build
|
||||||
|
|
||||||
$ rebar3 compile
|
$ rebar3 compile
|
||||||
@@ -32,7 +30,6 @@ how the type information is represented. The fully serialized form is
|
|||||||
produced by the `serialize` functions.
|
produced by the `serialize` functions.
|
||||||
|
|
||||||
The basic types supported by the encoder are:
|
The basic types supported by the encoder are:
|
||||||
* `integer()` (`anyint`, code: 246)
|
|
||||||
* `neg_integer()` (`negint`, code: 247)
|
* `neg_integer()` (`negint`, code: 247)
|
||||||
* `non_neg_integer()` (`int` , code: 248)
|
* `non_neg_integer()` (`int` , code: 248)
|
||||||
* `binary()` (`binary`, code: 249)
|
* `binary()` (`binary`, code: 249)
|
||||||
@@ -43,9 +40,6 @@ The basic types supported by the encoder are:
|
|||||||
* `gmser_id:id()` (`id` , code: 254)
|
* `gmser_id:id()` (`id` , code: 254)
|
||||||
* `atom()` (`label` , code: 255)
|
* `atom()` (`label` , code: 255)
|
||||||
|
|
||||||
(The range of codes is chosen because the `gmser_chain_objects` codes
|
|
||||||
range from 10 to 200, and also to stay within 1 byte.)
|
|
||||||
|
|
||||||
When encoding `map` types, the map elements are first sorted.
|
When encoding `map` types, the map elements are first sorted.
|
||||||
|
|
||||||
When specifying a map type for template-driven encoding, use
|
When specifying a map type for template-driven encoding, use
|
||||||
@@ -128,17 +122,8 @@ to encode as each type in the list, in the specified order, until one matches.
|
|||||||
gmser_dyn:encode_typed(#{alt => [negint,int]}, 5) -> [<<0>>,<<1>>,[<<247>>,<<5>>]]
|
gmser_dyn:encode_typed(#{alt => [negint,int]}, 5) -> [<<0>>,<<1>>,[<<247>>,<<5>>]]
|
||||||
gmser_dyn:encode_typed(#{alt => [negint,int]}, 5) -> [<<0>>,<<1>>,[<<248>>,<<5>>]]
|
gmser_dyn:encode_typed(#{alt => [negint,int]}, 5) -> [<<0>>,<<1>>,[<<248>>,<<5>>]]
|
||||||
|
|
||||||
|
gmser_dyn:register_type(246, anyint, #{alt => [negint, int]})
|
||||||
|
|
||||||
gmser_dyn:encode_typed(anyint,-5) -> [<<0>>,<<1>>,[<<246>>,[<<247>>,<<5>>]]]
|
gmser_dyn:encode_typed(anyint,-5) -> [<<0>>,<<1>>,[<<246>>,[<<247>>,<<5>>]]]
|
||||||
gmser_dyn:encode_typed(anyint,5) -> [<<0>>,<<1>>,[<<246>>,[<<248>>,<<5>>]]]
|
gmser_dyn:encode_typed(anyint,5) -> [<<0>>,<<1>>,[<<246>>,[<<248>>,<<5>>]]]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Notes
|
|
||||||
|
|
||||||
Note that `anyint` is a standard type. The static serializer supports only
|
|
||||||
positive integers (`int`), as negative numbers are forbidden on-chain.
|
|
||||||
For dynamic encoding e.g. in messaging protocols, handling negative numbers can
|
|
||||||
be useful, so the `negint` type was added as a dynamic type. To encode a full-range
|
|
||||||
integer, the `alt` construct is needed.
|
|
||||||
|
|
||||||
(Floats are not supported, as they are non-deterministic. Rationals and fixed-point
|
|
||||||
numbers could easily be handled as high-level types, e.g. as `{int,int}`.)
|
|
||||||
|
|||||||
@@ -1,83 +0,0 @@
|
|||||||
|
|
||||||
# Static Serialization
|
|
||||||
|
|
||||||
The `gmserialization` and `gmser_chain_objects` modules implement the
|
|
||||||
static serialization support used in the Gajumaru blockchain.
|
|
||||||
|
|
||||||
The purpose is to produce fully deterministic serialization, in order
|
|
||||||
to maintain predictable hashing.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
```erlang
|
|
||||||
%% deterministic canonical serialization.
|
|
||||||
-spec serialize_to_binary(signed_tx()) -> binary_signed_tx().
|
|
||||||
serialize_to_binary(#signed_tx{tx = Tx, signatures = Sigs}) ->
|
|
||||||
gmser_chain_objects:serialize(
|
|
||||||
?SIG_TX_TYPE,
|
|
||||||
?SIG_TX_VSN,
|
|
||||||
serialization_template(?SIG_TX_VSN),
|
|
||||||
[ {signatures, lists:sort(Sigs)}
|
|
||||||
, {transaction, aetx:serialize_to_binary(Tx)}
|
|
||||||
]).
|
|
||||||
|
|
||||||
-spec deserialize_from_binary(binary()) -> signed_tx().
|
|
||||||
deserialize_from_binary(SignedTxBin) when is_binary(SignedTxBin) ->
|
|
||||||
[ {signatures, Sigs}
|
|
||||||
, {transaction, TxBin}
|
|
||||||
] = gmser_chain_objects:deserialize(
|
|
||||||
?SIG_TX_TYPE,
|
|
||||||
?SIG_TX_VSN,
|
|
||||||
serialization_template(?SIG_TX_VSN),
|
|
||||||
SignedTxBin),
|
|
||||||
assert_sigs_size(Sigs),
|
|
||||||
#signed_tx{ tx = aetx:deserialize_from_binary(TxBin)
|
|
||||||
, signatures = Sigs
|
|
||||||
}.
|
|
||||||
|
|
||||||
serialization_template(?SIG_TX_VSN) ->
|
|
||||||
[ {signatures, [binary]}
|
|
||||||
, {transaction, binary}
|
|
||||||
].
|
|
||||||
```
|
|
||||||
|
|
||||||
The terms that can be encoded using these templates are given by
|
|
||||||
this type in `gmserialization.erl`:
|
|
||||||
|
|
||||||
```erlang
|
|
||||||
-type encodable_term() :: non_neg_integer()
|
|
||||||
| binary()
|
|
||||||
| boolean()
|
|
||||||
| [encodable_term()] %% Of any length
|
|
||||||
| #{atom() => encodable_term()}
|
|
||||||
| tuple() %% Any arity, containing encodable_term().
|
|
||||||
| gmser_id:id().
|
|
||||||
```
|
|
||||||
|
|
||||||
The template 'language' is defined by these types:
|
|
||||||
|
|
||||||
```erlang
|
|
||||||
-type template() :: [{field_name(), type()}].
|
|
||||||
-type field_name() :: atom().
|
|
||||||
-type type() :: 'int'
|
|
||||||
| 'bool'
|
|
||||||
| 'binary'
|
|
||||||
| 'id' %% As defined in aec_id.erl
|
|
||||||
| [type()] %% Length one in the type. This means a list of any length.
|
|
||||||
| #{items := [{field_name(), type()}]} %% Record with named fields
|
|
||||||
%% represented as a map.
|
|
||||||
%% Encoded as a list in the given
|
|
||||||
%% order.
|
|
||||||
| tuple(). %% Any arity, containing type(). This means a static size array.
|
|
||||||
```
|
|
||||||
|
|
||||||
The `gmser_chain_objects.erl` module specifies a serialization code for each
|
|
||||||
object that can go on-chain. E.g.:
|
|
||||||
|
|
||||||
```erlang
|
|
||||||
tag(signed_tx) -> 11;
|
|
||||||
...
|
|
||||||
rev_tag(11) -> signed_tx;
|
|
||||||
```
|
|
||||||
|
|
||||||
The `tag` and `vsn` are laid out in the beginning of the serialized object.
|
|
||||||
+8
-12
@@ -86,8 +86,7 @@ decode_tag_and_vsn([TagBin, VsnBin, Fields]) ->
|
|||||||
dynamic_types() ->
|
dynamic_types() ->
|
||||||
#{ vsn => ?VSN
|
#{ vsn => ?VSN
|
||||||
, codes =>
|
, codes =>
|
||||||
#{ 246 => anyint
|
#{ 247 => negint
|
||||||
, 247 => negint
|
|
||||||
, 248 => int
|
, 248 => int
|
||||||
, 249 => binary
|
, 249 => binary
|
||||||
, 250 => bool
|
, 250 => bool
|
||||||
@@ -97,8 +96,7 @@ dynamic_types() ->
|
|||||||
, 254 => id
|
, 254 => id
|
||||||
, 255 => label}
|
, 255 => label}
|
||||||
, rev =>
|
, rev =>
|
||||||
#{ anyint => 246
|
#{ negint => 247
|
||||||
, negint => 247
|
|
||||||
, int => 248
|
, int => 248
|
||||||
, binary => 249
|
, binary => 249
|
||||||
, bool => 250
|
, bool => 250
|
||||||
@@ -110,8 +108,7 @@ dynamic_types() ->
|
|||||||
, labels => #{}
|
, labels => #{}
|
||||||
, rev_labels => #{}
|
, rev_labels => #{}
|
||||||
, templates =>
|
, templates =>
|
||||||
#{ anyint => #{alt => [negint, int]}
|
#{ negint => negint
|
||||||
, negint => negint
|
|
||||||
, int => int
|
, int => int
|
||||||
, binary => binary
|
, binary => binary
|
||||||
, bool => bool
|
, bool => bool
|
||||||
@@ -200,6 +197,11 @@ encode_typed_(Tag, Term, E, Vsn, #{templates := Ts, rev := Rev} = Types)
|
|||||||
encode_typed_(MaybeTemplate, Term, _, Vsn, Types) ->
|
encode_typed_(MaybeTemplate, Term, _, Vsn, Types) ->
|
||||||
encode_maybe_template(MaybeTemplate, Term, Vsn, Types).
|
encode_maybe_template(MaybeTemplate, Term, Vsn, Types).
|
||||||
|
|
||||||
|
maybe_emit(E, Code, Enc) when E > 0 ->
|
||||||
|
[encode_basic(int, Code), Enc];
|
||||||
|
maybe_emit(0, _, Enc) ->
|
||||||
|
Enc.
|
||||||
|
|
||||||
encode_maybe_template(#{items := _} = Type, Term, Vsn, Types) ->
|
encode_maybe_template(#{items := _} = Type, Term, Vsn, Types) ->
|
||||||
case is_map(Term) of
|
case is_map(Term) of
|
||||||
true ->
|
true ->
|
||||||
@@ -629,7 +631,6 @@ dynamic_types_test_() ->
|
|||||||
[ ?_test(revert_to_default_types())
|
[ ?_test(revert_to_default_types())
|
||||||
, ?_test(t_typed_map())
|
, ?_test(t_typed_map())
|
||||||
, ?_test(t_alts())
|
, ?_test(t_alts())
|
||||||
, ?_test(t_anyints())
|
|
||||||
].
|
].
|
||||||
|
|
||||||
t_round_trip(T) ->
|
t_round_trip(T) ->
|
||||||
@@ -733,9 +734,4 @@ t_alts() ->
|
|||||||
t_round_trip_typed(#{alt => [negint, int]}, 4),
|
t_round_trip_typed(#{alt => [negint, int]}, 4),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
t_anyints() ->
|
|
||||||
t_round_trip_typed(anyint, -5),
|
|
||||||
t_round_trip_typed(anyint, 5),
|
|
||||||
ok.
|
|
||||||
|
|
||||||
-endif.
|
-endif.
|
||||||
|
|||||||
Reference in New Issue
Block a user