MCPcopy Create free account
hub / github.com/CPChain/chain / testGetNodeData

Function testGetNodeData

protocols/cpc/handler_test.go:294–373  ·  view source on GitHub ↗
(t *testing.T, protocol int)

Source from the content-addressed store, hash-verified

292func TestGetNodeData64(t *testing.T) { testGetNodeData(t, 64) }
293
294func testGetNodeData(t *testing.T, protocol int) {
295
296 // Define three accounts to simulate transactions with
297 acc1Key, _ := crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
298 acc2Key, _ := crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
299 acc1Addr := crypto.PubkeyToAddress(acc1Key.PublicKey)
300 acc2Addr := crypto.PubkeyToAddress(acc2Key.PublicKey)
301
302 signer := types.HomesteadSigner{}
303 // Create a chain generator with some simple transactions (blatantly stolen from @fjl/chain_markets_test)
304 generator := func(i int, block *core.BlockGen) {
305 switch i {
306 case 0:
307 // In block 1, the test bank sends account #1 some ether.
308 tx, _ := types.SignTx(types.NewTransaction(block.TxNonce(testBank), acc1Addr, big.NewInt(10000), configs.TxGas, nil, nil), signer, testBankKey)
309 block.AddTx(tx)
310 case 1:
311 // In block 2, the test bank sends some more ether to account #1.
312 // acc1Addr passes it on to account #2.
313 tx1, _ := types.SignTx(types.NewTransaction(block.TxNonce(testBank), acc1Addr, big.NewInt(1000), configs.TxGas, nil, nil), signer, testBankKey)
314 tx2, _ := types.SignTx(types.NewTransaction(block.TxNonce(acc1Addr), acc2Addr, big.NewInt(1000), configs.TxGas, nil, nil), signer, acc1Key)
315 block.AddTx(tx1)
316 block.AddTx(tx2)
317 case 2:
318 // Block 3 is empty but was mined by account #2.
319 block.SetCoinbase(acc2Addr)
320 }
321 }
322 // Assemble the test environment
323 pm, db := newTestProtocolManagerMust(t, 4, generator, nil)
324 peer, _ := newTestPeer("peer", protocol, pm, true)
325 defer peer.close()
326
327 // Fetch for now the entire chain db
328 hashes := []common.Hash{}
329 for _, key := range db.Keys() {
330 if len(key) == len(common.Hash{}) {
331 hashes = append(hashes, common.BytesToHash(key))
332 }
333 }
334 p2p.Send(peer.app, 0x0d, hashes)
335 msg, err := peer.app.ReadMsg()
336 if err != nil {
337 t.Fatalf("failed to read node data response: %v", err)
338 }
339 if msg.Code != 0x0e {
340 t.Fatalf("response packet code mismatch: have %x, want %x", msg.Code, 0x0c)
341 }
342 var data [][]byte
343 if err := msg.Decode(&data); err != nil {
344 t.Fatalf("failed to decode response node data: %v", err)
345 }
346 // Verify that all hashes correspond to the requested data, and reconstruct a state tree
347 for i, want := range hashes {
348 if hash := crypto.Keccak256Hash(data[i]); hash != want {
349 t.Errorf("data hash mismatch: have %x, want %x", hash, want)
350 }
351 }

Callers 1

TestGetNodeData64Function · 0.85

Calls 15

PutMethod · 0.95
newTestPeerFunction · 0.85
TxNonceMethod · 0.80
AddTxMethod · 0.80
NumberU64Method · 0.80
StateRootMethod · 0.80
CmpMethod · 0.80
SignTxMethod · 0.65
BytesMethod · 0.65
CurrentBlockMethod · 0.65
GetBlockByNumberMethod · 0.65

Tested by

no test coverage detected