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

feat: vm.MutableStack wrapper #31

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions core/vm/stack.libevm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package vm

import "github.com/holiman/uint256"

// A MutableStack embeds a Stack to expose unexported mutation methods.
type MutableStack struct {
*Stack
}

// Push pushes a value to the stack.
func (s MutableStack) Push(d *uint256.Int) { s.Stack.push(d) }

// Pop pops a value from the stack.
func (s MutableStack) Pop() uint256.Int { return s.Stack.pop() }
27 changes: 27 additions & 0 deletions core/vm/stack.libevm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package vm_test

import (
"testing"

"github.com/holiman/uint256"
"github.com/stretchr/testify/require"

"github.com/ethereum/go-ethereum/core/vm"
)

func TestMutableStack(t *testing.T) {
s := &vm.Stack{}
m := vm.MutableStack{Stack: s}

push := func(u uint64) uint256.Int {
u256 := uint256.NewInt(u)
m.Push(u256)
return *u256
}

require.Empty(t, s.Data(), "new stack")
want := []uint256.Int{push(42), push(314159), push(142857)}
require.Equalf(t, want, s.Data(), "after pushing %d values to empty stack", len(want))
require.Equal(t, want[len(want)-1], m.Pop(), "popped value")
require.Equal(t, want[:len(want)-1], s.Data(), "after popping a single value")
}