From 8adc30cb735ba61a09ddb09c6cd6c099761a02b6 Mon Sep 17 00:00:00 2001 From: Andrei Maiboroda Date: Tue, 8 Nov 2022 12:34:24 -0600 Subject: [PATCH] core/vm: Rename Contract.Code => Contract.Container --- core/vm/analysis.go | 2 +- core/vm/contract.go | 33 ++++++++++++++----------------- core/vm/instructions.go | 20 +++++++++---------- core/vm/interpreter.go | 2 +- eth/tracers/js/tracer_test.go | 6 +++--- eth/tracers/logger/logger_test.go | 4 ++-- 6 files changed, 32 insertions(+), 35 deletions(-) diff --git a/core/vm/analysis.go b/core/vm/analysis.go index 35337a3a69c1..9abd4ccb915a 100644 --- a/core/vm/analysis.go +++ b/core/vm/analysis.go @@ -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 diff --git a/core/vm/contract.go b/core/vm/contract.go index 3d0595c4c7b4..de5a3d8d357c 100644 --- a/core/vm/contract.go +++ b/core/vm/contract.go @@ -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 @@ -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 @@ -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 @@ -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) } diff --git a/core/vm/instructions.go b/core/vm/instructions.go index c7ac09bc7ded..cb31970454d9 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -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 @@ -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) { @@ -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()) } @@ -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 diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index 312977b75588..0faefe16110a 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -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 } diff --git a/eth/tracers/js/tracer_test.go b/eth/tracers/js/tracer_test.go index 5644523af784..9e07f33d9a5d 100644 --- a/eth/tracers/js/tracer_test.go +++ b/eth/tracers/js/tracer_test.go @@ -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) diff --git a/eth/tracers/logger/logger_test.go b/eth/tracers/logger/logger_test.go index 3af858d1859e..e7b9cd26f264 100644 --- a/eth/tracers/logger/logger_test.go +++ b/eth/tracers/logger/logger_test.go @@ -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)