github.com/annchain/OG@v0.0.9/vm/vm_test/vm_test.go (about)

     1  package vm_test
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/annchain/OG/common"
     6  	"github.com/annchain/OG/common/math"
     7  	"github.com/annchain/OG/vm/eth/common/hexutil"
     8  	"github.com/annchain/OG/vm/eth/core/vm"
     9  	"github.com/annchain/OG/vm/ovm"
    10  	vmtypes "github.com/annchain/OG/vm/types"
    11  	"github.com/sirupsen/logrus"
    12  	"github.com/stretchr/testify/assert"
    13  	"os"
    14  	"testing"
    15  )
    16  
    17  func TestContractSmallStorage(t *testing.T) {
    18  	from := common.HexToAddress("0x01")
    19  
    20  	txContext := &ovm.TxContext{
    21  		From: common.HexToAddress("0x01"),
    22  		//To:       common.HexToAddress("0x02"),
    23  		Value:      math.NewBigInt(0),
    24  		Data:       readFile("OwnedToken.bin"),
    25  		GasPrice:   math.NewBigInt(1),
    26  		GasLimit:   DefaultGasLimit,
    27  		Coinbase:   common.HexToAddress("0x01"),
    28  		SequenceID: 0,
    29  	}
    30  	coinBase := common.HexToAddress("0x1234567812345678AABBCCDDEEFF998877665544")
    31  
    32  	ldb := DefaultLDB(from, coinBase)
    33  
    34  	logrus.Info("init accounts done")
    35  
    36  	context := ovm.NewOVMContext(&ovm.DefaultChainContext{}, &coinBase, ldb)
    37  
    38  	tracer := vm.NewStructLogger(&vm.LogConfig{
    39  		Debug: true,
    40  	})
    41  
    42  	evmInterpreter := vm.NewEVMInterpreter(context, txContext, &vm.InterpreterConfig{
    43  		Debug:  true,
    44  		Tracer: tracer,
    45  	})
    46  
    47  	ovm := ovm.NewOVM(context, []ovm.Interpreter{evmInterpreter}, &ovm.OVMConfig{NoRecursion: false})
    48  
    49  	logrus.Info("Deploying contract")
    50  	ret, contractAddr, leftOverGas, err := ovm.Create(vmtypes.AccountRef(txContext.From), txContext.Data, txContext.GasLimit, txContext.Value.Value)
    51  	// make duplicate
    52  	//ovm.StateDB.SetNonce(coinBase, 0)
    53  	//ret, contractAddr, leftOverGas, err = ovm.Create(&context, vmtypes.AccountRef(coinBase), txContext.Data, txContext.GasLimit, txContext.Value.Value)
    54  	logrus.Info("Deployed contract")
    55  	fmt.Println("CP1", common.Bytes2Hex(ret), contractAddr.String(), leftOverGas, err)
    56  	fmt.Println(ldb.String())
    57  	vm.WriteTrace(os.Stdout, tracer.Logs)
    58  	assert.NoError(t, err)
    59  
    60  	txContext.Value = math.NewBigInt(0)
    61  
    62  	logrus.Info("Calling contract")
    63  
    64  	var name [32]byte
    65  	copy(name[:], "abcdefghijklmnopqrstuvwxyz")
    66  
    67  	var input []byte
    68  	contractAddress, err := hexutil.Decode("0x898855ed")
    69  	assert.NoError(t, err)
    70  	input = append(input, contractAddress...)
    71  	input = append(input, name[:]...)
    72  
    73  	ret, leftOverGas, err = ovm.Call(vmtypes.AccountRef(txContext.From), contractAddr, input, txContext.GasLimit, txContext.Value.Value)
    74  	logrus.Info("Called contract")
    75  	fmt.Println("CP2", common.Bytes2Hex(ret), contractAddr.String(), leftOverGas, err)
    76  	fmt.Println(ldb.String())
    77  	vm.WriteTrace(os.Stdout, tracer.Logs)
    78  	assert.NoError(t, err)
    79  }
    80  
    81  func TestContractHelloWorld(t *testing.T) {
    82  	from := common.HexToAddress("0x01")
    83  	coinBase := common.HexToAddress("0x1234567812345678AABBCCDDEEFF998877665544")
    84  
    85  	tracer := vm.NewStructLogger(&vm.LogConfig{
    86  		Debug: true,
    87  	})
    88  
    89  	ldb := DefaultLDB(from, coinBase)
    90  
    91  	rt := &Runtime{
    92  		Tracer:    tracer,
    93  		VmContext: ovm.NewOVMContext(&ovm.DefaultChainContext{}, &coinBase, ldb),
    94  		TxContext: &ovm.TxContext{
    95  			From: common.HexToAddress("0x01"),
    96  			//To:       common.HexToAddress("0x02"),
    97  			Value:      math.NewBigInt(0),
    98  			Data:       readFile("hello.bin"),
    99  			GasPrice:   math.NewBigInt(1),
   100  			GasLimit:   DefaultGasLimit,
   101  			Coinbase:   coinBase,
   102  			SequenceID: 0,
   103  		},
   104  	}
   105  
   106  	_, contractAddr, _, err := DeployContract("hello.bin", from, coinBase, rt, nil)
   107  	assert.NoError(t, err)
   108  
   109  	value := math.NewBigInt(0)
   110  
   111  	_, _, err = CallContract(contractAddr, from, coinBase, rt, value, "898855ed", []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
   112  	assert.NoError(t, err)
   113  
   114  }