gitlab.com/flarenetwork/coreth@v0.1.1/chain/subscribe_accepted_heads_test.go (about) 1 package chain 2 3 import ( 4 "math/big" 5 "testing" 6 7 "github.com/ethereum/go-ethereum/common" 8 "github.com/ethereum/go-ethereum/log" 9 "gitlab.com/flarenetwork/coreth/core" 10 "gitlab.com/flarenetwork/coreth/core/types" 11 ) 12 13 func TestAcceptedHeadSubscriptions(t *testing.T) { 14 chain, newTxPoolHeadChan, txSubmitCh := NewDefaultChain(t) 15 16 chain.Start() 17 defer chain.Stop() 18 19 ethBackend := chain.APIBackend() 20 21 acceptedChainCh := make(chan core.ChainEvent, 1000) 22 chainCh := make(chan core.ChainEvent, 1000) 23 ethBackend.SubscribeChainAcceptedEvent(acceptedChainCh) 24 ethBackend.SubscribeChainEvent(chainCh) 25 26 // *NOTE* this was pre-compiled for the test.. 27 // src := `pragma solidity >=0.6.0; 28 // 29 // contract Counter { 30 // uint256 x; 31 // 32 // constructor() public { 33 // x = 42; 34 // } 35 // 36 // function add(uint256 y) public returns (uint256) { 37 // x = x + y; 38 // return x; 39 // } 40 // }` 41 // contracts, err := compiler.CompileSolidityString("", src) 42 // checkError(err) 43 // contract, _ := contracts[fmt.Sprintf("%s:%s", ".", "Counter")] 44 // _ = contract 45 46 // solc-linux-amd64-v0.6.12+commit.27d51765 --bin -o counter.bin counter.sol 47 48 code := common.Hex2Bytes( 49 "6080604052348015600f57600080fd5b50602a60008190555060b9806100266000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80631003e2d214602d575b600080fd5b605660048036036020811015604157600080fd5b8101908080359060200190929190505050606c565b6040518082815260200191505060405180910390f35b60008160005401600081905550600054905091905056fea26469706673582212200dc7c76677426e8c621c6839348a7c8d60787c546a9b9c7fc91efa57f71d46a364736f6c634300060c0033", 50 // contract.Code[2:], 51 ) 52 tx := types.NewContractCreation(uint64(0), big.NewInt(0), uint64(gasLimit), gasPrice, code) 53 signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), fundedKey.PrivateKey) 54 if err != nil { 55 t.Fatal(err) 56 } 57 for _, err := range chain.AddRemoteTxs([]*types.Transaction{signedTx}) { 58 if err != nil { 59 t.Fatal(err) 60 } 61 } 62 <-txSubmitCh 63 64 block, err := chain.GenerateBlock() 65 if err != nil { 66 t.Fatal(err) 67 } 68 insertAndSetPreference(t, chain, block) 69 <-newTxPoolHeadChan 70 log.Info("Generated block with new counter contract creation", "blkNumber", block.NumberU64()) 71 72 if block.NumberU64() != uint64(1) { 73 t.Fatal(err) 74 } 75 76 select { 77 case fb := <-chainCh: 78 if fb.Block.NumberU64() != 1 { 79 t.Fatalf("received block with unexpected block number %d via chain channel", fb.Block.NumberU64()) 80 } 81 if fb.Block.Hash() != block.Hash() { 82 t.Fatalf("Received block with unexpected hash %s via chain channel", fb.Block.Hash().String()) 83 } 84 default: 85 t.Fatal("failed to received block via chain channel") 86 } 87 88 select { 89 case <-acceptedChainCh: 90 t.Fatalf("Received unexpected chain accept event before accepting block") 91 default: 92 } 93 94 if err := chain.Accept(block); err != nil { 95 t.Fatal(err) 96 } 97 98 select { 99 case fb := <-acceptedChainCh: 100 if fb.Block.NumberU64() != 1 { 101 t.Fatalf("received block with unexpected block number %d on accepted block channel", fb.Block.NumberU64()) 102 } 103 if fb.Block.Hash() != block.Hash() { 104 t.Fatalf("Received block with unexpected hash %s via accepted block channel", fb.Block.Hash().String()) 105 } 106 default: 107 t.Fatal("failed to received block via accepted block channel") 108 } 109 }