github.com/ethw3/go-ethereuma@v0.0.0-20221013053120-c14602a4c23c/ethclient/gethclient/gethclient_test.go (about)

     1  // Copyright 2021 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 gethclient
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"math/big"
    23  	"testing"
    24  
    25  	"github.com/ethw3/go-ethereuma"
    26  	"github.com/ethw3/go-ethereuma/common"
    27  	"github.com/ethw3/go-ethereuma/consensus/ethash"
    28  	"github.com/ethw3/go-ethereuma/core"
    29  	"github.com/ethw3/go-ethereuma/core/rawdb"
    30  	"github.com/ethw3/go-ethereuma/core/types"
    31  	"github.com/ethw3/go-ethereuma/crypto"
    32  	"github.com/ethw3/go-ethereuma/eth"
    33  	"github.com/ethw3/go-ethereuma/eth/ethconfig"
    34  	"github.com/ethw3/go-ethereuma/eth/filters"
    35  	"github.com/ethw3/go-ethereuma/ethclient"
    36  	"github.com/ethw3/go-ethereuma/node"
    37  	"github.com/ethw3/go-ethereuma/params"
    38  	"github.com/ethw3/go-ethereuma/rpc"
    39  )
    40  
    41  var (
    42  	testKey, _  = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    43  	testAddr    = crypto.PubkeyToAddress(testKey.PublicKey)
    44  	testSlot    = common.HexToHash("0xdeadbeef")
    45  	testValue   = crypto.Keccak256Hash(testSlot[:])
    46  	testBalance = big.NewInt(2e15)
    47  )
    48  
    49  func newTestBackend(t *testing.T) (*node.Node, []*types.Block) {
    50  	// Generate test chain.
    51  	genesis, blocks := generateTestChain()
    52  	// Create node
    53  	n, err := node.New(&node.Config{})
    54  	if err != nil {
    55  		t.Fatalf("can't create new node: %v", err)
    56  	}
    57  	// Create Ethereum Service
    58  	config := &ethconfig.Config{Genesis: genesis}
    59  	config.Ethash.PowMode = ethash.ModeFake
    60  	ethservice, err := eth.New(n, config)
    61  	if err != nil {
    62  		t.Fatalf("can't create new ethereum service: %v", err)
    63  	}
    64  	filterSystem := filters.NewFilterSystem(ethservice.APIBackend, filters.Config{})
    65  	n.RegisterAPIs([]rpc.API{{
    66  		Namespace: "eth",
    67  		Service:   filters.NewFilterAPI(filterSystem, false),
    68  	}})
    69  
    70  	// Import the test chain.
    71  	if err := n.Start(); err != nil {
    72  		t.Fatalf("can't start test node: %v", err)
    73  	}
    74  	if _, err := ethservice.BlockChain().InsertChain(blocks[1:]); err != nil {
    75  		t.Fatalf("can't import test blocks: %v", err)
    76  	}
    77  	return n, blocks
    78  }
    79  
    80  func generateTestChain() (*core.Genesis, []*types.Block) {
    81  	db := rawdb.NewMemoryDatabase()
    82  	config := params.AllEthashProtocolChanges
    83  	genesis := &core.Genesis{
    84  		Config:    config,
    85  		Alloc:     core.GenesisAlloc{testAddr: {Balance: testBalance, Storage: map[common.Hash]common.Hash{testSlot: testValue}}},
    86  		ExtraData: []byte("test genesis"),
    87  		Timestamp: 9000,
    88  	}
    89  	generate := func(i int, g *core.BlockGen) {
    90  		g.OffsetTime(5)
    91  		g.SetExtra([]byte("test"))
    92  	}
    93  	gblock := genesis.MustCommit(db)
    94  	engine := ethash.NewFaker()
    95  	blocks, _ := core.GenerateChain(config, gblock, engine, db, 1, generate)
    96  	blocks = append([]*types.Block{gblock}, blocks...)
    97  	return genesis, blocks
    98  }
    99  
   100  func TestGethClient(t *testing.T) {
   101  	backend, _ := newTestBackend(t)
   102  	client, err := backend.Attach()
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  	defer backend.Close()
   107  	defer client.Close()
   108  
   109  	tests := []struct {
   110  		name string
   111  		test func(t *testing.T)
   112  	}{
   113  		{
   114  			"TestAccessList",
   115  			func(t *testing.T) { testAccessList(t, client) },
   116  		},
   117  		{
   118  			"TestGetProof",
   119  			func(t *testing.T) { testGetProof(t, client) },
   120  		}, {
   121  			"TestGCStats",
   122  			func(t *testing.T) { testGCStats(t, client) },
   123  		}, {
   124  			"TestMemStats",
   125  			func(t *testing.T) { testMemStats(t, client) },
   126  		}, {
   127  			"TestGetNodeInfo",
   128  			func(t *testing.T) { testGetNodeInfo(t, client) },
   129  		}, {
   130  			"TestSetHead",
   131  			func(t *testing.T) { testSetHead(t, client) },
   132  		}, {
   133  			"TestSubscribePendingTxs",
   134  			func(t *testing.T) { testSubscribePendingTransactions(t, client) },
   135  		}, {
   136  			"TestCallContract",
   137  			func(t *testing.T) { testCallContract(t, client) },
   138  		},
   139  	}
   140  	t.Parallel()
   141  	for _, tt := range tests {
   142  		t.Run(tt.name, tt.test)
   143  	}
   144  }
   145  
   146  func testAccessList(t *testing.T, client *rpc.Client) {
   147  	ec := New(client)
   148  	// Test transfer
   149  	msg := ethereum.CallMsg{
   150  		From:     testAddr,
   151  		To:       &common.Address{},
   152  		Gas:      21000,
   153  		GasPrice: big.NewInt(765625000),
   154  		Value:    big.NewInt(1),
   155  	}
   156  	al, gas, vmErr, err := ec.CreateAccessList(context.Background(), msg)
   157  	if err != nil {
   158  		t.Fatalf("unexpected error: %v", err)
   159  	}
   160  	if vmErr != "" {
   161  		t.Fatalf("unexpected vm error: %v", vmErr)
   162  	}
   163  	if gas != 21000 {
   164  		t.Fatalf("unexpected gas used: %v", gas)
   165  	}
   166  	if len(*al) != 0 {
   167  		t.Fatalf("unexpected length of accesslist: %v", len(*al))
   168  	}
   169  	// Test reverting transaction
   170  	msg = ethereum.CallMsg{
   171  		From:     testAddr,
   172  		To:       nil,
   173  		Gas:      100000,
   174  		GasPrice: big.NewInt(1000000000),
   175  		Value:    big.NewInt(1),
   176  		Data:     common.FromHex("0x608060806080608155fd"),
   177  	}
   178  	al, gas, vmErr, err = ec.CreateAccessList(context.Background(), msg)
   179  	if err != nil {
   180  		t.Fatalf("unexpected error: %v", err)
   181  	}
   182  	if vmErr == "" {
   183  		t.Fatalf("wanted vmErr, got none")
   184  	}
   185  	if gas == 21000 {
   186  		t.Fatalf("unexpected gas used: %v", gas)
   187  	}
   188  	if len(*al) != 1 || al.StorageKeys() != 1 {
   189  		t.Fatalf("unexpected length of accesslist: %v", len(*al))
   190  	}
   191  	// address changes between calls, so we can't test for it.
   192  	if (*al)[0].Address == common.HexToAddress("0x0") {
   193  		t.Fatalf("unexpected address: %v", (*al)[0].Address)
   194  	}
   195  	if (*al)[0].StorageKeys[0] != common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000081") {
   196  		t.Fatalf("unexpected storage key: %v", (*al)[0].StorageKeys[0])
   197  	}
   198  }
   199  
   200  func testGetProof(t *testing.T, client *rpc.Client) {
   201  	ec := New(client)
   202  	ethcl := ethclient.NewClient(client)
   203  	result, err := ec.GetProof(context.Background(), testAddr, []string{testSlot.String()}, nil)
   204  	if err != nil {
   205  		t.Fatal(err)
   206  	}
   207  	if !bytes.Equal(result.Address[:], testAddr[:]) {
   208  		t.Fatalf("unexpected address, want: %v got: %v", testAddr, result.Address)
   209  	}
   210  	// test nonce
   211  	nonce, _ := ethcl.NonceAt(context.Background(), result.Address, nil)
   212  	if result.Nonce != nonce {
   213  		t.Fatalf("invalid nonce, want: %v got: %v", nonce, result.Nonce)
   214  	}
   215  	// test balance
   216  	balance, _ := ethcl.BalanceAt(context.Background(), result.Address, nil)
   217  	if result.Balance.Cmp(balance) != 0 {
   218  		t.Fatalf("invalid balance, want: %v got: %v", balance, result.Balance)
   219  	}
   220  	// test storage
   221  	if len(result.StorageProof) != 1 {
   222  		t.Fatalf("invalid storage proof, want 1 proof, got %v proof(s)", len(result.StorageProof))
   223  	}
   224  	proof := result.StorageProof[0]
   225  	slotValue, _ := ethcl.StorageAt(context.Background(), testAddr, testSlot, nil)
   226  	if !bytes.Equal(slotValue, proof.Value.Bytes()) {
   227  		t.Fatalf("invalid storage proof value, want: %v, got: %v", slotValue, proof.Value.Bytes())
   228  	}
   229  	if proof.Key != testSlot.String() {
   230  		t.Fatalf("invalid storage proof key, want: %v, got: %v", testSlot.String(), proof.Key)
   231  	}
   232  }
   233  
   234  func testGCStats(t *testing.T, client *rpc.Client) {
   235  	ec := New(client)
   236  	_, err := ec.GCStats(context.Background())
   237  	if err != nil {
   238  		t.Fatal(err)
   239  	}
   240  }
   241  
   242  func testMemStats(t *testing.T, client *rpc.Client) {
   243  	ec := New(client)
   244  	stats, err := ec.MemStats(context.Background())
   245  	if err != nil {
   246  		t.Fatal(err)
   247  	}
   248  	if stats.Alloc == 0 {
   249  		t.Fatal("Invalid mem stats retrieved")
   250  	}
   251  }
   252  
   253  func testGetNodeInfo(t *testing.T, client *rpc.Client) {
   254  	ec := New(client)
   255  	info, err := ec.GetNodeInfo(context.Background())
   256  	if err != nil {
   257  		t.Fatal(err)
   258  	}
   259  
   260  	if info.Name == "" {
   261  		t.Fatal("Invalid node info retrieved")
   262  	}
   263  }
   264  
   265  func testSetHead(t *testing.T, client *rpc.Client) {
   266  	ec := New(client)
   267  	err := ec.SetHead(context.Background(), big.NewInt(0))
   268  	if err != nil {
   269  		t.Fatal(err)
   270  	}
   271  }
   272  
   273  func testSubscribePendingTransactions(t *testing.T, client *rpc.Client) {
   274  	ec := New(client)
   275  	ethcl := ethclient.NewClient(client)
   276  	// Subscribe to Transactions
   277  	ch := make(chan common.Hash)
   278  	ec.SubscribePendingTransactions(context.Background(), ch)
   279  	// Send a transaction
   280  	chainID, err := ethcl.ChainID(context.Background())
   281  	if err != nil {
   282  		t.Fatal(err)
   283  	}
   284  	// Create transaction
   285  	tx := types.NewTransaction(0, common.Address{1}, big.NewInt(1), 22000, big.NewInt(1), nil)
   286  	signer := types.LatestSignerForChainID(chainID)
   287  	signature, err := crypto.Sign(signer.Hash(tx).Bytes(), testKey)
   288  	if err != nil {
   289  		t.Fatal(err)
   290  	}
   291  	signedTx, err := tx.WithSignature(signer, signature)
   292  	if err != nil {
   293  		t.Fatal(err)
   294  	}
   295  	// Send transaction
   296  	err = ethcl.SendTransaction(context.Background(), signedTx)
   297  	if err != nil {
   298  		t.Fatal(err)
   299  	}
   300  	// Check that the transaction was send over the channel
   301  	hash := <-ch
   302  	if hash != signedTx.Hash() {
   303  		t.Fatalf("Invalid tx hash received, got %v, want %v", hash, signedTx.Hash())
   304  	}
   305  }
   306  
   307  func testCallContract(t *testing.T, client *rpc.Client) {
   308  	ec := New(client)
   309  	msg := ethereum.CallMsg{
   310  		From:     testAddr,
   311  		To:       &common.Address{},
   312  		Gas:      21000,
   313  		GasPrice: big.NewInt(1000000000),
   314  		Value:    big.NewInt(1),
   315  	}
   316  	// CallContract without override
   317  	if _, err := ec.CallContract(context.Background(), msg, big.NewInt(0), nil); err != nil {
   318  		t.Fatalf("unexpected error: %v", err)
   319  	}
   320  	// CallContract with override
   321  	override := OverrideAccount{
   322  		Nonce: 1,
   323  	}
   324  	mapAcc := make(map[common.Address]OverrideAccount)
   325  	mapAcc[testAddr] = override
   326  	if _, err := ec.CallContract(context.Background(), msg, big.NewInt(0), &mapAcc); err != nil {
   327  		t.Fatalf("unexpected error: %v", err)
   328  	}
   329  }