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

     1  package tests
     2  
     3  import (
     4  	"io/ioutil"
     5  	"math/big"
     6  	"os"
     7  
     8  	"github.com/vntchain/go-vnt/accounts/abi"
     9  	"github.com/vntchain/go-vnt/common"
    10  	"github.com/vntchain/go-vnt/common/hexutil"
    11  	"github.com/vntchain/go-vnt/core"
    12  	"github.com/vntchain/go-vnt/core/state"
    13  	"github.com/vntchain/go-vnt/core/wavm"
    14  	"github.com/vntchain/go-vnt/crypto"
    15  	"github.com/vntchain/go-vnt/crypto/sha3"
    16  	"github.com/vntchain/go-vnt/log"
    17  	"github.com/vntchain/go-vnt/rlp"
    18  	"github.com/vntchain/go-vnt/vntdb"
    19  )
    20  
    21  var (
    22  	basepath = "./"
    23  )
    24  
    25  var (
    26  	activeKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f292")
    27  	activeAddr   = crypto.PubkeyToAddress(activeKey.PublicKey)
    28  )
    29  
    30  var logger *log.Logger
    31  
    32  func init() {
    33  	log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))
    34  }
    35  
    36  type vmJSON struct {
    37  	Env      stEnv             `json:"env"`
    38  	Exec     vmExec            `json:"exec"`
    39  	Pre      core.GenesisAlloc `json:"pre"`
    40  	TestCase []testCase        `json:"testcase"`
    41  }
    42  
    43  type stEnv struct {
    44  	Coinbase   common.Address `json:"currentCoinbase"`
    45  	Difficulty *big.Int       `json:"currentDifficulty"`
    46  	GasLimit   uint64         `json:"currentGasLimit"`
    47  	Number     uint64         `json:"currentNumber"`
    48  	Timestamp  uint64         `json:"currentTimestamp"`
    49  }
    50  
    51  type vmExec struct {
    52  	Address  common.Address `json:"address"`
    53  	Value    *big.Int       `json:"value"`
    54  	GasLimit uint64         `json:"gas"`
    55  	Caller   common.Address `json:"caller"`
    56  	Origin   common.Address `json:"origin"`
    57  	GasPrice *big.Int       `json:"gasPrice"`
    58  }
    59  
    60  type testCase struct {
    61  	Code     string   `json:"code"`
    62  	Abi      string   `json:"abi"`
    63  	InitCase initcase `json:"initcase"`
    64  	Tests    []tests  `json:"tests"`
    65  }
    66  
    67  type initcase struct {
    68  	NeedInit bool       `json:"needinit"`
    69  	Input    []argument `json:"input"`
    70  }
    71  
    72  type tests struct {
    73  	Function string        `json:"function"`
    74  	Input    []argument    `json:"input"`
    75  	RawInput hexutil.Bytes `json:"rawinput"`
    76  	Wanted   argument      `json:"wanted"`
    77  	Error    string        `json:"error"`
    78  	Event    []argument    `json:"event"`
    79  }
    80  
    81  type argument struct {
    82  	Data     string `json:"data"`
    83  	DataType string `json:"type"`
    84  }
    85  
    86  func vmTestBlockHash(n uint64) common.Hash {
    87  	return common.BytesToHash(crypto.Keccak256([]byte(big.NewInt(int64(n)).String())))
    88  }
    89  
    90  func MakePreState(db vntdb.Database, accounts core.GenesisAlloc) *state.StateDB {
    91  	sdb := state.NewDatabase(db)
    92  	statedb, _ := state.New(common.Hash{}, sdb)
    93  	activeAccount := core.GenesisAccount{
    94  		Code:    []byte{},
    95  		Storage: map[common.Hash]common.Hash{},
    96  		Balance: big.NewInt(0).Mul(big.NewInt(1e9), big.NewInt(1e18)),
    97  		Nonce:   0,
    98  	}
    99  	accounts[activeAddr] = activeAccount
   100  	for addr, a := range accounts {
   101  		statedb.SetCode(addr, a.Code)
   102  		statedb.SetNonce(addr, a.Nonce)
   103  		statedb.SetBalance(addr, a.Balance)
   104  		for k, v := range a.Storage {
   105  			statedb.SetState(addr, k, v)
   106  		}
   107  	}
   108  	// Commit and re-open to start with a clean state.
   109  	root, _ := statedb.Commit(false)
   110  	statedb, _ = state.New(root, sdb)
   111  	return statedb
   112  }
   113  
   114  func readFile(filepath string) []byte {
   115  	code, err := ioutil.ReadFile(filepath)
   116  	if err != nil {
   117  		panic(err)
   118  	}
   119  	return code
   120  }
   121  
   122  func getABI(filepath string) abi.ABI {
   123  	abi, err := ioutil.ReadFile(filepath)
   124  	if err != nil {
   125  		panic(err)
   126  	}
   127  	abiobj, err := wavm.GetAbi(abi)
   128  	if err != nil {
   129  		panic(err)
   130  	}
   131  	return abiobj
   132  }
   133  
   134  func rlpHash(x interface{}) (h common.Hash) {
   135  	hw := sha3.NewKeccak256()
   136  	rlp.Encode(hw, x)
   137  	hw.Sum(h[:0])
   138  	return h
   139  }