From 9cc95c05913b3643015e1b0a7cb7e46af0f330ec Mon Sep 17 00:00:00 2001 From: bing Date: Thu, 3 Aug 2017 12:01:51 +0800 Subject: [PATCH] Add gen block check time Add gen block check time to reduce the time of trigger off change view. Signed-off-by: wangbing wangbing@onchan.com --- common/config/config.go | 51 ++++++++++++++++++----------------- consensus/dbft/dbftService.go | 14 +++++++--- 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/common/config/config.go b/common/config/config.go index 8ca07778..81844910 100644 --- a/common/config/config.go +++ b/common/config/config.go @@ -15,31 +15,32 @@ const ( var Version string type Configuration struct { - Magic int64 `json:"Magic"` - Version int `json:"Version"` - SeedList []string `json:"SeedList"` - BookKeepers []string `json:"BookKeepers"` // The default book keepers' publickey - HttpRestPort int `json:"HttpRestPort"` - RestCertPath string `json:"RestCertPath"` - RestKeyPath string `json:"RestKeyPath"` - HttpWsPort int `json:"HttpWsPort"` - HttpJsonPort int `json:"HttpJsonPort"` - HttpLocalPort int `json:"HttpLocalPort"` - OauthServerUrl string `json:"OauthServerUrl"` - NoticeServerUrl string `json:"NoticeServerUrl"` - NodePort int `json:"NodePort"` - NodeType string `json:"NodeType"` - WebSocketPort int `json:"WebSocketPort"` - PrintLevel int `json:"PrintLevel"` - IsTLS bool `json:"IsTLS"` - CertPath string `json:"CertPath"` - KeyPath string `json:"KeyPath"` - CAPath string `json:"CAPath"` - GenBlockTime uint `json:"GenBlockTime"` - MultiCoreNum uint `json:"MultiCoreNum"` - EncryptAlg string `json:"EncryptAlg"` - MaxLogSize int64 `json:"MaxLogSize"` - MaxTxInBlock int `json:"MaxTransactionInBlock"` + Magic int64 `json:"Magic"` + Version int `json:"Version"` + SeedList []string `json:"SeedList"` + BookKeepers []string `json:"BookKeepers"` // The default book keepers' publickey + HttpRestPort int `json:"HttpRestPort"` + RestCertPath string `json:"RestCertPath"` + RestKeyPath string `json:"RestKeyPath"` + HttpWsPort int `json:"HttpWsPort"` + HttpJsonPort int `json:"HttpJsonPort"` + HttpLocalPort int `json:"HttpLocalPort"` + OauthServerUrl string `json:"OauthServerUrl"` + NoticeServerUrl string `json:"NoticeServerUrl"` + NodePort int `json:"NodePort"` + NodeType string `json:"NodeType"` + WebSocketPort int `json:"WebSocketPort"` + PrintLevel int `json:"PrintLevel"` + IsTLS bool `json:"IsTLS"` + CertPath string `json:"CertPath"` + KeyPath string `json:"KeyPath"` + CAPath string `json:"CAPath"` + GenBlockTime uint `json:"GenBlockTime"` + GenBlockCheckTime uint `json:"GenBlockCheckTime"` + MultiCoreNum uint `json:"MultiCoreNum"` + EncryptAlg string `json:"EncryptAlg"` + MaxLogSize int64 `json:"MaxLogSize"` + MaxTxInBlock int `json:"MaxTransactionInBlock"` } type ConfigFile struct { diff --git a/consensus/dbft/dbftService.go b/consensus/dbft/dbftService.go index 9a2480f9..e82d43ec 100644 --- a/consensus/dbft/dbftService.go +++ b/consensus/dbft/dbftService.go @@ -28,6 +28,7 @@ const ( ) var GenBlockTime = (MINGENBLOCKTIME * time.Second) +var GenBlockCheckTime = GenBlockTime type DbftService struct { context ConsensusContext @@ -260,7 +261,7 @@ func (ds *DbftService) InitializeConsensus(viewNum byte) error { ds.timeView = viewNum ds.timer.Stop() - ds.timer.Reset(GenBlockTime << (viewNum + 1)) + ds.timer.Reset((GenBlockTime + GenBlockCheckTime) << viewNum) } return nil } @@ -467,7 +468,7 @@ func (ds *DbftService) RequestChangeView() { ds.context.ViewNumber, ds.context.ExpectedView[ds.context.BookKeeperIndex], ds.context.GetStateDetail())) ds.timer.Stop() - ds.timer.Reset(GenBlockTime << (ds.context.ExpectedView[ds.context.BookKeeperIndex] + 1)) + ds.timer.Reset((GenBlockTime + GenBlockCheckTime) << (ds.context.ExpectedView[ds.context.BookKeeperIndex])) ds.SignAndRelay(ds.context.MakeChangeView()) ds.CheckExpectedView(ds.context.ExpectedView[ds.context.BookKeeperIndex]) @@ -507,6 +508,13 @@ func (ds *DbftService) Start() error { log.Warn("The Generate block time should be longer than 6 seconds, so set it to be 6.") } + genBlockCheckTime := config.Parameters.GenBlockCheckTime + if genBlockCheckTime <= 0 || genBlockCheckTime > config.Parameters.GenBlockTime { + GenBlockCheckTime = GenBlockTime + } else { + GenBlockCheckTime = time.Duration(config.Parameters.GenBlockCheckTime) * time.Second + } + ds.blockPersistCompletedSubscriber = ledger.DefaultLedger.Blockchain.BCEvents.Subscribe(events.EventBlockPersistCompleted, ds.BlockPersistCompleted) ds.newInventorySubscriber = ds.localNet.GetEvent("consensus").Subscribe(events.EventNewInventory, ds.LocalNodeNewInventory) @@ -560,7 +568,7 @@ func (ds *DbftService) Timeout() { payload := ds.context.MakePrepareRequest() ds.SignAndRelay(payload) ds.timer.Stop() - ds.timer.Reset(GenBlockTime << (ds.timeView + 1)) + ds.timer.Reset((GenBlockTime + GenBlockCheckTime) << ds.timeView) } else if (ds.context.State.HasFlag(Primary) && ds.context.State.HasFlag(RequestSent)) || ds.context.State.HasFlag(Backup) { ds.RequestChangeView() }