core/vm: implement EIP-5656, mcopy instruction (#26181)
Implements [EIP 5656](https://eips.ethereum.org/EIPS/eip-5656), MCOPY instruction, and enables it for Cancun. --------- Co-authored-by: Martin Holst Swende <martin@swende.se>
This commit is contained in:
parent
af8b138c1a
commit
5c9cbc218a
@ -26,6 +26,7 @@ import (
|
||||
)
|
||||
|
||||
var activators = map[int]func(*JumpTable){
|
||||
5656: enable5656,
|
||||
3855: enable3855,
|
||||
3860: enable3860,
|
||||
3529: enable3529,
|
||||
@ -242,6 +243,32 @@ func enable3860(jt *JumpTable) {
|
||||
jt[CREATE2].dynamicGas = gasCreate2Eip3860
|
||||
}
|
||||
|
||||
// enable5656 enables EIP-5656 (MCOPY opcode)
|
||||
// https://eips.ethereum.org/EIPS/eip-5656
|
||||
func enable5656(jt *JumpTable) {
|
||||
jt[MCOPY] = &operation{
|
||||
execute: opMcopy,
|
||||
constantGas: GasFastestStep,
|
||||
dynamicGas: gasMcopy,
|
||||
minStack: minStack(3, 0),
|
||||
maxStack: maxStack(3, 0),
|
||||
memorySize: memoryMcopy,
|
||||
}
|
||||
}
|
||||
|
||||
// opMcopy implements the MCOPY opcode (https://eips.ethereum.org/EIPS/eip-5656)
|
||||
func opMcopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
||||
var (
|
||||
dst = scope.Stack.pop()
|
||||
src = scope.Stack.pop()
|
||||
length = scope.Stack.pop()
|
||||
)
|
||||
// These values are checked for overflow during memory expansion calculation
|
||||
// (the memorySize function on the opcode).
|
||||
scope.Memory.Copy(dst.Uint64(), src.Uint64(), length.Uint64())
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// opBlobHash implements the BLOBHASH opcode
|
||||
func opBlobHash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
|
||||
index := scope.Stack.peek()
|
||||
|
@ -60,6 +60,7 @@ func memoryGasCost(mem *Memory, newMemSize uint64) (uint64, error) {
|
||||
// as argument:
|
||||
// CALLDATACOPY (stack position 2)
|
||||
// CODECOPY (stack position 2)
|
||||
// MCOPY (stack position 2)
|
||||
// EXTCODECOPY (stack position 3)
|
||||
// RETURNDATACOPY (stack position 2)
|
||||
func memoryCopierGas(stackpos int) gasFunc {
|
||||
@ -89,6 +90,7 @@ func memoryCopierGas(stackpos int) gasFunc {
|
||||
var (
|
||||
gasCallDataCopy = memoryCopierGas(2)
|
||||
gasCodeCopy = memoryCopierGas(2)
|
||||
gasMcopy = memoryCopierGas(2)
|
||||
gasExtCodeCopy = memoryCopierGas(3)
|
||||
gasReturnDataCopy = memoryCopierGas(2)
|
||||
)
|
||||
|
@ -22,9 +22,11 @@ import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/math"
|
||||
"github.com/ethereum/go-ethereum/core/rawdb"
|
||||
"github.com/ethereum/go-ethereum/core/state"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
@ -788,3 +790,141 @@ func TestBlobHash(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpMCopy(t *testing.T) {
|
||||
// Test cases from https://eips.ethereum.org/EIPS/eip-5656#test-cases
|
||||
for i, tc := range []struct {
|
||||
dst, src, len string
|
||||
pre string
|
||||
want string
|
||||
wantGas uint64
|
||||
}{
|
||||
{ // MCOPY 0 32 32 - copy 32 bytes from offset 32 to offset 0.
|
||||
dst: "0x0", src: "0x20", len: "0x20",
|
||||
pre: "0000000000000000000000000000000000000000000000000000000000000000 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
|
||||
want: "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
|
||||
wantGas: 6,
|
||||
},
|
||||
|
||||
{ // MCOPY 0 0 32 - copy 32 bytes from offset 0 to offset 0.
|
||||
dst: "0x0", src: "0x0", len: "0x20",
|
||||
pre: "0101010101010101010101010101010101010101010101010101010101010101",
|
||||
want: "0101010101010101010101010101010101010101010101010101010101010101",
|
||||
wantGas: 6,
|
||||
},
|
||||
{ // MCOPY 0 1 8 - copy 8 bytes from offset 1 to offset 0 (overlapping).
|
||||
dst: "0x0", src: "0x1", len: "0x8",
|
||||
pre: "000102030405060708 000000000000000000000000000000000000000000000000",
|
||||
want: "010203040506070808 000000000000000000000000000000000000000000000000",
|
||||
wantGas: 6,
|
||||
},
|
||||
{ // MCOPY 1 0 8 - copy 8 bytes from offset 0 to offset 1 (overlapping).
|
||||
dst: "0x1", src: "0x0", len: "0x8",
|
||||
pre: "000102030405060708 000000000000000000000000000000000000000000000000",
|
||||
want: "000001020304050607 000000000000000000000000000000000000000000000000",
|
||||
wantGas: 6,
|
||||
},
|
||||
// Tests below are not in the EIP, but maybe should be added
|
||||
{ // MCOPY 0xFFFFFFFFFFFF 0xFFFFFFFFFFFF 0 - copy zero bytes from out-of-bounds index(overlapping).
|
||||
dst: "0xFFFFFFFFFFFF", src: "0xFFFFFFFFFFFF", len: "0x0",
|
||||
pre: "11",
|
||||
want: "11",
|
||||
wantGas: 3,
|
||||
},
|
||||
{ // MCOPY 0xFFFFFFFFFFFF 0 0 - copy zero bytes from start of mem to out-of-bounds.
|
||||
dst: "0xFFFFFFFFFFFF", src: "0x0", len: "0x0",
|
||||
pre: "11",
|
||||
want: "11",
|
||||
wantGas: 3,
|
||||
},
|
||||
{ // MCOPY 0 0xFFFFFFFFFFFF 0 - copy zero bytes from out-of-bounds to start of mem
|
||||
dst: "0x0", src: "0xFFFFFFFFFFFF", len: "0x0",
|
||||
pre: "11",
|
||||
want: "11",
|
||||
wantGas: 3,
|
||||
},
|
||||
{ // MCOPY - copy 1 from space outside of uint64 space
|
||||
dst: "0x0", src: "0x10000000000000000", len: "0x1",
|
||||
pre: "0",
|
||||
},
|
||||
{ // MCOPY - copy 1 from 0 to space outside of uint64
|
||||
dst: "0x10000000000000000", src: "0x0", len: "0x1",
|
||||
pre: "0",
|
||||
},
|
||||
{ // MCOPY - copy nothing from 0 to space outside of uint64
|
||||
dst: "0x10000000000000000", src: "0x0", len: "0x0",
|
||||
pre: "",
|
||||
want: "",
|
||||
wantGas: 3,
|
||||
},
|
||||
{ // MCOPY - copy 1 from 0x20 to 0x10, with no prior allocated mem
|
||||
dst: "0x10", src: "0x20", len: "0x1",
|
||||
pre: "",
|
||||
// 64 bytes
|
||||
want: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
|
||||
wantGas: 12,
|
||||
},
|
||||
{ // MCOPY - copy 1 from 0x19 to 0x10, with no prior allocated mem
|
||||
dst: "0x10", src: "0x19", len: "0x1",
|
||||
pre: "",
|
||||
// 32 bytes
|
||||
want: "0x0000000000000000000000000000000000000000000000000000000000000000",
|
||||
wantGas: 9,
|
||||
},
|
||||
} {
|
||||
var (
|
||||
env = NewEVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{})
|
||||
stack = newstack()
|
||||
pc = uint64(0)
|
||||
evmInterpreter = env.interpreter
|
||||
)
|
||||
data := common.FromHex(strings.ReplaceAll(tc.pre, " ", ""))
|
||||
// Set pre
|
||||
mem := NewMemory()
|
||||
mem.Resize(uint64(len(data)))
|
||||
mem.Set(0, uint64(len(data)), data)
|
||||
// Push stack args
|
||||
len, _ := uint256.FromHex(tc.len)
|
||||
src, _ := uint256.FromHex(tc.src)
|
||||
dst, _ := uint256.FromHex(tc.dst)
|
||||
|
||||
stack.push(len)
|
||||
stack.push(src)
|
||||
stack.push(dst)
|
||||
wantErr := (tc.wantGas == 0)
|
||||
// Calc mem expansion
|
||||
var memorySize uint64
|
||||
if memSize, overflow := memoryMcopy(stack); overflow {
|
||||
if wantErr {
|
||||
continue
|
||||
}
|
||||
t.Errorf("overflow")
|
||||
} else {
|
||||
var overflow bool
|
||||
if memorySize, overflow = math.SafeMul(toWordSize(memSize), 32); overflow {
|
||||
t.Error(ErrGasUintOverflow)
|
||||
}
|
||||
}
|
||||
// and the dynamic cost
|
||||
var haveGas uint64
|
||||
if dynamicCost, err := gasMcopy(env, nil, stack, mem, memorySize); err != nil {
|
||||
t.Error(err)
|
||||
} else {
|
||||
haveGas = GasFastestStep + dynamicCost
|
||||
}
|
||||
// Expand mem
|
||||
if memorySize > 0 {
|
||||
mem.Resize(memorySize)
|
||||
}
|
||||
// Do the copy
|
||||
opMcopy(&pc, evmInterpreter, &ScopeContext{mem, stack, nil})
|
||||
want := common.FromHex(strings.ReplaceAll(tc.want, " ", ""))
|
||||
if have := mem.store; !bytes.Equal(want, have) {
|
||||
t.Errorf("case %d: \nwant: %#x\nhave: %#x\n", i, want, have)
|
||||
}
|
||||
wantGas := tc.wantGas
|
||||
if haveGas != wantGas {
|
||||
t.Errorf("case %d: gas wrong, want %d have %d\n", i, wantGas, haveGas)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -82,8 +82,9 @@ func validate(jt JumpTable) JumpTable {
|
||||
|
||||
func newCancunInstructionSet() JumpTable {
|
||||
instructionSet := newShanghaiInstructionSet()
|
||||
enable4844(&instructionSet) // BLOBHASH opcode
|
||||
enable4844(&instructionSet) // EIP-4844 (DATAHASH opcode)
|
||||
enable1153(&instructionSet) // EIP-1153 "Transient Storage"
|
||||
enable5656(&instructionSet) // EIP-5656 (MCOPY opcode)
|
||||
return validate(instructionSet)
|
||||
}
|
||||
|
||||
|
@ -103,3 +103,14 @@ func (m *Memory) Len() int {
|
||||
func (m *Memory) Data() []byte {
|
||||
return m.store
|
||||
}
|
||||
|
||||
// Copy copies data from the src position slice into the dst position.
|
||||
// The source and destination may overlap.
|
||||
// OBS: This operation assumes that any necessary memory expansion has already been performed,
|
||||
// and this method may panic otherwise.
|
||||
func (m *Memory) Copy(dst, src, len uint64) {
|
||||
if len == 0 {
|
||||
return
|
||||
}
|
||||
copy(m.store[dst:], m.store[src:src+len])
|
||||
}
|
||||
|
@ -48,6 +48,14 @@ func memoryMStore(stack *Stack) (uint64, bool) {
|
||||
return calcMemSize64WithUint(stack.Back(0), 32)
|
||||
}
|
||||
|
||||
func memoryMcopy(stack *Stack) (uint64, bool) {
|
||||
mStart := stack.Back(0) // stack[0]: dest
|
||||
if stack.Back(1).Gt(mStart) {
|
||||
mStart = stack.Back(1) // stack[1]: source
|
||||
}
|
||||
return calcMemSize64(mStart, stack.Back(2)) // stack[2]: length
|
||||
}
|
||||
|
||||
func memoryCreate(stack *Stack) (uint64, bool) {
|
||||
return calcMemSize64(stack.Back(1), stack.Back(2))
|
||||
}
|
||||
|
69
core/vm/memory_test.go
Normal file
69
core/vm/memory_test.go
Normal file
@ -0,0 +1,69 @@
|
||||
package vm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
)
|
||||
|
||||
func TestMemoryCopy(t *testing.T) {
|
||||
// Test cases from https://eips.ethereum.org/EIPS/eip-5656#test-cases
|
||||
for i, tc := range []struct {
|
||||
dst, src, len uint64
|
||||
pre string
|
||||
want string
|
||||
}{
|
||||
{ // MCOPY 0 32 32 - copy 32 bytes from offset 32 to offset 0.
|
||||
0, 32, 32,
|
||||
"0000000000000000000000000000000000000000000000000000000000000000 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
|
||||
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
|
||||
},
|
||||
|
||||
{ // MCOPY 0 0 32 - copy 32 bytes from offset 0 to offset 0.
|
||||
0, 0, 32,
|
||||
"0101010101010101010101010101010101010101010101010101010101010101",
|
||||
"0101010101010101010101010101010101010101010101010101010101010101",
|
||||
},
|
||||
{ // MCOPY 0 1 8 - copy 8 bytes from offset 1 to offset 0 (overlapping).
|
||||
0, 1, 8,
|
||||
"000102030405060708 000000000000000000000000000000000000000000000000",
|
||||
"010203040506070808 000000000000000000000000000000000000000000000000",
|
||||
},
|
||||
{ // MCOPY 1 0 8 - copy 8 bytes from offset 0 to offset 1 (overlapping).
|
||||
1, 0, 8,
|
||||
"000102030405060708 000000000000000000000000000000000000000000000000",
|
||||
"000001020304050607 000000000000000000000000000000000000000000000000",
|
||||
},
|
||||
// Tests below are not in the EIP, but maybe should be added
|
||||
{ // MCOPY 0xFFFFFFFFFFFF 0xFFFFFFFFFFFF 0 - copy zero bytes from out-of-bounds index(overlapping).
|
||||
0xFFFFFFFFFFFF, 0xFFFFFFFFFFFF, 0,
|
||||
"11",
|
||||
"11",
|
||||
},
|
||||
{ // MCOPY 0xFFFFFFFFFFFF 0 0 - copy zero bytes from start of mem to out-of-bounds.
|
||||
0xFFFFFFFFFFFF, 0, 0,
|
||||
"11",
|
||||
"11",
|
||||
},
|
||||
{ // MCOPY 0 0xFFFFFFFFFFFF 0 - copy zero bytes from out-of-bounds to start of mem
|
||||
0, 0xFFFFFFFFFFFF, 0,
|
||||
"11",
|
||||
"11",
|
||||
},
|
||||
} {
|
||||
m := NewMemory()
|
||||
// Clean spaces
|
||||
data := common.FromHex(strings.ReplaceAll(tc.pre, " ", ""))
|
||||
// Set pre
|
||||
m.Resize(uint64(len(data)))
|
||||
m.Set(0, uint64(len(data)), data)
|
||||
// Do the copy
|
||||
m.Copy(tc.dst, tc.src, tc.len)
|
||||
want := common.FromHex(strings.ReplaceAll(tc.want, " ", ""))
|
||||
if have := m.store; !bytes.Equal(want, have) {
|
||||
t.Errorf("case %d: want: %#x\nhave: %#x\n", i, want, have)
|
||||
}
|
||||
}
|
||||
}
|
@ -119,6 +119,7 @@ const (
|
||||
JUMPDEST OpCode = 0x5b
|
||||
TLOAD OpCode = 0x5c
|
||||
TSTORE OpCode = 0x5d
|
||||
MCOPY OpCode = 0x5e
|
||||
PUSH0 OpCode = 0x5f
|
||||
)
|
||||
|
||||
@ -302,6 +303,7 @@ var opCodeToString = map[OpCode]string{
|
||||
JUMPDEST: "JUMPDEST",
|
||||
TLOAD: "TLOAD",
|
||||
TSTORE: "TSTORE",
|
||||
MCOPY: "MCOPY",
|
||||
PUSH0: "PUSH0",
|
||||
|
||||
// 0x60 range - pushes.
|
||||
@ -473,6 +475,7 @@ var stringToOp = map[string]OpCode{
|
||||
"JUMPDEST": JUMPDEST,
|
||||
"TLOAD": TLOAD,
|
||||
"TSTORE": TSTORE,
|
||||
"MCOPY": MCOPY,
|
||||
"PUSH0": PUSH0,
|
||||
"PUSH1": PUSH1,
|
||||
"PUSH2": PUSH2,
|
||||
|
Loading…
Reference in New Issue
Block a user