From 6d075efcd1e6a5cf297034de63a09e350539b56a Mon Sep 17 00:00:00 2001 From: acassimiro Date: Wed, 23 Oct 2024 01:06:22 -0300 Subject: [PATCH] Adapting tests to properly encode call data --- tests/ukm-with-contract/erc_20_token.1.run | 135 +++++++++------------ tests/ukm-with-contract/events.1.run | 19 ++- tests/ukm-with-contract/require.false.run | 12 +- tests/ukm-with-contract/require.true.run | 12 +- tests/ukm-with-contract/storage.key.run | 27 ++--- tests/ukm-with-contract/storage.simple.run | 19 ++- ukm-semantics/main/encoding/encoder.md | 13 +- ukm-semantics/test/execution.md | 12 ++ 8 files changed, 121 insertions(+), 128 deletions(-) diff --git a/tests/ukm-with-contract/erc_20_token.1.run b/tests/ukm-with-contract/erc_20_token.1.run index 21b01e3..af6ed52 100644 --- a/tests/ukm-with-contract/erc_20_token.1.run +++ b/tests/ukm-with-contract/erc_20_token.1.run @@ -21,13 +21,15 @@ list_mock SetAccountStorageHook ( 7162266444908917614 , 300 ) ukmNoResult(); list_mock GetAccountStorageHook ( 7162266444907899391 ) ukmIntResult(9700, u256); list_mock GetAccountStorageHook ( 7162266444908917614 ) ukmIntResult(300, u256); -call :: bytes_hooks :: empty; -return_value_to_arg; -push "#init(Uint256)"; -call :: bytes_hooks :: append_str; -return_value_to_arg; +push "#init"; +hold_string_from_test_stack; +push "uint256"; +hold_list_values_from_test_stack; push 10000_u256; -call :: bytes_hooks :: append_u256; +hold_list_values_from_test_stack; +mock EncodeCallData; + + return_value; mock CallData; @@ -49,14 +51,13 @@ check_eq 0_u32; - -call :: bytes_hooks :: empty; -return_value_to_arg; -push "balanceOf(Uint160)"; -call :: bytes_hooks :: append_str; -return_value_to_arg; +push "balanceOf"; +hold_string_from_test_stack; +push "uint160"; +hold_list_values_from_test_stack; push 1010101_u160; -call :: bytes_hooks :: append_u160; +hold_list_values_from_test_stack; +mock EncodeCallData; return_value; mock CallData; @@ -74,17 +75,15 @@ return_value; check_eq 10000_u256; - -call :: bytes_hooks :: empty; -return_value_to_arg; -push "transfer(Uint160,Uint256)"; -call :: bytes_hooks :: append_str; -return_value_to_arg; +push "transfer"; +hold_string_from_test_stack; +push "uint160"; +push "uint256"; +hold_list_values_from_test_stack; push 2020202_u160; -call :: bytes_hooks :: append_u160; -return_value_to_arg; push 100_u256; -call :: bytes_hooks :: append_u256; +hold_list_values_from_test_stack; +mock EncodeCallData; return_value; mock CallData; @@ -105,14 +104,13 @@ return_value; check_eq 1_u64; - -call :: bytes_hooks :: empty; -return_value_to_arg; -push "balanceOf(Uint160)"; -call :: bytes_hooks :: append_str; -return_value_to_arg; +push "balanceOf"; +hold_string_from_test_stack; +push "uint160"; +hold_list_values_from_test_stack; push 1010101_u160; -call :: bytes_hooks :: append_u160; +hold_list_values_from_test_stack; +mock EncodeCallData; return_value; mock CallData; @@ -130,15 +128,13 @@ return_value; check_eq 9900_u256; - - -call :: bytes_hooks :: empty; -return_value_to_arg; -push "balanceOf(Uint160)"; -call :: bytes_hooks :: append_str; -return_value_to_arg; +push "balanceOf"; +hold_string_from_test_stack; +push "uint160"; +hold_list_values_from_test_stack; push 2020202_u160; -call :: bytes_hooks :: append_u160; +hold_list_values_from_test_stack; +mock EncodeCallData; return_value; mock CallData; @@ -156,20 +152,15 @@ return_value; check_eq 100_u256; - - - - -call :: bytes_hooks :: empty; -return_value_to_arg; -push "approve(Uint160,Uint256)"; -call :: bytes_hooks :: append_str; -return_value_to_arg; +push "approve"; +hold_string_from_test_stack; +push "uint160"; +push "uint256"; +hold_list_values_from_test_stack; push 3030303_u160; -call :: bytes_hooks :: append_u160; -return_value_to_arg; push 200_u256; -call :: bytes_hooks :: append_u256; +hold_list_values_from_test_stack; +mock EncodeCallData; return_value; mock CallData; @@ -190,20 +181,17 @@ return_value; check_eq 1_u64; - -call :: bytes_hooks :: empty; -return_value_to_arg; -push "transferFrom(Uint160,Uint160,Uint256)"; -call :: bytes_hooks :: append_str; -return_value_to_arg; +push "transferFrom"; +hold_string_from_test_stack; +push "uint160"; +push "uint160"; +push "uint256"; +hold_list_values_from_test_stack; push 1010101_u160; -call :: bytes_hooks :: append_u160; -return_value_to_arg; push 2020202_u160; -call :: bytes_hooks :: append_u160; -return_value_to_arg; push 200_u256; -call :: bytes_hooks :: append_u256; +hold_list_values_from_test_stack; +mock EncodeCallData; return_value; mock CallData; @@ -225,15 +213,13 @@ check_eq 1_u64; - - -call :: bytes_hooks :: empty; -return_value_to_arg; -push "balanceOf(Uint160)"; -call :: bytes_hooks :: append_str; -return_value_to_arg; +push "balanceOf"; +hold_string_from_test_stack; +push "uint160"; +hold_list_values_from_test_stack; push 1010101_u160; -call :: bytes_hooks :: append_u160; +hold_list_values_from_test_stack; +mock EncodeCallData; return_value; mock CallData; @@ -252,14 +238,13 @@ check_eq 9700_u256; - -call :: bytes_hooks :: empty; -return_value_to_arg; -push "balanceOf(Uint160)"; -call :: bytes_hooks :: append_str; -return_value_to_arg; +push "balanceOf"; +hold_string_from_test_stack; +push "uint160"; +hold_list_values_from_test_stack; push 2020202_u160; -call :: bytes_hooks :: append_u160; +hold_list_values_from_test_stack; +mock EncodeCallData; return_value; mock CallData; diff --git a/tests/ukm-with-contract/events.1.run b/tests/ukm-with-contract/events.1.run index 8c0c04b..17b925f 100644 --- a/tests/ukm-with-contract/events.1.run +++ b/tests/ukm-with-contract/events.1.run @@ -1,14 +1,13 @@ -call :: bytes_hooks :: empty; -return_value_to_arg; -push "logEvent(Uint64,Uint64)"; -call :: bytes_hooks :: append_str; -return_value_to_arg; +push "logEvent"; +hold_string_from_test_stack; +push "uint64"; +push "uint64"; +hold_list_values_from_test_stack; push 123_u64; -call :: bytes_hooks :: append_u64; -return_value_to_arg; -push 555_u64; -call :: bytes_hooks :: append_u64; -return_value; +push 555_u64; +hold_list_values_from_test_stack; +mock EncodeCallData; +return_value; mock CallData; call_contract 12345; diff --git a/tests/ukm-with-contract/require.false.run b/tests/ukm-with-contract/require.false.run index c8ee374..2122a5a 100644 --- a/tests/ukm-with-contract/require.false.run +++ b/tests/ukm-with-contract/require.false.run @@ -1,10 +1,10 @@ -call :: bytes_hooks :: empty; -return_value_to_arg; -push "myEndpoint(Uint64)"; -call :: bytes_hooks :: append_str; -return_value_to_arg; +push "myEndpoint"; +hold_string_from_test_stack; +push "uint64"; +hold_list_values_from_test_stack; push 0_u64; -call :: bytes_hooks :: append_u64; +hold_list_values_from_test_stack; +mock EncodeCallData; return_value; mock CallData; diff --git a/tests/ukm-with-contract/require.true.run b/tests/ukm-with-contract/require.true.run index 4264f65..10c46e1 100644 --- a/tests/ukm-with-contract/require.true.run +++ b/tests/ukm-with-contract/require.true.run @@ -1,10 +1,10 @@ -call :: bytes_hooks :: empty; -return_value_to_arg; -push "myEndpoint(Uint64)"; -call :: bytes_hooks :: append_str; -return_value_to_arg; +push "myEndpoint"; +hold_string_from_test_stack; +push "uint64"; +hold_list_values_from_test_stack; push 123_u64; -call :: bytes_hooks :: append_u64; +hold_list_values_from_test_stack; +mock EncodeCallData; return_value; mock CallData; diff --git a/tests/ukm-with-contract/storage.key.run b/tests/ukm-with-contract/storage.key.run index a2e618d..8859022 100644 --- a/tests/ukm-with-contract/storage.key.run +++ b/tests/ukm-with-contract/storage.key.run @@ -1,16 +1,15 @@ mock SetAccountStorageHook ( 7010817630605304703 , 123 ) ukmNoResult(); mock GetAccountStorageHook ( 7010817630605304703 ) ukmIntResult(123, u64); -call :: bytes_hooks :: empty; -return_value_to_arg; -push "setMyDataKey(Uint64,Uint64)"; -call :: bytes_hooks :: append_str; -return_value_to_arg; +push "setMyDataKey"; +hold_string_from_test_stack; +push "uint64"; +push "uint64"; +hold_list_values_from_test_stack; push 555_u64; -call :: bytes_hooks :: append_u64; -return_value_to_arg; push 123_u64; -call :: bytes_hooks :: append_u64; +hold_list_values_from_test_stack; +mock EncodeCallData; return_value; mock CallData; @@ -22,13 +21,13 @@ push_status; check_eq 2; -call :: bytes_hooks :: empty; -return_value_to_arg; -push "getMyDataKey(Uint64)"; -call :: bytes_hooks :: append_str; -return_value_to_arg; +push "getMyDataKey"; +hold_string_from_test_stack; +push "uint64"; +hold_list_values_from_test_stack; push 555_u64; -call :: bytes_hooks :: append_u64; +hold_list_values_from_test_stack; +mock EncodeCallData; return_value; mock CallData; diff --git a/tests/ukm-with-contract/storage.simple.run b/tests/ukm-with-contract/storage.simple.run index 4648776..5e30718 100644 --- a/tests/ukm-with-contract/storage.simple.run +++ b/tests/ukm-with-contract/storage.simple.run @@ -1,13 +1,13 @@ mock SetAccountStorageHook ( 1809217465971809 , 123 ) ukmNoResult(); mock GetAccountStorageHook ( 1809217465971809 ) ukmIntResult(123, u64); -call :: bytes_hooks :: empty; -return_value_to_arg; -push "setMyData(Uint64)"; -call :: bytes_hooks :: append_str; -return_value_to_arg; +push "setMyData"; +hold_string_from_test_stack; +push "uint64"; +hold_list_values_from_test_stack; push 123_u64; -call :: bytes_hooks :: append_u64; +hold_list_values_from_test_stack; +mock EncodeCallData; return_value; mock CallData; @@ -19,10 +19,9 @@ push_status; check_eq 2; -call :: bytes_hooks :: empty; -return_value_to_arg; -push "getMyData()"; -call :: bytes_hooks :: append_str; +push "getMyData"; +hold_string_from_test_stack; +mock EncodeCallData; return_value; mock CallData; diff --git a/ukm-semantics/main/encoding/encoder.md b/ukm-semantics/main/encoding/encoder.md index d4757e7..c0889b1 100644 --- a/ukm-semantics/main/encoding/encoder.md +++ b/ukm-semantics/main/encoding/encoder.md @@ -34,16 +34,16 @@ module UKM-CALLDATA-ENCODER // Function signature encoding rule encodeFunctionSignature(FuncName:String, RL:List, "") => - encodeFunctionSignature("", RL:List, FuncName +String "(") + encodeFunctionSignature("", RL:List, FuncName +String "(") [priority(40)] rule encodeFunctionSignature("", ListItem(FuncParam:String) RL:List, FS) => encodeFunctionSignature("", RL, FS +String FuncParam +String ",") [owise] // The last param does not have a follow up comma rule encodeFunctionSignature("", ListItem(FuncParam:String) .List, FS) => - encodeFunctionSignature("", .List, FS +String FuncParam +String ")" ) + encodeFunctionSignature("", .List, FS +String FuncParam ) - rule encodeFunctionSignature("", .List, FS) => String2Bytes(substrString(Keccak256(String2Bytes(FS)), 0, 8)) + rule encodeFunctionSignature("", .List, FS) => String2Bytes(substrString(Keccak256(String2Bytes(FS +String ")")), 0, 8)) rule encodeFunctionSignatureAsString(FS) => substrString(Keccak256(String2Bytes(FS)), 0, 8) rule encodeFunctionSignature(FS:String:StringOrError) => String2Bytes(substrString(Keccak256(String2Bytes(FS)), 0, 8)) @@ -68,10 +68,9 @@ module UKM-CALLDATA-ENCODER rule convertToKBytes(u128(V), "uint128") => Int2Bytes(32, MInt2Unsigned(V), BE:Endianness) rule convertToKBytes(true, "bool") => Int2Bytes(32, 1, BE:Endianness) rule convertToKBytes(false, "bool") => Int2Bytes(32, 0, BE:Endianness) - // TODO: as we currently do not support u160 (addresses) or u256, we're converting them to u64 for now - rule convertToKBytes(u64(V), "uint256") => Int2Bytes(32, MInt2Unsigned(V), BE:Endianness) - rule convertToKBytes(u64(V), "uint160") => Int2Bytes(32, MInt2Unsigned(V), BE:Endianness) - rule convertToKBytes(u64(V), "address") => Int2Bytes(32, MInt2Unsigned(V), BE:Endianness) + rule convertToKBytes(u256(V), "uint256") => Int2Bytes(32, MInt2Unsigned(V), BE:Endianness) + rule convertToKBytes(u160(V), "uint160") => Int2Bytes(32, MInt2Unsigned(V), BE:Endianness) + rule convertToKBytes(u160(V), "address") => Int2Bytes(32, MInt2Unsigned(V), BE:Endianness) endmodule diff --git a/ukm-semantics/test/execution.md b/ukm-semantics/test/execution.md index ec28bd0..c941c50 100644 --- a/ukm-semantics/test/execution.md +++ b/ukm-semantics/test/execution.md @@ -84,12 +84,24 @@ module UKM-TEST-EXECUTION ... + rule mock EncodeCallDataToString + ~> value_holder FNAME + => Bytes2String(encodeCallData(FNAME, .List, .List)) + ... + [owise] + rule mock EncodeCallData ~> list_values_holder ARGS , list_values_holder PTYPES , value_holder FNAME , .UKMTestTypeHolderList => ukmBytesNew(encodeCallData(FNAME, PTYPES, ARGS)) ... + rule mock EncodeCallData + ~> value_holder FNAME + => ukmBytesNew(encodeCallData(FNAME, .List, .List)) + ... + [owise] + rule mock CallData => mock(CallDataHook(), V) ...