|
|
|
package downloader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
|
|
|
"github.com/harmony-one/harmony/core"
|
|
|
|
"github.com/harmony-one/harmony/core/types"
|
|
|
|
"github.com/harmony-one/harmony/crypto/bls"
|
|
|
|
"github.com/harmony-one/harmony/internal/chain"
|
|
|
|
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
|
|
|
|
"github.com/harmony-one/harmony/internal/utils"
|
|
|
|
"github.com/harmony-one/harmony/p2p"
|
|
|
|
"github.com/harmony-one/harmony/p2p/stream/common/streammanager"
|
|
|
|
"github.com/harmony-one/harmony/p2p/stream/protocols/sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Downloader is responsible for sync task of one shard
|
|
|
|
Downloader struct {
|
|
|
|
bc blockChain
|
|
|
|
syncProtocol syncProtocol
|
|
|
|
bh *beaconHelper
|
|
|
|
|
|
|
|
downloadC chan struct{}
|
|
|
|
closeC chan struct{}
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
|
|
|
|
|
|
|
evtDownloadFinished event.Feed // channel for each download task finished
|
|
|
|
evtDownloadFinishedSubscribed bool
|
|
|
|
evtDownloadStarted event.Feed // channel for each download has started
|
|
|
|
evtDownloadStartedSubscribed bool
|
|
|
|
|
|
|
|
status status
|
|
|
|
config Config
|
|
|
|
logger zerolog.Logger
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewDownloader creates a new downloader
|
Mainnet Release Candidate 2023.1.2 (#4376)
* remove default timeouts
* store the evm call timeout in rosetta object
* [cmd] actually apply ToRPCServerConfig
* Removed unused method.
* Rotate external leaders on non-beacon chains.
* Fix nil panic.
* Bump github.com/aws/aws-sdk-go from 1.33.0 to 1.34.0
Bumps [github.com/aws/aws-sdk-go](https://github.com/aws/aws-sdk-go) from 1.33.0 to 1.34.0.
- [Release notes](https://github.com/aws/aws-sdk-go/releases)
- [Changelog](https://github.com/aws/aws-sdk-go/blob/v1.34.0/CHANGELOG.md)
- [Commits](https://github.com/aws/aws-sdk-go/compare/v1.33.0...v1.34.0)
---
updated-dependencies:
- dependency-name: github.com/aws/aws-sdk-go
dependency-type: direct:production
...
Signed-off-by: dependabot[bot] <support@github.com>
* Bump github.com/ipld/go-ipld-prime from 0.9.0 to 0.19.0
Bumps [github.com/ipld/go-ipld-prime](https://github.com/ipld/go-ipld-prime) from 0.9.0 to 0.19.0.
- [Release notes](https://github.com/ipld/go-ipld-prime/releases)
- [Changelog](https://github.com/ipld/go-ipld-prime/blob/master/CHANGELOG.md)
- [Commits](https://github.com/ipld/go-ipld-prime/compare/v0.9.0...v0.19.0)
---
updated-dependencies:
- dependency-name: github.com/ipld/go-ipld-prime
dependency-type: indirect
...
Signed-off-by: dependabot[bot] <support@github.com>
* Bump golang.org/x/net from 0.3.0 to 0.7.0
Bumps [golang.org/x/net](https://github.com/golang/net) from 0.3.0 to 0.7.0.
- [Release notes](https://github.com/golang/net/releases)
- [Commits](https://github.com/golang/net/compare/v0.3.0...v0.7.0)
---
updated-dependencies:
- dependency-name: golang.org/x/net
dependency-type: indirect
...
Signed-off-by: dependabot[bot] <support@github.com>
* Small fixes.
* in progress.
* in progress.
* in progress.
* consensus check is forked
* update master
* fix leader
* check leader for N blocks
* fix
* fix
* Cleanup and fix update pub keys.
* Rotate leader.
* fix
fix
fix
fix
fix
* Cleaned.
* Cache for `GetLeaderPubKeyFromCoinbase`, removed `NthNextHmyExt`.
* activate epoch
* comment activation
* 295 epoch
* Fix failed tests.
* Fixed code review.
* Fix review "--port flag".
* Fix review comments.
* Returned locks in rotateLeader.
* Rebased onto dev.
* Commented golangci.
* staged stream sync v1.0
* fix protocol tests
* fix spell
* remove unused struct
* fix rosetta test
* add comments and refactor verify sig
* add comments, remove extra function
* add comment
* refactor errors, rename metrics
* refactor p2p host creation
* fix initsync and host creation
* fix short range hash chain
* fix beacon node detection for p2p protocol
* refactor stream peer cooldown and fix protocol beacon node field
* refactor p2p host and routing
* fix p2p discovery test issue
* add MaxAdvertiseWaitTime to handle advertisements interval and address stream connection issue
* terminal print the peer id and proto id
* fix boot complete message when node is shut down
* add new config option ( ForceReachabilityPublic ) to fix local-net consensus issue
* fix self query issue
* fix test NewDNSSyncingPeerProvider
* [testnet] disable leader rotation
* fix discovery issue for legacy sync
* add watermark low/high options for p2p connection manager
* add test for new conn manager flags
* fix dedent
* add comment to inform about p2p connection manager options
* fix max height issue
* add a separate log for get max height error
* fix log
* feat: triesInMemory flag
* fix: panic if TriesInMemory is 1 to 2
* in progress.
* consensus check is forked
* fix
* Cleanup and fix update pub keys.
* fix
fix
fix
fix
fix
* activate epoch
* EpochTBD for leader rotation epoch.
* 295 epoch
* Decider no longer requires public keys as a dependency. (#4289)
* Consensus doesn't require anymore `Node` as a circular dependency.
* Proper blockchain initialization.
* Rwlock consensus.
* Removed channels.
* Removed view change locks.
* Removed timers locks.
* Removed fbft locks.
* Removed multiSigMutex locks.
* Removed leader locks.
* Removed additional locks and isViewChange.
* Added locks detected by race.
* Added locks detected by race.
* Locks for start.
* Removed additional logs.
* Removed additional locks.
* Removed additional locks.
* Make func private.
* Make VerifyBlock private.
* Make IsLeader private.
* Make ParseFBFTMessage private.
* Fix remove locks.
* Added additional locks.
* Added additional locks.
* Added readSignatureBitmapPayload locks.
* Added HandleMessageUpdate locks.
* Added LastMile locks.
* Locks for IsValidatorInCommittee.
* Fixed locks.
* Fixed tests.
* Fixed tests.
* Fixed lock.
* Rebased over leader rotation.
* Fix formatting.
* Rebased onto dev.
* in progress.
* consensus check is forked
* update master
* fix leader
* check leader for N blocks
* fix
* fix
* Cleanup and fix update pub keys.
* Rotate leader.
* fix
fix
fix
fix
fix
* Cleaned.
* Cache for `GetLeaderPubKeyFromCoinbase`, removed `NthNextHmyExt`.
* comment activation
* 295 epoch
* Fix failed tests.
* Fixed code review.
* Fix review comments.
* Merged leader rotation.
* Rebased on dev.
* Rebased on dev.
* Fix usage of private methods.
* Fix usage of private methods.
* Fix usage of private methods.
* Removed deadcode, LockedFBFTPhase.
* Fix review comment.
* Fix review comment.
* Go mod tidy.
* Set to EpochTBD.
* Fix tests.
* [core] fix state handling of self destruct
If a contract self destructs to self and then receives funds within the
same transaction, it is possible for its stale state to be saved. This
change removes that possibility by checking for deleted state objects
before returning them.
* Fixed race error.
* rpc: add configurable http and `eth_call` timeout
* remove default timeouts
* store the evm call timeout in rosetta object
* [cmd] actually apply ToRPCServerConfig
* Removed unused method.
* Rotate external leaders on non-beacon chains.
* Fix nil panic.
* in progress.
* in progress.
* in progress.
* consensus check is forked
* update master
* fix leader
* check leader for N blocks
* fix
* fix
* Cleanup and fix update pub keys.
* Rotate leader.
* fix
fix
fix
fix
fix
* Cleaned.
* Cache for `GetLeaderPubKeyFromCoinbase`, removed `NthNextHmyExt`.
* Fixed code review.
* Fix review comments.
* Returned locks in rotateLeader.
* Rebased onto dev.
* staged stream sync v1.0
* refactor errors, rename metrics
* fix p2p discovery test issue
* add watermark low/high options for p2p connection manager
* fix dedent
* in progress.
* consensus check is forked
* fix
* Cleanup and fix update pub keys.
* fix
fix
fix
fix
fix
* activate epoch
* EpochTBD for leader rotation epoch.
* 295 epoch
* Decider no longer requires public keys as a dependency. (#4289)
* Consensus doesn't require anymore `Node` as a circular dependency.
* Proper blockchain initialization.
* Rwlock consensus.
* Removed channels.
* Removed view change locks.
* Removed multiSigMutex locks.
* Removed leader locks.
* Removed additional locks and isViewChange.
* Added locks detected by race.
* Added locks detected by race.
* Locks for start.
* Removed additional locks.
* Removed additional locks.
* Make func private.
* Make VerifyBlock private.
* Make IsLeader private.
* Make ParseFBFTMessage private.
* Fix remove locks.
* Added additional locks.
* Added additional locks.
* Added readSignatureBitmapPayload locks.
* Added HandleMessageUpdate locks.
* Added LastMile locks.
* Locks for IsValidatorInCommittee.
* Fixed locks.
* Fixed tests.
* Fixed lock.
* Rebased over leader rotation.
* in progress.
* consensus check is forked
* update master
* fix leader
* check leader for N blocks
* fix
* fix
* Cleanup and fix update pub keys.
* Rotate leader.
* fix
fix
fix
fix
fix
* Cleaned.
* Cache for `GetLeaderPubKeyFromCoinbase`, removed `NthNextHmyExt`.
* Fix failed tests.
* Fixed code review.
* Fix review comments.
* Merged leader rotation.
* Rebased on dev.
* Rebased on dev.
* Fix usage of private methods.
* Fix usage of private methods.
* Fix usage of private methods.
* Removed deadcode, LockedFBFTPhase.
* Fix review comment.
* Go mod tidy.
* remove default timeouts
* Rotate external leaders on non-beacon chains.
* Fix nil panic.
* Fixes.
* Update singleton.go
* evm: don't return extcode for validators
Due to technical debt, validator information is stored in the code field
of the address. The code field can be accessed in Solidity for an
arbitrary address using `extcodesize`, `extcodehash`, and `extcodecopy`
or helper commands (such as `address.code.Length`). The presence of this
field is used by contract developers to (erroneously) deny smart
contract access to other smart contracts (and therefore, validators).
This PR fixes that oversight by returning the same values as other EOAs
for known validator addresses. Obviously, it needs a hard fork that will
be scheduled separately.
* Fix context passing.
* Clean up code.
* Removed engine dependency.
* Fix possible panic.
* Clean up code.
* Network type.
* Fix tests.
* Revert "Removed engine dependency." (#4392)
* Revert "Fix tests."
This reverts commit 597ba2d6f1ed54ff599b9d9b8940c7285ab1277a.
* Revert "Network type."
This reverts commit 5e1878aedca3989dc0f34161dae1833e43ca6a76.
* Revert "Clean up code."
This reverts commit 15885f4c9b9263746827172b4f4f56d0926d18e2.
* Revert "Fix possible panic."
This reverts commit 1a70d5eb66cdbf8a23791806b71a323eed320085.
* Revert "Removed engine dependency."
This reverts commit 8c2ff803f709f944cfc8b1278f35cf5b2cacf859.
* gitignore the cache folder (#4389)
* stable localnet with external validator (#4388)
* stable localnet with external validator
* ignore deploy config file comments
* reduce node launched in localnet
* update makefile
* localnet configuration - add more fn
* fix validator information command typo
---------
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: MaxMustermann2 <82761650+MaxMustermann2@users.noreply.github.com>
Co-authored-by: frozen <355847+Frozen@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: “GheisMohammadi” <“Gheis.Mohammadi@gmail.com”>
Co-authored-by: “GheisMohammadi” <36589218+GheisMohammadi@users.noreply.github.com>
Co-authored-by: Sun Hyuk Ahn <sunhyukahn@Suns-MacBook-Pro.local>
Co-authored-by: Soph <35721420+sophoah@users.noreply.github.com>
2 years ago
|
|
|
func NewDownloader(host p2p.Host, bc core.BlockChain, isBeaconNode bool, config Config) *Downloader {
|
|
|
|
config.fixValues()
|
|
|
|
|
|
|
|
sp := sync.NewProtocol(sync.Config{
|
Mainnet Release Candidate 2023.1.2 (#4376)
* remove default timeouts
* store the evm call timeout in rosetta object
* [cmd] actually apply ToRPCServerConfig
* Removed unused method.
* Rotate external leaders on non-beacon chains.
* Fix nil panic.
* Bump github.com/aws/aws-sdk-go from 1.33.0 to 1.34.0
Bumps [github.com/aws/aws-sdk-go](https://github.com/aws/aws-sdk-go) from 1.33.0 to 1.34.0.
- [Release notes](https://github.com/aws/aws-sdk-go/releases)
- [Changelog](https://github.com/aws/aws-sdk-go/blob/v1.34.0/CHANGELOG.md)
- [Commits](https://github.com/aws/aws-sdk-go/compare/v1.33.0...v1.34.0)
---
updated-dependencies:
- dependency-name: github.com/aws/aws-sdk-go
dependency-type: direct:production
...
Signed-off-by: dependabot[bot] <support@github.com>
* Bump github.com/ipld/go-ipld-prime from 0.9.0 to 0.19.0
Bumps [github.com/ipld/go-ipld-prime](https://github.com/ipld/go-ipld-prime) from 0.9.0 to 0.19.0.
- [Release notes](https://github.com/ipld/go-ipld-prime/releases)
- [Changelog](https://github.com/ipld/go-ipld-prime/blob/master/CHANGELOG.md)
- [Commits](https://github.com/ipld/go-ipld-prime/compare/v0.9.0...v0.19.0)
---
updated-dependencies:
- dependency-name: github.com/ipld/go-ipld-prime
dependency-type: indirect
...
Signed-off-by: dependabot[bot] <support@github.com>
* Bump golang.org/x/net from 0.3.0 to 0.7.0
Bumps [golang.org/x/net](https://github.com/golang/net) from 0.3.0 to 0.7.0.
- [Release notes](https://github.com/golang/net/releases)
- [Commits](https://github.com/golang/net/compare/v0.3.0...v0.7.0)
---
updated-dependencies:
- dependency-name: golang.org/x/net
dependency-type: indirect
...
Signed-off-by: dependabot[bot] <support@github.com>
* Small fixes.
* in progress.
* in progress.
* in progress.
* consensus check is forked
* update master
* fix leader
* check leader for N blocks
* fix
* fix
* Cleanup and fix update pub keys.
* Rotate leader.
* fix
fix
fix
fix
fix
* Cleaned.
* Cache for `GetLeaderPubKeyFromCoinbase`, removed `NthNextHmyExt`.
* activate epoch
* comment activation
* 295 epoch
* Fix failed tests.
* Fixed code review.
* Fix review "--port flag".
* Fix review comments.
* Returned locks in rotateLeader.
* Rebased onto dev.
* Commented golangci.
* staged stream sync v1.0
* fix protocol tests
* fix spell
* remove unused struct
* fix rosetta test
* add comments and refactor verify sig
* add comments, remove extra function
* add comment
* refactor errors, rename metrics
* refactor p2p host creation
* fix initsync and host creation
* fix short range hash chain
* fix beacon node detection for p2p protocol
* refactor stream peer cooldown and fix protocol beacon node field
* refactor p2p host and routing
* fix p2p discovery test issue
* add MaxAdvertiseWaitTime to handle advertisements interval and address stream connection issue
* terminal print the peer id and proto id
* fix boot complete message when node is shut down
* add new config option ( ForceReachabilityPublic ) to fix local-net consensus issue
* fix self query issue
* fix test NewDNSSyncingPeerProvider
* [testnet] disable leader rotation
* fix discovery issue for legacy sync
* add watermark low/high options for p2p connection manager
* add test for new conn manager flags
* fix dedent
* add comment to inform about p2p connection manager options
* fix max height issue
* add a separate log for get max height error
* fix log
* feat: triesInMemory flag
* fix: panic if TriesInMemory is 1 to 2
* in progress.
* consensus check is forked
* fix
* Cleanup and fix update pub keys.
* fix
fix
fix
fix
fix
* activate epoch
* EpochTBD for leader rotation epoch.
* 295 epoch
* Decider no longer requires public keys as a dependency. (#4289)
* Consensus doesn't require anymore `Node` as a circular dependency.
* Proper blockchain initialization.
* Rwlock consensus.
* Removed channels.
* Removed view change locks.
* Removed timers locks.
* Removed fbft locks.
* Removed multiSigMutex locks.
* Removed leader locks.
* Removed additional locks and isViewChange.
* Added locks detected by race.
* Added locks detected by race.
* Locks for start.
* Removed additional logs.
* Removed additional locks.
* Removed additional locks.
* Make func private.
* Make VerifyBlock private.
* Make IsLeader private.
* Make ParseFBFTMessage private.
* Fix remove locks.
* Added additional locks.
* Added additional locks.
* Added readSignatureBitmapPayload locks.
* Added HandleMessageUpdate locks.
* Added LastMile locks.
* Locks for IsValidatorInCommittee.
* Fixed locks.
* Fixed tests.
* Fixed tests.
* Fixed lock.
* Rebased over leader rotation.
* Fix formatting.
* Rebased onto dev.
* in progress.
* consensus check is forked
* update master
* fix leader
* check leader for N blocks
* fix
* fix
* Cleanup and fix update pub keys.
* Rotate leader.
* fix
fix
fix
fix
fix
* Cleaned.
* Cache for `GetLeaderPubKeyFromCoinbase`, removed `NthNextHmyExt`.
* comment activation
* 295 epoch
* Fix failed tests.
* Fixed code review.
* Fix review comments.
* Merged leader rotation.
* Rebased on dev.
* Rebased on dev.
* Fix usage of private methods.
* Fix usage of private methods.
* Fix usage of private methods.
* Removed deadcode, LockedFBFTPhase.
* Fix review comment.
* Fix review comment.
* Go mod tidy.
* Set to EpochTBD.
* Fix tests.
* [core] fix state handling of self destruct
If a contract self destructs to self and then receives funds within the
same transaction, it is possible for its stale state to be saved. This
change removes that possibility by checking for deleted state objects
before returning them.
* Fixed race error.
* rpc: add configurable http and `eth_call` timeout
* remove default timeouts
* store the evm call timeout in rosetta object
* [cmd] actually apply ToRPCServerConfig
* Removed unused method.
* Rotate external leaders on non-beacon chains.
* Fix nil panic.
* in progress.
* in progress.
* in progress.
* consensus check is forked
* update master
* fix leader
* check leader for N blocks
* fix
* fix
* Cleanup and fix update pub keys.
* Rotate leader.
* fix
fix
fix
fix
fix
* Cleaned.
* Cache for `GetLeaderPubKeyFromCoinbase`, removed `NthNextHmyExt`.
* Fixed code review.
* Fix review comments.
* Returned locks in rotateLeader.
* Rebased onto dev.
* staged stream sync v1.0
* refactor errors, rename metrics
* fix p2p discovery test issue
* add watermark low/high options for p2p connection manager
* fix dedent
* in progress.
* consensus check is forked
* fix
* Cleanup and fix update pub keys.
* fix
fix
fix
fix
fix
* activate epoch
* EpochTBD for leader rotation epoch.
* 295 epoch
* Decider no longer requires public keys as a dependency. (#4289)
* Consensus doesn't require anymore `Node` as a circular dependency.
* Proper blockchain initialization.
* Rwlock consensus.
* Removed channels.
* Removed view change locks.
* Removed multiSigMutex locks.
* Removed leader locks.
* Removed additional locks and isViewChange.
* Added locks detected by race.
* Added locks detected by race.
* Locks for start.
* Removed additional locks.
* Removed additional locks.
* Make func private.
* Make VerifyBlock private.
* Make IsLeader private.
* Make ParseFBFTMessage private.
* Fix remove locks.
* Added additional locks.
* Added additional locks.
* Added readSignatureBitmapPayload locks.
* Added HandleMessageUpdate locks.
* Added LastMile locks.
* Locks for IsValidatorInCommittee.
* Fixed locks.
* Fixed tests.
* Fixed lock.
* Rebased over leader rotation.
* in progress.
* consensus check is forked
* update master
* fix leader
* check leader for N blocks
* fix
* fix
* Cleanup and fix update pub keys.
* Rotate leader.
* fix
fix
fix
fix
fix
* Cleaned.
* Cache for `GetLeaderPubKeyFromCoinbase`, removed `NthNextHmyExt`.
* Fix failed tests.
* Fixed code review.
* Fix review comments.
* Merged leader rotation.
* Rebased on dev.
* Rebased on dev.
* Fix usage of private methods.
* Fix usage of private methods.
* Fix usage of private methods.
* Removed deadcode, LockedFBFTPhase.
* Fix review comment.
* Go mod tidy.
* remove default timeouts
* Rotate external leaders on non-beacon chains.
* Fix nil panic.
* Fixes.
* Update singleton.go
* evm: don't return extcode for validators
Due to technical debt, validator information is stored in the code field
of the address. The code field can be accessed in Solidity for an
arbitrary address using `extcodesize`, `extcodehash`, and `extcodecopy`
or helper commands (such as `address.code.Length`). The presence of this
field is used by contract developers to (erroneously) deny smart
contract access to other smart contracts (and therefore, validators).
This PR fixes that oversight by returning the same values as other EOAs
for known validator addresses. Obviously, it needs a hard fork that will
be scheduled separately.
* Fix context passing.
* Clean up code.
* Removed engine dependency.
* Fix possible panic.
* Clean up code.
* Network type.
* Fix tests.
* Revert "Removed engine dependency." (#4392)
* Revert "Fix tests."
This reverts commit 597ba2d6f1ed54ff599b9d9b8940c7285ab1277a.
* Revert "Network type."
This reverts commit 5e1878aedca3989dc0f34161dae1833e43ca6a76.
* Revert "Clean up code."
This reverts commit 15885f4c9b9263746827172b4f4f56d0926d18e2.
* Revert "Fix possible panic."
This reverts commit 1a70d5eb66cdbf8a23791806b71a323eed320085.
* Revert "Removed engine dependency."
This reverts commit 8c2ff803f709f944cfc8b1278f35cf5b2cacf859.
* gitignore the cache folder (#4389)
* stable localnet with external validator (#4388)
* stable localnet with external validator
* ignore deploy config file comments
* reduce node launched in localnet
* update makefile
* localnet configuration - add more fn
* fix validator information command typo
---------
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: MaxMustermann2 <82761650+MaxMustermann2@users.noreply.github.com>
Co-authored-by: frozen <355847+Frozen@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: “GheisMohammadi” <“Gheis.Mohammadi@gmail.com”>
Co-authored-by: “GheisMohammadi” <36589218+GheisMohammadi@users.noreply.github.com>
Co-authored-by: Sun Hyuk Ahn <sunhyukahn@Suns-MacBook-Pro.local>
Co-authored-by: Soph <35721420+sophoah@users.noreply.github.com>
2 years ago
|
|
|
Chain: bc,
|
|
|
|
Host: host.GetP2PHost(),
|
|
|
|
Discovery: host.GetDiscovery(),
|
|
|
|
ShardID: nodeconfig.ShardID(bc.ShardID()),
|
|
|
|
Network: config.Network,
|
|
|
|
BeaconNode: isBeaconNode,
|
|
|
|
|
|
|
|
SmSoftLowCap: config.SmSoftLowCap,
|
|
|
|
SmHardLowCap: config.SmHardLowCap,
|
|
|
|
SmHiCap: config.SmHiCap,
|
|
|
|
DiscBatch: config.SmDiscBatch,
|
|
|
|
})
|
|
|
|
host.AddStreamProtocol(sp)
|
|
|
|
|
|
|
|
var bh *beaconHelper
|
|
|
|
if config.BHConfig != nil && bc.ShardID() == 0 {
|
|
|
|
bh = newBeaconHelper(bc, config.BHConfig.BlockC, config.BHConfig.InsertHook)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
return &Downloader{
|
|
|
|
bc: bc,
|
|
|
|
syncProtocol: sp,
|
|
|
|
bh: bh,
|
|
|
|
|
|
|
|
downloadC: make(chan struct{}),
|
|
|
|
closeC: make(chan struct{}),
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
|
|
|
|
status: newStatus(),
|
|
|
|
config: config,
|
|
|
|
logger: utils.Logger().With().Str("module", "downloader").Uint32("ShardID", bc.ShardID()).Logger(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start start the downloader
|
|
|
|
func (d *Downloader) Start() {
|
|
|
|
go d.run()
|
|
|
|
|
|
|
|
if d.bh != nil {
|
|
|
|
d.bh.start()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close close the downloader
|
|
|
|
func (d *Downloader) Close() {
|
|
|
|
close(d.closeC)
|
|
|
|
d.cancel()
|
|
|
|
|
|
|
|
if d.bh != nil {
|
|
|
|
d.bh.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadAsync triggers the download async.
|
|
|
|
func (d *Downloader) DownloadAsync() {
|
|
|
|
select {
|
|
|
|
case d.downloadC <- struct{}{}:
|
|
|
|
consensusTriggeredDownloadCounterVec.With(d.promLabels()).Inc()
|
|
|
|
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumPeers returns the number of peers connected of a specific shard.
|
|
|
|
func (d *Downloader) NumPeers() int {
|
|
|
|
return d.syncProtocol.NumStreams()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSyncing return the current sync status
|
|
|
|
func (d *Downloader) SyncStatus() (bool, uint64, uint64) {
|
|
|
|
current := d.bc.CurrentBlock().NumberU64()
|
|
|
|
syncing, target := d.status.get()
|
|
|
|
if !syncing { // means synced
|
|
|
|
target = current
|
|
|
|
}
|
|
|
|
// isSyncing, target, blocks to target
|
|
|
|
return syncing, target, target - current
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeDownloadStarted subscribe download started
|
|
|
|
func (d *Downloader) SubscribeDownloadStarted(ch chan struct{}) event.Subscription {
|
|
|
|
d.evtDownloadStartedSubscribed = true
|
|
|
|
return d.evtDownloadStarted.Subscribe(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeDownloadFinished subscribe the download finished
|
|
|
|
func (d *Downloader) SubscribeDownloadFinished(ch chan struct{}) event.Subscription {
|
|
|
|
d.evtDownloadFinishedSubscribed = true
|
|
|
|
return d.evtDownloadFinished.Subscribe(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Downloader) run() {
|
|
|
|
d.waitForBootFinish()
|
|
|
|
d.loop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// waitForBootFinish wait for stream manager to finish the initial discovery and have
|
|
|
|
// enough peers to start downloader
|
|
|
|
func (d *Downloader) waitForBootFinish() {
|
|
|
|
evtCh := make(chan streammanager.EvtStreamAdded, 1)
|
|
|
|
sub := d.syncProtocol.SubscribeAddStreamEvent(evtCh)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
checkCh := make(chan struct{}, 1)
|
|
|
|
trigger := func() {
|
|
|
|
select {
|
|
|
|
case checkCh <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
trigger()
|
|
|
|
|
|
|
|
t := time.NewTicker(10 * time.Second)
|
|
|
|
defer t.Stop()
|
|
|
|
for {
|
|
|
|
d.logger.Info().Msg("waiting for initial bootstrap discovery")
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
trigger()
|
|
|
|
|
|
|
|
case <-evtCh:
|
|
|
|
trigger()
|
|
|
|
|
|
|
|
case <-checkCh:
|
|
|
|
if d.syncProtocol.NumStreams() >= d.config.InitStreams {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-d.closeC:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Downloader) loop() {
|
|
|
|
ticker := time.NewTicker(10 * time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
initSync := true
|
|
|
|
trigger := func() {
|
|
|
|
select {
|
|
|
|
case d.downloadC <- struct{}{}:
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go trigger()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
go trigger()
|
|
|
|
|
|
|
|
case <-d.downloadC:
|
|
|
|
addedBN, err := d.doDownload(initSync)
|
|
|
|
if err != nil {
|
|
|
|
// If error happens, sleep 5 seconds and retry
|
|
|
|
d.logger.Warn().Err(err).Bool("bootstrap", initSync).Msg("failed to download")
|
|
|
|
go func() {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
trigger()
|
|
|
|
}()
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
d.logger.Info().Int("block added", addedBN).
|
|
|
|
Uint64("current height", d.bc.CurrentBlock().NumberU64()).
|
|
|
|
Bool("initSync", initSync).
|
|
|
|
Uint32("shard", d.bc.ShardID()).
|
|
|
|
Msg("sync finished")
|
|
|
|
|
|
|
|
if addedBN != 0 {
|
|
|
|
// If block number has been changed, trigger another sync
|
|
|
|
// and try to add last mile from pub-sub (blocking)
|
|
|
|
go trigger()
|
|
|
|
if d.bh != nil {
|
|
|
|
d.bh.insertSync()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
initSync = false
|
|
|
|
|
|
|
|
case <-d.closeC:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Downloader) doDownload(initSync bool) (n int, err error) {
|
|
|
|
if initSync {
|
|
|
|
d.logger.Info().Uint64("current number", d.bc.CurrentBlock().NumberU64()).
|
|
|
|
Uint32("shard ID", d.bc.ShardID()).Msg("start long range sync")
|
|
|
|
|
|
|
|
n, err = d.doLongRangeSync()
|
|
|
|
} else {
|
|
|
|
d.logger.Info().Uint64("current number", d.bc.CurrentBlock().NumberU64()).
|
|
|
|
Uint32("shard ID", d.bc.ShardID()).Msg("start short range sync")
|
|
|
|
|
|
|
|
n, err = d.doShortRangeSync()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
pl := d.promLabels()
|
|
|
|
pl["error"] = err.Error()
|
|
|
|
numFailedDownloadCounterVec.With(pl).Inc()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Downloader) startSyncing() {
|
|
|
|
d.status.startSyncing()
|
|
|
|
if d.evtDownloadStartedSubscribed {
|
|
|
|
d.evtDownloadStarted.Send(struct{}{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Downloader) finishSyncing() {
|
|
|
|
d.status.finishSyncing()
|
|
|
|
if d.evtDownloadFinishedSubscribed {
|
|
|
|
d.evtDownloadFinished.Send(struct{}{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var emptySigVerifyErr *sigVerifyErr
|
|
|
|
|
|
|
|
type sigVerifyErr struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *sigVerifyErr) Error() string {
|
|
|
|
return fmt.Sprintf("[VerifyHeaderSignature] %v", e.err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyAndInsertBlocks(bc blockChain, blocks types.Blocks) (int, error) {
|
|
|
|
for i, block := range blocks {
|
|
|
|
if err := verifyAndInsertBlock(bc, block, blocks[i+1:]...); err != nil {
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(blocks), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyAndInsertBlock(bc blockChain, block *types.Block, nextBlocks ...*types.Block) error {
|
|
|
|
var (
|
|
|
|
sigBytes bls.SerializedSignature
|
|
|
|
bitmap []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if len(nextBlocks) > 0 {
|
|
|
|
// get commit sig from the next block
|
|
|
|
next := nextBlocks[0]
|
|
|
|
sigBytes = next.Header().LastCommitSignature()
|
|
|
|
bitmap = next.Header().LastCommitBitmap()
|
|
|
|
} else {
|
|
|
|
// get commit sig from current block
|
|
|
|
sigBytes, bitmap, err = chain.ParseCommitSigAndBitmap(block.GetCurrentCommitSig())
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "parse commitSigAndBitmap")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bc.Engine().VerifyHeaderSignature(bc, block.Header(), sigBytes, bitmap); err != nil {
|
|
|
|
return &sigVerifyErr{err}
|
|
|
|
}
|
|
|
|
if err := bc.Engine().VerifyHeader(bc, block.Header(), true); err != nil {
|
|
|
|
return errors.Wrap(err, "[VerifyHeader]")
|
|
|
|
}
|
|
|
|
if _, err := bc.InsertChain(types.Blocks{block}, false); err != nil {
|
|
|
|
return errors.Wrap(err, "[InsertChain]")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|