github.com/ylsGit/go-ethereum@v1.6.5/light/vm_env.go (about)

     1  // Copyright 2016 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 light
    18  
    19  import (
    20  	"context"
    21  	"math/big"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/core/types"
    25  	"github.com/ethereum/go-ethereum/crypto"
    26  )
    27  
    28  // VMState is a wrapper for the light state that holds the actual context and
    29  // passes it to any state operation that requires it.
    30  type VMState struct {
    31  	ctx       context.Context
    32  	state     *LightState
    33  	snapshots []*LightState
    34  	err       error
    35  }
    36  
    37  func NewVMState(ctx context.Context, state *LightState) *VMState {
    38  	return &VMState{ctx: ctx, state: state}
    39  }
    40  
    41  func (s *VMState) Error() error {
    42  	return s.err
    43  }
    44  
    45  func (s *VMState) AddLog(log *types.Log) {}
    46  
    47  func (s *VMState) AddPreimage(hash common.Hash, preimage []byte) {}
    48  
    49  // errHandler handles and stores any state error that happens during execution.
    50  func (s *VMState) errHandler(err error) {
    51  	if err != nil && s.err == nil {
    52  		s.err = err
    53  	}
    54  }
    55  
    56  func (self *VMState) Snapshot() int {
    57  	self.snapshots = append(self.snapshots, self.state.Copy())
    58  	return len(self.snapshots) - 1
    59  }
    60  
    61  func (self *VMState) RevertToSnapshot(idx int) {
    62  	self.state.Set(self.snapshots[idx])
    63  	self.snapshots = self.snapshots[:idx]
    64  }
    65  
    66  // CreateAccount creates creates a new account object and takes ownership.
    67  func (s *VMState) CreateAccount(addr common.Address) {
    68  	_, err := s.state.CreateStateObject(s.ctx, addr)
    69  	s.errHandler(err)
    70  }
    71  
    72  // AddBalance adds the given amount to the balance of the specified account
    73  func (s *VMState) AddBalance(addr common.Address, amount *big.Int) {
    74  	err := s.state.AddBalance(s.ctx, addr, amount)
    75  	s.errHandler(err)
    76  }
    77  
    78  // SubBalance adds the given amount to the balance of the specified account
    79  func (s *VMState) SubBalance(addr common.Address, amount *big.Int) {
    80  	err := s.state.SubBalance(s.ctx, addr, amount)
    81  	s.errHandler(err)
    82  }
    83  
    84  // ForEachStorage calls a callback function for every key/value pair found
    85  // in the local storage cache. Note that unlike core/state.StateObject,
    86  // light.StateObject only returns cached values and doesn't download the
    87  // entire storage tree.
    88  func (s *VMState) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) {
    89  	err := s.state.ForEachStorage(s.ctx, addr, cb)
    90  	s.errHandler(err)
    91  }
    92  
    93  // GetBalance retrieves the balance from the given address or 0 if the account does
    94  // not exist
    95  func (s *VMState) GetBalance(addr common.Address) *big.Int {
    96  	res, err := s.state.GetBalance(s.ctx, addr)
    97  	s.errHandler(err)
    98  	return res
    99  }
   100  
   101  // GetNonce returns the nonce at the given address or 0 if the account does
   102  // not exist
   103  func (s *VMState) GetNonce(addr common.Address) uint64 {
   104  	res, err := s.state.GetNonce(s.ctx, addr)
   105  	s.errHandler(err)
   106  	return res
   107  }
   108  
   109  // SetNonce sets the nonce of the specified account
   110  func (s *VMState) SetNonce(addr common.Address, nonce uint64) {
   111  	err := s.state.SetNonce(s.ctx, addr, nonce)
   112  	s.errHandler(err)
   113  }
   114  
   115  // GetCode returns the contract code at the given address or nil if the account
   116  // does not exist
   117  func (s *VMState) GetCode(addr common.Address) []byte {
   118  	res, err := s.state.GetCode(s.ctx, addr)
   119  	s.errHandler(err)
   120  	return res
   121  }
   122  
   123  // GetCodeHash returns the contract code hash at the given address
   124  func (s *VMState) GetCodeHash(addr common.Address) common.Hash {
   125  	res, err := s.state.GetCode(s.ctx, addr)
   126  	s.errHandler(err)
   127  	return crypto.Keccak256Hash(res)
   128  }
   129  
   130  // GetCodeSize returns the contract code size at the given address
   131  func (s *VMState) GetCodeSize(addr common.Address) int {
   132  	res, err := s.state.GetCode(s.ctx, addr)
   133  	s.errHandler(err)
   134  	return len(res)
   135  }
   136  
   137  // SetCode sets the contract code at the specified account
   138  func (s *VMState) SetCode(addr common.Address, code []byte) {
   139  	err := s.state.SetCode(s.ctx, addr, code)
   140  	s.errHandler(err)
   141  }
   142  
   143  // AddRefund adds an amount to the refund value collected during a vm execution
   144  func (s *VMState) AddRefund(gas *big.Int) {
   145  	s.state.AddRefund(gas)
   146  }
   147  
   148  // GetRefund returns the refund value collected during a vm execution
   149  func (s *VMState) GetRefund() *big.Int {
   150  	return s.state.GetRefund()
   151  }
   152  
   153  // GetState returns the contract storage value at storage address b from the
   154  // contract address a or common.Hash{} if the account does not exist
   155  func (s *VMState) GetState(a common.Address, b common.Hash) common.Hash {
   156  	res, err := s.state.GetState(s.ctx, a, b)
   157  	s.errHandler(err)
   158  	return res
   159  }
   160  
   161  // SetState sets the storage value at storage address key of the account addr
   162  func (s *VMState) SetState(addr common.Address, key common.Hash, value common.Hash) {
   163  	err := s.state.SetState(s.ctx, addr, key, value)
   164  	s.errHandler(err)
   165  }
   166  
   167  // Suicide marks an account to be removed and clears its balance
   168  func (s *VMState) Suicide(addr common.Address) bool {
   169  	res, err := s.state.Suicide(s.ctx, addr)
   170  	s.errHandler(err)
   171  	return res
   172  }
   173  
   174  // Exist returns true if an account exists at the given address
   175  func (s *VMState) Exist(addr common.Address) bool {
   176  	res, err := s.state.HasAccount(s.ctx, addr)
   177  	s.errHandler(err)
   178  	return res
   179  }
   180  
   181  // Empty returns true if the account at the given address is considered empty
   182  func (s *VMState) Empty(addr common.Address) bool {
   183  	so, err := s.state.GetStateObject(s.ctx, addr)
   184  	s.errHandler(err)
   185  	return so == nil || so.empty()
   186  }
   187  
   188  // HasSuicided returns true if the given account has been marked for deletion
   189  // or false if the account does not exist
   190  func (s *VMState) HasSuicided(addr common.Address) bool {
   191  	res, err := s.state.HasSuicided(s.ctx, addr)
   192  	s.errHandler(err)
   193  	return res
   194  }