Skip to content

Commit

Permalink
Fix storage types (#179)
Browse files Browse the repository at this point in the history
* Fix storage types

* Fix TODO text
  • Loading branch information
virgil-serbanuta authored Nov 5, 2024
1 parent 1d30605 commit 61d754d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
39 changes: 39 additions & 0 deletions tests/ulm-with-contract/storage.256.run
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions tests/ulm-with-contract/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub trait Storage {
#[storage_mapper("myData")]
fn my_data(&self) -> ::single_value_mapper::SingleValueMapper<u64>;

#[storage_mapper("myData256")]
fn my_data_256(&self) -> ::single_value_mapper::SingleValueMapper<u256>;

#[storage_mapper("myDataKey")]
fn my_data_key(&self, key: u64) -> ::single_value_mapper::SingleValueMapper<u64>;

Expand All @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions ulm-semantics/main/execution/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,29 @@ module ULM-EXECUTION-STORAGE
| "single_value_mapper" [token]
| "SingleValueMapper" [token]
| "ulm" [token]
| "value_type" [token]
rule normalizedFunctionCall
( :: single_value_mapper :: SingleValueMapper :: set :: .PathExprSegments
, (SelfPtr:Ptr , ValuePtr:Ptr , .PtrList)
)
=> :: ulm :: SetAccountStorage :: .PathExprSegments
( SelfPtr . key
, ulmCast(ValuePtr, ptrValue(null, rustType(u64)))
, ulmCast(ValuePtr, SelfPtr . value_type)
, .CallParamsList
)
rule normalizedFunctionCall
( :: 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
Expand Down
1 change: 1 addition & 0 deletions ulm-semantics/main/hooks/bytes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))
Expand Down
11 changes: 7 additions & 4 deletions ulm-semantics/main/hooks/ulm.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 61d754d

Please sign in to comment.