From 1166b6858435ec2911d51ef3b740f23c8d76e38f Mon Sep 17 00:00:00 2001 From: Arran Schlosberg Date: Wed, 18 Sep 2024 08:47:08 -0400 Subject: [PATCH 1/2] feat: `vm.MutableStack` wrapper --- core/vm/stack.libevm.go | 14 ++++++++++++++ core/vm/stack.libevm_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 core/vm/stack.libevm.go create mode 100644 core/vm/stack.libevm_test.go diff --git a/core/vm/stack.libevm.go b/core/vm/stack.libevm.go new file mode 100644 index 000000000000..bc16735276cd --- /dev/null +++ b/core/vm/stack.libevm.go @@ -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() } diff --git a/core/vm/stack.libevm_test.go b/core/vm/stack.libevm_test.go new file mode 100644 index 000000000000..9f8bb1ea69dc --- /dev/null +++ b/core/vm/stack.libevm_test.go @@ -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.Len(t, s.Data(), 0) + 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") +} From 1dbaa4a7bdbf16c33d078a0a176dde950bd35553 Mon Sep 17 00:00:00 2001 From: Arran Schlosberg Date: Wed, 18 Sep 2024 08:51:25 -0400 Subject: [PATCH 2/2] refactor: use `require.Empty()` --- core/vm/stack.libevm_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/vm/stack.libevm_test.go b/core/vm/stack.libevm_test.go index 9f8bb1ea69dc..f6ce05e1a6bc 100644 --- a/core/vm/stack.libevm_test.go +++ b/core/vm/stack.libevm_test.go @@ -19,7 +19,7 @@ func TestMutableStack(t *testing.T) { return *u256 } - require.Len(t, s.Data(), 0) + 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")