From 0f0032e652a41a811325648dd60108f753656d2a Mon Sep 17 00:00:00 2001 From: Jaynti Kanani Date: Thu, 29 Oct 2020 18:59:18 +0530 Subject: [PATCH] fix: websocket subscribe for state sync logs --- core/blockchain.go | 21 ++++++++++++++++++++- core/types/bor_receipt.go | 17 +++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/core/blockchain.go b/core/blockchain.go index b77978042b..8b048d26a8 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -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 { diff --git a/core/types/bor_receipt.go b/core/types/bor_receipt.go index e00739f094..821bfbe268 100644 --- a/core/types/bor_receipt.go +++ b/core/types/bor_receipt.go @@ -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...)