github.com/phillinzzz/newBsc@v1.1.6/core/vm/contract.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package vm
    18  
    19  import (
    20  	"math/big"
    21  
    22  	lru "github.com/hashicorp/golang-lru"
    23  
    24  	"github.com/phillinzzz/newBsc/common"
    25  	"github.com/holiman/uint256"
    26  )
    27  
    28  const codeBitmapCacheSize = 2000
    29  
    30  var codeBitmapCache, _ = lru.New(codeBitmapCacheSize)
    31  
    32  // ContractRef is a reference to the contract's backing object
    33  type ContractRef interface {
    34  	Address() common.Address
    35  }
    36  
    37  // AccountRef implements ContractRef.
    38  //
    39  // Account references are used during EVM initialisation and
    40  // it's primary use is to fetch addresses. Removing this object
    41  // proves difficult because of the cached jump destinations which
    42  // are fetched from the parent contract (i.e. the caller), which
    43  // is a ContractRef.
    44  type AccountRef common.Address
    45  
    46  // Address casts AccountRef to a Address
    47  func (ar AccountRef) Address() common.Address { return (common.Address)(ar) }
    48  
    49  // Contract represents an ethereum contract in the state database. It contains
    50  // the contract code, calling arguments. Contract implements ContractRef
    51  type Contract struct {
    52  	// CallerAddress is the result of the caller which initialised this
    53  	// contract. However when the "call method" is delegated this value
    54  	// needs to be initialised to that of the caller's caller.
    55  	CallerAddress common.Address
    56  	caller        ContractRef
    57  	self          ContractRef
    58  
    59  	jumpdests map[common.Hash]bitvec // Aggregated result of JUMPDEST analysis.
    60  	analysis  bitvec                 // Locally cached result of JUMPDEST analysis
    61  
    62  	Code     []byte
    63  	CodeHash common.Hash
    64  	CodeAddr *common.Address
    65  	Input    []byte
    66  
    67  	Gas   uint64
    68  	value *big.Int
    69  }
    70  
    71  // NewContract returns a new contract environment for the execution of EVM.
    72  func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uint64) *Contract {
    73  	c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object}
    74  
    75  	if parent, ok := caller.(*Contract); ok {
    76  		// Reuse JUMPDEST analysis from parent context if available.
    77  		c.jumpdests = parent.jumpdests
    78  	} else {
    79  		c.jumpdests = make(map[common.Hash]bitvec)
    80  	}
    81  
    82  	// Gas should be a pointer so it can safely be reduced through the run
    83  	// This pointer will be off the state transition
    84  	c.Gas = gas
    85  	// ensures a value is set
    86  	c.value = value
    87  
    88  	return c
    89  }
    90  
    91  func (c *Contract) validJumpdest(dest *uint256.Int) bool {
    92  	udest, overflow := dest.Uint64WithOverflow()
    93  	// PC cannot go beyond len(code) and certainly can't be bigger than 63bits.
    94  	// Don't bother checking for JUMPDEST in that case.
    95  	if overflow || udest >= uint64(len(c.Code)) {
    96  		return false
    97  	}
    98  	// Only JUMPDESTs allowed for destinations
    99  	if OpCode(c.Code[udest]) != JUMPDEST {
   100  		return false
   101  	}
   102  	return c.isCode(udest)
   103  }
   104  
   105  // isCode returns true if the provided PC location is an actual opcode, as
   106  // opposed to a data-segment following a PUSHN operation.
   107  func (c *Contract) isCode(udest uint64) bool {
   108  	// Do we already have an analysis laying around?
   109  	if c.analysis != nil {
   110  		return c.analysis.codeSegment(udest)
   111  	}
   112  	// Do we have a contract hash already?
   113  	// If we do have a hash, that means it's a 'regular' contract. For regular
   114  	// contracts ( not temporary initcode), we store the analysis in a map
   115  	if c.CodeHash != (common.Hash{}) {
   116  		// Does parent context have the analysis?
   117  		analysis, exist := c.jumpdests[c.CodeHash]
   118  		if !exist {
   119  			if cached, ok := codeBitmapCache.Get(c.CodeHash); ok {
   120  				analysis = cached.(bitvec)
   121  			} else {
   122  				// Do the analysis and save in parent context
   123  				// We do not need to store it in c.analysis
   124  				analysis = codeBitmap(c.Code)
   125  				c.jumpdests[c.CodeHash] = analysis
   126  				codeBitmapCache.Add(c.CodeHash, analysis)
   127  			}
   128  		}
   129  		// Also stash it in current contract for faster access
   130  		c.analysis = analysis
   131  		return analysis.codeSegment(udest)
   132  	}
   133  	// We don't have the code hash, most likely a piece of initcode not already
   134  	// in state trie. In that case, we do an analysis, and save it locally, so
   135  	// we don't have to recalculate it for every JUMP instruction in the execution
   136  	// However, we don't save it within the parent context
   137  	if c.analysis == nil {
   138  		c.analysis = codeBitmap(c.Code)
   139  	}
   140  	return c.analysis.codeSegment(udest)
   141  }
   142  
   143  // AsDelegate sets the contract to be a delegate call and returns the current
   144  // contract (for chaining calls)
   145  func (c *Contract) AsDelegate() *Contract {
   146  	// NOTE: caller must, at all times be a contract. It should never happen
   147  	// that caller is something other than a Contract.
   148  	parent := c.caller.(*Contract)
   149  	c.CallerAddress = parent.CallerAddress
   150  	c.value = parent.value
   151  
   152  	return c
   153  }
   154  
   155  // GetOp returns the n'th element in the contract's byte array
   156  func (c *Contract) GetOp(n uint64) OpCode {
   157  	return OpCode(c.GetByte(n))
   158  }
   159  
   160  // GetByte returns the n'th byte in the contract's byte array
   161  func (c *Contract) GetByte(n uint64) byte {
   162  	if n < uint64(len(c.Code)) {
   163  		return c.Code[n]
   164  	}
   165  
   166  	return 0
   167  }
   168  
   169  // Caller returns the caller of the contract.
   170  //
   171  // Caller will recursively call caller when the contract is a delegate
   172  // call, including that of caller's caller.
   173  func (c *Contract) Caller() common.Address {
   174  	return c.CallerAddress
   175  }
   176  
   177  // UseGas attempts the use gas and subtracts it and returns true on success
   178  func (c *Contract) UseGas(gas uint64) (ok bool) {
   179  	if c.Gas < gas {
   180  		return false
   181  	}
   182  	c.Gas -= gas
   183  	return true
   184  }
   185  
   186  // Address returns the contracts address
   187  func (c *Contract) Address() common.Address {
   188  	return c.self.Address()
   189  }
   190  
   191  // Value returns the contract's value (sent to it from it's caller)
   192  func (c *Contract) Value() *big.Int {
   193  	return c.value
   194  }
   195  
   196  // SetCallCode sets the code of the contract and address of the backing data
   197  // object
   198  func (c *Contract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
   199  	c.Code = code
   200  	c.CodeHash = hash
   201  	c.CodeAddr = addr
   202  }
   203  
   204  // SetCodeOptionalHash can be used to provide code, but it's optional to provide hash.
   205  // In case hash is not provided, the jumpdest analysis will not be saved to the parent context
   206  func (c *Contract) SetCodeOptionalHash(addr *common.Address, codeAndHash *codeAndHash) {
   207  	c.Code = codeAndHash.code
   208  	c.CodeHash = codeAndHash.hash
   209  	c.CodeAddr = addr
   210  }