mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-15 09:20:44 +00:00
feat: vm.MutableStack wrapper (#31)
* feat: `vm.MutableStack` wrapper * refactor: use `require.Empty()`
This commit is contained in:
parent
c70b3e35a1
commit
df1338920b
2 changed files with 41 additions and 0 deletions
14
core/vm/stack.libevm.go
Normal file
14
core/vm/stack.libevm.go
Normal file
|
|
@ -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
core/vm/stack.libevm_test.go
Normal file
27
core/vm/stack.libevm_test.go
Normal file
|
|
@ -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")
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue