gitlab.com/flarenetwork/coreth@v0.1.1/chain/subscribe_transactions_test.go (about) 1 package chain 2 3 import ( 4 "math/big" 5 "testing" 6 7 "gitlab.com/flarenetwork/coreth/eth/filters" 8 9 "github.com/ethereum/go-ethereum/common" 10 "gitlab.com/flarenetwork/coreth/core/types" 11 ) 12 13 func TestSubscribeTransactions(t *testing.T) { 14 chain, newTxPoolHeadChan, txSubmitCh := NewDefaultChain(t) 15 16 ethBackend := chain.APIBackend() 17 eventSystem := filters.NewEventSystem(ethBackend, true) 18 19 acceptedTxsEventsChannel := make(chan []common.Hash) 20 acceptedTxsEvents := eventSystem.SubscribeAcceptedTxs(acceptedTxsEventsChannel) 21 22 pendingTxsEventsChannel := make(chan []common.Hash) 23 pendingTxsEvents := eventSystem.SubscribePendingTxs(pendingTxsEventsChannel) 24 25 chain.Start() 26 defer chain.Stop() 27 28 // *NOTE* this was pre-compiled for the test.. 29 /* 30 pragma solidity >=0.6.0; 31 32 contract Counter { 33 uint256 x; 34 35 event CounterEmit(uint256 indexed oldval, uint256 indexed newval); 36 37 constructor() public { 38 emit CounterEmit(0, 42); 39 x = 42; 40 } 41 42 function add(uint256 y) public returns (uint256) { 43 x = x + y; 44 emit CounterEmit(y, x); 45 return x; 46 } 47 } 48 */ 49 // contracts, err := compiler.CompileSolidityString("", src) 50 // checkError(err) 51 // contract, _ := contracts[fmt.Sprintf("%s:%s", ".", "Counter")] 52 // _ = contract 53 54 // solc-linux-amd64-v0.6.12+commit.27d51765 --bin -o counter.bin counter.sol 55 56 code := common.Hex2Bytes( 57 "608060405234801561001057600080fd5b50602a60007f53564ba0be98bdbd40460eb78d2387edab91de6a842e1449053dae1f07439a3160405160405180910390a3602a60008190555060e9806100576000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80631003e2d214602d575b600080fd5b605660048036036020811015604157600080fd5b8101908080359060200190929190505050606c565b6040518082815260200191505060405180910390f35b60008160005401600081905550600054827f53564ba0be98bdbd40460eb78d2387edab91de6a842e1449053dae1f07439a3160405160405180910390a3600054905091905056fea2646970667358221220dd9c84516cd903bf6a151cbdaef2f2514c28f2f422782a388a2774412b81f08864736f6c634300060c0033", 58 // contract.Code[2:], 59 ) 60 61 tx := types.NewContractCreation(uint64(0), big.NewInt(0), uint64(gasLimit), gasPrice, code) 62 signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), fundedKey.PrivateKey) 63 if err != nil { 64 t.Fatal(err) 65 } 66 for _, err := range chain.AddRemoteTxs([]*types.Transaction{signedTx}) { 67 if err != nil { 68 t.Fatal(err) 69 } 70 } 71 txs := <-txSubmitCh 72 block, err := chain.GenerateBlock() 73 if err != nil { 74 t.Fatal(err) 75 } 76 insertAndSetPreference(t, chain, block) 77 78 <-newTxPoolHeadChan 79 80 select { 81 case <-acceptedTxsEventsChannel: 82 t.Fatal("Unexpected accepted tx head") 83 default: 84 } 85 86 pendingTx := <-pendingTxsEventsChannel 87 if len(pendingTx) != 1 { 88 t.Fatal("Expected a new pending tx") 89 } 90 if pendingTx[0] != signedTx.Hash() { 91 t.Fatalf("Expected a new pending tx for signed hash %s", signedTx.Hash().String()) 92 } 93 94 if err := chain.Accept(block); err != nil { 95 t.Fatal(err) 96 } 97 98 pendingTxsEventHash := <-pendingTxsEventsChannel 99 100 acceptedTxsEventHash := <-acceptedTxsEventsChannel 101 102 pendingTxsEvents.Unsubscribe() 103 acceptedTxsEvents.Unsubscribe() 104 105 if block.NumberU64() != uint64(1) { 106 t.Fatalf("Expected to create a new block with height 1, but found %d", block.NumberU64()) 107 } 108 109 if len(pendingTxsEventHash) != 1 { 110 t.Fatal("Expected a new pending tx") 111 } 112 if pendingTxsEventHash[0] != signedTx.Hash() { 113 t.Fatalf("Expected a new pending tx for signed hash %s", signedTx.Hash().String()) 114 } 115 116 if len(acceptedTxsEventHash) != 1 { 117 t.Fatal("Expected a new accepted tx") 118 } 119 if acceptedTxsEventHash[0] != signedTx.Hash() { 120 t.Fatalf("Expected a new accepted tx for signed hash %s", signedTx.Hash().String()) 121 } 122 123 if len(txs.Txs) != 1 { 124 t.Fatal("Expected to create a new tx") 125 } 126 if txs.Txs[0].Hash() != signedTx.Hash() { 127 t.Fatalf("Expected subscription for signed hash %s", signedTx.Hash().String()) 128 } 129 }