From 1510c10aa7163ffd455b85c9ea9e34b8cdcacfee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Nosovi=C4=87?= <118283942+dusannosovic-ethernal@users.noreply.github.com> Date: Mon, 5 Feb 2024 08:17:45 +0100 Subject: [PATCH] Fix for assert equal big int (#83) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * first fix * fix for all tests * lint fix * logical test fix * Comment fix * lint fix * Minor change --------- Co-authored-by: Stefan Negovanović --- state/runtime/evm/instructions_test.go | 92 +++++++++++++++----------- state/runtime/evm/state.go | 4 ++ 2 files changed, 57 insertions(+), 39 deletions(-) diff --git a/state/runtime/evm/instructions_test.go b/state/runtime/evm/instructions_test.go index 95bab07923..1e75aa4407 100644 --- a/state/runtime/evm/instructions_test.go +++ b/state/runtime/evm/instructions_test.go @@ -38,9 +38,9 @@ func testLogicalOperation(t *testing.T, f instruction, test OperandsLogical, s * f(s) if test.expectedResult { - assert.Equal(t, uint64(1), s.pop().Uint64()) + assert.Equal(t, one, s.pop()) } else { - assert.Equal(t, uint64(0), s.pop().Uint64()) + assert.Equal(t, zero, s.pop()) } } @@ -58,7 +58,7 @@ func testArithmeticOperation(t *testing.T, f instruction, test OperandsArithmeti f(s) - assert.EqualValues(t, test.expectedResult.Uint64(), s.pop().Uint64()) + assert.Equal(t, test.expectedResult, s.pop()) } func TestAdd(t *testing.T) { @@ -464,10 +464,17 @@ func TestSignExtension(t *testing.T) { s, cancelFn := getState(&chain.ForksInTime{}) defer cancelFn() + firstValue, ok := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007913129639808", 10) + require.True(t, ok) + secondValue, ok := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007913129607168", 10) + require.True(t, ok) + thirdValue, ok := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007913121251328", 10) + require.True(t, ok) + testOperands := []OperandsArithmetic{ - {[]*big.Int{big.NewInt(128), zero}, new(big.Int).SetUint64(18446744073709551488)}, - {[]*big.Int{big.NewInt(32768), one}, new(big.Int).SetUint64(18446744073709518848)}, - {[]*big.Int{big.NewInt(8388608), two}, new(big.Int).SetUint64(18446744073701163008)}, + {[]*big.Int{big.NewInt(128), zero}, firstValue}, + {[]*big.Int{big.NewInt(32768), one}, secondValue}, + {[]*big.Int{big.NewInt(8388608), two}, thirdValue}, } for _, testOperand := range testOperands { @@ -495,10 +502,13 @@ func TestNot(t *testing.T) { defer closeFn() testOperands := []OperandsArithmetic{ - {[]*big.Int{zero}, new(big.Int).SetUint64(uint64(math.MaxUint64))}, - {[]*big.Int{one}, new(big.Int).SetUint64(uint64(math.MaxUint64) - 1)}, + {[]*big.Int{big.NewInt(-1)}, zero}, + {[]*big.Int{zero}, tt256m1}, + {[]*big.Int{one}, new(big.Int).Sub(tt256m1, big.NewInt(1))}, + {[]*big.Int{big.NewInt(10)}, new(big.Int).Sub(tt256m1, big.NewInt(10))}, } for _, testOperand := range testOperands { + t.Log(testOperand.expectedResult) testArithmeticOperation(t, opNot, testOperand, s) } } @@ -533,7 +543,7 @@ func TestMStore(t *testing.T) { opMLoad(s) - assert.Equal(t, one.Uint64(), s.pop().Uint64()) + assert.Equal(t, one, s.pop()) } func TestMStore8(t *testing.T) { @@ -552,7 +562,7 @@ func TestMStore8(t *testing.T) { opMLoad(s) - assert.Equal(t, one.Uint64(), s.pop().Uint64()) + assert.Equal(t, one, s.pop()) } func TestSload(t *testing.T) { @@ -714,7 +724,7 @@ func TestBalance(t *testing.T) { opBalance(s) - assert.EqualValues(t, balance, s.pop()) + assert.Equal(t, balance, s.pop()) assert.Equal(t, gasLeft, s.gas) }) @@ -728,7 +738,7 @@ func TestBalance(t *testing.T) { opBalance(s) - assert.Equal(t, int64(100), s.pop().Int64()) + assert.Equal(t, big.NewInt(100), s.pop()) assert.Equal(t, gasLeft, s.gas) }) @@ -742,7 +752,7 @@ func TestBalance(t *testing.T) { opBalance(s) - assert.EqualValues(t, balance, s.pop()) + assert.Equal(t, balance, s.pop()) assert.Equal(t, gasLeft, s.gas) }) } @@ -760,7 +770,7 @@ func TestSelfBalance(t *testing.T) { opSelfBalance(s) - assert.Equal(t, int64(100), s.pop().Int64()) + assert.Equal(t, big.NewInt(100), s.pop()) }) t.Run("NoForkErrorExpected", func(t *testing.T) { @@ -791,7 +801,7 @@ func TestChainID(t *testing.T) { opChainID(s) - assert.Equal(t, chainID, s.pop().Int64()) + assert.Equal(t, big.NewInt(chainID), s.pop()) }) t.Run("NoForksErrorExpected", func(t *testing.T) { s, cancelFn := getState(&chain.ForksInTime{}) @@ -855,26 +865,30 @@ func TestCallValue(t *testing.T) { defer cancelFn() opCallValue(s) - assert.Equal(t, uint64(0), s.pop().Uint64()) + assert.Equal(t, zero, s.pop()) }) } func TestCallDataLoad(t *testing.T) { - t.Run("Zero", func(t *testing.T) { + t.Run("NonZeroOffset", func(t *testing.T) { s, cancelFn := getState(&chain.ForksInTime{}) defer cancelFn() s.push(one) + s.msg = &runtime.Contract{Input: big.NewInt(7).Bytes()} + opCallDataLoad(s) - assert.Equal(t, zero.Uint64(), s.pop().Uint64()) + assert.Equal(t, zero, s.pop()) }) - t.Run("NotZero", func(t *testing.T) { + t.Run("ZeroOffset", func(t *testing.T) { s, cancelFn := getState(&chain.ForksInTime{}) defer cancelFn() s.push(zero) + s.msg = &runtime.Contract{Input: big.NewInt(7).Bytes()} + opCallDataLoad(s) assert.NotEqual(t, zero, s.pop()) }) @@ -887,7 +901,7 @@ func TestCallDataSize(t *testing.T) { s.msg.Input = make([]byte, 10) opCallDataSize(s) - assert.Equal(t, uint64(10), s.pop().Uint64()) + assert.Equal(t, big.NewInt(10), s.pop()) } func TestCodeSize(t *testing.T) { @@ -897,7 +911,7 @@ func TestCodeSize(t *testing.T) { s.code = make([]byte, 10) opCodeSize(s) - assert.Equal(t, uint64(10), s.pop().Uint64()) + assert.Equal(t, big.NewInt(10), s.pop()) } func TestExtCodeSize(t *testing.T) { @@ -917,7 +931,7 @@ func TestExtCodeSize(t *testing.T) { opExtCodeSize(s) assert.Equal(t, gasLeft, s.gas) - assert.Equal(t, uint64(codeSize), s.pop().Uint64()) + assert.Equal(t, big.NewInt(int64(codeSize)), s.pop()) }) t.Run("NoForks", func(t *testing.T) { gasLeft := uint64(980) @@ -934,27 +948,27 @@ func TestExtCodeSize(t *testing.T) { opExtCodeSize(s) assert.Equal(t, gasLeft, s.gas) - assert.Equal(t, uint64(codeSize), s.pop().Uint64()) + assert.Equal(t, big.NewInt(int64(codeSize)), s.pop()) }) } func TestGasPrice(t *testing.T) { - gasPrice := 10 + gasPrice := int64(10) s, cancelFn := getState(&chain.ForksInTime{}) defer cancelFn() mockHost := &mockHost{} - mockHost.On("GetTxContext").Return(runtime.TxContext{GasPrice: bigToHash(big.NewInt(int64(gasPrice)))}).Once() + mockHost.On("GetTxContext").Return(runtime.TxContext{GasPrice: bigToHash(big.NewInt(gasPrice))}).Once() s.host = mockHost opGasPrice(s) - assert.Equal(t, bigToHash(big.NewInt(int64(gasPrice))), s.popHash()) + assert.Equal(t, bigToHash(big.NewInt(gasPrice)), s.popHash()) } func TestReturnDataSize(t *testing.T) { - dataSize := uint64(1024) + dataSize := int64(1024) t.Run("Byzantium", func(t *testing.T) { s, cancelFn := getState(&chain.ForksInTime{Byzantium: true}) @@ -964,7 +978,7 @@ func TestReturnDataSize(t *testing.T) { opReturnDataSize(s) - assert.Equal(t, dataSize, s.pop().Uint64()) + assert.Equal(t, big.NewInt(dataSize), s.pop()) }) t.Run("NoForks", func(t *testing.T) { s, cancelFn := getState(&chain.ForksInTime{}) @@ -999,7 +1013,7 @@ func TestExtCodeHash(t *testing.T) { opExtCodeHash(s) assert.Equal(t, s.gas, gasLeft) - assert.Equal(t, one.Uint64(), s.pop().Uint64()) + assert.Equal(t, one, s.pop()) }) t.Run("NonIstanbul", func(t *testing.T) { @@ -1018,7 +1032,7 @@ func TestExtCodeHash(t *testing.T) { opExtCodeHash(s) assert.Equal(t, gasLeft, s.gas) - assert.Equal(t, zero.Int64(), s.pop().Int64()) + assert.Equal(t, zero, s.pop()) }) t.Run("NoForks", func(t *testing.T) { @@ -1048,7 +1062,7 @@ func TestPCMSizeGas(t *testing.T) { s.ip = 1 opPC(s) - assert.Equal(t, uint64(1), s.pop().Uint64()) + assert.Equal(t, one, s.pop()) }) t.Run("MSize", func(t *testing.T) { @@ -1056,13 +1070,13 @@ func TestPCMSizeGas(t *testing.T) { opMSize(s) - assert.Equal(t, memorySize, s.pop().Uint64()) + assert.Equal(t, new(big.Int).SetUint64(memorySize), s.pop()) }) t.Run("Gas", func(t *testing.T) { opGas(s) - assert.Equal(t, gasLeft, s.pop().Uint64()) + assert.Equal(t, new(big.Int).SetUint64(gasLeft), s.pop()) }) } @@ -1197,7 +1211,7 @@ func TestTimeStamp(t *testing.T) { opTimestamp(s) - assert.Equal(t, uint64(335), s.pop().Uint64()) + assert.Equal(t, big.NewInt(335), s.pop()) } func TestNumber(t *testing.T) { @@ -1210,7 +1224,7 @@ func TestNumber(t *testing.T) { opNumber(s) - assert.Equal(t, uint64(5), s.pop().Uint64()) + assert.Equal(t, five, s.pop()) } func TestDifficulty(t *testing.T) { @@ -1251,7 +1265,7 @@ func TestGasLimit(t *testing.T) { opBaseFee(s) - assert.Equal(t, baseFee, s.pop().Uint64()) + assert.Equal(t, new(big.Int).SetUint64(baseFee), s.pop()) }) } @@ -1320,7 +1334,7 @@ func TestDup(t *testing.T) { instr := opDup(4) instr(s) - assert.Equal(t, uint64(2), s.pop().Uint64()) + assert.Equal(t, two, s.pop()) } func TestSwap(t *testing.T) { @@ -1336,8 +1350,8 @@ func TestSwap(t *testing.T) { instr := opSwap(4) instr(s) - assert.Equal(t, uint64(5), s.stack[1].Uint64()) - assert.Equal(t, uint64(1), s.stack[6-1].Uint64()) + assert.Equal(t, five, s.stack[1]) + assert.Equal(t, one, s.stack[6-1]) } func TestLog(t *testing.T) { diff --git a/state/runtime/evm/state.go b/state/runtime/evm/state.go index 92a8876464..72c1b8d70e 100644 --- a/state/runtime/evm/state.go +++ b/state/runtime/evm/state.go @@ -180,6 +180,10 @@ func (c *state) pop() *big.Int { o := c.stack[c.sp-1] c.sp-- + if o.Cmp(zero) == 0 { + return big.NewInt(0) + } + return o }