Skip to content

Commit 690bd8a

Browse files
rjl493456442karalabe
authored andcommitted
core: re-omit new log event when logs rebirth
1 parent d5cae48 commit 690bd8a

2 files changed

Lines changed: 224 additions & 9 deletions

File tree

‎core/blockchain.go‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,21 +1475,25 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
14751475
commonBlock *types.Block
14761476
deletedTxs types.Transactions
14771477
deletedLogs []*types.Log
1478+
rebirthLogs []*types.Log
14781479
// collectLogs collects the logs that were generated during the
14791480
// processing of the block that corresponds with the given hash.
1480-
// These logs are later announced as deleted.
1481-
collectLogs = func(hash common.Hash) {
1482-
// Coalesce logs and set 'Removed'.
1481+
// These logs are later announced as deleted or reborn
1482+
collectLogs = func(hash common.Hash, removed bool) {
14831483
number := bc.hc.GetBlockNumber(hash)
14841484
if number == nil {
14851485
return
14861486
}
14871487
receipts := rawdb.ReadReceipts(bc.db, hash, *number)
14881488
for _, receipt := range receipts {
14891489
for _, log := range receipt.Logs {
1490-
del := *log
1491-
del.Removed = true
1492-
deletedLogs = append(deletedLogs, &del)
1490+
l := *log
1491+
if removed {
1492+
l.Removed = true
1493+
deletedLogs = append(deletedLogs, &l)
1494+
} else {
1495+
rebirthLogs = append(rebirthLogs, &l)
1496+
}
14931497
}
14941498
}
14951499
}
@@ -1502,7 +1506,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
15021506
oldChain = append(oldChain, oldBlock)
15031507
deletedTxs = append(deletedTxs, oldBlock.Transactions()...)
15041508

1505-
collectLogs(oldBlock.Hash())
1509+
collectLogs(oldBlock.Hash(), true)
15061510
}
15071511
} else {
15081512
// reduce new chain and append new chain blocks for inserting later on
@@ -1526,7 +1530,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
15261530
oldChain = append(oldChain, oldBlock)
15271531
newChain = append(newChain, newBlock)
15281532
deletedTxs = append(deletedTxs, oldBlock.Transactions()...)
1529-
collectLogs(oldBlock.Hash())
1533+
collectLogs(oldBlock.Hash(), true)
15301534

15311535
oldBlock, newBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1), bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1)
15321536
if oldBlock == nil {
@@ -1552,6 +1556,10 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
15521556
for i := len(newChain) - 1; i >= 0; i-- {
15531557
// insert the block in the canonical way, re-writing history
15541558
bc.insert(newChain[i])
1559+
// collect reborn logs due to chain reorg(except head block)
1560+
if i != 0 {
1561+
collectLogs(newChain[i].Hash(), false)
1562+
}
15551563
// write lookup entries for hash based transaction/receipt searches
15561564
rawdb.WriteTxLookupEntries(bc.db, newChain[i])
15571565
addedTxs = append(addedTxs, newChain[i].Transactions()...)
@@ -1569,6 +1577,9 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
15691577
if len(deletedLogs) > 0 {
15701578
go bc.rmLogsFeed.Send(RemovedLogsEvent{deletedLogs})
15711579
}
1580+
if len(rebirthLogs) > 0 {
1581+
go bc.logsFeed.Send(rebirthLogs)
1582+
}
15721583
if len(oldChain) > 0 {
15731584
go func() {
15741585
for _, block := range oldChain {

‎core/blockchain_test.go‎

Lines changed: 205 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,6 @@ func TestChainTxReorgs(t *testing.T) {
867867
}
868868

869869
func TestLogReorgs(t *testing.T) {
870-
871870
var (
872871
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
873872
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
@@ -913,6 +912,211 @@ func TestLogReorgs(t *testing.T) {
913912
}
914913
}
915914

915+
func TestLogRebirth(t *testing.T) {
916+
var (
917+
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
918+
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
919+
db = ethdb.NewMemDatabase()
920+
// this code generates a log
921+
code = common.Hex2Bytes("60606040525b7f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405180905060405180910390a15b600a8060416000396000f360606040526008565b00")
922+
gspec = &Genesis{Config: params.TestChainConfig, Alloc: GenesisAlloc{addr1: {Balance: big.NewInt(10000000000000)}}}
923+
genesis = gspec.MustCommit(db)
924+
signer = types.NewEIP155Signer(gspec.Config.ChainID)
925+
newLogCh = make(chan bool)
926+
)
927+
928+
// listenNewLog checks whether the received logs number is equal with expected.
929+
listenNewLog := func(sink chan []*types.Log, expect int) {
930+
cnt := 0
931+
for {
932+
select {
933+
case logs := <-sink:
934+
cnt += len(logs)
935+
case <-time.NewTimer(5 * time.Second).C:
936+
// new logs timeout
937+
newLogCh <- false
938+
return
939+
}
940+
if cnt == expect {
941+
break
942+
} else if cnt > expect {
943+
// redundant logs received
944+
newLogCh <- false
945+
return
946+
}
947+
}
948+
select {
949+
case <-sink:
950+
// redundant logs received
951+
newLogCh <- false
952+
case <-time.NewTimer(100 * time.Millisecond).C:
953+
newLogCh <- true
954+
}
955+
}
956+
957+
blockchain, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil)
958+
defer blockchain.Stop()
959+
960+
logsCh := make(chan []*types.Log)
961+
blockchain.SubscribeLogsEvent(logsCh)
962+
963+
rmLogsCh := make(chan RemovedLogsEvent)
964+
blockchain.SubscribeRemovedLogsEvent(rmLogsCh)
965+
966+
chain, _ := GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 2, func(i int, gen *BlockGen) {
967+
if i == 1 {
968+
tx, err := types.SignTx(types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), 1000000, new(big.Int), code), signer, key1)
969+
if err != nil {
970+
t.Fatalf("failed to create tx: %v", err)
971+
}
972+
gen.AddTx(tx)
973+
}
974+
})
975+
976+
// Spawn a goroutine to receive log events
977+
go listenNewLog(logsCh, 1)
978+
if _, err := blockchain.InsertChain(chain); err != nil {
979+
t.Fatalf("failed to insert chain: %v", err)
980+
}
981+
if !<-newLogCh {
982+
t.Fatalf("failed to receive new log event")
983+
}
984+
985+
// Generate long reorg chain
986+
forkChain, _ := GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 2, func(i int, gen *BlockGen) {
987+
if i == 1 {
988+
tx, err := types.SignTx(types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), 1000000, new(big.Int), code), signer, key1)
989+
if err != nil {
990+
t.Fatalf("failed to create tx: %v", err)
991+
}
992+
gen.AddTx(tx)
993+
// Higher block difficulty
994+
gen.OffsetTime(-9)
995+
}
996+
})
997+
998+
// Spawn a goroutine to receive log events
999+
go listenNewLog(logsCh, 1)
1000+
if _, err := blockchain.InsertChain(forkChain); err != nil {
1001+
t.Fatalf("failed to insert forked chain: %v", err)
1002+
}
1003+
if !<-newLogCh {
1004+
t.Fatalf("failed to receive new log event")
1005+
}
1006+
// Ensure removedLog events received
1007+
select {
1008+
case ev := <-rmLogsCh:
1009+
if len(ev.Logs) == 0 {
1010+
t.Error("expected logs")
1011+
}
1012+
case <-time.NewTimer(1 * time.Second).C:
1013+
t.Fatal("Timeout. There is no RemovedLogsEvent has been sent.")
1014+
}
1015+
1016+
newBlocks, _ := GenerateChain(params.TestChainConfig, chain[len(chain)-1], ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
1017+
go listenNewLog(logsCh, 1)
1018+
if _, err := blockchain.InsertChain(newBlocks); err != nil {
1019+
t.Fatalf("failed to insert forked chain: %v", err)
1020+
}
1021+
// Rebirth logs should omit a newLogEvent
1022+
if !<-newLogCh {
1023+
t.Fatalf("failed to receive new log event")
1024+
}
1025+
// Ensure removedLog events received
1026+
select {
1027+
case ev := <-rmLogsCh:
1028+
if len(ev.Logs) == 0 {
1029+
t.Error("expected logs")
1030+
}
1031+
case <-time.NewTimer(1 * time.Second).C:
1032+
t.Fatal("Timeout. There is no RemovedLogsEvent has been sent.")
1033+
}
1034+
}
1035+
1036+
func TestSideLogRebirth(t *testing.T) {
1037+
var (
1038+
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
1039+
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
1040+
db = ethdb.NewMemDatabase()
1041+
// this code generates a log
1042+
code = common.Hex2Bytes("60606040525b7f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405180905060405180910390a15b600a8060416000396000f360606040526008565b00")
1043+
gspec = &Genesis{Config: params.TestChainConfig, Alloc: GenesisAlloc{addr1: {Balance: big.NewInt(10000000000000)}}}
1044+
genesis = gspec.MustCommit(db)
1045+
signer = types.NewEIP155Signer(gspec.Config.ChainID)
1046+
newLogCh = make(chan bool)
1047+
)
1048+
1049+
// listenNewLog checks whether the received logs number is equal with expected.
1050+
listenNewLog := func(sink chan []*types.Log, expect int) {
1051+
cnt := 0
1052+
for {
1053+
select {
1054+
case logs := <-sink:
1055+
cnt += len(logs)
1056+
case <-time.NewTimer(5 * time.Second).C:
1057+
// new logs timeout
1058+
newLogCh <- false
1059+
return
1060+
}
1061+
if cnt == expect {
1062+
break
1063+
} else if cnt > expect {
1064+
// redundant logs received
1065+
newLogCh <- false
1066+
return
1067+
}
1068+
}
1069+
select {
1070+
case <-sink:
1071+
// redundant logs received
1072+
newLogCh <- false
1073+
case <-time.NewTimer(100 * time.Millisecond).C:
1074+
newLogCh <- true
1075+
}
1076+
}
1077+
1078+
blockchain, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil)
1079+
defer blockchain.Stop()
1080+
1081+
logsCh := make(chan []*types.Log)
1082+
blockchain.SubscribeLogsEvent(logsCh)
1083+
1084+
chain, _ := GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 2, func(i int, gen *BlockGen) {
1085+
if i == 1 {
1086+
// Higher block difficulty
1087+
gen.OffsetTime(-9)
1088+
}
1089+
})
1090+
if _, err := blockchain.InsertChain(chain); err != nil {
1091+
t.Fatalf("failed to insert forked chain: %v", err)
1092+
}
1093+
1094+
// Generate side chain with lower difficulty
1095+
sideChain, _ := GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, 2, func(i int, gen *BlockGen) {
1096+
if i == 1 {
1097+
tx, err := types.SignTx(types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), 1000000, new(big.Int), code), signer, key1)
1098+
if err != nil {
1099+
t.Fatalf("failed to create tx: %v", err)
1100+
}
1101+
gen.AddTx(tx)
1102+
}
1103+
})
1104+
if _, err := blockchain.InsertChain(sideChain); err != nil {
1105+
t.Fatalf("failed to insert forked chain: %v", err)
1106+
}
1107+
1108+
// Generate a new block based on side chain
1109+
newBlocks, _ := GenerateChain(params.TestChainConfig, sideChain[len(sideChain)-1], ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
1110+
go listenNewLog(logsCh, 1)
1111+
if _, err := blockchain.InsertChain(newBlocks); err != nil {
1112+
t.Fatalf("failed to insert forked chain: %v", err)
1113+
}
1114+
// Rebirth logs should omit a newLogEvent
1115+
if !<-newLogCh {
1116+
t.Fatalf("failed to receive new log event")
1117+
}
1118+
}
1119+
9161120
func TestReorgSideEvent(t *testing.T) {
9171121
var (
9181122
db = rawdb.NewMemoryDatabase()

0 commit comments

Comments
 (0)