From 47d87d51c2568e1fb7a47b63e91d744f552ff657 Mon Sep 17 00:00:00 2001 From: Virgil Date: Fri, 18 Oct 2024 14:43:46 +0300 Subject: [PATCH] Use the new int types for erc-20 --- tests/ukm-contracts/address.rs | 4 +- tests/ukm-contracts/bytes_hooks.rs | 4 + tests/ukm-contracts/test_helpers.rs | 8 ++ tests/ukm-contracts/ukm.rs | 46 ++++---- tests/ukm-with-contract/erc_20_token.1.run | 126 ++++++++++----------- tests/ukm-with-contract/erc_20_token.rs | 39 ++++--- ukm-semantics/main/hooks/ukm.md | 6 + ukm-semantics/test/execution.md | 2 +- 8 files changed, 129 insertions(+), 106 deletions(-) diff --git a/tests/ukm-contracts/address.rs b/tests/ukm-contracts/address.rs index 47d0329..14a27d2 100644 --- a/tests/ukm-contracts/address.rs +++ b/tests/ukm-contracts/address.rs @@ -1,3 +1,3 @@ -fn is_zero(address: u64) -> bool { - address == 0_u64 +fn is_zero(address: u160) -> bool { + address == 0_u160 } \ No newline at end of file diff --git a/tests/ukm-contracts/bytes_hooks.rs b/tests/ukm-contracts/bytes_hooks.rs index aa1a786..5620de9 100644 --- a/tests/ukm-contracts/bytes_hooks.rs +++ b/tests/ukm-contracts/bytes_hooks.rs @@ -2,6 +2,8 @@ extern "C" { fn empty() -> u64; fn length(bytes_id: u64) -> u32; + fn append_u256(bytes_id: u64, value: u256) -> u64; + fn append_u160(bytes_id: u64, value: u160) -> u64; fn append_u128(bytes_id: u64, value: u128) -> u64; fn append_u64(bytes_id: u64, value: u64) -> u64; fn append_u32(bytes_id: u64, value: u32) -> u64; @@ -10,6 +12,8 @@ extern "C" { fn append_bool(bytes_id: u64, value: bool) -> u64; fn append_str(bytes_id: u64, value: &str) -> u64; + fn decode_u256(bytes_id: u64) -> (u64, u256); + fn decode_u160(bytes_id: u64) -> (u64, u160); fn decode_u128(bytes_id: u64) -> (u64, u128); fn decode_u64(bytes_id: u64) -> (u64, u64); fn decode_u32(bytes_id: u64) -> (u64, u32); diff --git a/tests/ukm-contracts/test_helpers.rs b/tests/ukm-contracts/test_helpers.rs index e3be902..5d7ba17 100644 --- a/tests/ukm-contracts/test_helpers.rs +++ b/tests/ukm-contracts/test_helpers.rs @@ -6,3 +6,11 @@ fn decode_single_u64(bytes_id: u64) -> u64 { }; value } + +fn decode_single_u256(bytes_id: u64) -> u256 { + let (remaining_id, value) = :: bytes_hooks :: decode_u256(bytes_id); + if :: bytes_hooks :: length(remaining_id) > 0_u32 { + fail(); + }; + value +} diff --git a/tests/ukm-contracts/ukm.rs b/tests/ukm-contracts/ukm.rs index 42ace10..d510b2c 100644 --- a/tests/ukm-contracts/ukm.rs +++ b/tests/ukm-contracts/ukm.rs @@ -23,8 +23,8 @@ pub const EVMC_NONCE_EXCEEDED: u64 = 15_u64; extern { // block parameters fn sample_method(&self) -> u64; - fn GasLimit(&self) -> u64; - fn BaseFee(&self) -> u64; + fn GasLimit(&self) -> u256; + fn BaseFee(&self) -> u256; fn Coinbase(&self) -> u64; fn BlockTimestamp(&self) -> u64; fn BlockNumber(&self) -> u64; @@ -37,30 +37,30 @@ extern { fn Origin(&self) -> u64; // message parameters - fn Address(&self) -> u64; - fn Caller(&self) -> u64; - fn CallValue(&self) -> u64; + fn Address(&self) -> u160; + fn Caller(&self) -> u160; + fn CallValue(&self) -> u256; fn CallData(&self) -> Bytes; // chain parameters fn ChainId(&self) -> u64; // account getters - fn GetAccountBalance(&self, acct: u64) -> u64; - fn GetAccountCode(&self, acct: u64) -> u64; - fn GetAccountStorage(&self, key: u64) -> u64; - fn GetAccountOrigStorage(&self, key: u64) -> u64; - fn GetAccountTransientStorage(&self, key: u64) -> u64; - fn IsAccountEmpty(&self, acct: u64) -> bool; + fn GetAccountBalance(&self, acct: u160) -> u256; + fn GetAccountCode(&self, acct: u160) -> Bytes; + fn GetAccountStorage(&self, key: u256) -> u256; + fn GetAccountOrigStorage(&self, key: u256) -> u256; + fn GetAccountTransientStorage(&self, key: u256) -> u256; + fn IsAccountEmpty(&self, acct: u160) -> bool; // to be removed in final version - fn AccessedStorage(&self, key: u64) -> bool; - fn AccessedAccount(&self, acct: u64) -> bool; + fn AccessedStorage(&self, key: u256) -> bool; + fn AccessedAccount(&self, acct: u256) -> bool; - fn Transfer(&self, to: u64, value: u64) -> bool; + fn Transfer(&self, to: u160, value: u256) -> bool; fn SelfDestruct(&self, to: u64); - fn SetAccountStorage(&self, key: u64, value: u64); - fn SetAccountTransientStorage(&self, key: u64, value: u64); + fn SetAccountStorage(&self, key: u256, value: u256); + fn SetAccountTransientStorage(&self, key: u256, value: u256); fn Log0(data: Bytes); fn Log1(topic0: u64, data: Bytes); @@ -68,11 +68,11 @@ extern { fn Log3(topic0: u64, topic1: u64, topic2: u64, data: Bytes); fn Log4(topic0: u64, topic1: u64, topic2: u64, topic3: u64, data: Bytes); - fn MessageResult(gas: u64, data: Bytes, status: u64, target: u64) -> MessageResult; - fn Create(value: u64, data: Bytes, gas: u64) -> MessageResult; - fn Create2(value: u64, data: Bytes, salt: Bytes, gas: u64) -> MessageResult; - fn Call(gas: u64, to: u64, value: u64, data: Bytes) -> MessageResult; - fn CallCode(gas: u64, to: u64, value: u64, data: Bytes) -> MessageResult; - fn DelegateCall(gas: u64, to: u64, data: Bytes) -> MessageResult; - fn StaticCall(gas: u64, to: u64, data: Bytes) -> MessageResult; + fn MessageResult(gas: u256, data: Bytes, status: u64, target: u64) -> MessageResult; + fn Create(value: u256, data: Bytes, gas: u256) -> MessageResult; + fn Create2(value: u256, data: Bytes, salt: Bytes, gas: u256) -> MessageResult; + fn Call(gas: u256, to: u160, value: u256, data: Bytes) -> MessageResult; + fn CallCode(gas: u256, to: u160, value: u256, data: Bytes) -> MessageResult; + fn DelegateCall(gas: u256, to: u160, data: Bytes) -> MessageResult; + fn StaticCall(gas: u256, to: u160, data: Bytes) -> MessageResult; } diff --git a/tests/ukm-with-contract/erc_20_token.1.run b/tests/ukm-with-contract/erc_20_token.1.run index a87b559..e477965 100644 --- a/tests/ukm-with-contract/erc_20_token.1.run +++ b/tests/ukm-with-contract/erc_20_token.1.run @@ -1,37 +1,37 @@ -list_mock GetAccountStorageHook ( 7809087261546347641 ) ukmInt64Result(0); +list_mock GetAccountStorageHook ( 7809087261546347641 ) ukmInt256Result(0); list_mock SetAccountStorageHook ( 7809087261546347641 , 10000 ) ukmNoResult(); -list_mock GetAccountStorageHook ( 7162266444907899391 ) ukmInt64Result(0); +list_mock GetAccountStorageHook ( 7162266444907899391 ) ukmInt256Result(0); list_mock SetAccountStorageHook ( 7162266444907899391 , 10000 ) ukmNoResult(); -list_mock GetAccountStorageHook ( 7162266444907899391 ) ukmInt64Result(10000); -list_mock GetAccountStorageHook ( 7162266444907899391 ) ukmInt64Result(10000); -list_mock GetAccountStorageHook ( 7162266444907899391 ) ukmInt64Result(10000); +list_mock GetAccountStorageHook ( 7162266444907899391 ) ukmInt256Result(10000); +list_mock GetAccountStorageHook ( 7162266444907899391 ) ukmInt256Result(10000); +list_mock GetAccountStorageHook ( 7162266444907899391 ) ukmInt256Result(10000); list_mock SetAccountStorageHook ( 7162266444907899391 , 9900 ) ukmNoResult(); -list_mock GetAccountStorageHook ( 7162266444908917614 ) ukmInt64Result(0); +list_mock GetAccountStorageHook ( 7162266444908917614 ) ukmInt256Result(0); list_mock SetAccountStorageHook ( 7162266444908917614 , 100 ) ukmNoResult(); -list_mock GetAccountStorageHook ( 7162266444907899391 ) ukmInt64Result(9900); -list_mock GetAccountStorageHook ( 7162266444908917614 ) ukmInt64Result(100); +list_mock GetAccountStorageHook ( 7162266444907899391 ) ukmInt256Result(9900); +list_mock GetAccountStorageHook ( 7162266444908917614 ) ukmInt256Result(100); list_mock SetAccountStorageHook ( 8028228613167873919 , 200 ) ukmNoResult(); -list_mock GetAccountStorageHook ( 8028228613167873919 ) ukmInt64Result(200); +list_mock GetAccountStorageHook ( 8028228613167873919 ) ukmInt256Result(200); list_mock SetAccountStorageHook ( 8028228613167873919 , 0 ) ukmNoResult(); -list_mock GetAccountStorageHook ( 7162266444907899391 ) ukmInt64Result(9900); -list_mock GetAccountStorageHook ( 7162266444907899391 ) ukmInt64Result(9900); +list_mock GetAccountStorageHook ( 7162266444907899391 ) ukmInt256Result(9900); +list_mock GetAccountStorageHook ( 7162266444907899391 ) ukmInt256Result(9900); list_mock SetAccountStorageHook ( 7162266444907899391 , 9700 ) ukmNoResult(); -list_mock GetAccountStorageHook ( 7162266444908917614 ) ukmInt64Result(100); +list_mock GetAccountStorageHook ( 7162266444908917614 ) ukmInt256Result(100); list_mock SetAccountStorageHook ( 7162266444908917614 , 300 ) ukmNoResult(); -list_mock GetAccountStorageHook ( 7162266444907899391 ) ukmInt64Result(9700); -list_mock GetAccountStorageHook ( 7162266444908917614 ) ukmInt64Result(300); +list_mock GetAccountStorageHook ( 7162266444907899391 ) ukmInt256Result(9700); +list_mock GetAccountStorageHook ( 7162266444908917614 ) ukmInt256Result(300); call :: bytes_hooks :: empty; return_value_to_arg; -push "#init(Uint64)"; +push "#init(Uint256)"; call :: bytes_hooks :: append_str; return_value_to_arg; -push 10000_u64; -call :: bytes_hooks :: append_u64; +push 10000_u256; +call :: bytes_hooks :: append_u256; return_value; mock CallData; -push_value 1010101_u64; +push_value 1010101_u160; mock Caller; call_contract 12345; @@ -52,11 +52,11 @@ check_eq 0_u32; call :: bytes_hooks :: empty; return_value_to_arg; -push "balanceOf(Uint64)"; +push "balanceOf(Uint160)"; call :: bytes_hooks :: append_str; return_value_to_arg; -push 1010101_u64; -call :: bytes_hooks :: append_u64; +push 1010101_u160; +call :: bytes_hooks :: append_u160; return_value; mock CallData; @@ -68,27 +68,27 @@ push_status; check_eq 2; output_to_arg; -call :: test_helpers :: decode_single_u64; +call :: test_helpers :: decode_single_u256; return_value; -check_eq 10000_u64; +check_eq 10000_u256; call :: bytes_hooks :: empty; return_value_to_arg; -push "transfer(Uint64,Uint64)"; +push "transfer(Uint160,Uint256)"; call :: bytes_hooks :: append_str; return_value_to_arg; -push 2020202_u64; -call :: bytes_hooks :: append_u64; +push 2020202_u160; +call :: bytes_hooks :: append_u160; return_value_to_arg; -push 100_u64; -call :: bytes_hooks :: append_u64; +push 100_u256; +call :: bytes_hooks :: append_u256; return_value; mock CallData; -push_value 1010101_u64; +push_value 1010101_u160; mock Caller; call_contract 12345; @@ -108,11 +108,11 @@ check_eq 1_u64; call :: bytes_hooks :: empty; return_value_to_arg; -push "balanceOf(Uint64)"; +push "balanceOf(Uint160)"; call :: bytes_hooks :: append_str; return_value_to_arg; -push 1010101_u64; -call :: bytes_hooks :: append_u64; +push 1010101_u160; +call :: bytes_hooks :: append_u160; return_value; mock CallData; @@ -124,21 +124,21 @@ push_status; check_eq 2; output_to_arg; -call :: test_helpers :: decode_single_u64; +call :: test_helpers :: decode_single_u256; return_value; -check_eq 9900_u64; +check_eq 9900_u256; call :: bytes_hooks :: empty; return_value_to_arg; -push "balanceOf(Uint64)"; +push "balanceOf(Uint160)"; call :: bytes_hooks :: append_str; return_value_to_arg; -push 2020202_u64; -call :: bytes_hooks :: append_u64; +push 2020202_u160; +call :: bytes_hooks :: append_u160; return_value; mock CallData; @@ -150,10 +150,10 @@ push_status; check_eq 2; output_to_arg; -call :: test_helpers :: decode_single_u64; +call :: test_helpers :: decode_single_u256; return_value; -check_eq 100_u64; +check_eq 100_u256; @@ -162,18 +162,18 @@ check_eq 100_u64; call :: bytes_hooks :: empty; return_value_to_arg; -push "approve(Uint64,Uint64)"; +push "approve(Uint160,Uint256)"; call :: bytes_hooks :: append_str; return_value_to_arg; -push 3030303_u64; -call :: bytes_hooks :: append_u64; +push 3030303_u160; +call :: bytes_hooks :: append_u160; return_value_to_arg; -push 200_u64; -call :: bytes_hooks :: append_u64; +push 200_u256; +call :: bytes_hooks :: append_u256; return_value; mock CallData; -push_value 1010101_u64; +push_value 1010101_u160; mock Caller; call_contract 12345; @@ -193,21 +193,21 @@ check_eq 1_u64; call :: bytes_hooks :: empty; return_value_to_arg; -push "transferFrom(Uint64,Uint64,Uint64)"; +push "transferFrom(Uint160,Uint160,Uint256)"; call :: bytes_hooks :: append_str; return_value_to_arg; -push 1010101_u64; -call :: bytes_hooks :: append_u64; +push 1010101_u160; +call :: bytes_hooks :: append_u160; return_value_to_arg; -push 2020202_u64; -call :: bytes_hooks :: append_u64; +push 2020202_u160; +call :: bytes_hooks :: append_u160; return_value_to_arg; -push 200_u64; -call :: bytes_hooks :: append_u64; +push 200_u256; +call :: bytes_hooks :: append_u256; return_value; mock CallData; -push_value 3030303_u64; +push_value 3030303_u160; mock Caller; call_contract 12345; @@ -229,11 +229,11 @@ check_eq 1_u64; call :: bytes_hooks :: empty; return_value_to_arg; -push "balanceOf(Uint64)"; +push "balanceOf(Uint160)"; call :: bytes_hooks :: append_str; return_value_to_arg; -push 1010101_u64; -call :: bytes_hooks :: append_u64; +push 1010101_u160; +call :: bytes_hooks :: append_u160; return_value; mock CallData; @@ -245,21 +245,21 @@ push_status; check_eq 2; output_to_arg; -call :: test_helpers :: decode_single_u64; +call :: test_helpers :: decode_single_u256; return_value; -check_eq 9700_u64; +check_eq 9700_u256; call :: bytes_hooks :: empty; return_value_to_arg; -push "balanceOf(Uint64)"; +push "balanceOf(Uint160)"; call :: bytes_hooks :: append_str; return_value_to_arg; -push 2020202_u64; -call :: bytes_hooks :: append_u64; +push 2020202_u160; +call :: bytes_hooks :: append_u160; return_value; mock CallData; @@ -271,9 +271,9 @@ push_status; check_eq 2; output_to_arg; -call :: test_helpers :: decode_single_u64; +call :: test_helpers :: decode_single_u256; return_value; -check_eq 300_u64 +check_eq 300_u256 diff --git a/tests/ukm-with-contract/erc_20_token.rs b/tests/ukm-with-contract/erc_20_token.rs index 77c01ab..f88b02a 100644 --- a/tests/ukm-with-contract/erc_20_token.rs +++ b/tests/ukm-with-contract/erc_20_token.rs @@ -32,7 +32,7 @@ Observations: pub trait Erc20Token { // #[view(totalSupply)] #[storage_mapper("total_supply")] - fn s_total_supply(&self) -> ::single_value_mapper::SingleValueMapper; + fn s_total_supply(&self) -> ::single_value_mapper::SingleValueMapper; // #[view(getName)] #[storage_mapper("name")] @@ -44,21 +44,21 @@ pub trait Erc20Token { // #[view(getBalances)] #[storage_mapper("balances")] - fn s_balances(&self, address: u64) -> ::single_value_mapper::SingleValueMapper; + fn s_balances(&self, address: u160) -> ::single_value_mapper::SingleValueMapper; // #[view(getAllowances)] #[storage_mapper("balances")] - fn s_allowances(&self, account: u64, spender: u64) -> ::single_value_mapper::SingleValueMapper; + fn s_allowances(&self, account: u160, spender: u160) -> ::single_value_mapper::SingleValueMapper; #[event("Transfer")] - fn transfer_event(&self, #[indexed] from: u64, #[indexed] to: u64, value: u64); + fn transfer_event(&self, #[indexed] from: u160, #[indexed] to: u160, value: u256); #[event("Approval")] - fn approval_event(&self, #[indexed] owner: u64, #[indexed] spender: u64, value: u64); + fn approval_event(&self, #[indexed] owner: u160, #[indexed] spender: u160, value: u256); #[init] - fn init(&self, /*name: &ManagedBuffer, symbol: &ManagedBuffer, */init_supply: u64) { + fn init(&self, /*name: &ManagedBuffer, symbol: &ManagedBuffer, */init_supply: u256) { // self.s_name().set_if_empty(name); // self.s_symbol().set_if_empty(symbol); self._mint(::ukm::Caller(), init_supply); @@ -72,6 +72,11 @@ pub trait Erc20Token { 18 } + #[view(totalSupply)] + fn total_supply(&self) -> u256 { + self.s_total_supply().get() + } + // #[view(name)] // fn name(&self) -> ManagedBuffer { // self.s_name().get() @@ -83,45 +88,45 @@ pub trait Erc20Token { // } #[view(balanceOf)] - fn balance_of(&self, account: u64) -> u64 { + fn balance_of(&self, account: u160) -> u256 { self.s_balances(account).get() } #[endpoint(transfer)] - fn transfer(&self, to: u64, value: u64) -> bool { + fn transfer(&self, to: u160, value: u256) -> bool { let owner = ::ukm::Caller(); self._transfer(&owner, to, &value); true } #[view(allowance)] - fn allowance(&self, owner: u64, spender: u64) -> u64 { + fn allowance(&self, owner: u160, spender: u160) -> u256 { self.s_allowances(owner, spender).get() } #[endpoint(approve)] - fn approve(&self, spender: u64, value: u64) -> bool { + fn approve(&self, spender: u160, value: u256) -> bool { let owner = ::ukm::Caller(); self._approve(&owner, spender, value, true); true } #[endpoint(transferFrom)] - fn transfer_from(&self, from: u64, to: u64, value: u64) -> bool { + fn transfer_from(&self, from: u160, to: u160, value: u256) -> bool { let spender = ::ukm::Caller(); self._spend_allowance(from, &spender, value); self._transfer(from, to, value); true } - fn _transfer(&self, from: u64, to: u64, value: u64) { + fn _transfer(&self, from: u160, to: u160, value: u256) { ::helpers::require(!::address::is_zero(from), "Invalid sender"); ::helpers::require(!::address::is_zero(to), "Invalid receiver"); self._update(from, to, value); self.transfer_event(from, to, value); } - fn _update(&self, from: u64, to: u64, value: u64) { + fn _update(&self, from: u160, to: u160, value: u256) { if ::address::is_zero(from) { self.s_total_supply().set(self.s_total_supply().get() + value); } else { @@ -137,12 +142,12 @@ pub trait Erc20Token { } } - fn _mint(&self, account: u64, value: u64) { + fn _mint(&self, account: u160, value: u256) { ::helpers::require(!::address::is_zero(account), "Zero address"); - self._update(0_u64, account, value); + self._update(0_u160, account, value); } - fn _approve(&self, owner: u64, spender: u64, value: u64, emit_event: bool) { + fn _approve(&self, owner: u160, spender: u160, value: u256, emit_event: bool) { ::helpers::require(!::address::is_zero(owner), "Invalid approver"); ::helpers::require(!::address::is_zero(spender), "Invalid spender"); self.s_allowances(owner, spender).set(value); @@ -151,7 +156,7 @@ pub trait Erc20Token { } } - fn _spend_allowance(&self, owner: u64, spender: u64, value: u64) { + fn _spend_allowance(&self, owner: u160, spender: u160, value: u256) { let current_allowance = self.allowance(owner, spender); ::helpers::require(value <= ¤t_allowance, "Insuficient allowance"); self._approve(owner, spender, &(current_allowance - value), false); diff --git a/ukm-semantics/main/hooks/ukm.md b/ukm-semantics/main/hooks/ukm.md index 94f5151..fa02046 100644 --- a/ukm-semantics/main/hooks/ukm.md +++ b/ukm-semantics/main/hooks/ukm.md @@ -10,6 +10,7 @@ module UKM-HOOKS-UKM-SYNTAX syntax UkmHookResult ::= ukmNoResult() | ukmInt64Result(Int) + | ukmInt256Result(Int) endmodule @@ -52,10 +53,15 @@ module UKM-HOOKS-UKM => GetAccountStorageHook(MInt2Unsigned(Key)) rule ukmNoResult() => ptrValue(null, tuple(.ValueList)) + syntax UkmHook ::= #ukmInt64Result(ValueOrError) rule ukmInt64Result(Value:Int) => #ukmInt64Result(integerToValue(Value, u64)) rule #ukmInt64Result(V:Value) => ptrValue(null, V) + syntax UkmHook ::= #ukmInt256Result(ValueOrError) + rule ukmInt256Result(Value:Int) => #ukmInt256Result(integerToValue(Value, u256)) + rule #ukmInt256Result(V:Value) => ptrValue(null, V) + endmodule ``` diff --git a/ukm-semantics/test/execution.md b/ukm-semantics/test/execution.md index fef9839..20f1a61 100644 --- a/ukm-semantics/test/execution.md +++ b/ukm-semantics/test/execution.md @@ -39,7 +39,7 @@ module UKM-TEST-EXECUTION rule mock Caller => mock(CallerHook(), V) ... - (ListItem(ptrValue(_, u64(_BytesId)) #as V:PtrValue) => .List) + (ListItem(ptrValue(_, u160(_AccountId)) #as V:PtrValue) => .List) ...