try use insertblock in fetcher

This commit is contained in:
wanwiset25 2024-06-24 11:39:00 +04:00
parent da42d75839
commit 57b11ab4c9
3 changed files with 20 additions and 14 deletions

View file

@ -1774,10 +1774,10 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
return 0, events, coalescedLogs, nil
}
func (bc *BlockChain) InsertBlock(block *types.Block) error {
func (bc *BlockChain) InsertBlock(block *types.Block) (int, error) {
events, logs, err := bc.insertBlock(block)
bc.PostChainEvents(events, logs)
return err
return 0, err
}
func (bc *BlockChain) PrepareBlock(block *types.Block) (err error) {

View file

@ -93,6 +93,9 @@ type chainHeightFn func() uint64
// chainInsertFn is a callback type to insert a batch of blocks into the local chain.
type chainInsertFn func(types.Blocks) (int, error)
// blockInsertFn is a callback type to insert a batch of blocks into the local chain.
type blockInsertFn func(types.Block) (int, error)
type blockPrepareFn func(block *types.Block) error
// peerDropFn is a callback type for dropping a peer detected as malicious.
@ -166,9 +169,10 @@ type BlockFetcher struct {
handleProposedBlock proposeBlockHandlerFn // Consensus v2 specific: Hanle new proposed block
broadcastBlock blockBroadcasterFn // Broadcasts a block to connected peers
chainHeight chainHeightFn // Retrieves the current chain's height
insertChain chainInsertFn // Injects a batch of blocks into the chain
prepareBlock blockPrepareFn
dropPeer peerDropFn // Drops a peer for misbehaving
// insertChain chainInsertFn // Injects a batch of blocks into the chain
insertBlock blockInsertFn // Injects a batch of blocks into the chain
prepareBlock blockPrepareFn
dropPeer peerDropFn // Drops a peer for misbehaving
// Testing hooks
announceChangeHook func(common.Hash, bool) // Method to call upon adding or deleting a hash from the blockAnnounce list
@ -211,7 +215,7 @@ type BlockFetcher struct {
// dropPeer: dropPeer,
// NewBlockFetcher creates a block fetcher to retrieve blocks based on hash announcements.
func NewBlockFetcher(getBlock blockRetrievalFn, verifyHeader headerVerifierFn, handleProposedBlock proposeBlockHandlerFn, broadcastBlock blockBroadcasterFn, chainHeight chainHeightFn, insertChain chainInsertFn, prepareBlock blockPrepareFn, dropPeer peerDropFn) *BlockFetcher {
func NewBlockFetcher(getBlock blockRetrievalFn, verifyHeader headerVerifierFn, handleProposedBlock proposeBlockHandlerFn, broadcastBlock blockBroadcasterFn, chainHeight chainHeightFn, insertBlock blockInsertFn, prepareBlock blockPrepareFn, dropPeer peerDropFn) *BlockFetcher {
knownBlocks, _ := lru.NewARC(blockLimit)
return &BlockFetcher{
notify: make(chan *blockAnnounce),
@ -234,9 +238,10 @@ func NewBlockFetcher(getBlock blockRetrievalFn, verifyHeader headerVerifierFn, h
handleProposedBlock: handleProposedBlock,
broadcastBlock: broadcastBlock,
chainHeight: chainHeight,
insertChain: insertChain,
prepareBlock: prepareBlock,
dropPeer: dropPeer,
insertBlock: insertBlock,
// insertChain: insertChain,
prepareBlock: prepareBlock,
dropPeer: dropPeer,
}
}
@ -772,7 +777,7 @@ func (f *BlockFetcher) insert(peer string, block *types.Block) {
return
}
// Run the actual import and log any issues
if _, err := f.insertChain(types.Blocks{block}); err != nil {
if _, err := f.insertBlock(*block); err != nil {
log.Warn("Propagated block import failed", "peer", peer, "number", block.Number(), "hash", hash, "err", err)
return
}

View file

@ -246,7 +246,7 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
return blockchain.CurrentBlock().NumberU64()
}
inserter := func(blocks types.Blocks) (int, error) {
inserter := func(block types.Block) (int, error) {
// If sync hasn't reached the checkpoint yet, deny importing weird blocks.
//
// Ideally we would also compare the head block's timestamp and similarly reject
@ -254,7 +254,7 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
// case when starting new networks, where the genesis might be ancient (0 unix)
// which would prevent full nodes from accepting it.
if manager.blockchain.CurrentBlock().NumberU64() < manager.checkpointNumber {
log.Warn("Unsynced yet, discarded propagated block", "number", blocks[0].Number(), "hash", blocks[0].Hash())
log.Warn("Unsynced yet, discarded propagated block", "number", block.Number(), "hash", block.Hash())
return 0, nil
}
// If fast sync is running, deny importing weird blocks. This is a problematic
@ -263,10 +263,11 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
// out a way yet where nodes can decide unilaterally whether the network is new
// or not. This should be fixed if we figure out a solution.
if atomic.LoadUint32(&manager.fastSync) == 1 {
log.Warn("Fast syncing, discarded propagated block", "number", blocks[0].Number(), "hash", blocks[0].Hash())
log.Warn("Fast syncing, discarded propagated block", "number", block.Number(), "hash", block.Hash())
return 0, nil
}
n, err := manager.blockchain.InsertChain(blocks)
n, err := manager.blockchain.InsertBlock(&block)
// n, err := manager.blockchain.InsertChain(blocks)
if err == nil {
atomic.StoreUint32(&manager.acceptTxs, 1) // Mark initial sync done on any fetcher import
}