From 61d754d43994756e9b744e041bf2d569244a117c Mon Sep 17 00:00:00 2001 From: Virgil <25692529+virgil-serbanuta@users.noreply.github.com> Date: Tue, 5 Nov 2024 19:59:12 +0200 Subject: [PATCH] Fix storage types (#179) * Fix storage types * Fix TODO text --- tests/ulm-with-contract/storage.256.run | 39 +++++++++++++++++++++++++ tests/ulm-with-contract/storage.rs | 13 +++++++++ ulm-semantics/main/execution/storage.md | 14 +++++---- ulm-semantics/main/hooks/bytes.md | 1 + ulm-semantics/main/hooks/ulm.md | 11 ++++--- 5 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 tests/ulm-with-contract/storage.256.run diff --git a/tests/ulm-with-contract/storage.256.run b/tests/ulm-with-contract/storage.256.run new file mode 100644 index 00000000..43ceec91 --- /dev/null +++ b/tests/ulm-with-contract/storage.256.run @@ -0,0 +1,39 @@ +mock SetAccountStorageHook ( 8738216329480387967 , 1000000000000000000000000000000000000000000000000000000000000 ) ulmNoResult(); +mock GetAccountStorageHook ( 8738216329480387967 ) ulmIntResult(1000000000000000000000000000000000000000000000000000000000000, u256); + +push "setMyData256"; +hold_string_from_test_stack; +push "uint256"; +hold_list_values_from_test_stack; +push 1_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_u256; +hold_list_values_from_test_stack; +encode_call_data; +return_value; +mock CallData; + +call_contract 12345; +return_value; +check_eq (); + +push_status; +check_eq 2; + + +push "getMyData256"; +hold_string_from_test_stack; +encode_call_data; +return_value; +mock CallData; + +call_contract 12345; +return_value; +check_eq (); + +push_status; +check_eq 2; + +output_to_arg; +call :: test_helpers :: decode_single_u256; +return_value; + +check_eq 1_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_u256 diff --git a/tests/ulm-with-contract/storage.rs b/tests/ulm-with-contract/storage.rs index ed877ed2..30dc17a1 100644 --- a/tests/ulm-with-contract/storage.rs +++ b/tests/ulm-with-contract/storage.rs @@ -8,6 +8,9 @@ pub trait Storage { #[storage_mapper("myData")] fn my_data(&self) -> ::single_value_mapper::SingleValueMapper; + #[storage_mapper("myData256")] + fn my_data_256(&self) -> ::single_value_mapper::SingleValueMapper; + #[storage_mapper("myDataKey")] fn my_data_key(&self, key: u64) -> ::single_value_mapper::SingleValueMapper; @@ -24,6 +27,16 @@ pub trait Storage { self.my_data().get() } + #[endpoint(setMyData256)] + fn set_256(&self, value: u256) { + self.my_data_256().set(value) + } + + #[endpoint(getMyData256)] + fn get_256(&self) -> u256 { + self.my_data_256().get() + } + #[endpoint(setMyDataKey)] fn set_key(&self, key: u64, value: u64) { self.my_data_key(key).set(value) diff --git a/ulm-semantics/main/execution/storage.md b/ulm-semantics/main/execution/storage.md index b26ba9cb..703b995a 100644 --- a/ulm-semantics/main/execution/storage.md +++ b/ulm-semantics/main/execution/storage.md @@ -13,6 +13,7 @@ module ULM-EXECUTION-STORAGE | "single_value_mapper" [token] | "SingleValueMapper" [token] | "ulm" [token] + | "value_type" [token] rule normalizedFunctionCall ( :: single_value_mapper :: SingleValueMapper :: set :: .PathExprSegments @@ -20,7 +21,7 @@ module ULM-EXECUTION-STORAGE ) => :: ulm :: SetAccountStorage :: .PathExprSegments ( SelfPtr . key - , ulmCast(ValuePtr, ptrValue(null, rustType(u64))) + , ulmCast(ValuePtr, SelfPtr . value_type) , .CallParamsList ) @@ -28,10 +29,13 @@ module ULM-EXECUTION-STORAGE ( :: single_value_mapper :: SingleValueMapper :: get :: .PathExprSegments , (SelfPtr:Ptr , .PtrList) ) - => :: ulm :: GetAccountStorage :: .PathExprSegments - ( SelfPtr . key - , .CallParamsList - ) + => ulmCast + ( :: ulm :: GetAccountStorage :: .PathExprSegments + ( SelfPtr . key + , .CallParamsList + ) + , SelfPtr . value_type + ) endmodule diff --git a/ulm-semantics/main/hooks/bytes.md b/ulm-semantics/main/hooks/bytes.md index 7501f3d8..e83d21c0 100644 --- a/ulm-semantics/main/hooks/bytes.md +++ b/ulm-semantics/main/hooks/bytes.md @@ -345,6 +345,7 @@ module ULM-SEMANTICS-HOOKS-BYTES rule ulmBytesHash(ptrValue(_, u64(BytesId))) => ulmBytesHash(ulmBytesId(BytesId)) + // TODO: use a better hash function here (lower collision probability, use 256 bytes of hash). rule ulmBytesHash(ulmBytesValue(B:Bytes)) => ptrValue(null, u64(Int2MInt(#ulmBytesHash(Bytes2Int(B, BE, Unsigned))))) diff --git a/ulm-semantics/main/hooks/ulm.md b/ulm-semantics/main/hooks/ulm.md index c1d2fcf0..87899455 100644 --- a/ulm-semantics/main/hooks/ulm.md +++ b/ulm-semantics/main/hooks/ulm.md @@ -37,22 +37,25 @@ module ULM-SEMANTICS-HOOKS-ULM ( :: ulm :: SetAccountStorage :: .PathExprSegments , (KeyPtr:Ptr , ValuePtr:Ptr , .PtrList) ) - => #SetAccountStorageHook(KeyPtr, ValuePtr) + => #SetAccountStorageHook + ( ulmCast(KeyPtr, ptrValue(null, rustType(u256))) + , ulmCast(ValuePtr, ptrValue(null, rustType(u256))) + ) syntax UlmHook ::= #SetAccountStorageHook(Expression, Expression) [seqstrict] - rule #SetAccountStorageHook(ptrValue(_, u64(Key)), ptrValue(_, u64(Value))) + rule #SetAccountStorageHook(ptrValue(_, u256(Key)), ptrValue(_, u256(Value))) => SetAccountStorageHook(MInt2Unsigned(Key), MInt2Unsigned(Value)) rule normalizedFunctionCall ( :: ulm :: GetAccountStorage :: .PathExprSegments , (KeyPtr:Ptr , .PtrList) ) - => #GetAccountStorageHook(KeyPtr) + => #GetAccountStorageHook(ulmCast(KeyPtr, ptrValue(null, rustType(u256)))) syntax UlmHook ::= #GetAccountStorageHook(Expression) [strict] - rule #GetAccountStorageHook(ptrValue(_, u64(Key))) + rule #GetAccountStorageHook(ptrValue(_, u256(Key))) => GetAccountStorageHook(MInt2Unsigned(Key)) rule ulmNoResult() => ptrValue(null, tuple(.ValueList))