Make fast_sync a command-line flag

This commit is contained in:
Jae Kwon 2015-03-26 00:35:16 -07:00
parent 7171823fc6
commit bd767c1fab
3 changed files with 42 additions and 28 deletions

View File

@ -35,6 +35,7 @@ type BlockchainReactor struct {
state *sm.State state *sm.State
store *BlockStore store *BlockStore
pool *BlockPool pool *BlockPool
sync bool
requestsCh chan BlockRequest requestsCh chan BlockRequest
timeoutsCh chan string timeoutsCh chan string
lastBlock *types.Block lastBlock *types.Block
@ -42,7 +43,7 @@ type BlockchainReactor struct {
running uint32 running uint32
} }
func NewBlockchainReactor(state *sm.State, store *BlockStore) *BlockchainReactor { func NewBlockchainReactor(state *sm.State, store *BlockStore, sync bool) *BlockchainReactor {
if state.LastBlockHeight != store.Height() { if state.LastBlockHeight != store.Height() {
panic(Fmt("state (%v) and store (%v) height mismatch", state.LastBlockHeight, store.Height())) panic(Fmt("state (%v) and store (%v) height mismatch", state.LastBlockHeight, store.Height()))
} }
@ -57,6 +58,7 @@ func NewBlockchainReactor(state *sm.State, store *BlockStore) *BlockchainReactor
state: state, state: state,
store: store, store: store,
pool: pool, pool: pool,
sync: sync,
requestsCh: requestsCh, requestsCh: requestsCh,
timeoutsCh: timeoutsCh, timeoutsCh: timeoutsCh,
quit: make(chan struct{}), quit: make(chan struct{}),
@ -71,7 +73,9 @@ func (bcR *BlockchainReactor) Start(sw *p2p.Switch) {
log.Info("Starting BlockchainReactor") log.Info("Starting BlockchainReactor")
bcR.sw = sw bcR.sw = sw
bcR.pool.Start() bcR.pool.Start()
go bcR.poolRoutine() if bcR.sync {
go bcR.poolRoutine()
}
} }
} }
@ -169,7 +173,7 @@ FOR_LOOP:
bcR.sw.StopPeerForError(peer, errors.New("BlockchainReactor Timeout")) bcR.sw.StopPeerForError(peer, errors.New("BlockchainReactor Timeout"))
} }
case _ = <-trySyncTicker.C: // chan time case _ = <-trySyncTicker.C: // chan time
var lastValidatedBlock *types.Block //var lastValidatedBlock *types.Block
SYNC_LOOP: SYNC_LOOP:
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
// See if there are any blocks to sync. // See if there are any blocks to sync.
@ -197,30 +201,32 @@ FOR_LOOP:
} }
bcR.store.SaveBlock(first, firstParts, second.Validation) bcR.store.SaveBlock(first, firstParts, second.Validation)
bcR.state.Save() bcR.state.Save()
lastValidatedBlock = first //lastValidatedBlock = first
} }
} }
// We're done syncing for now (will do again shortly) /*
// See if we want to stop syncing and turn on the // We're done syncing for now (will do again shortly)
// consensus reactor. // See if we want to stop syncing and turn on the
// TODO: use other heuristics too besides blocktime. // consensus reactor.
// It's not a security concern, as it only needs to happen // TODO: use other heuristics too besides blocktime.
// upon node sync, and there's also a second (slower) // It's not a security concern, as it only needs to happen
// method of syncing in the consensus reactor. // upon node sync, and there's also a second (slower)
if lastValidatedBlock != nil && time.Now().Sub(lastValidatedBlock.Time) < stopSyncingDurationMinutes*time.Minute { // method of syncing in the consensus reactor.
go func() {
log.Info("Stopping blockpool syncing, turning on consensus...") if lastValidatedBlock != nil && time.Now().Sub(lastValidatedBlock.Time) < stopSyncingDurationMinutes*time.Minute {
//bcR.sw.Reactor("BLOCKCHAIN").Stop() go func() {
trySyncTicker.Stop() // Just stop the block requests. Still serve blocks to others. log.Info("Stopping blockpool syncing, turning on consensus...")
conR := bcR.sw.Reactor("CONSENSUS") trySyncTicker.Stop() // Just stop the block requests. Still serve blocks to others.
conR.(stateResetter).ResetToState(bcR.state) conR := bcR.sw.Reactor("CONSENSUS")
conR.Start(bcR.sw) conR.(stateResetter).ResetToState(bcR.state)
for _, peer := range bcR.sw.Peers().List() { conR.Start(bcR.sw)
conR.AddPeer(peer) for _, peer := range bcR.sw.Peers().List() {
} conR.AddPeer(peer)
}() }
break FOR_LOOP }()
} break FOR_LOOP
}
*/
continue FOR_LOOP continue FOR_LOOP
case <-bcR.quit: case <-bcR.quit:
break FOR_LOOP break FOR_LOOP

View File

@ -104,6 +104,8 @@ func initDefaults(rootDir string) {
app.SetDefault("GenesisFile", rootDir+"/genesis.json") app.SetDefault("GenesisFile", rootDir+"/genesis.json")
app.SetDefault("AddrBookFile", rootDir+"/addrbook.json") app.SetDefault("AddrBookFile", rootDir+"/addrbook.json")
app.SetDefault("PrivValidatorfile", rootDir+"/priv_validator.json") app.SetDefault("PrivValidatorfile", rootDir+"/priv_validator.json")
app.SetDefault("FastSync", false)
} }
func Init(rootDir string) { func Init(rootDir string) {
@ -161,6 +163,7 @@ func ParseFlags(args []string) {
flags.BoolVar(&printHelp, "help", false, "Print this help message.") flags.BoolVar(&printHelp, "help", false, "Print this help message.")
flags.String("listen_addr", app.GetString("ListenAddr"), "Listen address. (0.0.0.0:0 means any interface, any port)") flags.String("listen_addr", app.GetString("ListenAddr"), "Listen address. (0.0.0.0:0 means any interface, any port)")
flags.String("seed_node", app.GetString("SeedNode"), "Address of seed node") flags.String("seed_node", app.GetString("SeedNode"), "Address of seed node")
flags.Bool("fast_sync", app.GetBool("FastSync"), "Fast blockchain syncing")
flags.String("rpc_http_listen_addr", app.GetString("RPC.HTTP.ListenAddr"), "RPC listen address. Port required") flags.String("rpc_http_listen_addr", app.GetString("RPC.HTTP.ListenAddr"), "RPC listen address. Port required")
flags.Parse(args) flags.Parse(args)
if printHelp { if printHelp {
@ -171,6 +174,7 @@ func ParseFlags(args []string) {
// Merge parsed flag values onto app. // Merge parsed flag values onto app.
app.BindPFlag("ListenAddr", flags.Lookup("listen_addr")) app.BindPFlag("ListenAddr", flags.Lookup("listen_addr"))
app.BindPFlag("SeedNode", flags.Lookup("seed_node")) app.BindPFlag("SeedNode", flags.Lookup("seed_node"))
app.BindPFlag("FastSync", flags.Lookup("fast_sync"))
app.BindPFlag("RPC.HTTP.ListenAddr", flags.Lookup("rpc_http_listen_addr")) app.BindPFlag("RPC.HTTP.ListenAddr", flags.Lookup("rpc_http_listen_addr"))
// Confused? // Confused?

View File

@ -55,7 +55,7 @@ func NewNode() *Node {
pexReactor := p2p.NewPEXReactor(book) pexReactor := p2p.NewPEXReactor(book)
// Get BlockchainReactor // Get BlockchainReactor
bcReactor := bc.NewBlockchainReactor(state, blockStore) bcReactor := bc.NewBlockchainReactor(state, blockStore, config.App().GetBool("FastSync"))
// Get MempoolReactor // Get MempoolReactor
mempool := mempl.NewMempool(state.Copy()) mempool := mempl.NewMempool(state.Copy())
@ -71,9 +71,13 @@ func NewNode() *Node {
sw := p2p.NewSwitch() sw := p2p.NewSwitch()
sw.SetNetwork(config.App().GetString("Network")) sw.SetNetwork(config.App().GetString("Network"))
sw.AddReactor("PEX", pexReactor).Start(sw) sw.AddReactor("PEX", pexReactor).Start(sw)
sw.AddReactor("BLOCKCHAIN", bcReactor).Start(sw)
sw.AddReactor("MEMPOOL", mempoolReactor).Start(sw) sw.AddReactor("MEMPOOL", mempoolReactor).Start(sw)
sw.AddReactor("CONSENSUS", consensusReactor) // Do not start yet. sw.AddReactor("BLOCKCHAIN", bcReactor).Start(sw)
if !config.App().GetBool("FastSync") {
sw.AddReactor("CONSENSUS", consensusReactor).Start(sw)
} else {
sw.AddReactor("CONSENSUS", consensusReactor)
}
return &Node{ return &Node{
sw: sw, sw: sw,