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

Function TestChainTxReorgs

core/blockchain_test.go:458–573  ·  view source on GitHub ↗

Tests that chain reorganisations handle transaction removals and reinsertions.

(t *testing.T)

Source from the content-addressed store, hash-verified

456
457// Tests that chain reorganisations handle transaction removals and reinsertions.
458func TestChainTxReorgs(t *testing.T) {
459 t.Skip("=== Diff TestChainTxReorgs invalid memory address or nil pointer dereference")
460 var (
461 key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
462 key2, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
463 key3, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
464 addr1 = crypto.PubkeyToAddress(key1.PublicKey)
465 addr2 = crypto.PubkeyToAddress(key2.PublicKey)
466 addr3 = crypto.PubkeyToAddress(key3.PublicKey)
467 db = database.NewMemDatabase()
468 remoteDB = database.NewIpfsDbWithAdapter(database.NewFakeIpfsAdapter())
469 gspec = &Genesis{
470 Config: configs.TestChainConfig,
471 GasLimit: 3141592,
472 Alloc: GenesisAlloc{
473 addr1: {Balance: big.NewInt(1000000)},
474 addr2: {Balance: big.NewInt(1000000)},
475 addr3: {Balance: big.NewInt(1000000)},
476 },
477 }
478 genesis = gspec.MustCommit(db)
479 signer = types.NewCep1Signer(gspec.Config.ChainID)
480 )
481
482 // Create two transactions shared between the chains:
483 // - postponed: transaction included at a later block in the forked chain
484 // - swapped: transaction included at the same block number in the forked chain
485 postponed, _ := types.SignTx(types.NewTransaction(0, addr1, big.NewInt(1000), configs.TxGas, nil, nil), signer, key1)
486 swapped, _ := types.SignTx(types.NewTransaction(1, addr1, big.NewInt(1000), configs.TxGas, nil, nil), signer, key1)
487
488 // Create two transactions that will be dropped by the forked chain:
489 // - pastDrop: transaction dropped retroactively from a past block
490 // - freshDrop: transaction dropped exactly at the block where the reorg is detected
491 var pastDrop, freshDrop *types.Transaction
492
493 // Create three transactions that will be added in the forked chain:
494 // - pastAdd: transaction added before the reorganization is detected
495 // - freshAdd: transaction added at the exact block the reorg is detected
496 // - futureAdd: transaction added after the reorg has already finished
497 var pastAdd, freshAdd, futureAdd *types.Transaction
498
499 chain, _ := GenerateChain(gspec.Config, genesis, fakeDpor(db), db, remoteDB, 3, func(i int, gen *BlockGen) {
500 switch i {
501 case 0:
502 pastDrop, _ = types.SignTx(types.NewTransaction(gen.TxNonce(addr2), addr2, big.NewInt(1000), configs.TxGas, nil, nil), signer, key2)
503
504 gen.AddTx(pastDrop) // This transaction will be dropped in the fork from below the split point
505 gen.AddTx(postponed) // This transaction will be postponed till block #3 in the fork
506
507 case 2:
508 freshDrop, _ = types.SignTx(types.NewTransaction(gen.TxNonce(addr2), addr2, big.NewInt(1000), configs.TxGas, nil, nil), signer, key2)
509
510 gen.AddTx(freshDrop) // This transaction will be dropped in the fork from exactly at the split point
511 gen.AddTx(swapped) // This transaction will be swapped out at the exact height
512
513 gen.OffsetTime(9) // Lower the block difficulty to simulate a weaker chain
514 }
515 })

Callers

nothing calls this directly

Calls 11

InsertChainMethod · 0.95
StopMethod · 0.95
GenerateChainFunction · 0.85
fakeDporFunction · 0.85
NewBlockChainFunction · 0.85
MustCommitMethod · 0.80
TxNonceMethod · 0.80
AddTxMethod · 0.80
OffsetTimeMethod · 0.80
SignTxMethod · 0.65
HashMethod · 0.65

Tested by

no test coverage detected