github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/core/wavm/contract/wasm_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 contract
    18  
    19  import (
    20  	"math/big"
    21  
    22  	"github.com/vntchain/go-vnt/core/vm/interface"
    23  
    24  	"github.com/vntchain/go-vnt/common"
    25  )
    26  
    27  type WasmCode struct {
    28  	Code     []byte
    29  	Abi      []byte
    30  	Compiled []byte
    31  }
    32  
    33  type WASMContractRef interface {
    34  	Address() common.Address
    35  }
    36  
    37  // Contract represents an VNTChain contract in the state database. It contains
    38  // the the contract code, calling arguments. Contract implements ContractRef
    39  type WASMContract struct {
    40  	// CallerAddress is the result of the caller which initialised this
    41  	// contract. However when the "call method" is delegated this value
    42  	// needs to be initialised to that of the caller's caller.
    43  	CallerAddress common.Address
    44  	caller        WASMContractRef
    45  	self          WASMContractRef
    46  
    47  	Code     []byte
    48  	CodeHash common.Hash
    49  	CodeAddr *common.Address
    50  	Input    []byte
    51  
    52  	GasLimit uint64
    53  	Gas      uint64
    54  	value    *big.Int
    55  
    56  	Args []byte
    57  
    58  	DelegateCall bool
    59  }
    60  
    61  // NewWASMContract returns a new contract environment for the execution of WAVM.
    62  func NewWASMContract(caller WASMContractRef, object WASMContractRef, value *big.Int, gas uint64) *WASMContract {
    63  	c := &WASMContract{CallerAddress: caller.Address(), caller: caller, self: object, Args: nil}
    64  
    65  	// Gas should be a pointer so it can safely be reduced through the run
    66  	// This pointer will be off the state transition
    67  	c.Gas = gas
    68  	c.GasLimit = gas
    69  	// ensures a value is set
    70  	c.value = value
    71  
    72  	return c
    73  }
    74  
    75  // AsDelegate sets the contract to be a delegate call and returns the current
    76  // contract (for chaining calls)
    77  func (c *WASMContract) AsDelegate() inter.Contract {
    78  	c.DelegateCall = true
    79  	// NOTE: caller must, at all times be a contract. It should never happen
    80  	// that caller is something other than a Contract.
    81  	parent := c.caller.(*WASMContract)
    82  	c.CallerAddress = parent.CallerAddress
    83  	c.value = parent.value
    84  
    85  	return c
    86  }
    87  
    88  // GetByte returns the n'th byte in the contract's byte array
    89  func (c *WASMContract) GetByte(n uint64) byte {
    90  	if n < uint64(len(c.Code)) {
    91  		return c.Code[n]
    92  	}
    93  
    94  	return 0
    95  }
    96  
    97  // Caller returns the caller of the contract.
    98  //
    99  // Caller will recursively call caller when the contract is a delegate
   100  // call, including that of caller's caller.
   101  func (c *WASMContract) Caller() common.Address {
   102  	return c.CallerAddress
   103  }
   104  
   105  // UseGas attempts the use gas and subtracts it and returns true on success
   106  func (c *WASMContract) UseGas(gas uint64) (ok bool) {
   107  	if c.Gas < gas {
   108  		return false
   109  	}
   110  	c.Gas -= gas
   111  	return true
   112  }
   113  
   114  // Address returns the contracts address
   115  func (c *WASMContract) Address() common.Address {
   116  	return c.self.Address()
   117  }
   118  
   119  // Value returns the contracts value (sent to it from it's caller)
   120  func (c *WASMContract) Value() *big.Int {
   121  	return c.value
   122  }
   123  
   124  // SetCode sets the code to the contract
   125  func (c *WASMContract) SetCode(hash common.Hash, code []byte) {
   126  	c.Code = code
   127  	c.CodeHash = hash
   128  }
   129  
   130  // SetCallCode sets the code of the contract and address of the backing data
   131  // object
   132  func (c *WASMContract) SetCallCode(addr *common.Address, hash common.Hash, code []byte) {
   133  	c.Code = code
   134  	c.CodeHash = hash
   135  	c.CodeAddr = addr
   136  }