gitlab.com/flarenetwork/coreth@v0.1.1/chain/counter_test.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. All rights reserved. 2 3 // NOTE from Ted: to compile from solidity source using the code, make sure 4 // your solc<0.8.0, as geth 1.9.21 does not support the JSON output from 5 // solc>=0.8.0: 6 // See: 7 // - https://github.com/ethereum/go-ethereum/issues/22041 8 // - https://github.com/ethereum/go-ethereum/pull/22092 9 10 package chain 11 12 import ( 13 "fmt" 14 "math/big" 15 16 "testing" 17 18 "github.com/ethereum/go-ethereum/common" 19 "gitlab.com/flarenetwork/coreth/core/types" 20 21 "github.com/ethereum/go-ethereum/log" 22 ) 23 24 func TestCounter(t *testing.T) { 25 chain, newTxPoolHeadChan, txSubmitCh := NewDefaultChain(t) 26 27 // Mark the genesis block as accepted and start the chain 28 chain.Start() 29 defer chain.Stop() 30 31 // NOTE: use precompiled `counter.sol` for portability, do not remove the 32 // following code (for debug purpose) 33 //counterSrc, err := filepath.Abs(gopath + "/src/github.com/ava-labs/coreth/examples/counter/counter.sol") 34 // if err != nil { 35 // t.Fatal(err) 36 // } 37 //contracts, err := compiler.CompileSolidity("", counterSrc) 38 // if err != nil { 39 // t.Fatal(err) 40 // } 41 //contract, _ := contracts[fmt.Sprintf("%s:%s", counterSrc, "Counter")] 42 //code := common.Hex2Bytes(contract.Code[2:]) 43 contract := "6080604052348015600f57600080fd5b50602a60008190555060b9806100266000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80631003e2d214602d575b600080fd5b605660048036036020811015604157600080fd5b8101908080359060200190929190505050606c565b6040518082815260200191505060405180910390f35b60008160005401600081905550600054905091905056fea264697066735822122066dad7255aac3ea41858c2a0fe986696876ac85b2bb4e929d2062504c244054964736f6c63430007060033" 44 code := common.Hex2Bytes(contract) 45 46 nonce := uint64(0) 47 tx := types.NewContractCreation(nonce, big.NewInt(0), uint64(gasLimit), gasPrice, code) 48 signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), fundedKey.PrivateKey) 49 if err != nil { 50 t.Fatal(err) 51 } 52 for _, err := range chain.AddRemoteTxs([]*types.Transaction{signedTx}) { 53 if err != nil { 54 t.Fatal(err) 55 } 56 } 57 <-txSubmitCh 58 nonce++ 59 block, err := chain.GenerateBlock() 60 if err != nil { 61 t.Fatal(err) 62 } 63 insertAndAccept(t, chain, block) 64 65 <-newTxPoolHeadChan 66 log.Info("Generated block with new counter contract creation", "blkNumber", block.NumberU64()) 67 68 receipts := chain.GetReceiptsByHash(block.Hash()) 69 if len(receipts) != 1 { 70 t.Fatalf("Expected length of receipts to be 1, but found %d", len(receipts)) 71 } 72 contractAddr := receipts[0].ContractAddress 73 74 call := common.Hex2Bytes("1003e2d20000000000000000000000000000000000000000000000000000000000000001") 75 txs := make([]*types.Transaction, 0) 76 for i := 0; i < 10; i++ { 77 tx := types.NewTransaction(nonce, contractAddr, big.NewInt(0), uint64(gasLimit), gasPrice, call) 78 signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), fundedKey.PrivateKey) 79 if err != nil { 80 t.Fatal(err) 81 } 82 txs = append(txs, signedTx) 83 nonce++ 84 } 85 for _, err := range chain.AddRemoteTxs(txs) { 86 if err != nil { 87 t.Fatal(err) 88 } 89 } 90 <-txSubmitCh 91 // Generate block 92 block, err = chain.GenerateBlock() 93 if err != nil { 94 t.Fatal(err) 95 } 96 insertAndAccept(t, chain, block) 97 98 <-newTxPoolHeadChan 99 log.Info("Generated block with counter contract interactions", "blkNumber", block.NumberU64()) 100 receipts = chain.GetReceiptsByHash(block.Hash()) 101 if len(receipts) != 10 { 102 t.Fatalf("Expected 10 receipts, but found %d", len(receipts)) 103 } 104 105 state, err := chain.CurrentState() 106 if err != nil { 107 t.Fatal(err) 108 } 109 xState := state.GetState(contractAddr, common.BigToHash(big.NewInt(0))) 110 111 log.Info(fmt.Sprintf("genesis balance = %s", state.GetBalance(fundedKey.Address))) 112 log.Info(fmt.Sprintf("contract balance = %s", state.GetBalance(contractAddr))) 113 log.Info(fmt.Sprintf("x = %s", xState.String())) 114 if xState.Big().Cmp(big.NewInt(52)) != 0 { 115 t.Fatal("incorrect state value") 116 } 117 }