From d60eb5eab5bcb7948498add78b8a61ca0aeabcfd Mon Sep 17 00:00:00 2001 From: Erik Stenman Date: Tue, 26 Feb 2019 08:05:34 +0100 Subject: [PATCH] Replace local rlp with aeserialization repo. Use ref till first release is available. --- rebar.config | 6 ++- rebar.lock | 10 ++++- src/aeb_fate_asm.erl | 18 ++++---- src/aeb_fate_encoding.erl | 24 +++++------ src/aeb_rlp.erl | 91 --------------------------------------- src/aebytecode.app.src | 1 + 6 files changed, 36 insertions(+), 114 deletions(-) delete mode 100644 src/aeb_rlp.erl diff --git a/rebar.config b/rebar.config index f27d50a..08234fd 100644 --- a/rebar.config +++ b/rebar.config @@ -3,10 +3,12 @@ {erl_opts, [debug_info]}. {deps, [ {eblake2, "1.0.0"} + , {aeserialization, {git, "https://github.com/aeternity/aeserialization.git", + {ref, "b55c372"}}} , {getopt, "1.0.1"} ]}. -{escript_incl_apps, [aebytecode, eblake2, getopt]}. +{escript_incl_apps, [aebytecode, eblake2, aeserialization, getopt]}. {escript_main_app, aebytecode}. {escript_name, aefateasm}. {escript_emu_args, "%%!"}. @@ -30,6 +32,8 @@ {profiles, [{binary, [ {deps, [ {eblake2, "1.0.0"} + , {aeserialization, {git, "https://github.com/aeternity/aeserialization.git", + {ref, "b55c372"}}} , {getopt, "1.0.1"} ]}, diff --git a/rebar.lock b/rebar.lock index ca74f91..ee15e69 100644 --- a/rebar.lock +++ b/rebar.lock @@ -1,5 +1,13 @@ {"1.1.0", -[{<<"eblake2">>,{pkg,<<"eblake2">>,<<"1.0.0">>},0}, +[{<<"aeserialization">>, + {git,"https://github.com/aeternity/aeserialization.git", + {ref,"b55c3726f4a21063721c68d6fa7fda39121edf11"}}, + 0}, + {<<"base58">>, + {git,"https://github.com/aeternity/erl-base58.git", + {ref,"60a335668a60328a29f9731b67c4a0e9e3d50ab6"}}, + 1}, + {<<"eblake2">>,{pkg,<<"eblake2">>,<<"1.0.0">>},0}, {<<"getopt">>,{pkg,<<"getopt">>,<<"1.0.1">>},0}]}. [ {pkg_hash,[ diff --git a/src/aeb_fate_asm.erl b/src/aeb_fate_asm.erl index b3ea0ea..1ea0bcb 100644 --- a/src/aeb_fate_asm.erl +++ b/src/aeb_fate_asm.erl @@ -350,10 +350,10 @@ asm_to_bytecode(AssemblerCode, Options) -> Signatures = serialize_sigs(Env), SymbolTable = serialize_symbol_table(Env), Annotatations = serialize_annotations(Env), - ByteCode = << (aeb_rlp:encode(list_to_binary(ByteList)))/binary, - (aeb_rlp:encode(list_to_binary(Signatures)))/binary, - (aeb_rlp:encode(SymbolTable))/binary, - (aeb_rlp:encode(Annotatations))/binary + ByteCode = << (aeser_rlp:encode(list_to_binary(ByteList)))/binary, + (aeser_rlp:encode(list_to_binary(Signatures)))/binary, + (aeser_rlp:encode(SymbolTable))/binary, + (aeser_rlp:encode(Annotatations))/binary >>, case proplists:lookup(pp_hex_string, Options) of @@ -366,14 +366,14 @@ asm_to_bytecode(AssemblerCode, Options) -> {Env, ByteCode}. strip(ByteCode) -> - {Code, _Rest} = aeb_rlp:decode_one(ByteCode), + {Code, _Rest} = aeser_rlp:decode_one(ByteCode), Code. bytecode_to_fate_code(Bytes, _Options) -> - {ByteCode, Rest1} = aeb_rlp:decode_one(Bytes), - {Signatures, Rest2} = aeb_rlp:decode_one(Rest1), - {SymbolTable, Rest3} = aeb_rlp:decode_one(Rest2), - {Annotations, <<>>} = aeb_rlp:decode_one(Rest3), + {ByteCode, Rest1} = aeser_rlp:decode_one(Bytes), + {Signatures, Rest2} = aeser_rlp:decode_one(Rest1), + {SymbolTable, Rest3} = aeser_rlp:decode_one(Rest2), + {Annotations, <<>>} = aeser_rlp:decode_one(Rest3), Env1 = deserialize(ByteCode, #{ function => none , bb => 0 diff --git a/src/aeb_fate_encoding.erl b/src/aeb_fate_encoding.erl index 4a5daa2..1fcbd9e 100644 --- a/src/aeb_fate_encoding.erl +++ b/src/aeb_fate_encoding.erl @@ -104,9 +104,9 @@ serialize(String) when ?IS_FATE_STRING(String), ?FATE_STRING_SIZE(String) > 0, ?FATE_STRING_SIZE(String) >= ?SHORT_STRING_SIZE -> Bytes = ?FATE_STRING_VALUE(String), - <>; + <>; serialize(?FATE_ADDRESS(Address)) when is_binary(Address) -> - <>; + <>; serialize(?FATE_TUPLE(T)) when size(T) > 0 -> S = size(T), L = tuple_to_list(T), @@ -148,7 +148,7 @@ serialize(?FATE_VARIANT(Size, Tag, Values)) when 0 =< Size %% ----------------------------------------------------- rlp_integer(S) when S >= 0 -> - aeb_rlp:encode(binary:encode_unsigned(S)). + aeser_rlp:encode(binary:encode_unsigned(S)). serialize_integer(I) when ?IS_FATE_INTEGER(I) -> V = ?FATE_INTEGER_VALUE(I), @@ -187,28 +187,28 @@ deserialize2(<>) -> deserialize2(<>) -> {?MAKE_FATE_INTEGER(-I), Rest}; deserialize2(<>) -> - {Bint, Rest2} = aeb_rlp:decode_one(Rest), + {Bint, Rest2} = aeser_rlp:decode_one(Rest), {?MAKE_FATE_INTEGER(-binary:decode_unsigned(Bint) - ?SMALL_INT_SIZE), Rest2}; deserialize2(<>) -> - {Bint, Rest2} = aeb_rlp:decode_one(Rest), + {Bint, Rest2} = aeser_rlp:decode_one(Rest), {?MAKE_FATE_INTEGER(binary:decode_unsigned(Bint) + ?SMALL_INT_SIZE), Rest2}; deserialize2(<>) -> - {Bint, Rest2} = aeb_rlp:decode_one(Rest), + {Bint, Rest2} = aeser_rlp:decode_one(Rest), {?FATE_BITS(-binary:decode_unsigned(Bint)), Rest2}; deserialize2(<>) -> - {Bint, Rest2} = aeb_rlp:decode_one(Rest), + {Bint, Rest2} = aeser_rlp:decode_one(Rest), {?FATE_BITS(binary:decode_unsigned(Bint)), Rest2}; deserialize2(<>) -> - {String, Rest2} = aeb_rlp:decode_one(Rest), + {String, Rest2} = aeser_rlp:decode_one(Rest), {?MAKE_FATE_STRING(String), Rest2}; deserialize2(<>) -> String = binary:part(Rest, 0, S), Rest2 = binary:part(Rest, byte_size(Rest), - (byte_size(Rest) - S)), {?MAKE_FATE_STRING(String), Rest2}; deserialize2(<>) -> - {A, Rest2} = aeb_rlp:decode_one(Rest), + {A, Rest2} = aeser_rlp:decode_one(Rest), {?FATE_ADDRESS(A), Rest2}; deserialize2(<>) -> {?FATE_TRUE, Rest}; @@ -223,7 +223,7 @@ deserialize2(<>) -> deserialize2(<>) -> {?FATE_EMPTY_STRING, Rest}; deserialize2(<>) -> - {BSize, Rest1} = aeb_rlp:decode_one(Rest), + {BSize, Rest1} = aeser_rlp:decode_one(Rest), N = binary:decode_unsigned(BSize) + ?SHORT_TUPLE_SIZE, {List, Rest2} = deserialize_elements(N, Rest1), {?FATE_TUPLE(list_to_tuple(List)), Rest2}; @@ -231,7 +231,7 @@ deserialize2(<>) -> {List, Rest1} = deserialize_elements(S, Rest), {?FATE_TUPLE(list_to_tuple(List)), Rest1}; deserialize2(<>) -> - {BLength, Rest1} = aeb_rlp:decode_one(Rest), + {BLength, Rest1} = aeser_rlp:decode_one(Rest), Length = binary:decode_unsigned(BLength) + ?SHORT_LIST_SIZE, {List, Rest2} = deserialize_elements(Length, Rest1), {?MAKE_FATE_LIST(List), Rest2}; @@ -239,7 +239,7 @@ deserialize2(<>) -> {List, Rest1} = deserialize_elements(S, Rest), {?MAKE_FATE_LIST(List), Rest1}; deserialize2(<>) -> - {BSize, Rest1} = aeb_rlp:decode_one(Rest), + {BSize, Rest1} = aeser_rlp:decode_one(Rest), Size = binary:decode_unsigned(BSize), {List, Rest2} = deserialize_elements(2*Size, Rest1), Map = insert_kv(List, #{}), diff --git a/src/aeb_rlp.erl b/src/aeb_rlp.erl deleted file mode 100644 index 46a27fa..0000000 --- a/src/aeb_rlp.erl +++ /dev/null @@ -1,91 +0,0 @@ -%%%------------------------------------------------------------------- -%%% @copyright (C) 2017, Aeternity Anstalt -%%% @doc -%%% Implementation of the Recursive Length Prefix. -%%% -%%% https://github.com/ethereum/wiki/wiki/RLP -%%% -%%% @end -%%%------------------------------------------------------------------- - --module(aeb_rlp). --export([ decode/1 - , decode_one/1 - , encode/1 - ]). - --export_type([ encodable/0 - , encoded/0 - ]). - --type encodable() :: [encodable()] | binary(). --type encoded() :: <<_:8, _:_*8>>. - --define(UNTAGGED_SIZE_LIMIT , 55). --define(UNTAGGED_LIMIT , 127). --define(BYTE_ARRAY_OFFSET , 128). --define(LIST_OFFSET , 192). - - --spec encode(encodable()) -> encoded(). -encode(X) -> - encode(X, []). - -encode(<> = X,_Opts) when B =< ?UNTAGGED_LIMIT -> - %% An untagged value - X; -encode(X,_Opts) when is_binary(X) -> - %% Byte array - add_size(?BYTE_ARRAY_OFFSET, X); -encode(L, Opts) when is_list(L) -> - %% Lists items are encoded and concatenated - ByteArray = << << (encode(X, Opts))/binary >> || X <- L >>, - add_size(?LIST_OFFSET, ByteArray). - -add_size(Offset, X) when byte_size(X) =< ?UNTAGGED_SIZE_LIMIT -> - %% The size fits in one tagged byte - <<(Offset + byte_size(X)), X/binary>>; -add_size(Offset, X) when is_binary(X) -> - %% The size itself needs to be encoded as a byte array - %% Add the tagged size of the size byte array - SizeBin = binary:encode_unsigned(byte_size(X)), - TaggedSize = ?UNTAGGED_SIZE_LIMIT + Offset + byte_size(SizeBin), - true = (TaggedSize < 256 ), %% Assert - <>. - --spec decode(encoded()) -> encodable(). -decode(Bin) when is_binary(Bin), byte_size(Bin) > 0 -> - case decode_one(Bin) of - {X, <<>>} -> X; - {X, Left} -> error({trailing, X, Bin, Left}) - end. - -decode_one(<>) when X =< ?UNTAGGED_LIMIT -> - %% Untagged value - {<>, B}; -decode_one(<> = B) when L < ?LIST_OFFSET -> - %% Byte array - {Size, Rest} = decode_size(B, ?BYTE_ARRAY_OFFSET), - <> = Rest, - {X, Tail}; -decode_one(<<_/binary>> = B) -> - %% List - {Size, Rest} = decode_size(B, ?LIST_OFFSET), - <> = Rest, - {decode_list(X), Tail}. - -decode_size(<>, Offset) when L =< Offset + ?UNTAGGED_SIZE_LIMIT-> - %% One byte tagged size. - {L - Offset, B}; -decode_size(<<_, 0, _/binary>>,_Offset) -> - error(leading_zeroes_in_size); -decode_size(<>, Offset) -> - %% Actual size is in a byte array. - BinSize = L - Offset - ?UNTAGGED_SIZE_LIMIT, - <> = B, - {Size, Rest}. - -decode_list(<<>>) -> []; -decode_list(B) -> - {Element, Rest} = decode_one(B), - [Element|decode_list(Rest)]. diff --git a/src/aebytecode.app.src b/src/aebytecode.app.src index cbd443f..7116b7e 100644 --- a/src/aebytecode.app.src +++ b/src/aebytecode.app.src @@ -6,6 +6,7 @@ [kernel, stdlib, eblake2, + aeserialization, getopt ]}, {env,[]},