fix: websocket subscribe for state sync logs

This commit is contained in:
Jaynti Kanani 2020-10-29 18:59:18 +05:30
parent 34e9724c0d
commit 0f0032e652
No known key found for this signature in database
GPG key ID: 4396982C976BAE5E
2 changed files with 37 additions and 1 deletions

View file

@ -1457,16 +1457,23 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
//
// block logs = receipt logs + state sync logs = `state.Logs()`
blockLogs := state.Logs()
var stateSyncLogs []*types.Log
if len(blockLogs) > 0 {
sort.SliceStable(blockLogs, func(i, j int) bool {
return blockLogs[i].Index < blockLogs[j].Index
})
if len(blockLogs) > len(logs) {
stateSyncLogs = blockLogs[len(logs):] // get state-sync logs from `state.Logs()`
// State sync logs don't have tx index, tx hash and other necessary fields
// DeriveFieldsForBorLogs will fill those fields for websocket subscriptions
types.DeriveFieldsForBorLogs(stateSyncLogs, block.Hash(), block.NumberU64(), uint(len(receipts)), uint(len(logs)))
// Write bor receipt
rawdb.WriteBorReceipt(blockBatch, block.Hash(), block.NumberU64(), &types.ReceiptForStorage{
Status: types.ReceiptStatusSuccessful, // make receipt status successful
Logs: blockLogs[len(logs):], // get state-sync logs from `state.Logs()`
Logs: stateSyncLogs,
})
// Write bor tx reverse lookup
@ -1577,6 +1584,12 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
if len(logs) > 0 {
bc.logsFeed.Send(logs)
}
// send state sync logs into logs feed
if len(stateSyncLogs) > 0 {
bc.logsFeed.Send(stateSyncLogs)
}
// In theory we should fire a ChainHeadEvent when we inject
// a canonical block, but sometimes we can insert a batch of
// canonicial blocks. Avoid firing too much ChainHeadEvents,
@ -2091,6 +2104,12 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
}
receipts := rawdb.ReadReceipts(bc.db, hash, *number, bc.chainConfig)
// Append bor receipt
borReceipt := rawdb.ReadBorReceipt(bc.db, hash, *number)
if borReceipt != nil {
receipts = append(receipts, borReceipt)
}
var logs []*types.Log
for _, receipt := range receipts {
for _, log := range receipt.Logs {

View file

@ -63,6 +63,23 @@ func DeriveFieldsForBorReceipt(receipt *Receipt, hash common.Hash, number uint64
return nil
}
// DeriveFieldsForBorLogs fills the receipts with their computed fields based on consensus
// data and contextual infos like containing block and transactions.
func DeriveFieldsForBorLogs(logs []*Log, hash common.Hash, number uint64, txIndex uint, logIndex uint) {
// get derived tx hash
txHash := GetDerivedBorTxHash(BorReceiptKey(number, hash))
// the derived log fields can simply be set from the block and transaction
for j := 0; j < len(logs); j++ {
logs[j].BlockNumber = number
logs[j].BlockHash = hash
logs[j].TxHash = txHash
logs[j].TxIndex = txIndex
logs[j].Index = logIndex
logIndex++
}
}
// MergeBorLogs merges receipt logs and block receipt logs
func MergeBorLogs(logs []*Log, borLogs []*Log) []*Log {
result := append(logs, borLogs...)