From 86882404d48b0542f75141f90f1a6f22d7ccf941 Mon Sep 17 00:00:00 2001 From: Jorge Silva Date: Mon, 27 Nov 2023 13:57:49 +0000 Subject: [PATCH] fix: revert panic on non-0 remainder EVM --- packages/vm/core/evm/emulator/statedb.go | 13 ++----------- packages/vm/core/evm/evmimpl/iscmagic_sandbox.go | 6 +----- packages/vm/core/evm/evmtest/evm_test.go | 10 +++++----- 3 files changed, 8 insertions(+), 21 deletions(-) diff --git a/packages/vm/core/evm/emulator/statedb.go b/packages/vm/core/evm/emulator/statedb.go index de6c7a26d5..7506d527b2 100644 --- a/packages/vm/core/evm/emulator/statedb.go +++ b/packages/vm/core/evm/emulator/statedb.go @@ -17,7 +17,6 @@ import ( "github.com/iotaledger/wasp/packages/kv" "github.com/iotaledger/wasp/packages/kv/codec" "github.com/iotaledger/wasp/packages/util" - "github.com/iotaledger/wasp/packages/vm/core/errors/coreerrors" ) const ( @@ -75,8 +74,6 @@ func (s *StateDB) CreateAccount(addr common.Address) { CreateAccount(s.kv, addr) } -var ErrNonZeroWeiRemainder = coreerrors.Register("cannot convert %d to base tokens decimals: non-zero remainder (%d)") - func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) { if amount.Sign() == 0 { return @@ -84,10 +81,7 @@ func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) { if amount.Sign() == -1 { panic("unexpected negative amount") } - baseTokens, remainder := util.EthereumDecimalsToBaseTokenDecimals(amount, s.ctx.BaseTokensDecimals()) - if remainder.Sign() != 0 { - panic(ErrNonZeroWeiRemainder.Create(amount.Uint64(), remainder.Uint64())) - } + baseTokens, _ := util.EthereumDecimalsToBaseTokenDecimals(amount, s.ctx.BaseTokensDecimals()) s.ctx.SubBaseTokensBalance(addr, baseTokens) } @@ -98,10 +92,7 @@ func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) { if amount.Sign() == -1 { panic("unexpected negative amount") } - baseTokens, remainder := util.EthereumDecimalsToBaseTokenDecimals(amount, s.ctx.BaseTokensDecimals()) - if remainder.Sign() != 0 { - panic(ErrNonZeroWeiRemainder.Create(amount.Uint64(), remainder.Uint64())) - } + baseTokens, _ := util.EthereumDecimalsToBaseTokenDecimals(amount, s.ctx.BaseTokensDecimals()) s.ctx.AddBaseTokensBalance(addr, baseTokens) } diff --git a/packages/vm/core/evm/evmimpl/iscmagic_sandbox.go b/packages/vm/core/evm/evmimpl/iscmagic_sandbox.go index d682ded8da..e3a8e768df 100644 --- a/packages/vm/core/evm/evmimpl/iscmagic_sandbox.go +++ b/packages/vm/core/evm/evmimpl/iscmagic_sandbox.go @@ -16,7 +16,6 @@ import ( "github.com/iotaledger/wasp/packages/util" "github.com/iotaledger/wasp/packages/vm/core/errors/coreerrors" "github.com/iotaledger/wasp/packages/vm/core/evm" - "github.com/iotaledger/wasp/packages/vm/core/evm/emulator" "github.com/iotaledger/wasp/packages/vm/core/evm/iscmagic" ) @@ -59,10 +58,7 @@ func (h *magicContractHandler) TakeAllowedFunds(addr common.Address, allowance i var errInvalidAllowance = coreerrors.Register("allowance must not be greater than sent tokens").Create() func (h *magicContractHandler) handleCallValue(callValue *big.Int) uint64 { - adjustedTxValue, remainder := util.EthereumDecimalsToBaseTokenDecimals(callValue, parameters.L1().BaseToken.Decimals) - if remainder.Sign() != 0 { - panic(emulator.ErrNonZeroWeiRemainder.Create(callValue, remainder.Uint64())) - } + adjustedTxValue, _ := util.EthereumDecimalsToBaseTokenDecimals(callValue, parameters.L1().BaseToken.Decimals) evmAddr := isc.NewEthereumAddressAgentID(h.ctx.ChainID(), iscmagic.Address) caller := isc.NewEthereumAddressAgentID(h.ctx.ChainID(), h.caller.Address()) diff --git a/packages/vm/core/evm/evmtest/evm_test.go b/packages/vm/core/evm/evmtest/evm_test.go index 98eff99cfd..13706e4679 100644 --- a/packages/vm/core/evm/evmtest/evm_test.go +++ b/packages/vm/core/evm/evmtest/evm_test.go @@ -2179,20 +2179,20 @@ func TestDecimalsConversion(t *testing.T) { iscTest := env.deployISCTestContract(ethKey) // call any function including 999999999999 wei as value (which is just 1 wei short of 1 base token) - lessThanOneSMR := new(big.Int).SetUint64(999999999999) + lessThanOneGlow := new(big.Int).SetUint64(999999999999) valueInBaseTokens, remainder := util.EthereumDecimalsToBaseTokenDecimals( - lessThanOneSMR, + lessThanOneGlow, parameters.L1().BaseToken.Decimals, ) t.Log(valueInBaseTokens) require.Zero(t, valueInBaseTokens) - require.EqualValues(t, lessThanOneSMR.Uint64(), remainder.Uint64()) + require.EqualValues(t, lessThanOneGlow.Uint64(), remainder.Uint64()) _, err := iscTest.CallFn( - []ethCallOptions{{sender: ethKey, value: lessThanOneSMR, gasLimit: 100000}}, + []ethCallOptions{{sender: ethKey, value: lessThanOneGlow, gasLimit: 100000}}, "sendTo", iscTest.address, big.NewInt(0), ) - require.ErrorContains(t, err, "non-zero remainder") + require.ErrorContains(t, err, "execution reverted") }