Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce EIP-3855 and PUSH0 opcode #59

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion chain/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const (
EIP158 = "EIP158"
EIP155 = "EIP155"
Governance = "governance"
EIP3855 = "EIP3855"
)

// Forks is map which contains all forks and their starting blocks from genesis
Expand Down Expand Up @@ -128,6 +129,7 @@ func (f *Forks) At(block uint64) ForksInTime {
EIP158: f.IsActive(EIP158, block),
EIP155: f.IsActive(EIP155, block),
Governance: f.IsActive(Governance, block),
EIP3855: f.IsActive(EIP3855, block),
}
}

Expand Down Expand Up @@ -178,7 +180,8 @@ type ForksInTime struct {
EIP150,
EIP158,
EIP155,
Governance bool
Governance,
EIP3855 bool
}

// AllForksEnabled should contain all supported forks by current edge version
Expand All @@ -193,4 +196,5 @@ var AllForksEnabled = &Forks{
Istanbul: NewFork(0),
London: NewFork(0),
Governance: NewFork(0),
EIP3855: NewFork(0),
}
1 change: 1 addition & 0 deletions state/runtime/evm/dispatch_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func init() {
register(SMOD, handler{opSMod, 2, 5})
register(EXP, handler{opExp, 2, 10})

register(PUSH0, handler{opPush0, 0, 2})
registerRange(PUSH1, PUSH32, opPush, 3)
registerRange(DUP1, DUP16, opDup, 3)
registerRange(SWAP1, SWAP16, opSwap, 3)
Expand Down
10 changes: 10 additions & 0 deletions state/runtime/evm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,16 @@ func opJumpi(c *state) {
func opJumpDest(c *state) {
}

func opPush0(c *state) {
if !c.config.EIP3855 {
c.exit(errOpCodeNotFound)

return
}

c.push(zero)
}

func opPush(n int) instruction {
return func(c *state) {
ins := c.code
Expand Down
95 changes: 77 additions & 18 deletions state/runtime/evm/instructions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/0xPolygon/polygon-edge/state/runtime"
"github.com/0xPolygon/polygon-edge/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var (
Expand All @@ -18,47 +19,47 @@ var (
allEnabledForks = chain.AllForksEnabled.At(0)
)

type cases2To1 []struct {
a *big.Int
b *big.Int
c *big.Int
type twoOperandsArithmetic []struct {
a *big.Int
b *big.Int
expectedResult *big.Int
}

func test2to1(t *testing.T, f instruction, tests cases2To1) {
func testArithmeticOperation(t *testing.T, f instruction, tests twoOperandsArithmetic) {
t.Helper()

s, closeFn := getState()
defer closeFn()

for _, i := range tests {
s.push(i.a)
s.push(i.b)
s.push(i.a)

f(s)

assert.Equal(t, i.c, s.pop())
assert.EqualValues(t, i.expectedResult, s.pop())
}
}

type cases2ToBool []struct {
a *big.Int
b *big.Int
c bool
type twoOperandsLogical []struct {
a *big.Int
b *big.Int
expectedResult bool
}

func test2toBool(t *testing.T, f instruction, tests cases2ToBool) {
func testLogicalOperation(t *testing.T, f instruction, tests twoOperandsLogical) {
t.Helper()

s, closeFn := getState()
defer closeFn()

for _, i := range tests {
s.push(i.a)
s.push(i.b)
s.push(i.a)

f(s)

if i.c {
if i.expectedResult {
assert.Equal(t, uint64(1), s.pop().Uint64())
} else {
assert.Equal(t, uint64(0), s.pop().Uint64())
Expand All @@ -67,22 +68,80 @@ func test2toBool(t *testing.T, f instruction, tests cases2ToBool) {
}

func TestAdd(t *testing.T) {
test2to1(t, opAdd, cases2To1{
testArithmeticOperation(t, opAdd, twoOperandsArithmetic{
{one, one, two},
{zero, one, one},
})
}

func TestMul(t *testing.T) {
testArithmeticOperation(t, opMul, twoOperandsArithmetic{
{two, two, big.NewInt(4)},
{big.NewInt(3), two, big.NewInt(6)},
})
}

func TestSub(t *testing.T) {
testArithmeticOperation(t, opSub, twoOperandsArithmetic{
{two, one, one},
{two, zero, two},
})
}

func TestPush0(t *testing.T) {
t.Run("single push0 success", func(t *testing.T) {
s, closeFn := getState()
s.config = &allEnabledForks
defer closeFn()

opPush0(s)
assert.Equal(t, zero, s.pop())
})

t.Run("single push0 (EIP-3855 disabled)", func(t *testing.T) {
s, closeFn := getState()
disabledEIP3855Fork := chain.AllForksEnabled.Copy().RemoveFork(chain.EIP3855).At(0)
s.config = &disabledEIP3855Fork
defer closeFn()

opPush0(s)
assert.Error(t, errOpCodeNotFound, s.err)
})

t.Run("within stack size push0", func(t *testing.T) {
s, closeFn := getState()
s.config = &allEnabledForks
defer closeFn()

for i := 0; i < stackSize; i++ {
opPush0(s)
require.NoError(t, s.err)
}

for i := 0; i < stackSize; i++ {
require.Equal(t, zero, s.pop())
}
})
}

func TestGt(t *testing.T) {
test2toBool(t, opGt, cases2ToBool{
testLogicalOperation(t, opGt, twoOperandsLogical{
{one, one, false},
{one, two, false},
{two, one, true},
})
}

func TestLt(t *testing.T) {
testLogicalOperation(t, opLt, twoOperandsLogical{
{one, one, false},
{two, one, false},
{one, two, true},
{two, one, false},
})
}

func TestIsZero(t *testing.T) {
test2toBool(t, opIsZero, cases2ToBool{
testLogicalOperation(t, opIsZero, twoOperandsLogical{
{one, one, false},
{zero, zero, true},
{two, two, false},
Expand Down
3 changes: 3 additions & 0 deletions state/runtime/evm/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ const (
// JUMPDEST corresponds to a possible jump destination
JUMPDEST = 0x5B

// PUSH0 pushes a 0 constant onto the stack
PUSH0 = 0x5F

// PUSH1 pushes a 1-byte value onto the stack
PUSH1 = 0x60

Expand Down
Loading