Skip to content

Commit

Permalink
core/vm: Rename Contract.Code => Contract.Container
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Nov 8, 2022
1 parent d5cf7a2 commit 8adc30c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 35 deletions.
2 changes: 1 addition & 1 deletion core/vm/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func codeBitmap(c *Contract) bitvec {
func codeBitmapInternal(c *Contract, bits bitvec) bitvec {
codeBegin := c.CodeBeginOffset
for pc := uint64(0); pc < c.CodeSize; {
op := OpCode(c.Code[codeBegin+pc])
op := OpCode(c.Container[codeBegin+pc])
pc++
if int8(op) < int8(PUSH1) { // If not PUSH (the int8(op) > int(PUSH32) is always false).
continue
Expand Down
33 changes: 15 additions & 18 deletions core/vm/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ type Contract struct {
jumpdests map[common.Hash]bitvec // Aggregated result of JUMPDEST analysis.
analysis bitvec // Locally cached result of JUMPDEST analysis

Code []byte
CodeHash common.Hash
CodeAddr *common.Address
Input []byte
Container []byte
ContainerHash common.Hash
Input []byte

Gas uint64
value *big.Int
Expand Down Expand Up @@ -111,14 +110,14 @@ func (c *Contract) isCode(udest uint64) bool {
// Do we have a contract hash already?
// If we do have a hash, that means it's a 'regular' contract. For regular
// contracts ( not temporary initcode), we store the analysis in a map
if c.CodeHash != (common.Hash{}) {
if c.ContainerHash != (common.Hash{}) {
// Does parent context have the analysis?
analysis, exist := c.jumpdests[c.CodeHash]
analysis, exist := c.jumpdests[c.ContainerHash]
if !exist {
// Do the analysis and save in parent context
// We do not need to store it in c.analysis
analysis = codeBitmap(c)
c.jumpdests[c.CodeHash] = analysis
c.jumpdests[c.ContainerHash] = analysis
}
// Also stash it in current contract for faster access
c.analysis = analysis
Expand Down Expand Up @@ -150,7 +149,7 @@ func (c *Contract) AsDelegate() *Contract {
// n is offset inside code section in case of EOF contract
func (c *Contract) GetOp(n uint64) OpCode {
if n < c.CodeSize {
return OpCode(c.Code[c.CodeBeginOffset+n])
return OpCode(c.Container[c.CodeBeginOffset+n])
}

return STOP
Expand Down Expand Up @@ -200,20 +199,18 @@ func getCodeBounds(container []byte, header *EOF1Header) (begin uint64, size uin

// SetCallCode sets the code of the contract and address of the backing data
// object
func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte, header *EOF1Header) {
c.Code = code
c.CodeHash = hash
c.CodeAddr = addr
func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, container []byte, header *EOF1Header) {
c.Container = container
c.ContainerHash = hash

c.CodeBeginOffset, c.CodeSize = getCodeBounds(code, header)
c.CodeBeginOffset, c.CodeSize = getCodeBounds(container, header)
}

// SetCodeOptionalHash can be used to provide code, but it's optional to provide hash.
// In case hash is not provided, the jumpdest analysis will not be saved to the parent context
func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash, header *EOF1Header) {
c.Code = codeAndHash.code
c.CodeHash = codeAndHash.hash
c.CodeAddr = addr
func (c *Contract) SetCodeOptionalHash(addr *common.Address, containerAndHash *codeAndHash, header *EOF1Header) {
c.Container = containerAndHash.code
c.ContainerHash = containerAndHash.hash

c.CodeBeginOffset, c.CodeSize = getCodeBounds(codeAndHash.code, header)
c.CodeBeginOffset, c.CodeSize = getCodeBounds(containerAndHash.code, header)
}
20 changes: 10 additions & 10 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,22 +349,22 @@ func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext)

func opCodeSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
l := new(uint256.Int)
l.SetUint64(uint64(len(scope.Contract.Code)))
l.SetUint64(uint64(len(scope.Contract.Container)))
scope.Stack.push(l)
return nil, nil
}

func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
var (
memOffset = scope.Stack.pop()
codeOffset = scope.Stack.pop()
length = scope.Stack.pop()
memOffset = scope.Stack.pop()
containerOffset = scope.Stack.pop()
length = scope.Stack.pop()
)
uint64CodeOffset, overflow := codeOffset.Uint64WithOverflow()
uint64ContainerOffset, overflow := containerOffset.Uint64WithOverflow()
if overflow {
uint64CodeOffset = 0xffffffffffffffff
uint64ContainerOffset = 0xffffffffffffffff
}
codeCopy := getData(scope.Contract.Code, uint64CodeOffset, length.Uint64())
codeCopy := getData(scope.Contract.Container, uint64ContainerOffset, length.Uint64())
scope.Memory.Set(memOffset.Uint64(), length.Uint64(), codeCopy)

return nil, nil
Expand Down Expand Up @@ -812,7 +812,7 @@ func opRevert(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]b
}

func opUndefined(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
return nil, &ErrInvalidOpCode{opcode: OpCode(scope.Contract.Code[*pc])}
return nil, &ErrInvalidOpCode{opcode: OpCode(scope.Contract.Container[*pc])}
}

func opStop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
Expand Down Expand Up @@ -873,7 +873,7 @@ func opPush1(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]by
*pc += 1
dataPos := scope.Contract.CodeBeginOffset + *pc
if dataPos < codeEnd {
scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Code[dataPos])))
scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Container[dataPos])))
} else {
scope.Stack.push(integer.Clear())
}
Expand All @@ -898,7 +898,7 @@ func makePush(size uint64, pushByteSize int) executionFunc {

integer := new(uint256.Int)
scope.Stack.push(integer.SetBytes(common.RightPadBytes(
scope.Contract.Code[startMin:endMin], pushByteSize)))
scope.Contract.Container[startMin:endMin], pushByteSize)))

*pc += size
return nil, nil
Expand Down
2 changes: 1 addition & 1 deletion core/vm/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (
in.returnData = nil

// Don't bother with the execution if there's no code.
if len(contract.Code) == 0 {
if len(contract.Container) == 0 {
return nil, nil
}

Expand Down
6 changes: 3 additions & 3 deletions eth/tracers/js/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ func runTrace(tracer tracers.Tracer, vmctx *vmContext, chaincfg *params.ChainCon
value = big.NewInt(0)
contract = vm.NewContract(account{}, account{}, value, startGas)
)
contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0}
contract.Container = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0}
if contractCode != nil {
contract.Code = contractCode
contract.Container = contractCode
}
contract.CodeSize = uint64(len(contract.Code))
contract.CodeSize = uint64(len(contract.Container))

tracer.CaptureTxStart(gasLimit)
tracer.CaptureStart(env, contract.Caller(), contract.Address(), false, []byte{}, startGas, value)
Expand Down
4 changes: 2 additions & 2 deletions eth/tracers/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func TestStoreCapture(t *testing.T) {
env = vm.NewEVM(vm.BlockContext{}, vm.TxContext{}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: logger})
contract = vm.NewContract(&dummyContractRef{}, &dummyContractRef{}, new(big.Int), 100000)
)
contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x0, byte(vm.SSTORE)}
contract.CodeSize = uint64(len(contract.Code))
contract.Container = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x0, byte(vm.SSTORE)}
contract.CodeSize = uint64(len(contract.Container))
var index common.Hash
logger.CaptureStart(env, common.Address{}, contract.Address(), false, nil, 0, nil)
_, err := env.Interpreter().Run(contract, []byte{}, false)
Expand Down

0 comments on commit 8adc30c

Please sign in to comment.