github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/core/wavm/tests/call_test.go (about)

     1  package tests
     2  
     3  // import (
     4  // 	"fmt"
     5  // 	"math/big"
     6  // 	"path/filepath"
     7  // 	"testing"
     8  
     9  // 	"github.com/vntchain/go-vnt/common"
    10  // 	"github.com/vntchain/go-vnt/core/vm"
    11  // 	"github.com/vntchain/go-vnt/core/wavm"
    12  // 	wasmContract "github.com/vntchain/go-vnt/core/wavm/contract"
    13  // 	"github.com/vntchain/go-vnt/crypto"
    14  // )
    15  
    16  // var (
    17  // 	basepath         = "./"
    18  // 	callCode         = filepath.Join(basepath, "call/$TestCall.compress")
    19  // 	callAbi          = filepath.Join(basepath, "call/abi.json")
    20  // 	callContractAddr = common.HexToAddress("0xdddddddddddddddddddddddddddddddddddddddf")
    21  // )
    22  
    23  // func newCallVM(iscreated bool) *wavm.Wavm {
    24  // 	wavm := newWavm(callCode, callAbi, iscreated)
    25  // 	code := getCode(callCode)
    26  // 	contract := wasmContract.NewWASMContract(vm.AccountRef(caller), vm.AccountRef(callContractAddr), value, gas)
    27  // 	contract.SetCallCode(&callContractAddr, crypto.Keccak256Hash(code), code)
    28  // 	wavm.ChainContext.Contract = contract
    29  // 	return wavm
    30  // }
    31  
    32  // func TestCall(t *testing.T) {
    33  // 	erc20addr := createERC20()
    34  // 	fmt.Printf("erc20address %s\n", erc20addr)
    35  // 	calladdr := createCall()
    36  // 	fmt.Println(calladdr)
    37  // 	runCall(t, calladdr, erc20addr)
    38  // 	//for i := 0; i < 100; i++ {
    39  // 	//	runCall(t, calladdr, erc20addr)
    40  // 	//}
    41  // 	//
    42  // 	//runCall(t, calladdr, common.HexToAddress("0x0"))
    43  // }
    44  
    45  // func createERC20() common.Address {
    46  // 	initialSupply := new(big.Int)
    47  // 	initialSupply.SetUint64(1000000000)
    48  // 	tokenName := "bitcoin"
    49  // 	tokenSymbol := "BTC"
    50  // 	ctx := vm.Context{
    51  // 		GetHash: getHash,
    52  // 		// Message information
    53  // 		Origin:   origin,
    54  // 		GasPrice: gasPrice,
    55  
    56  // 		// Block information
    57  // 		Coinbase:    coinbase,
    58  // 		GasLimit:    gasLimit,
    59  // 		BlockNumber: blockNumber,
    60  // 		Time:        time,
    61  // 		Difficulty:  difficulty,
    62  // 		CanTransfer: CanTransfer,
    63  // 		Transfer:    Transfer,
    64  // 	}
    65  // 	wvm := newWAVM(ctx)
    66  // 	code := getCode(erc20Code)
    67  // 	input := packInput(getABI(erc20Abi), "", initialSupply, tokenName, tokenSymbol)
    68  // 	res := append(code, input...)
    69  // 	// hexres := hex.EncodeToString(res)
    70  // 	res, ctrAddr, leftgas, err := wvm.Create(vm.AccountRef(caller), res, gas, new(big.Int).SetUint64(0))
    71  // 	if err != nil {
    72  // 		fmt.Printf("ctrAddr %s leftGas %d err %s", ctrAddr.Hex(), leftgas, err)
    73  // 	} else {
    74  // 		fmt.Printf("ctrAddr %s leftGas %d err %s", ctrAddr.Hex(), leftgas, err)
    75  // 	}
    76  // 	return ctrAddr
    77  // }
    78  
    79  // func createCall() common.Address {
    80  // 	ctx := vm.Context{
    81  // 		GetHash: getHash,
    82  // 		// Message information
    83  // 		Origin:   origin,
    84  // 		GasPrice: gasPrice,
    85  
    86  // 		// Block information
    87  // 		Coinbase:    coinbase,
    88  // 		GasLimit:    gasLimit,
    89  // 		BlockNumber: new(big.Int).Add(blockNumber, new(big.Int).SetInt64(1)),
    90  // 		Time:        time,
    91  // 		Difficulty:  difficulty,
    92  // 		CanTransfer: CanTransfer,
    93  // 		Transfer:    Transfer,
    94  // 	}
    95  // 	wvm := newWAVM(ctx)
    96  // 	code := getCode(callCode)
    97  // 	input := packInput(getABI(callAbi), "")
    98  // 	res := append(code, input...)
    99  // 	res, ctrAddr, _, _ := wvm.Create(vm.AccountRef(caller), res, gas, new(big.Int).SetUint64(0))
   100  // 	return ctrAddr
   101  
   102  // }
   103  
   104  // //calladdr 0x553E6c30Af61e7A3576f31311EA8a620F80D047e erc20addr 0x8Af6A7AF30d840ba137e8F3F34d54CfB8BEbA6E2
   105  // func runCall(t *testing.T, calladdr common.Address, erc20addr common.Address) {
   106  // 	fmt.Printf("runCall calladdr %s erc20addr %s", calladdr.Hex(), erc20addr.Hex())
   107  // 	ctx := vm.Context{
   108  // 		GetHash: getHash,
   109  // 		// Message information
   110  // 		Origin:   origin,
   111  // 		GasPrice: gasPrice,
   112  
   113  // 		// Block information
   114  // 		Coinbase:    coinbase,
   115  // 		GasLimit:    gasLimit,
   116  // 		BlockNumber: new(big.Int).Add(blockNumber, new(big.Int).SetInt64(2)),
   117  // 		Time:        time,
   118  // 		Difficulty:  difficulty,
   119  // 		CanTransfer: CanTransfer,
   120  // 		Transfer:    Transfer,
   121  // 	}
   122  // 	wavm := newWAVM(ctx)
   123  // 	callgas := uint64(1000000)
   124  // 	amount := new(big.Int).SetUint64(0)
   125  // 	input := packInput(getABI(callAbi), "Test_GetTokenName", erc20addr, amount, callgas)
   126  // 	res, leftgas, err := wavm.Call(vm.AccountRef(caller), calladdr, input, gas, new(big.Int).SetUint64(0))
   127  // 	var str string
   128  // 	unpackOutput(getABI(callAbi), &str, "Test_GetTokenName", res)
   129  // 	fmt.Printf("res str %s\n", str)
   130  // 	fmt.Printf("res %s gas %d leftGas %d err %s\n", res, gas, leftgas, err)
   131  // 	if str != "bitcoin" {
   132  // 		t.Errorf("unexpected value : want %s, got %s", "bitcoin", str)
   133  // 	} else {
   134  // 		t.Logf("get expected name %s", str)
   135  // 	}
   136  // }