gitlab.com/flarenetwork/coreth@v0.1.1/chain/payment_test.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package chain
     5  
     6  import (
     7  	"math/big"
     8  	"testing"
     9  
    10  	"github.com/ethereum/go-ethereum/log"
    11  	"gitlab.com/flarenetwork/coreth/core/types"
    12  )
    13  
    14  // TestPayment tests basic payment (balance, not multi-coin)
    15  func TestPayment(t *testing.T) {
    16  	chain, newTxPoolHeadChan, txSubmitCh := NewDefaultChain(t)
    17  
    18  	// Mark the genesis block as accepted and start the chain
    19  	chain.Start()
    20  	defer chain.Stop()
    21  
    22  	nonce := uint64(0)
    23  	numTxs := 10
    24  	txs := make([]*types.Transaction, 0)
    25  	for i := 0; i < numTxs; i++ {
    26  		tx := types.NewTransaction(nonce, bob.Address, value, uint64(basicTxGasLimit), gasPrice, nil)
    27  		signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), fundedKey.PrivateKey)
    28  		if err != nil {
    29  			t.Fatal(err)
    30  		}
    31  		txs = append(txs, signedTx)
    32  		nonce++
    33  	}
    34  	for _, err := range chain.AddRemoteTxs(txs) {
    35  		if err != nil {
    36  			t.Fatalf("Failed to add remote transactions due to %s", err)
    37  		}
    38  	}
    39  	<-txSubmitCh
    40  
    41  	block, err := chain.GenerateBlock()
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	insertAndAccept(t, chain, block)
    46  	<-newTxPoolHeadChan
    47  
    48  	if txs := block.Transactions(); len(txs) != numTxs {
    49  		t.Fatalf("Expected block to contain %d transactions, but found %d transactions", numTxs, len(txs))
    50  	}
    51  	log.Info("Generated block", "BlockHash", block.Hash(), "BlockNumber", block.NumberU64())
    52  	currentBlock := chain.BlockChain().CurrentBlock()
    53  	if currentBlock.Hash() != block.Hash() {
    54  		t.Fatalf("Found unexpected current block (%s, %d), expected (%s, %d)", currentBlock.Hash(), currentBlock.NumberU64(), block.Hash(), block.NumberU64())
    55  	}
    56  
    57  	// state, err := chain.BlockState(currentBlock)
    58  	state, err := chain.CurrentState()
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	genBalance := state.GetBalance(fundedKey.Address)
    63  	bobBalance := state.GetBalance(bob.Address)
    64  	expectedBalance := new(big.Int).Mul(value, big.NewInt(int64(numTxs)))
    65  	if bobBalance.Cmp(expectedBalance) != 0 {
    66  		t.Fatalf("Found incorrect balance %d for bob's address, expected %d", bobBalance, expectedBalance)
    67  	}
    68  
    69  	totalBalance := bobBalance.Add(bobBalance, genBalance)
    70  	totalFees := new(big.Int).Mul(big.NewInt(int64(numTxs*basicTxGasLimit)), gasPrice)
    71  	expectedTotalBalance := new(big.Int).Sub(initialBalance, totalFees)
    72  	if totalBalance.Cmp(expectedTotalBalance) != 0 {
    73  		t.Fatalf("Found incorrect total balance %d, expected total balance of %d", totalBalance, expectedTotalBalance)
    74  	}
    75  }