core: Send tx events for all removed transactions, even if they are added back in the same reorg.

This commit is contained in:
Domino Valdano 2018-06-17 19:39:58 -07:00
parent 20eba82751
commit db99b68db8
No known key found for this signature in database
GPG key ID: 3FDFE30EE92AC05E

View file

@ -1294,8 +1294,6 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
return fmt.Errorf("Invalid new chain") return fmt.Errorf("Invalid new chain")
} }
blockLookup := make(map[common.Hash]*big.Int)
for { for {
if oldBlock.Hash() == newBlock.Hash() { if oldBlock.Hash() == newBlock.Hash() {
commonBlock = oldBlock commonBlock = oldBlock
@ -1306,10 +1304,6 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
newChain = append(newChain, newBlock) newChain = append(newChain, newBlock)
deletedTxs = append(deletedTxs, oldBlock.Transactions()...) deletedTxs = append(deletedTxs, oldBlock.Transactions()...)
collectLogs(oldBlock.Hash()) collectLogs(oldBlock.Hash())
// save block # of each deleted transaction; will need this to deduce signer for TransactionEvent
for _, tx := range oldBlock.Transactions() {
blockLookup[tx.Hash()] = oldBlock.Number()
}
oldBlock, newBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1), bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1) oldBlock, newBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1), bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1)
if oldBlock == nil { if oldBlock == nil {
@ -1339,23 +1333,28 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
rawdb.WriteTxLookupEntries(bc.db, newChain[i]) rawdb.WriteTxLookupEntries(bc.db, newChain[i])
addedTxs = append(addedTxs, newChain[i].Transactions()...) addedTxs = append(addedTxs, newChain[i].Transactions()...)
} }
for i := len(oldChain) - 1; i >= 0; i-- {
for _, tx := range oldChain[i].Transactions() {
// Construct TransactionEvent for subscribers of txPostFeed
from, _ := types.Sender(types.MakeSigner(bc.chainConfig, oldChain[i].Number()), tx)
data := types.ReturnData{TxHash: tx.Hash(), Removed: true}
txEvent := TransactionEvent{
TxHash: tx.Hash(),
From: &from,
To: tx.To(),
RetData: &data,
}
txPostEvents = append(txPostEvents, &txEvent)
}
}
// calculate the difference between deleted and added transactions // calculate the difference between deleted and added transactions
diff := types.TxDifference(deletedTxs, addedTxs) diff := types.TxDifference(deletedTxs, addedTxs)
for _, tx := range diff { for _, tx := range diff {
// When transactions get deleted from the database that means the // When transactions get deleted from the database that means the
// receipts that were created in the fork must also be deleted // receipts that were created in the fork must also be deleted
rawdb.DeleteTxLookupEntry(bc.db, tx.Hash()) rawdb.DeleteTxLookupEntry(bc.db, tx.Hash())
// Construct TransactionEvent for subscribers of txPostFeed
from, _ := types.Sender(types.MakeSigner(bc.chainConfig, blockLookup[tx.Hash()]), tx)
data := types.ReturnData{TxHash: tx.Hash(), Removed: true}
txEvent := TransactionEvent{TxHash: tx.Hash(),
From: &from,
To: tx.To(),
RetData: &data,
}
txPostEvents = append(txPostEvents, &txEvent)
} }
// Let subscribers know when a transaction is removed from canonical chain // Let subscribers know when a transaction is removed from canonical chain