github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/les/helper_test.go (about) 1 // Copyright 2016 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 // This file contains some shares testing functionality, common to multiple 18 // different files and modules being tested. 19 20 package les 21 22 import ( 23 "crypto/rand" 24 "math/big" 25 "sync" 26 "testing" 27 "time" 28 29 "github.com/ethereum/go-ethereum/common" 30 "github.com/ethereum/go-ethereum/consensus/ethash" 31 "github.com/ethereum/go-ethereum/core" 32 "github.com/ethereum/go-ethereum/core/types" 33 "github.com/ethereum/go-ethereum/core/vm" 34 "github.com/ethereum/go-ethereum/crypto" 35 "github.com/ethereum/go-ethereum/eth" 36 "github.com/ethereum/go-ethereum/eth/downloader" 37 "github.com/ethereum/go-ethereum/ethdb" 38 "github.com/ethereum/go-ethereum/event" 39 "github.com/ethereum/go-ethereum/les/flowcontrol" 40 "github.com/ethereum/go-ethereum/light" 41 "github.com/ethereum/go-ethereum/p2p" 42 "github.com/ethereum/go-ethereum/p2p/enode" 43 "github.com/ethereum/go-ethereum/params" 44 ) 45 46 var ( 47 testBankKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 48 testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey) 49 testBankFunds = big.NewInt(1000000000000000000) 50 51 acc1Key, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") 52 acc2Key, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee") 53 acc1Addr = crypto.PubkeyToAddress(acc1Key.PublicKey) 54 acc2Addr = crypto.PubkeyToAddress(acc2Key.PublicKey) 55 56 testContractCode = common.Hex2Bytes("606060405260cc8060106000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806360cd2685146041578063c16431b914606b57603f565b005b6055600480803590602001909190505060a9565b6040518082815260200191505060405180910390f35b60886004808035906020019091908035906020019091905050608a565b005b80600060005083606481101560025790900160005b50819055505b5050565b6000600060005082606481101560025790900160005b5054905060c7565b91905056") 57 testContractAddr common.Address 58 testContractCodeDeployed = testContractCode[16:] 59 testContractDeployed = uint64(2) 60 61 testEventEmitterCode = common.Hex2Bytes("60606040523415600e57600080fd5b7f57050ab73f6b9ebdd9f76b8d4997793f48cf956e965ee070551b9ca0bb71584e60405160405180910390a160358060476000396000f3006060604052600080fd00a165627a7a723058203f727efcad8b5811f8cb1fc2620ce5e8c63570d697aef968172de296ea3994140029") 62 testEventEmitterAddr common.Address 63 64 testBufLimit = uint64(100) 65 ) 66 67 /* 68 contract test { 69 70 uint256[100] data; 71 72 function Put(uint256 addr, uint256 value) { 73 data[addr] = value; 74 } 75 76 function Get(uint256 addr) constant returns (uint256 value) { 77 return data[addr]; 78 } 79 } 80 */ 81 82 func testChainGen(i int, block *core.BlockGen) { 83 signer := types.HomesteadSigner{} 84 85 switch i { 86 case 0: 87 // In block 1, the test bank sends account #1 some ether. 88 tx, _ := types.SignTx(types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(10000), params.TxGas, nil, nil, nil, nil, nil), signer, testBankKey) 89 block.AddTx(tx) 90 case 1: 91 // In block 2, the test bank sends some more ether to account #1. 92 // acc1Addr passes it on to account #2. 93 // acc1Addr creates a test contract. 94 // acc1Addr creates a test event. 95 nonce := block.TxNonce(acc1Addr) 96 97 tx1, _ := types.SignTx(types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(1000), params.TxGas, nil, nil, nil, nil, nil), signer, testBankKey) 98 tx2, _ := types.SignTx(types.NewTransaction(nonce, acc2Addr, big.NewInt(1000), params.TxGas, nil, nil, nil, nil, nil), signer, acc1Key) 99 tx3, _ := types.SignTx(types.NewContractCreation(nonce+1, big.NewInt(0), 200000, big.NewInt(0), nil, nil, nil, testContractCode), signer, acc1Key) 100 testContractAddr = crypto.CreateAddress(acc1Addr, nonce+1) 101 tx4, _ := types.SignTx(types.NewContractCreation(nonce+2, big.NewInt(0), 200000, big.NewInt(0), nil, nil, nil, testEventEmitterCode), signer, acc1Key) 102 testEventEmitterAddr = crypto.CreateAddress(acc1Addr, nonce+2) 103 block.AddTx(tx1) 104 block.AddTx(tx2) 105 block.AddTx(tx3) 106 block.AddTx(tx4) 107 case 2: 108 // Block 3 is empty but was mined by account #2. 109 block.SetCoinbase(acc2Addr) 110 block.SetExtra([]byte("yeehaw")) 111 data := common.Hex2Bytes("C16431B900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001") 112 tx, _ := types.SignTx(types.NewTransaction(block.TxNonce(testBankAddress), testContractAddr, big.NewInt(0), 100000, nil, nil, nil, nil, data), signer, testBankKey) 113 block.AddTx(tx) 114 case 3: 115 // Block 4 includes blocks 2 and 3 as uncle headers (with modified extra data). 116 b2 := block.PrevBlock(1).Header() 117 b2.Extra = []byte("foo") 118 block.AddUncle(b2) 119 b3 := block.PrevBlock(2).Header() 120 b3.Extra = []byte("foo") 121 block.AddUncle(b3) 122 data := common.Hex2Bytes("C16431B900000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002") 123 tx, _ := types.SignTx(types.NewTransaction(block.TxNonce(testBankAddress), testContractAddr, big.NewInt(0), 100000, nil, nil, nil, nil, data), signer, testBankKey) 124 block.AddTx(tx) 125 } 126 } 127 128 // testIndexers creates a set of indexers with specified params for testing purpose. 129 func testIndexers(db ethdb.Database, odr light.OdrBackend, iConfig *light.IndexerConfig) (*core.ChainIndexer, *core.ChainIndexer, *core.ChainIndexer) { 130 chtIndexer := light.NewChtIndexer(db, odr, iConfig.ChtSize, iConfig.ChtConfirms, true) 131 bloomIndexer := eth.NewBloomIndexer(db, iConfig.BloomSize, iConfig.BloomConfirms, true) 132 bloomTrieIndexer := light.NewBloomTrieIndexer(db, odr, iConfig.BloomSize, iConfig.BloomTrieSize, true) 133 bloomIndexer.AddChildIndexer(bloomTrieIndexer) 134 return chtIndexer, bloomIndexer, bloomTrieIndexer 135 } 136 137 func testRCL() RequestCostList { 138 cl := make(RequestCostList, len(reqList)) 139 for i, code := range reqList { 140 cl[i].MsgCode = code 141 cl[i].BaseCost = 0 142 cl[i].ReqCost = 0 143 } 144 return cl 145 } 146 147 // newTestProtocolManager creates a new protocol manager for testing purposes, 148 // with the given number of blocks already known, potential notification 149 // channels for different events and relative chain indexers array. 150 func newTestProtocolManager(syncmode downloader.SyncMode, blocks int, generator func(int, *core.BlockGen), odr *LesOdr, peers *peerSet, db ethdb.Database) (*ProtocolManager, error) { 151 var ( 152 evmux = new(event.TypeMux) 153 engine = ethash.NewFaker() 154 gspec = core.Genesis{ 155 Config: params.TestChainConfig, 156 Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}}, 157 } 158 genesis = gspec.MustCommit(db) 159 chain BlockChain 160 ) 161 if peers == nil { 162 peers = newPeerSet() 163 } 164 lightSync := !syncmode.SyncFullBlockChain() 165 166 if lightSync { 167 chain, _ = light.NewLightChain(odr, gspec.Config, engine) 168 } else { 169 blockchain, _ := core.NewBlockChain(db, nil, gspec.Config, engine, vm.Config{}, nil) 170 gchain, _ := core.GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, blocks, generator) 171 if _, err := blockchain.InsertChain(gchain); err != nil { 172 panic(err) 173 } 174 chain = blockchain 175 } 176 177 indexConfig := light.TestServerIndexerConfig 178 if lightSync { 179 indexConfig = light.TestClientIndexerConfig 180 } 181 pm, err := NewProtocolManager(gspec.Config, indexConfig, syncmode, NetworkId, evmux, engine, peers, chain, nil, db, odr, nil, nil, make(chan struct{}), new(sync.WaitGroup), common.Address{}, nil) 182 if err != nil { 183 return nil, err 184 } 185 if !lightSync { 186 srv := &LesServer{lesCommons: lesCommons{protocolManager: pm}} 187 pm.server = srv 188 189 srv.defParams = &flowcontrol.ServerParams{ 190 BufLimit: testBufLimit, 191 MinRecharge: 1, 192 } 193 194 srv.fcManager = flowcontrol.NewClientManager(50, 10, 1000000000) 195 srv.fcCostStats = newCostStats(nil) 196 } 197 pm.Start(1000) 198 return pm, nil 199 } 200 201 // newTestProtocolManagerMust creates a new protocol manager for testing purposes, 202 // with the given number of blocks already known, potential notification 203 // channels for different events and relative chain indexers array. In case of an error, the constructor force- 204 // fails the test. 205 func newTestProtocolManagerMust(t *testing.T, syncmode downloader.SyncMode, blocks int, generator func(int, *core.BlockGen), odr *LesOdr, peers *peerSet, db ethdb.Database) *ProtocolManager { 206 pm, err := newTestProtocolManager(syncmode, blocks, generator, odr, peers, db) 207 if err != nil { 208 t.Fatalf("Failed to create protocol manager: %v", err) 209 } 210 return pm 211 } 212 213 // testPeer is a simulated peer to allow testing direct network calls. 214 type testPeer struct { 215 net p2p.MsgReadWriter // Network layer reader/writer to simulate remote messaging 216 app *p2p.MsgPipeRW // Application layer reader/writer to simulate the local side 217 *peer 218 } 219 220 // newTestPeer creates a new peer registered at the given protocol manager. 221 func newTestPeer(t *testing.T, name string, version int, pm *ProtocolManager, shake bool) (*testPeer, <-chan error) { 222 // Create a message pipe to communicate through 223 app, net := p2p.MsgPipe() 224 225 // Generate a random id and create the peer 226 var id enode.ID 227 rand.Read(id[:]) 228 229 peer := pm.newPeer(version, NetworkId, p2p.NewPeer(id, name, nil), net) 230 231 // Start the peer on a new thread 232 errc := make(chan error, 1) 233 go func() { 234 select { 235 case pm.newPeerCh <- peer: 236 errc <- pm.handle(peer) 237 case <-pm.quitSync: 238 errc <- p2p.DiscQuitting 239 } 240 }() 241 tp := &testPeer{ 242 app: app, 243 net: net, 244 peer: peer, 245 } 246 // Execute any implicitly requested handshakes and return 247 if shake { 248 var ( 249 genesis = pm.blockchain.Genesis() 250 head = pm.blockchain.CurrentHeader() 251 td = pm.blockchain.GetTd(head.Hash(), head.Number.Uint64()) 252 ) 253 tp.handshake(t, td, head.Hash(), head.Number.Uint64(), genesis.Hash()) 254 } 255 return tp, errc 256 } 257 258 func newTestPeerPair(name string, version int, pm, pm2 *ProtocolManager) (*peer, <-chan error, *peer, <-chan error) { 259 // Create a message pipe to communicate through 260 app, net := p2p.MsgPipe() 261 262 // Generate a random id and create the peer 263 var id enode.ID 264 rand.Read(id[:]) 265 266 peer := pm.newPeer(version, NetworkId, p2p.NewPeer(id, name, nil), net) 267 peer2 := pm2.newPeer(version, NetworkId, p2p.NewPeer(id, name, nil), app) 268 269 // Start the peer on a new thread 270 errc := make(chan error, 1) 271 errc2 := make(chan error, 1) 272 go func() { 273 select { 274 case pm.newPeerCh <- peer: 275 errc <- pm.handle(peer) 276 case <-pm.quitSync: 277 errc <- p2p.DiscQuitting 278 } 279 }() 280 go func() { 281 select { 282 case pm2.newPeerCh <- peer2: 283 errc2 <- pm2.handle(peer2) 284 case <-pm2.quitSync: 285 errc2 <- p2p.DiscQuitting 286 } 287 }() 288 return peer, errc, peer2, errc2 289 } 290 291 // handshake simulates a trivial handshake that expects the same state from the 292 // remote side as we are simulating locally. 293 func (p *testPeer) handshake(t *testing.T, td *big.Int, head common.Hash, headNum uint64, genesis common.Hash) { 294 var expList keyValueList 295 expList = expList.add("protocolVersion", uint64(p.version)) 296 expList = expList.add("networkId", uint64(NetworkId)) 297 expList = expList.add("headTd", td) 298 expList = expList.add("headHash", head) 299 expList = expList.add("headNum", headNum) 300 expList = expList.add("genesisHash", genesis) 301 sendList := make(keyValueList, len(expList)) 302 copy(sendList, expList) 303 expList = expList.add("serveHeaders", nil) 304 expList = expList.add("serveChainSince", uint64(0)) 305 expList = expList.add("serveStateSince", uint64(0)) 306 expList = expList.add("txRelay", nil) 307 expList = expList.add("flowControl/BL", testBufLimit) 308 expList = expList.add("flowControl/MRR", uint64(1)) 309 expList = expList.add("flowControl/MRC", testRCL()) 310 311 if err := p2p.ExpectMsg(p.app, StatusMsg, expList); err != nil { 312 t.Fatalf("status recv: %v", err) 313 } 314 if err := p2p.Send(p.app, StatusMsg, sendList); err != nil { 315 t.Fatalf("status send: %v", err) 316 } 317 318 p.fcServerParams = &flowcontrol.ServerParams{ 319 BufLimit: testBufLimit, 320 MinRecharge: 1, 321 } 322 } 323 324 // close terminates the local side of the peer, notifying the remote protocol 325 // manager of termination. 326 func (p *testPeer) close() { 327 p.app.Close() 328 } 329 330 // TestEntity represents a network entity for testing with necessary auxiliary fields. 331 type TestEntity struct { 332 db ethdb.Database 333 rPeer *peer 334 tPeer *testPeer 335 peers *peerSet 336 pm *ProtocolManager 337 // Indexers 338 chtIndexer *core.ChainIndexer 339 bloomIndexer *core.ChainIndexer 340 bloomTrieIndexer *core.ChainIndexer 341 } 342 343 // newServerEnv creates a server testing environment with a connected test peer for testing purpose. 344 func newServerEnv(t *testing.T, blocks int, protocol int, waitIndexers func(*core.ChainIndexer, *core.ChainIndexer, *core.ChainIndexer)) (*TestEntity, func()) { 345 db := ethdb.NewMemDatabase() 346 cIndexer, bIndexer, btIndexer := testIndexers(db, nil, light.TestServerIndexerConfig) 347 348 pm := newTestProtocolManagerMust(t, downloader.FullSync, blocks, testChainGen, nil, nil, db) 349 peer, _ := newTestPeer(t, "peer", protocol, pm, true) 350 351 cIndexer.Start(pm.blockchain.(*core.BlockChain)) 352 bIndexer.Start(pm.blockchain.(*core.BlockChain)) 353 354 // Wait until indexers generate enough index data. 355 if waitIndexers != nil { 356 waitIndexers(cIndexer, bIndexer, btIndexer) 357 } 358 359 return &TestEntity{ 360 db: db, 361 tPeer: peer, 362 pm: pm, 363 chtIndexer: cIndexer, 364 bloomIndexer: bIndexer, 365 bloomTrieIndexer: btIndexer, 366 }, func() { 367 peer.close() 368 // Note bloom trie indexer will be closed by it parent recursively. 369 cIndexer.Close() 370 bIndexer.Close() 371 } 372 } 373 374 // newClientServerEnv creates a client/server arch environment with a connected les server and light client pair 375 // for testing purpose. 376 func newClientServerEnv(t *testing.T, blocks int, protocol int, waitIndexers func(*core.ChainIndexer, *core.ChainIndexer, *core.ChainIndexer), newPeer bool) (*TestEntity, *TestEntity, func()) { 377 db, ldb := ethdb.NewMemDatabase(), ethdb.NewMemDatabase() 378 peers, lPeers := newPeerSet(), newPeerSet() 379 380 dist := newRequestDistributor(lPeers, make(chan struct{})) 381 rm := newRetrieveManager(lPeers, dist, nil) 382 odr := NewLesOdr(ldb, light.TestClientIndexerConfig, rm) 383 384 cIndexer, bIndexer, btIndexer := testIndexers(db, nil, light.TestServerIndexerConfig) 385 lcIndexer, lbIndexer, lbtIndexer := testIndexers(ldb, odr, light.TestClientIndexerConfig) 386 odr.SetIndexers(lcIndexer, lbtIndexer, lbIndexer) 387 388 pm := newTestProtocolManagerMust(t, downloader.FullSync, blocks, testChainGen, nil, peers, db) 389 lpm := newTestProtocolManagerMust(t, downloader.LightSync, 0, nil, odr, lPeers, ldb) 390 391 startIndexers := func(clientMode bool, pm *ProtocolManager) { 392 if clientMode { 393 lcIndexer.Start(pm.blockchain.(*light.LightChain)) 394 lbIndexer.Start(pm.blockchain.(*light.LightChain)) 395 } else { 396 cIndexer.Start(pm.blockchain.(*core.BlockChain)) 397 bIndexer.Start(pm.blockchain.(*core.BlockChain)) 398 } 399 } 400 401 startIndexers(false, pm) 402 startIndexers(true, lpm) 403 404 // Execute wait until function if it is specified. 405 if waitIndexers != nil { 406 waitIndexers(cIndexer, bIndexer, btIndexer) 407 } 408 409 var ( 410 peer, lPeer *peer 411 err1, err2 <-chan error 412 ) 413 if newPeer { 414 peer, err1, lPeer, err2 = newTestPeerPair("peer", protocol, pm, lpm) 415 select { 416 case <-time.After(time.Millisecond * 100): 417 case err := <-err1: 418 t.Fatalf("peer 1 handshake error: %v", err) 419 case err := <-err2: 420 t.Fatalf("peer 2 handshake error: %v", err) 421 } 422 } 423 424 return &TestEntity{ 425 db: db, 426 pm: pm, 427 rPeer: peer, 428 peers: peers, 429 chtIndexer: cIndexer, 430 bloomIndexer: bIndexer, 431 bloomTrieIndexer: btIndexer, 432 }, &TestEntity{ 433 db: ldb, 434 pm: lpm, 435 rPeer: lPeer, 436 peers: lPeers, 437 chtIndexer: lcIndexer, 438 bloomIndexer: lbIndexer, 439 bloomTrieIndexer: lbtIndexer, 440 }, func() { 441 // Note bloom trie indexers will be closed by their parents recursively. 442 cIndexer.Close() 443 bIndexer.Close() 444 lcIndexer.Close() 445 lbIndexer.Close() 446 } 447 }