github.com/frankli-dev/go-ethereum@v1.1.1/eth/catalyst/api_test.go (about) 1 // Copyright 2020 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 catalyst 18 19 import ( 20 "math/big" 21 "testing" 22 23 "github.com/frankli-dev/go-ethereum/consensus/ethash" 24 "github.com/frankli-dev/go-ethereum/core" 25 "github.com/frankli-dev/go-ethereum/core/rawdb" 26 "github.com/frankli-dev/go-ethereum/core/types" 27 "github.com/frankli-dev/go-ethereum/crypto" 28 "github.com/frankli-dev/go-ethereum/eth" 29 "github.com/frankli-dev/go-ethereum/eth/ethconfig" 30 "github.com/frankli-dev/go-ethereum/node" 31 "github.com/frankli-dev/go-ethereum/params" 32 ) 33 34 var ( 35 // testKey is a private key to use for funding a tester account. 36 testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 37 38 // testAddr is the Ethereum address of the tester account. 39 testAddr = crypto.PubkeyToAddress(testKey.PublicKey) 40 41 testBalance = big.NewInt(2e10) 42 ) 43 44 func generateTestChain() (*core.Genesis, []*types.Block) { 45 db := rawdb.NewMemoryDatabase() 46 config := params.AllEthashProtocolChanges 47 genesis := &core.Genesis{ 48 Config: config, 49 Alloc: core.GenesisAlloc{testAddr: {Balance: testBalance}}, 50 ExtraData: []byte("test genesis"), 51 Timestamp: 9000, 52 } 53 generate := func(i int, g *core.BlockGen) { 54 g.OffsetTime(5) 55 g.SetExtra([]byte("test")) 56 } 57 gblock := genesis.ToBlock(db) 58 engine := ethash.NewFaker() 59 blocks, _ := core.GenerateChain(config, gblock, engine, db, 10, generate) 60 blocks = append([]*types.Block{gblock}, blocks...) 61 return genesis, blocks 62 } 63 64 func generateTestChainWithFork(n int, fork int) (*core.Genesis, []*types.Block, []*types.Block) { 65 if fork >= n { 66 fork = n - 1 67 } 68 db := rawdb.NewMemoryDatabase() 69 config := ¶ms.ChainConfig{ 70 ChainID: big.NewInt(1337), 71 HomesteadBlock: big.NewInt(0), 72 EIP150Block: big.NewInt(0), 73 EIP155Block: big.NewInt(0), 74 EIP158Block: big.NewInt(0), 75 ByzantiumBlock: big.NewInt(0), 76 ConstantinopleBlock: big.NewInt(0), 77 PetersburgBlock: big.NewInt(0), 78 IstanbulBlock: big.NewInt(0), 79 MuirGlacierBlock: big.NewInt(0), 80 BerlinBlock: big.NewInt(0), 81 CatalystBlock: big.NewInt(0), 82 Ethash: new(params.EthashConfig), 83 } 84 genesis := &core.Genesis{ 85 Config: config, 86 Alloc: core.GenesisAlloc{testAddr: {Balance: testBalance}}, 87 ExtraData: []byte("test genesis"), 88 Timestamp: 9000, 89 } 90 generate := func(i int, g *core.BlockGen) { 91 g.OffsetTime(5) 92 g.SetExtra([]byte("test")) 93 } 94 generateFork := func(i int, g *core.BlockGen) { 95 g.OffsetTime(5) 96 g.SetExtra([]byte("testF")) 97 } 98 gblock := genesis.ToBlock(db) 99 engine := ethash.NewFaker() 100 blocks, _ := core.GenerateChain(config, gblock, engine, db, n, generate) 101 blocks = append([]*types.Block{gblock}, blocks...) 102 forkedBlocks, _ := core.GenerateChain(config, blocks[fork], engine, db, n-fork, generateFork) 103 return genesis, blocks, forkedBlocks 104 } 105 106 func TestEth2AssembleBlock(t *testing.T) { 107 genesis, blocks := generateTestChain() 108 n, ethservice := startEthService(t, genesis, blocks[1:9]) 109 defer n.Close() 110 111 api := newConsensusAPI(ethservice) 112 signer := types.NewEIP155Signer(ethservice.BlockChain().Config().ChainID) 113 tx, err := types.SignTx(types.NewTransaction(0, blocks[8].Coinbase(), big.NewInt(1000), params.TxGas, nil, nil), signer, testKey) 114 if err != nil { 115 t.Fatalf("error signing transaction, err=%v", err) 116 } 117 ethservice.TxPool().AddLocal(tx) 118 blockParams := assembleBlockParams{ 119 ParentHash: blocks[8].ParentHash(), 120 Timestamp: blocks[8].Time(), 121 } 122 execData, err := api.AssembleBlock(blockParams) 123 124 if err != nil { 125 t.Fatalf("error producing block, err=%v", err) 126 } 127 128 if len(execData.Transactions) != 1 { 129 t.Fatalf("invalid number of transactions %d != 1", len(execData.Transactions)) 130 } 131 } 132 133 func TestEth2AssembleBlockWithAnotherBlocksTxs(t *testing.T) { 134 genesis, blocks := generateTestChain() 135 n, ethservice := startEthService(t, genesis, blocks[1:9]) 136 defer n.Close() 137 138 api := newConsensusAPI(ethservice) 139 140 // Put the 10th block's tx in the pool and produce a new block 141 api.addBlockTxs(blocks[9]) 142 blockParams := assembleBlockParams{ 143 ParentHash: blocks[9].ParentHash(), 144 Timestamp: blocks[9].Time(), 145 } 146 execData, err := api.AssembleBlock(blockParams) 147 if err != nil { 148 t.Fatalf("error producing block, err=%v", err) 149 } 150 151 if len(execData.Transactions) != blocks[9].Transactions().Len() { 152 t.Fatalf("invalid number of transactions %d != 1", len(execData.Transactions)) 153 } 154 } 155 156 func TestEth2NewBlock(t *testing.T) { 157 genesis, blocks, forkedBlocks := generateTestChainWithFork(10, 4) 158 n, ethservice := startEthService(t, genesis, blocks[1:5]) 159 defer n.Close() 160 161 api := newConsensusAPI(ethservice) 162 for i := 5; i < 10; i++ { 163 p := executableData{ 164 ParentHash: ethservice.BlockChain().CurrentBlock().Hash(), 165 Miner: blocks[i].Coinbase(), 166 StateRoot: blocks[i].Root(), 167 GasLimit: blocks[i].GasLimit(), 168 GasUsed: blocks[i].GasUsed(), 169 Transactions: encodeTransactions(blocks[i].Transactions()), 170 ReceiptRoot: blocks[i].ReceiptHash(), 171 LogsBloom: blocks[i].Bloom().Bytes(), 172 BlockHash: blocks[i].Hash(), 173 Timestamp: blocks[i].Time(), 174 Number: uint64(i), 175 } 176 success, err := api.NewBlock(p) 177 if err != nil || !success.Valid { 178 t.Fatalf("Failed to insert block: %v", err) 179 } 180 } 181 182 exp := ethservice.BlockChain().CurrentBlock().Hash() 183 184 // Introduce the fork point. 185 lastBlockNum := blocks[4].Number() 186 lastBlock := blocks[4] 187 for i := 0; i < 4; i++ { 188 lastBlockNum.Add(lastBlockNum, big.NewInt(1)) 189 p := executableData{ 190 ParentHash: lastBlock.Hash(), 191 Miner: forkedBlocks[i].Coinbase(), 192 StateRoot: forkedBlocks[i].Root(), 193 Number: lastBlockNum.Uint64(), 194 GasLimit: forkedBlocks[i].GasLimit(), 195 GasUsed: forkedBlocks[i].GasUsed(), 196 Transactions: encodeTransactions(blocks[i].Transactions()), 197 ReceiptRoot: forkedBlocks[i].ReceiptHash(), 198 LogsBloom: forkedBlocks[i].Bloom().Bytes(), 199 BlockHash: forkedBlocks[i].Hash(), 200 Timestamp: forkedBlocks[i].Time(), 201 } 202 success, err := api.NewBlock(p) 203 if err != nil || !success.Valid { 204 t.Fatalf("Failed to insert forked block #%d: %v", i, err) 205 } 206 lastBlock, err = insertBlockParamsToBlock(p) 207 if err != nil { 208 t.Fatal(err) 209 } 210 } 211 212 if ethservice.BlockChain().CurrentBlock().Hash() != exp { 213 t.Fatalf("Wrong head after inserting fork %x != %x", exp, ethservice.BlockChain().CurrentBlock().Hash()) 214 } 215 } 216 217 // startEthService creates a full node instance for testing. 218 func startEthService(t *testing.T, genesis *core.Genesis, blocks []*types.Block) (*node.Node, *eth.Ethereum) { 219 t.Helper() 220 221 n, err := node.New(&node.Config{}) 222 if err != nil { 223 t.Fatal("can't create node:", err) 224 } 225 226 ethcfg := ðconfig.Config{Genesis: genesis, Ethash: ethash.Config{PowMode: ethash.ModeFake}} 227 ethservice, err := eth.New(n, ethcfg) 228 if err != nil { 229 t.Fatal("can't create eth service:", err) 230 } 231 if err := n.Start(); err != nil { 232 t.Fatal("can't start node:", err) 233 } 234 if _, err := ethservice.BlockChain().InsertChain(blocks); err != nil { 235 n.Close() 236 t.Fatal("can't import test blocks:", err) 237 } 238 ethservice.SetEtherbase(testAddr) 239 240 return n, ethservice 241 }