github.com/amazechain/amc@v0.1.3/internal/vm/mock_vm.go (about)

     1  package vm
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/amazechain/amc/common/types"
     6  	"github.com/amazechain/amc/modules/state"
     7  	"math/big"
     8  
     9  	"github.com/holiman/uint256"
    10  )
    11  
    12  type readonlyGetSetter interface {
    13  	setReadonly(outerReadonly bool) func()
    14  	getReadonly() bool
    15  }
    16  
    17  type testVM struct {
    18  	readonlyGetSetter
    19  
    20  	recordedReadOnlies  *[]*readOnlyState
    21  	recordedIsEVMCalled *[]bool
    22  
    23  	env               *EVM
    24  	isEVMSliceTest    []bool
    25  	readOnlySliceTest []bool
    26  	currentIdx        *int
    27  }
    28  
    29  func (evm *testVM) Run(_ *Contract, _ []byte, readOnly bool) (ret []byte, err error) {
    30  	currentReadOnly := new(readOnlyState)
    31  
    32  	currentReadOnly.outer = readOnly
    33  	currentReadOnly.before = evm.getReadonly()
    34  
    35  	currentIndex := *evm.currentIdx
    36  
    37  	callback := evm.setReadonly(readOnly)
    38  	defer func() {
    39  		callback()
    40  		currentReadOnly.after = evm.getReadonly()
    41  	}()
    42  
    43  	currentReadOnly.in = evm.getReadonly()
    44  
    45  	(*evm.recordedReadOnlies)[currentIndex] = currentReadOnly
    46  	(*evm.recordedIsEVMCalled)[currentIndex] = true
    47  
    48  	*evm.currentIdx++
    49  
    50  	if *evm.currentIdx < len(evm.readOnlySliceTest) {
    51  		res, err := run(evm.env, NewContract(
    52  			&dummyContractRef{},
    53  			&dummyContractRef{},
    54  			new(uint256.Int),
    55  			0,
    56  			false,
    57  		), nil, evm.readOnlySliceTest[*evm.currentIdx])
    58  		return res, err
    59  	}
    60  
    61  	return
    62  }
    63  
    64  func (evm *testVM) Depth() int {
    65  	return 0
    66  }
    67  
    68  type readOnlyState struct {
    69  	outer  bool
    70  	before bool
    71  	in     bool
    72  	after  bool
    73  }
    74  
    75  func (r *readOnlyState) String() string {
    76  	return fmt.Sprintf("READONLY Status: outer %t; before %t; in %t; after %t", r.outer, r.before, r.in, r.after)
    77  }
    78  
    79  type dummyContractRef struct {
    80  	calledForEach bool
    81  }
    82  
    83  func (dummyContractRef) ReturnGas(*big.Int)         {}
    84  func (dummyContractRef) Address() types.Address     { return types.Address{} }
    85  func (dummyContractRef) Value() *big.Int            { return new(big.Int) }
    86  func (dummyContractRef) SetCode(types.Hash, []byte) {}
    87  func (d *dummyContractRef) ForEachStorage(callback func(key, value types.Hash) bool) {
    88  	d.calledForEach = true
    89  }
    90  func (d *dummyContractRef) SubBalance(amount *big.Int) {}
    91  func (d *dummyContractRef) AddBalance(amount *big.Int) {}
    92  func (d *dummyContractRef) SetBalance(*big.Int)        {}
    93  func (d *dummyContractRef) SetNonce(uint64)            {}
    94  func (d *dummyContractRef) Balance() *big.Int          { return new(big.Int) }
    95  
    96  type dummyStatedb struct {
    97  	state.IntraBlockState
    98  }
    99  
   100  func (*dummyStatedb) GetRefund() uint64 { return 1337 }