Skip to content

Commit

Permalink
various test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gballet committed Aug 9, 2022
1 parent 9d04136 commit b14ec6e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
trieUtils "github.com/ethereum/go-ethereum/trie/utils"
"github.com/gballet/go-verkle"
"github.com/holiman/uint256"
Expand Down
43 changes: 38 additions & 5 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,9 @@ func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([
}
uint64EndOffset, overflow := endOffset.Uint64WithOverflow()
if overflow {
uint64CodeOffset = 0xffffffffffffffff
uint64EndOffset = 0xffffffffffffffff
}
uint64Length := uint64EndOffset - uint64CodeOffset

if interpreter.evm.chainConfig.IsCancun(interpreter.evm.Context.BlockNumber) {
chunkedOffset := uint64CodeOffset + 1 + uint64CodeOffset/31 // codeOffset translated into the chunked space
Expand All @@ -399,6 +400,12 @@ func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([
scope.Memory.Set(memOffset.Uint64()+copied, 31, chunk[1+start:])
copied += uint64(len(chunk)) - start - 1
}

// Being chunked, the code will be padded to the next 31-byte boundary.
if copied < uint64Length {
chunk := chunks.GetChunk(uint64EndOffset / 31)
scope.Memory.Set(memOffset.Uint64()+copied, uint64EndOffset%31, chunk[1:1+uint64EndOffset%31])
}
} else {
codeCopy := getData(scope.Contract.Code, uint64CodeOffset, length.Uint64())
scope.Memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
Expand Down Expand Up @@ -501,17 +508,43 @@ func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext)
memOffset = stack.pop()
codeOffset = stack.pop()
length = stack.pop()
copied = uint64(0)
)
uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow()
if overflow {
uint64CodeOffset = 0xffffffffffffffff
}
uint64EndOffset, overflow := length.Uint64WithOverflow()
if overflow {
uint64EndOffset = 0xffffffffffffffff
}
uint64Length := uint64EndOffset - uint64CodeOffset

addr := common.Address(a.Bytes20())
if interpreter.evm.chainConfig.IsCancun(interpreter.evm.Context.BlockNumber) {
code := interpreter.evm.StateDB.GetCode(addr)
paddedCodeCopy, copyOffset, nonPaddedCopyLength := getDataAndAdjustedBounds(code, uint64CodeOffset, length.Uint64())
touchEachChunksOnReadAndChargeGasWithAddress(copyOffset, nonPaddedCopyLength, addr[:], code, interpreter.evm.Accesses, false)
scope.Memory.Set(memOffset.Uint64(), length.Uint64(), paddedCodeCopy)
chunks := trie.ChunkedCode(interpreter.evm.StateDB.GetCode(addr))

chunkedOffset := uint64CodeOffset + 1 + uint64CodeOffset/31 // codeOffset translated into the chunked space
chunkedEnd := uint64EndOffset + 1 + uint64EndOffset/31 // endOffset translated into the chunked space
touchEachChunksOnReadAndChargeGas(chunkedOffset, chunkedEnd-chunkedOffset, scope.Contract.AddressPoint(), scope.Contract.Code, interpreter.evm.Accesses, scope.Contract.IsDeployment)

// Copy all full chunks in the middle
it := chunks.Iter(uint64CodeOffset/31, uint64EndOffset/31)
for {
chunk, err := it()
if err != nil {
break // the only possible error is when the iterator has reached the end
}
start := (uint64CodeOffset + copied) % 31
scope.Memory.Set(memOffset.Uint64()+copied, 31, chunk[1+start:])
copied += uint64(len(chunk)) - start - 1
}

// Being chunked, the code will be padded to the next 31-byte boundary.
if copied < uint64Length {
chunk := chunks.GetChunk(uint64EndOffset / 31)
scope.Memory.Set(memOffset.Uint64()+copied, uint64EndOffset%31, chunk[1:1+uint64EndOffset%31])
}
} else {
codeCopy := getData(interpreter.evm.StateDB.GetCode(addr), uint64CodeOffset, length.Uint64())
scope.Memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)
Expand Down
3 changes: 2 additions & 1 deletion trie/verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ func (cc ChunkedCode) Iter(start, end uint64) func() ([]byte, error) {
// GetChunk gets the chunk number `number`.
func (cc ChunkedCode) GetChunk(number uint64) []byte {
if number*32 > uint64(len(cc)) {
return nil
var padding [32]byte
return padding[:]
}

return cc[32*number : 32*(number+1)]
Expand Down

0 comments on commit b14ec6e

Please sign in to comment.