From fbaab570f2c84dc452f8377d06e1d1e95c3439a6 Mon Sep 17 00:00:00 2001 From: Hans Svensson Date: Tue, 23 Jan 2024 16:09:37 +0100 Subject: [PATCH] [Ceres]: Handle Bytes.to_any_size in calldata (#495) * [Ceres] Handle Bytes.to_any_size in calldata * Edit CHANGELOG --- CHANGELOG.md | 2 ++ src/aeso_fcode_to_fate.erl | 2 ++ src/aeso_vm_decode.erl | 4 +++- test/aeso_calldata_tests.erl | 1 + test/contracts/funargs.aes | 2 ++ 5 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f561e45..078a84d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 and `Int.to_bytes`; and adjust `Bytes.concat` to allow both fixed and arbitrary sized byte arrays. - `Chain.network_id` - a function to get hold of the Chain's network id. +- Allowing `Bytes.to_any_size` in calldata creation, to enable creation of arguments + with arbitray size. ### Changed - `Crypto.verify_sig` is changed to have `msg : bytes()`. I.e. the signed data can be of any length (used to be limited to `bytes(32)`/`hash`). diff --git a/src/aeso_fcode_to_fate.erl b/src/aeso_fcode_to_fate.erl index e218778..3328d36 100644 --- a/src/aeso_fcode_to_fate.erl +++ b/src/aeso_fcode_to_fate.erl @@ -285,6 +285,8 @@ term_to_fate(_GlobEnv, _Env, {builtin, _, map_empty, []}) -> term_to_fate(GlobEnv, Env, {op, _, map_set, [M, K, V]}) -> Map = term_to_fate(GlobEnv, Env, M), Map#{term_to_fate(GlobEnv, Env, K) => term_to_fate(GlobEnv, Env, V)}; +term_to_fate(GlobEnv, Env, {builtin, _, bytes_to_any_size, [Bs]}) -> + term_to_fate(GlobEnv, Env, Bs); term_to_fate(_GlobEnv, _Env, _) -> throw(not_a_fate_value). diff --git a/src/aeso_vm_decode.erl b/src/aeso_vm_decode.erl index cbbebd5..f158c8f 100644 --- a/src/aeso_vm_decode.erl +++ b/src/aeso_vm_decode.erl @@ -15,7 +15,7 @@ from_fate({id, _, "address"}, ?FATE_ADDRESS(Bin)) -> {account_pubkey, [], Bin}; from_fate({app_t, _, {id, _, "oracle"}, _}, ?FATE_ORACLE(Bin)) -> {oracle_pubkey, [], Bin}; from_fate({app_t, _, {id, _, "oracle_query"}, _}, ?FATE_ORACLE_Q(Bin)) -> {oracle_query_id, [], Bin}; from_fate({con, _, _Name}, ?FATE_CONTRACT(Bin)) -> {contract_pubkey, [], Bin}; -from_fate({bytes_t, _, any}, ?FATE_BYTES(Bin)) -> {bytes, [], Bin}; +from_fate({bytes_t, _, any}, ?FATE_BYTES(Bin)) -> make_any_bytes(Bin); from_fate({bytes_t, _, N}, ?FATE_BYTES(Bin)) when byte_size(Bin) == N -> {bytes, [], Bin}; from_fate({id, _, "bits"}, ?FATE_BITS(N)) -> make_bits(N); from_fate({id, _, "int"}, N) when is_integer(N) -> @@ -187,3 +187,5 @@ make_bits(Set, Zero, I, N) when 0 == N rem 2 -> make_bits(Set, Zero, I, N) -> {app, [], Set, [make_bits(Set, Zero, I + 1, N div 2), {int, [], I}]}. +make_any_bytes(Bin) -> + {app, [], {qid, [], ["Bytes", "to_any_size"]}, [{bytes, [], Bin}]}. diff --git a/test/aeso_calldata_tests.erl b/test/aeso_calldata_tests.erl index 88d7bae..3eb5af5 100644 --- a/test/aeso_calldata_tests.erl +++ b/test/aeso_calldata_tests.erl @@ -85,6 +85,7 @@ compilable_contracts() -> {"funargs", "bitsum", ["Bits.clear(Bits.clear(Bits.all, 4), 2)"]}, %% Order matters for test {"funargs", "bitsum", ["Bits.set(Bits.set(Bits.none, 4), 2)"]}, {"funargs", "read", ["{label = \"question 1\", result = 4}"]}, + {"funargs", "any_bytes", ["Bytes.to_any_size(#0011AA)"]}, {"funargs", "sjutton", ["#0011012003100011012003100011012003"]}, {"funargs", "sextiosju", ["#01020304050607080910111213141516171819202122232425262728293031323334353637383940" "414243444546474849505152535455565758596061626364656667"]}, diff --git a/test/contracts/funargs.aes b/test/contracts/funargs.aes index cd54fa5..32f0f0b 100644 --- a/test/contracts/funargs.aes +++ b/test/contracts/funargs.aes @@ -20,6 +20,8 @@ contract FunctionArguments = entrypoint read(a : answer(int)) = a.result + entrypoint any_bytes(b : bytes()) = b + entrypoint sjutton(b : bytes(17)) = b