|
@@ -14,7 +14,7 @@
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
-// Package fetcher contains the block announcement based synchronisation.
|
|
|
|
|
|
|
+// Package fetcher contains the announcement based blocks or transaction synchronisation.
|
|
|
package fetcher
|
|
package fetcher
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
@@ -30,13 +30,16 @@ import (
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
const (
|
|
|
- arriveTimeout = 500 * time.Millisecond // Time allowance before an announced block is explicitly requested
|
|
|
|
|
|
|
+ arriveTimeout = 500 * time.Millisecond // Time allowance before an announced block/transaction is explicitly requested
|
|
|
gatherSlack = 100 * time.Millisecond // Interval used to collate almost-expired announces with fetches
|
|
gatherSlack = 100 * time.Millisecond // Interval used to collate almost-expired announces with fetches
|
|
|
- fetchTimeout = 5 * time.Second // Maximum allotted time to return an explicitly requested block
|
|
|
|
|
- maxUncleDist = 7 // Maximum allowed backward distance from the chain head
|
|
|
|
|
- maxQueueDist = 32 // Maximum allowed distance from the chain head to queue
|
|
|
|
|
- hashLimit = 256 // Maximum number of unique blocks a peer may have announced
|
|
|
|
|
- blockLimit = 64 // Maximum number of unique blocks a peer may have delivered
|
|
|
|
|
|
|
+ fetchTimeout = 5 * time.Second // Maximum allotted time to return an explicitly requested block/transaction
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+const (
|
|
|
|
|
+ maxUncleDist = 7 // Maximum allowed backward distance from the chain head
|
|
|
|
|
+ maxQueueDist = 32 // Maximum allowed distance from the chain head to queue
|
|
|
|
|
+ hashLimit = 256 // Maximum number of unique blocks a peer may have announced
|
|
|
|
|
+ blockLimit = 64 // Maximum number of unique blocks a peer may have delivered
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
var (
|
|
@@ -67,9 +70,9 @@ type chainInsertFn func(types.Blocks) (int, 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.
|
|
|
type peerDropFn func(id string)
|
|
type peerDropFn func(id string)
|
|
|
|
|
|
|
|
-// announce is the hash notification of the availability of a new block in the
|
|
|
|
|
|
|
+// blockAnnounce is the hash notification of the availability of a new block in the
|
|
|
// network.
|
|
// network.
|
|
|
-type announce struct {
|
|
|
|
|
|
|
+type blockAnnounce struct {
|
|
|
hash common.Hash // Hash of the block being announced
|
|
hash common.Hash // Hash of the block being announced
|
|
|
number uint64 // Number of the block being announced (0 = unknown | old protocol)
|
|
number uint64 // Number of the block being announced (0 = unknown | old protocol)
|
|
|
header *types.Header // Header of the block partially reassembled (new protocol)
|
|
header *types.Header // Header of the block partially reassembled (new protocol)
|
|
@@ -97,18 +100,18 @@ type bodyFilterTask struct {
|
|
|
time time.Time // Arrival time of the blocks' contents
|
|
time time.Time // Arrival time of the blocks' contents
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// inject represents a schedules import operation.
|
|
|
|
|
-type inject struct {
|
|
|
|
|
|
|
+// blockInject represents a schedules import operation.
|
|
|
|
|
+type blockInject struct {
|
|
|
origin string
|
|
origin string
|
|
|
block *types.Block
|
|
block *types.Block
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Fetcher is responsible for accumulating block announcements from various peers
|
|
|
|
|
|
|
+// BlockFetcher is responsible for accumulating block announcements from various peers
|
|
|
// and scheduling them for retrieval.
|
|
// and scheduling them for retrieval.
|
|
|
-type Fetcher struct {
|
|
|
|
|
|
|
+type BlockFetcher struct {
|
|
|
// Various event channels
|
|
// Various event channels
|
|
|
- notify chan *announce
|
|
|
|
|
- inject chan *inject
|
|
|
|
|
|
|
+ notify chan *blockAnnounce
|
|
|
|
|
+ inject chan *blockInject
|
|
|
|
|
|
|
|
headerFilter chan chan *headerFilterTask
|
|
headerFilter chan chan *headerFilterTask
|
|
|
bodyFilter chan chan *bodyFilterTask
|
|
bodyFilter chan chan *bodyFilterTask
|
|
@@ -117,16 +120,16 @@ type Fetcher struct {
|
|
|
quit chan struct{}
|
|
quit chan struct{}
|
|
|
|
|
|
|
|
// Announce states
|
|
// Announce states
|
|
|
- announces map[string]int // Per peer announce counts to prevent memory exhaustion
|
|
|
|
|
- announced map[common.Hash][]*announce // Announced blocks, scheduled for fetching
|
|
|
|
|
- fetching map[common.Hash]*announce // Announced blocks, currently fetching
|
|
|
|
|
- fetched map[common.Hash][]*announce // Blocks with headers fetched, scheduled for body retrieval
|
|
|
|
|
- completing map[common.Hash]*announce // Blocks with headers, currently body-completing
|
|
|
|
|
|
|
+ announces map[string]int // Per peer blockAnnounce counts to prevent memory exhaustion
|
|
|
|
|
+ announced map[common.Hash][]*blockAnnounce // Announced blocks, scheduled for fetching
|
|
|
|
|
+ fetching map[common.Hash]*blockAnnounce // Announced blocks, currently fetching
|
|
|
|
|
+ fetched map[common.Hash][]*blockAnnounce // Blocks with headers fetched, scheduled for body retrieval
|
|
|
|
|
+ completing map[common.Hash]*blockAnnounce // Blocks with headers, currently body-completing
|
|
|
|
|
|
|
|
// Block cache
|
|
// Block cache
|
|
|
- queue *prque.Prque // Queue containing the import operations (block number sorted)
|
|
|
|
|
- queues map[string]int // Per peer block counts to prevent memory exhaustion
|
|
|
|
|
- queued map[common.Hash]*inject // Set of already queued blocks (to dedupe imports)
|
|
|
|
|
|
|
+ queue *prque.Prque // Queue containing the import operations (block number sorted)
|
|
|
|
|
+ queues map[string]int // Per peer block counts to prevent memory exhaustion
|
|
|
|
|
+ queued map[common.Hash]*blockInject // Set of already queued blocks (to dedupe imports)
|
|
|
|
|
|
|
|
// Callbacks
|
|
// Callbacks
|
|
|
getBlock blockRetrievalFn // Retrieves a block from the local chain
|
|
getBlock blockRetrievalFn // Retrieves a block from the local chain
|
|
@@ -137,30 +140,30 @@ type Fetcher struct {
|
|
|
dropPeer peerDropFn // Drops a peer for misbehaving
|
|
dropPeer peerDropFn // Drops a peer for misbehaving
|
|
|
|
|
|
|
|
// Testing hooks
|
|
// Testing hooks
|
|
|
- announceChangeHook func(common.Hash, bool) // Method to call upon adding or deleting a hash from the announce list
|
|
|
|
|
|
|
+ announceChangeHook func(common.Hash, bool) // Method to call upon adding or deleting a hash from the blockAnnounce list
|
|
|
queueChangeHook func(common.Hash, bool) // Method to call upon adding or deleting a block from the import queue
|
|
queueChangeHook func(common.Hash, bool) // Method to call upon adding or deleting a block from the import queue
|
|
|
fetchingHook func([]common.Hash) // Method to call upon starting a block (eth/61) or header (eth/62) fetch
|
|
fetchingHook func([]common.Hash) // Method to call upon starting a block (eth/61) or header (eth/62) fetch
|
|
|
completingHook func([]common.Hash) // Method to call upon starting a block body fetch (eth/62)
|
|
completingHook func([]common.Hash) // Method to call upon starting a block body fetch (eth/62)
|
|
|
importedHook func(*types.Block) // Method to call upon successful block import (both eth/61 and eth/62)
|
|
importedHook func(*types.Block) // Method to call upon successful block import (both eth/61 and eth/62)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// New creates a block fetcher to retrieve blocks based on hash announcements.
|
|
|
|
|
-func New(getBlock blockRetrievalFn, verifyHeader headerVerifierFn, broadcastBlock blockBroadcasterFn, chainHeight chainHeightFn, insertChain chainInsertFn, dropPeer peerDropFn) *Fetcher {
|
|
|
|
|
- return &Fetcher{
|
|
|
|
|
- notify: make(chan *announce),
|
|
|
|
|
- inject: make(chan *inject),
|
|
|
|
|
|
|
+// NewBlockFetcher creates a block fetcher to retrieve blocks based on hash announcements.
|
|
|
|
|
+func NewBlockFetcher(getBlock blockRetrievalFn, verifyHeader headerVerifierFn, broadcastBlock blockBroadcasterFn, chainHeight chainHeightFn, insertChain chainInsertFn, dropPeer peerDropFn) *BlockFetcher {
|
|
|
|
|
+ return &BlockFetcher{
|
|
|
|
|
+ notify: make(chan *blockAnnounce),
|
|
|
|
|
+ inject: make(chan *blockInject),
|
|
|
headerFilter: make(chan chan *headerFilterTask),
|
|
headerFilter: make(chan chan *headerFilterTask),
|
|
|
bodyFilter: make(chan chan *bodyFilterTask),
|
|
bodyFilter: make(chan chan *bodyFilterTask),
|
|
|
done: make(chan common.Hash),
|
|
done: make(chan common.Hash),
|
|
|
quit: make(chan struct{}),
|
|
quit: make(chan struct{}),
|
|
|
announces: make(map[string]int),
|
|
announces: make(map[string]int),
|
|
|
- announced: make(map[common.Hash][]*announce),
|
|
|
|
|
- fetching: make(map[common.Hash]*announce),
|
|
|
|
|
- fetched: make(map[common.Hash][]*announce),
|
|
|
|
|
- completing: make(map[common.Hash]*announce),
|
|
|
|
|
|
|
+ announced: make(map[common.Hash][]*blockAnnounce),
|
|
|
|
|
+ fetching: make(map[common.Hash]*blockAnnounce),
|
|
|
|
|
+ fetched: make(map[common.Hash][]*blockAnnounce),
|
|
|
|
|
+ completing: make(map[common.Hash]*blockAnnounce),
|
|
|
queue: prque.New(nil),
|
|
queue: prque.New(nil),
|
|
|
queues: make(map[string]int),
|
|
queues: make(map[string]int),
|
|
|
- queued: make(map[common.Hash]*inject),
|
|
|
|
|
|
|
+ queued: make(map[common.Hash]*blockInject),
|
|
|
getBlock: getBlock,
|
|
getBlock: getBlock,
|
|
|
verifyHeader: verifyHeader,
|
|
verifyHeader: verifyHeader,
|
|
|
broadcastBlock: broadcastBlock,
|
|
broadcastBlock: broadcastBlock,
|
|
@@ -172,21 +175,21 @@ func New(getBlock blockRetrievalFn, verifyHeader headerVerifierFn, broadcastBloc
|
|
|
|
|
|
|
|
// Start boots up the announcement based synchroniser, accepting and processing
|
|
// Start boots up the announcement based synchroniser, accepting and processing
|
|
|
// hash notifications and block fetches until termination requested.
|
|
// hash notifications and block fetches until termination requested.
|
|
|
-func (f *Fetcher) Start() {
|
|
|
|
|
|
|
+func (f *BlockFetcher) Start() {
|
|
|
go f.loop()
|
|
go f.loop()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Stop terminates the announcement based synchroniser, canceling all pending
|
|
// Stop terminates the announcement based synchroniser, canceling all pending
|
|
|
// operations.
|
|
// operations.
|
|
|
-func (f *Fetcher) Stop() {
|
|
|
|
|
|
|
+func (f *BlockFetcher) Stop() {
|
|
|
close(f.quit)
|
|
close(f.quit)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Notify announces the fetcher of the potential availability of a new block in
|
|
// Notify announces the fetcher of the potential availability of a new block in
|
|
|
// the network.
|
|
// the network.
|
|
|
-func (f *Fetcher) Notify(peer string, hash common.Hash, number uint64, time time.Time,
|
|
|
|
|
|
|
+func (f *BlockFetcher) Notify(peer string, hash common.Hash, number uint64, time time.Time,
|
|
|
headerFetcher headerRequesterFn, bodyFetcher bodyRequesterFn) error {
|
|
headerFetcher headerRequesterFn, bodyFetcher bodyRequesterFn) error {
|
|
|
- block := &announce{
|
|
|
|
|
|
|
+ block := &blockAnnounce{
|
|
|
hash: hash,
|
|
hash: hash,
|
|
|
number: number,
|
|
number: number,
|
|
|
time: time,
|
|
time: time,
|
|
@@ -203,8 +206,8 @@ func (f *Fetcher) Notify(peer string, hash common.Hash, number uint64, time time
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Enqueue tries to fill gaps the fetcher's future import queue.
|
|
// Enqueue tries to fill gaps the fetcher's future import queue.
|
|
|
-func (f *Fetcher) Enqueue(peer string, block *types.Block) error {
|
|
|
|
|
- op := &inject{
|
|
|
|
|
|
|
+func (f *BlockFetcher) Enqueue(peer string, block *types.Block) error {
|
|
|
|
|
+ op := &blockInject{
|
|
|
origin: peer,
|
|
origin: peer,
|
|
|
block: block,
|
|
block: block,
|
|
|
}
|
|
}
|
|
@@ -218,7 +221,7 @@ func (f *Fetcher) Enqueue(peer string, block *types.Block) error {
|
|
|
|
|
|
|
|
// FilterHeaders extracts all the headers that were explicitly requested by the fetcher,
|
|
// FilterHeaders extracts all the headers that were explicitly requested by the fetcher,
|
|
|
// returning those that should be handled differently.
|
|
// returning those that should be handled differently.
|
|
|
-func (f *Fetcher) FilterHeaders(peer string, headers []*types.Header, time time.Time) []*types.Header {
|
|
|
|
|
|
|
+func (f *BlockFetcher) FilterHeaders(peer string, headers []*types.Header, time time.Time) []*types.Header {
|
|
|
log.Trace("Filtering headers", "peer", peer, "headers", len(headers))
|
|
log.Trace("Filtering headers", "peer", peer, "headers", len(headers))
|
|
|
|
|
|
|
|
// Send the filter channel to the fetcher
|
|
// Send the filter channel to the fetcher
|
|
@@ -246,7 +249,7 @@ func (f *Fetcher) FilterHeaders(peer string, headers []*types.Header, time time.
|
|
|
|
|
|
|
|
// FilterBodies extracts all the block bodies that were explicitly requested by
|
|
// FilterBodies extracts all the block bodies that were explicitly requested by
|
|
|
// the fetcher, returning those that should be handled differently.
|
|
// the fetcher, returning those that should be handled differently.
|
|
|
-func (f *Fetcher) FilterBodies(peer string, transactions [][]*types.Transaction, uncles [][]*types.Header, time time.Time) ([][]*types.Transaction, [][]*types.Header) {
|
|
|
|
|
|
|
+func (f *BlockFetcher) FilterBodies(peer string, transactions [][]*types.Transaction, uncles [][]*types.Header, time time.Time) ([][]*types.Transaction, [][]*types.Header) {
|
|
|
log.Trace("Filtering bodies", "peer", peer, "txs", len(transactions), "uncles", len(uncles))
|
|
log.Trace("Filtering bodies", "peer", peer, "txs", len(transactions), "uncles", len(uncles))
|
|
|
|
|
|
|
|
// Send the filter channel to the fetcher
|
|
// Send the filter channel to the fetcher
|
|
@@ -274,7 +277,7 @@ func (f *Fetcher) FilterBodies(peer string, transactions [][]*types.Transaction,
|
|
|
|
|
|
|
|
// Loop is the main fetcher loop, checking and processing various notification
|
|
// Loop is the main fetcher loop, checking and processing various notification
|
|
|
// events.
|
|
// events.
|
|
|
-func (f *Fetcher) loop() {
|
|
|
|
|
|
|
+func (f *BlockFetcher) loop() {
|
|
|
// Iterate the block fetching until a quit is requested
|
|
// Iterate the block fetching until a quit is requested
|
|
|
fetchTimer := time.NewTimer(0)
|
|
fetchTimer := time.NewTimer(0)
|
|
|
completeTimer := time.NewTimer(0)
|
|
completeTimer := time.NewTimer(0)
|
|
@@ -289,7 +292,7 @@ func (f *Fetcher) loop() {
|
|
|
// Import any queued blocks that could potentially fit
|
|
// Import any queued blocks that could potentially fit
|
|
|
height := f.chainHeight()
|
|
height := f.chainHeight()
|
|
|
for !f.queue.Empty() {
|
|
for !f.queue.Empty() {
|
|
|
- op := f.queue.PopItem().(*inject)
|
|
|
|
|
|
|
+ op := f.queue.PopItem().(*blockInject)
|
|
|
hash := op.block.Hash()
|
|
hash := op.block.Hash()
|
|
|
if f.queueChangeHook != nil {
|
|
if f.queueChangeHook != nil {
|
|
|
f.queueChangeHook(hash, false)
|
|
f.queueChangeHook(hash, false)
|
|
@@ -313,24 +316,24 @@ func (f *Fetcher) loop() {
|
|
|
// Wait for an outside event to occur
|
|
// Wait for an outside event to occur
|
|
|
select {
|
|
select {
|
|
|
case <-f.quit:
|
|
case <-f.quit:
|
|
|
- // Fetcher terminating, abort all operations
|
|
|
|
|
|
|
+ // BlockFetcher terminating, abort all operations
|
|
|
return
|
|
return
|
|
|
|
|
|
|
|
case notification := <-f.notify:
|
|
case notification := <-f.notify:
|
|
|
// A block was announced, make sure the peer isn't DOSing us
|
|
// A block was announced, make sure the peer isn't DOSing us
|
|
|
- propAnnounceInMeter.Mark(1)
|
|
|
|
|
|
|
+ blockAnnounceInMeter.Mark(1)
|
|
|
|
|
|
|
|
count := f.announces[notification.origin] + 1
|
|
count := f.announces[notification.origin] + 1
|
|
|
if count > hashLimit {
|
|
if count > hashLimit {
|
|
|
log.Debug("Peer exceeded outstanding announces", "peer", notification.origin, "limit", hashLimit)
|
|
log.Debug("Peer exceeded outstanding announces", "peer", notification.origin, "limit", hashLimit)
|
|
|
- propAnnounceDOSMeter.Mark(1)
|
|
|
|
|
|
|
+ blockAnnounceDOSMeter.Mark(1)
|
|
|
break
|
|
break
|
|
|
}
|
|
}
|
|
|
// If we have a valid block number, check that it's potentially useful
|
|
// If we have a valid block number, check that it's potentially useful
|
|
|
if notification.number > 0 {
|
|
if notification.number > 0 {
|
|
|
if dist := int64(notification.number) - int64(f.chainHeight()); dist < -maxUncleDist || dist > maxQueueDist {
|
|
if dist := int64(notification.number) - int64(f.chainHeight()); dist < -maxUncleDist || dist > maxQueueDist {
|
|
|
log.Debug("Peer discarded announcement", "peer", notification.origin, "number", notification.number, "hash", notification.hash, "distance", dist)
|
|
log.Debug("Peer discarded announcement", "peer", notification.origin, "number", notification.number, "hash", notification.hash, "distance", dist)
|
|
|
- propAnnounceDropMeter.Mark(1)
|
|
|
|
|
|
|
+ blockAnnounceDropMeter.Mark(1)
|
|
|
break
|
|
break
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -352,7 +355,7 @@ func (f *Fetcher) loop() {
|
|
|
|
|
|
|
|
case op := <-f.inject:
|
|
case op := <-f.inject:
|
|
|
// A direct block insertion was requested, try and fill any pending gaps
|
|
// A direct block insertion was requested, try and fill any pending gaps
|
|
|
- propBroadcastInMeter.Mark(1)
|
|
|
|
|
|
|
+ blockBroadcastInMeter.Mark(1)
|
|
|
f.enqueue(op.origin, op.block)
|
|
f.enqueue(op.origin, op.block)
|
|
|
|
|
|
|
|
case hash := <-f.done:
|
|
case hash := <-f.done:
|
|
@@ -439,7 +442,7 @@ func (f *Fetcher) loop() {
|
|
|
|
|
|
|
|
// Split the batch of headers into unknown ones (to return to the caller),
|
|
// Split the batch of headers into unknown ones (to return to the caller),
|
|
|
// known incomplete ones (requiring body retrievals) and completed blocks.
|
|
// known incomplete ones (requiring body retrievals) and completed blocks.
|
|
|
- unknown, incomplete, complete := []*types.Header{}, []*announce{}, []*types.Block{}
|
|
|
|
|
|
|
+ unknown, incomplete, complete := []*types.Header{}, []*blockAnnounce{}, []*types.Block{}
|
|
|
for _, header := range task.headers {
|
|
for _, header := range task.headers {
|
|
|
hash := header.Hash()
|
|
hash := header.Hash()
|
|
|
|
|
|
|
@@ -475,7 +478,7 @@ func (f *Fetcher) loop() {
|
|
|
f.forgetHash(hash)
|
|
f.forgetHash(hash)
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
- // Fetcher doesn't know about it, add to the return list
|
|
|
|
|
|
|
+ // BlockFetcher doesn't know about it, add to the return list
|
|
|
unknown = append(unknown, header)
|
|
unknown = append(unknown, header)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -562,8 +565,8 @@ func (f *Fetcher) loop() {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// rescheduleFetch resets the specified fetch timer to the next announce timeout.
|
|
|
|
|
-func (f *Fetcher) rescheduleFetch(fetch *time.Timer) {
|
|
|
|
|
|
|
+// rescheduleFetch resets the specified fetch timer to the next blockAnnounce timeout.
|
|
|
|
|
+func (f *BlockFetcher) rescheduleFetch(fetch *time.Timer) {
|
|
|
// Short circuit if no blocks are announced
|
|
// Short circuit if no blocks are announced
|
|
|
if len(f.announced) == 0 {
|
|
if len(f.announced) == 0 {
|
|
|
return
|
|
return
|
|
@@ -579,7 +582,7 @@ func (f *Fetcher) rescheduleFetch(fetch *time.Timer) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// rescheduleComplete resets the specified completion timer to the next fetch timeout.
|
|
// rescheduleComplete resets the specified completion timer to the next fetch timeout.
|
|
|
-func (f *Fetcher) rescheduleComplete(complete *time.Timer) {
|
|
|
|
|
|
|
+func (f *BlockFetcher) rescheduleComplete(complete *time.Timer) {
|
|
|
// Short circuit if no headers are fetched
|
|
// Short circuit if no headers are fetched
|
|
|
if len(f.fetched) == 0 {
|
|
if len(f.fetched) == 0 {
|
|
|
return
|
|
return
|
|
@@ -596,27 +599,27 @@ func (f *Fetcher) rescheduleComplete(complete *time.Timer) {
|
|
|
|
|
|
|
|
// enqueue schedules a new future import operation, if the block to be imported
|
|
// enqueue schedules a new future import operation, if the block to be imported
|
|
|
// has not yet been seen.
|
|
// has not yet been seen.
|
|
|
-func (f *Fetcher) enqueue(peer string, block *types.Block) {
|
|
|
|
|
|
|
+func (f *BlockFetcher) enqueue(peer string, block *types.Block) {
|
|
|
hash := block.Hash()
|
|
hash := block.Hash()
|
|
|
|
|
|
|
|
// Ensure the peer isn't DOSing us
|
|
// Ensure the peer isn't DOSing us
|
|
|
count := f.queues[peer] + 1
|
|
count := f.queues[peer] + 1
|
|
|
if count > blockLimit {
|
|
if count > blockLimit {
|
|
|
log.Debug("Discarded propagated block, exceeded allowance", "peer", peer, "number", block.Number(), "hash", hash, "limit", blockLimit)
|
|
log.Debug("Discarded propagated block, exceeded allowance", "peer", peer, "number", block.Number(), "hash", hash, "limit", blockLimit)
|
|
|
- propBroadcastDOSMeter.Mark(1)
|
|
|
|
|
|
|
+ blockBroadcastDOSMeter.Mark(1)
|
|
|
f.forgetHash(hash)
|
|
f.forgetHash(hash)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
// Discard any past or too distant blocks
|
|
// Discard any past or too distant blocks
|
|
|
if dist := int64(block.NumberU64()) - int64(f.chainHeight()); dist < -maxUncleDist || dist > maxQueueDist {
|
|
if dist := int64(block.NumberU64()) - int64(f.chainHeight()); dist < -maxUncleDist || dist > maxQueueDist {
|
|
|
log.Debug("Discarded propagated block, too far away", "peer", peer, "number", block.Number(), "hash", hash, "distance", dist)
|
|
log.Debug("Discarded propagated block, too far away", "peer", peer, "number", block.Number(), "hash", hash, "distance", dist)
|
|
|
- propBroadcastDropMeter.Mark(1)
|
|
|
|
|
|
|
+ blockBroadcastDropMeter.Mark(1)
|
|
|
f.forgetHash(hash)
|
|
f.forgetHash(hash)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
// Schedule the block for future importing
|
|
// Schedule the block for future importing
|
|
|
if _, ok := f.queued[hash]; !ok {
|
|
if _, ok := f.queued[hash]; !ok {
|
|
|
- op := &inject{
|
|
|
|
|
|
|
+ op := &blockInject{
|
|
|
origin: peer,
|
|
origin: peer,
|
|
|
block: block,
|
|
block: block,
|
|
|
}
|
|
}
|
|
@@ -633,7 +636,7 @@ func (f *Fetcher) enqueue(peer string, block *types.Block) {
|
|
|
// insert spawns a new goroutine to run a block insertion into the chain. If the
|
|
// insert spawns a new goroutine to run a block insertion into the chain. If the
|
|
|
// block's number is at the same height as the current import phase, it updates
|
|
// block's number is at the same height as the current import phase, it updates
|
|
|
// the phase states accordingly.
|
|
// the phase states accordingly.
|
|
|
-func (f *Fetcher) insert(peer string, block *types.Block) {
|
|
|
|
|
|
|
+func (f *BlockFetcher) insert(peer string, block *types.Block) {
|
|
|
hash := block.Hash()
|
|
hash := block.Hash()
|
|
|
|
|
|
|
|
// Run the import on a new thread
|
|
// Run the import on a new thread
|
|
@@ -651,7 +654,7 @@ func (f *Fetcher) insert(peer string, block *types.Block) {
|
|
|
switch err := f.verifyHeader(block.Header()); err {
|
|
switch err := f.verifyHeader(block.Header()); err {
|
|
|
case nil:
|
|
case nil:
|
|
|
// All ok, quickly propagate to our peers
|
|
// All ok, quickly propagate to our peers
|
|
|
- propBroadcastOutTimer.UpdateSince(block.ReceivedAt)
|
|
|
|
|
|
|
+ blockBroadcastOutTimer.UpdateSince(block.ReceivedAt)
|
|
|
go f.broadcastBlock(block, true)
|
|
go f.broadcastBlock(block, true)
|
|
|
|
|
|
|
|
case consensus.ErrFutureBlock:
|
|
case consensus.ErrFutureBlock:
|
|
@@ -669,7 +672,7 @@ func (f *Fetcher) insert(peer string, block *types.Block) {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
// If import succeeded, broadcast the block
|
|
// If import succeeded, broadcast the block
|
|
|
- propAnnounceOutTimer.UpdateSince(block.ReceivedAt)
|
|
|
|
|
|
|
+ blockAnnounceOutTimer.UpdateSince(block.ReceivedAt)
|
|
|
go f.broadcastBlock(block, false)
|
|
go f.broadcastBlock(block, false)
|
|
|
|
|
|
|
|
// Invoke the testing hook if needed
|
|
// Invoke the testing hook if needed
|
|
@@ -681,7 +684,7 @@ func (f *Fetcher) insert(peer string, block *types.Block) {
|
|
|
|
|
|
|
|
// forgetHash removes all traces of a block announcement from the fetcher's
|
|
// forgetHash removes all traces of a block announcement from the fetcher's
|
|
|
// internal state.
|
|
// internal state.
|
|
|
-func (f *Fetcher) forgetHash(hash common.Hash) {
|
|
|
|
|
|
|
+func (f *BlockFetcher) forgetHash(hash common.Hash) {
|
|
|
// Remove all pending announces and decrement DOS counters
|
|
// Remove all pending announces and decrement DOS counters
|
|
|
for _, announce := range f.announced[hash] {
|
|
for _, announce := range f.announced[hash] {
|
|
|
f.announces[announce.origin]--
|
|
f.announces[announce.origin]--
|
|
@@ -723,7 +726,7 @@ func (f *Fetcher) forgetHash(hash common.Hash) {
|
|
|
|
|
|
|
|
// forgetBlock removes all traces of a queued block from the fetcher's internal
|
|
// forgetBlock removes all traces of a queued block from the fetcher's internal
|
|
|
// state.
|
|
// state.
|
|
|
-func (f *Fetcher) forgetBlock(hash common.Hash) {
|
|
|
|
|
|
|
+func (f *BlockFetcher) forgetBlock(hash common.Hash) {
|
|
|
if insert := f.queued[hash]; insert != nil {
|
|
if insert := f.queued[hash]; insert != nil {
|
|
|
f.queues[insert.origin]--
|
|
f.queues[insert.origin]--
|
|
|
if f.queues[insert.origin] == 0 {
|
|
if f.queues[insert.origin] == 0 {
|