mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 10:50:44 +00:00
try use insertblock in fetcher
This commit is contained in:
parent
da42d75839
commit
57b11ab4c9
3 changed files with 20 additions and 14 deletions
|
|
@ -1774,10 +1774,10 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
|
||||||
return 0, events, coalescedLogs, nil
|
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)
|
events, logs, err := bc.insertBlock(block)
|
||||||
bc.PostChainEvents(events, logs)
|
bc.PostChainEvents(events, logs)
|
||||||
return err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bc *BlockChain) PrepareBlock(block *types.Block) (err error) {
|
func (bc *BlockChain) PrepareBlock(block *types.Block) (err error) {
|
||||||
|
|
|
||||||
|
|
@ -93,6 +93,9 @@ type chainHeightFn func() uint64
|
||||||
// chainInsertFn is a callback type to insert a batch of blocks into the local chain.
|
// chainInsertFn is a callback type to insert a batch of blocks into the local chain.
|
||||||
type chainInsertFn func(types.Blocks) (int, error)
|
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
|
type blockPrepareFn func(block *types.Block) error
|
||||||
|
|
||||||
// peerDropFn is a callback type for dropping a peer detected as malicious.
|
// peerDropFn is a callback type for dropping a peer detected as malicious.
|
||||||
|
|
@ -166,7 +169,8 @@ type BlockFetcher struct {
|
||||||
handleProposedBlock proposeBlockHandlerFn // Consensus v2 specific: Hanle new proposed block
|
handleProposedBlock proposeBlockHandlerFn // Consensus v2 specific: Hanle new proposed block
|
||||||
broadcastBlock blockBroadcasterFn // Broadcasts a block to connected peers
|
broadcastBlock blockBroadcasterFn // Broadcasts a block to connected peers
|
||||||
chainHeight chainHeightFn // Retrieves the current chain's height
|
chainHeight chainHeightFn // Retrieves the current chain's height
|
||||||
insertChain chainInsertFn // Injects a batch of blocks into the chain
|
// insertChain chainInsertFn // Injects a batch of blocks into the chain
|
||||||
|
insertBlock blockInsertFn // Injects a batch of blocks into the chain
|
||||||
prepareBlock blockPrepareFn
|
prepareBlock blockPrepareFn
|
||||||
dropPeer peerDropFn // Drops a peer for misbehaving
|
dropPeer peerDropFn // Drops a peer for misbehaving
|
||||||
|
|
||||||
|
|
@ -211,7 +215,7 @@ type BlockFetcher struct {
|
||||||
// dropPeer: dropPeer,
|
// dropPeer: dropPeer,
|
||||||
// NewBlockFetcher creates a block fetcher to retrieve blocks based on hash announcements.
|
// 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)
|
knownBlocks, _ := lru.NewARC(blockLimit)
|
||||||
return &BlockFetcher{
|
return &BlockFetcher{
|
||||||
notify: make(chan *blockAnnounce),
|
notify: make(chan *blockAnnounce),
|
||||||
|
|
@ -234,7 +238,8 @@ func NewBlockFetcher(getBlock blockRetrievalFn, verifyHeader headerVerifierFn, h
|
||||||
handleProposedBlock: handleProposedBlock,
|
handleProposedBlock: handleProposedBlock,
|
||||||
broadcastBlock: broadcastBlock,
|
broadcastBlock: broadcastBlock,
|
||||||
chainHeight: chainHeight,
|
chainHeight: chainHeight,
|
||||||
insertChain: insertChain,
|
insertBlock: insertBlock,
|
||||||
|
// insertChain: insertChain,
|
||||||
prepareBlock: prepareBlock,
|
prepareBlock: prepareBlock,
|
||||||
dropPeer: dropPeer,
|
dropPeer: dropPeer,
|
||||||
}
|
}
|
||||||
|
|
@ -772,7 +777,7 @@ func (f *BlockFetcher) insert(peer string, block *types.Block) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Run the actual import and log any issues
|
// 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)
|
log.Warn("Propagated block import failed", "peer", peer, "number", block.Number(), "hash", hash, "err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -246,7 +246,7 @@ func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, ne
|
||||||
return blockchain.CurrentBlock().NumberU64()
|
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.
|
// 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
|
// 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)
|
// case when starting new networks, where the genesis might be ancient (0 unix)
|
||||||
// which would prevent full nodes from accepting it.
|
// which would prevent full nodes from accepting it.
|
||||||
if manager.blockchain.CurrentBlock().NumberU64() < manager.checkpointNumber {
|
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
|
return 0, nil
|
||||||
}
|
}
|
||||||
// If fast sync is running, deny importing weird blocks. This is a problematic
|
// 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
|
// 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.
|
// or not. This should be fixed if we figure out a solution.
|
||||||
if atomic.LoadUint32(&manager.fastSync) == 1 {
|
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
|
return 0, nil
|
||||||
}
|
}
|
||||||
n, err := manager.blockchain.InsertChain(blocks)
|
n, err := manager.blockchain.InsertBlock(&block)
|
||||||
|
// n, err := manager.blockchain.InsertChain(blocks)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
atomic.StoreUint32(&manager.acceptTxs, 1) // Mark initial sync done on any fetcher import
|
atomic.StoreUint32(&manager.acceptTxs, 1) // Mark initial sync done on any fetcher import
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue