Skip to content

Commit

Permalink
Merge pull request #3115 from jorgemmsilva/fix/revert-decimals-panic
Browse files Browse the repository at this point in the history
fix: revert panic on non-0 remainder EVM
  • Loading branch information
jorgemmsilva authored Nov 27, 2023
2 parents ea396b2 + 8688240 commit 746d22b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 21 deletions.
13 changes: 2 additions & 11 deletions packages/vm/core/evm/emulator/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -75,19 +74,14 @@ 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
}
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)
}

Expand All @@ -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)
}

Expand Down
6 changes: 1 addition & 5 deletions packages/vm/core/evm/evmimpl/iscmagic_sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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())
Expand Down
10 changes: 5 additions & 5 deletions packages/vm/core/evm/evmtest/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit 746d22b

Please sign in to comment.