Skip to content

Commit fece1fa

Browse files
committed
Merge pull request bnb-chain#2024 from obscuren/exp-logs
core: added a new RemovedLogEvent
2 parents 6d3a924 + 9901a40 commit fece1fa

4 files changed

Lines changed: 68 additions & 4 deletions

File tree

‎core/blockchain.go‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,13 +1240,26 @@ func (self *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
12401240
oldStart = oldBlock
12411241
newStart = newBlock
12421242
deletedTxs types.Transactions
1243+
deletedLogs vm.Logs
1244+
// collectLogs collects the logs that were generated during the
1245+
// processing of the block that corresponds with the given hash.
1246+
// These logs are later announced as deleted.
1247+
collectLogs = func(h common.Hash) {
1248+
// Coalesce logs
1249+
receipts := GetBlockReceipts(self.chainDb, h)
1250+
for _, receipt := range receipts {
1251+
deletedLogs = append(deletedLogs, receipt.Logs...)
1252+
}
1253+
}
12431254
)
12441255

12451256
// first reduce whoever is higher bound
12461257
if oldBlock.NumberU64() > newBlock.NumberU64() {
12471258
// reduce old chain
12481259
for oldBlock = oldBlock; oldBlock != nil && oldBlock.NumberU64() != newBlock.NumberU64(); oldBlock = self.GetBlock(oldBlock.ParentHash()) {
12491260
deletedTxs = append(deletedTxs, oldBlock.Transactions()...)
1261+
1262+
collectLogs(oldBlock.Hash())
12501263
}
12511264
} else {
12521265
// reduce new chain and append new chain blocks for inserting later on
@@ -1269,6 +1282,7 @@ func (self *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
12691282
}
12701283
newChain = append(newChain, newBlock)
12711284
deletedTxs = append(deletedTxs, oldBlock.Transactions()...)
1285+
collectLogs(oldBlock.Hash())
12721286

12731287
oldBlock, newBlock = self.GetBlock(oldBlock.ParentHash()), self.GetBlock(newBlock.ParentHash())
12741288
if oldBlock == nil {
@@ -1302,7 +1316,6 @@ func (self *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
13021316
if err := WriteMipmapBloom(self.chainDb, block.NumberU64(), receipts); err != nil {
13031317
return err
13041318
}
1305-
13061319
addedTxs = append(addedTxs, block.Transactions()...)
13071320
}
13081321

@@ -1316,7 +1329,12 @@ func (self *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
13161329
}
13171330
// Must be posted in a goroutine because of the transaction pool trying
13181331
// to acquire the chain manager lock
1319-
go self.eventMux.Post(RemovedTransactionEvent{diff})
1332+
if len(diff) > 0 {
1333+
go self.eventMux.Post(RemovedTransactionEvent{diff})
1334+
}
1335+
if len(deletedLogs) > 0 {
1336+
go self.eventMux.Post(RemovedLogEvent{deletedLogs})
1337+
}
13201338

13211339
return nil
13221340
}

‎core/blockchain_test.go‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,3 +963,46 @@ func TestChainTxReorgs(t *testing.T) {
963963
}
964964
}
965965
}
966+
967+
func TestLogReorgs(t *testing.T) {
968+
params.MinGasLimit = big.NewInt(125000) // Minimum the gas limit may ever be.
969+
params.GenesisGasLimit = big.NewInt(3141592) // Gas limit of the Genesis block.
970+
971+
var (
972+
key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
973+
addr1 = crypto.PubkeyToAddress(key1.PublicKey)
974+
db, _ = ethdb.NewMemDatabase()
975+
// this code generates a log
976+
code = common.Hex2Bytes("60606040525b7f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405180905060405180910390a15b600a8060416000396000f360606040526008565b00")
977+
)
978+
genesis := WriteGenesisBlockForTesting(db,
979+
GenesisAccount{addr1, big.NewInt(10000000000000)},
980+
)
981+
982+
evmux := &event.TypeMux{}
983+
blockchain, _ := NewBlockChain(db, FakePow{}, evmux)
984+
985+
subs := evmux.Subscribe(RemovedLogEvent{})
986+
chain, _ := GenerateChain(genesis, db, 2, func(i int, gen *BlockGen) {
987+
if i == 1 {
988+
tx, err := types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), big.NewInt(1000000), new(big.Int), code).SignECDSA(key1)
989+
if err != nil {
990+
t.Fatalf("failed to create tx: %v", err)
991+
}
992+
gen.AddTx(tx)
993+
}
994+
})
995+
if _, err := blockchain.InsertChain(chain); err != nil {
996+
t.Fatalf("failed to insert chain: %v", err)
997+
}
998+
999+
chain, _ = GenerateChain(genesis, db, 3, func(i int, gen *BlockGen) {})
1000+
if _, err := blockchain.InsertChain(chain); err != nil {
1001+
t.Fatalf("failed to insert forked chain: %v", err)
1002+
}
1003+
1004+
ev := <-subs.Chan()
1005+
if len(ev.Data.(RemovedLogEvent).Logs) == 0 {
1006+
t.Error("expected logs")
1007+
}
1008+
}

‎core/chain_makers.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ func (b *BlockGen) AddTx(tx *types.Transaction) {
9090
if b.gasPool == nil {
9191
b.SetCoinbase(common.Address{})
9292
}
93+
b.statedb.StartRecord(tx.Hash(), common.Hash{}, len(b.txs))
9394
_, gas, err := ApplyMessage(NewEnv(b.statedb, nil, tx, b.header), tx, b.gasPool)
9495
if err != nil {
9596
panic(err)
9697
}
9798
root := b.statedb.IntermediateRoot()
9899
b.header.GasUsed.Add(b.header.GasUsed, gas)
99100
receipt := types.NewReceipt(root.Bytes(), b.header.GasUsed)
100-
logs := b.statedb.GetLogs(tx.Hash())
101-
receipt.Logs = logs
101+
receipt.Logs = b.statedb.GetLogs(tx.Hash())
102102
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
103103
b.txs = append(b.txs, tx)
104104
b.receipts = append(b.receipts, receipt)

‎core/events.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ type NewMinedBlockEvent struct{ Block *types.Block }
3939
// RemovedTransactionEvent is posted when a reorg happens
4040
type RemovedTransactionEvent struct{ Txs types.Transactions }
4141

42+
// RemovedLogEvent is posted when a reorg happens
43+
type RemovedLogEvent struct{ Logs vm.Logs }
44+
4245
// ChainSplit is posted when a new head is detected
4346
type ChainSplitEvent struct {
4447
Block *types.Block

0 commit comments

Comments
 (0)