github.com/annchain/OG@v0.0.9/vm/types/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 types
    18  
    19  import (
    20  	ogTypes "github.com/annchain/OG/arefactor/og_interface"
    21  	"github.com/annchain/OG/arefactor/ogcrypto"
    22  	"github.com/annchain/OG/common/hexutil"
    23  	"github.com/annchain/OG/common/math"
    24  	"github.com/annchain/OG/vm/code"
    25  	"github.com/annchain/OG/vm/common"
    26  	"github.com/annchain/OG/vm/instruction"
    27  
    28  	"github.com/sirupsen/logrus"
    29  	"math/big"
    30  )
    31  
    32  type CodeAndHash struct {
    33  	Code []byte
    34  	hash ogTypes.Hash32
    35  }
    36  
    37  func (c *CodeAndHash) Hash() ogTypes.Hash32 {
    38  	if c.hash == (ogTypes.Hash32{}) {
    39  		c.hash = *ogTypes.BytesToHash32(crypto.Keccak256Hash(c.Code))
    40  	}
    41  	return c.hash
    42  }
    43  
    44  // ContractRef is a reference to the contract's backing object
    45  type ContractRef interface {
    46  	Address() ogTypes.Address20
    47  }
    48  
    49  // AccountRef implements ContractRef.
    50  //
    51  // Account references are used during OVM initialisation and
    52  // it's primary use is to fetch addresses. Removing this object
    53  // proves difficult because of the cached jump destinations which
    54  // are fetched from the parent contract (i.e. the caller), which
    55  // is a ContractRef.
    56  type AccountRef ogTypes.Address20
    57  
    58  // Address casts AccountRef to a Address
    59  func (ar AccountRef) Address() ogTypes.Address20 {
    60  	a := (ogTypes.Address20)(ar)
    61  	return a
    62  }
    63  
    64  // Contract represents an general contract in the state database. It contains
    65  // the contract code, calling arguments. Contract implements ContractRef
    66  type Contract struct {
    67  	// CallerAddress is the result of the caller which initialised this
    68  	// contract. However when the "call method" is delegated this Value
    69  	// needs to be initialised to that of the caller's caller.
    70  	CallerAddress ogTypes.Address20
    71  	caller        ContractRef
    72  	self          ContractRef
    73  
    74  	jumpdests map[ogTypes.Hash32]common.Bitvec // Aggregated result of JUMPDEST analysis.
    75  	analysis  common.Bitvec                    // Locally cached result of JUMPDEST analysis
    76  
    77  	Code     []byte
    78  	CodeHash ogTypes.Hash32
    79  	CodeAddr *ogTypes.Address20
    80  	Input    []byte
    81  
    82  	Gas   uint64
    83  	value *big.Int
    84  }
    85  
    86  // NewContract returns a new contract environment for the execution of OVM.
    87  func NewContract(caller ContractRef, object ContractRef, value *big.Int, gas uint64) *Contract {
    88  	c := &Contract{CallerAddress: caller.Address(), caller: caller, self: object}
    89  
    90  	if parent, ok := caller.(*Contract); ok {
    91  		// Reuse JUMPDEST analysis from parent context if available.
    92  		c.jumpdests = parent.jumpdests
    93  	} else {
    94  		c.jumpdests = make(map[ogTypes.Hash32]common.Bitvec)
    95  	}
    96  
    97  	// Gas should be a pointer so it can safely be reduced through the run
    98  	// This pointer will be off the state transition
    99  	c.Gas = gas
   100  	// ensures a Value is set
   101  	c.value = value
   102  
   103  	return c
   104  }
   105  
   106  func (c *Contract) ValidJumpdest(dest *big.Int) bool {
   107  	udest := dest.Uint64()
   108  	// PC cannot go beyond len(code) and certainly can't be bigger than 63bits.
   109  	// Don't bother checking for JUMPDEST in that case.
   110  	if dest.BitLen() >= 63 || udest >= uint64(len(c.Code)) {
   111  		return false
   112  	}
   113  	// Only JUMPDESTs allowed for destinations
   114  	if instruction.OpCode(c.Code[udest]) != instruction.JUMPDEST {
   115  		return false
   116  	}
   117  	// Do we have a contract hash already?
   118  	if c.CodeHash != (ogTypes.Hash32{}) {
   119  		// Does parent context have the analysis?
   120  		analysis, exist := c.jumpdests[c.CodeHash]
   121  		if !exist {
   122  			// Do the analysis and save in parent context
   123  			// We do not need to store it in c.analysis
   124  			analysis = code.CodeBitmap(c.Code)
   125  			c.jumpdests[c.CodeHash] = analysis
   126  		}
   127  		return analysis.CodeSegment(udest)
   128  	}
   129  	// We don't have the code hash, most likely a piece of initcode not already
   130  	// in state trie. In that case, we do an analysis, and save it locally, so
   131  	// we don't have to recalculate it for every JUMP instruction in the execution
   132  	// However, we don't save it within the parent context
   133  	if c.analysis == nil {
   134  		c.analysis = code.CodeBitmap(c.Code)
   135  	}
   136  	return c.analysis.CodeSegment(udest)
   137  }
   138  
   139  // AsDelegate sets the contract to be a delegate call and returns the current
   140  // contract (for chaining calls)
   141  func (c *Contract) AsDelegate() *Contract {
   142  	// NOTE: caller must, at all times be a contract. It should never happen
   143  	// that caller is something other than a Contract.
   144  	parent := c.caller.(*Contract)
   145  	c.CallerAddress = parent.CallerAddress
   146  	c.value = parent.value
   147  
   148  	return c
   149  }
   150  
   151  // GetOp returns the n'th element in the contract's byte array
   152  func (c *Contract) GetOp(n uint64) instruction.OpCode {
   153  	return instruction.OpCode(c.GetByte(n))
   154  }
   155  
   156  // GetByte returns the n'th byte in the contract's byte array
   157  func (c *Contract) GetByte(n uint64) byte {
   158  	if n < uint64(len(c.Code)) {
   159  		return c.Code[n]
   160  	}
   161  
   162  	return 0
   163  }
   164  
   165  // Caller returns the caller of the contract.
   166  //
   167  // Caller will recursively call caller when the contract is a delegate
   168  // call, including that of caller's caller.
   169  func (c *Contract) Caller() ogTypes.Address20 {
   170  	return c.CallerAddress
   171  }
   172  
   173  // UseGas attempts the use gas and subtracts it and returns true on success
   174  func (c *Contract) UseGas(gas uint64) (ok bool) {
   175  	if c.Gas < gas {
   176  		return false
   177  	}
   178  	c.Gas -= gas
   179  	return true
   180  }
   181  
   182  // Address returns the contracts address
   183  func (c *Contract) Address() ogTypes.Address20 {
   184  	return c.self.Address()
   185  }
   186  
   187  // Value returns the contracts Value (sent to it from it's caller)
   188  func (c *Contract) Value() *big.Int {
   189  	return c.value
   190  }
   191  
   192  // SetCallCode sets the code of the contract and address of the backing data
   193  // object
   194  func (c *Contract) SetCallCode(addr ogTypes.Address20, hash ogTypes.Hash32, code []byte) {
   195  	logrus.WithFields(logrus.Fields{
   196  		"addr":  addr.Hex(),
   197  		"hash":  hash.Hex(),
   198  		"bytes": hexutil.Encode(code[0:math.MinInt(20, len(code))]) + "...",
   199  	}).Info("SetCallCode")
   200  	c.Code = code
   201  	c.CodeHash = hash
   202  	c.CodeAddr = &addr
   203  }
   204  
   205  // SetCodeOptionalHash can be used to provide code, but it's optional to provide hash.
   206  // In case hash is not provided, the jumpdest analysis will not be saved to the parent context
   207  func (c *Contract) SetCodeOptionalHash(addr ogTypes.Address20, codeAndHash *CodeAndHash) {
   208  	c.Code = codeAndHash.Code
   209  	c.CodeHash = codeAndHash.hash
   210  	c.CodeAddr = &addr
   211  }