Skip to content

Commit

Permalink
Fix for assert equal big int (#83)
Browse files Browse the repository at this point in the history
* first fix

* fix for all tests

* lint fix

* logical test fix

* Comment fix

* lint fix

* Minor change

---------

Co-authored-by: Stefan Negovanović <[email protected]>
  • Loading branch information
dusannosovic-ethernal and Stefan-Ethernal authored Feb 5, 2024
1 parent 40fa03d commit 1510c10
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 39 deletions.
92 changes: 53 additions & 39 deletions state/runtime/evm/instructions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

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

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

Expand All @@ -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)
})
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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())
})
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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})
Expand All @@ -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{})
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -1048,21 +1062,21 @@ 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) {
s.memory = make([]byte, memorySize)

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())
})
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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())
})
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions state/runtime/evm/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 1510c10

Please sign in to comment.