From 2df16bbd371b5e85f85d46210157664c4292809b Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 31 May 2023 23:40:50 +1000 Subject: [PATCH] merge waitPeriod into minePeriod (#274) * merge waitperiod into mindePeriod * merge waitperiod into mindePeriod --- cmd/puppeth/wizard_genesis.go | 1 - consensus/XDPoS/XDPoS.go | 14 +++++++------- consensus/XDPoS/engines/engine_v1/engine.go | 12 +++++++----- consensus/XDPoS/engines/engine_v2/engine.go | 12 ++++++------ .../tests/engine_v2_tests/initial_test.go | 4 ++-- miner/worker.go | 18 +++++++++--------- params/config.go | 10 +--------- 7 files changed, 32 insertions(+), 39 deletions(-) diff --git a/cmd/puppeth/wizard_genesis.go b/cmd/puppeth/wizard_genesis.go index de8d243853..c312e91a8a 100644 --- a/cmd/puppeth/wizard_genesis.go +++ b/cmd/puppeth/wizard_genesis.go @@ -125,7 +125,6 @@ func (w *wizard) makeGenesis() { fmt.Println() fmt.Println("How many seconds should blocks take? (default = 2)") genesis.Config.XDPoS.Period = uint64(w.readDefaultInt(2)) - genesis.Config.XDPoS.V2.CurrentConfig.WaitPeriod = int(genesis.Config.XDPoS.Period) genesis.Config.XDPoS.V2.CurrentConfig.MinePeriod = int(genesis.Config.XDPoS.Period) fmt.Println() diff --git a/consensus/XDPoS/XDPoS.go b/consensus/XDPoS/XDPoS.go index b6ff00490c..118aacbed7 100644 --- a/consensus/XDPoS/XDPoS.go +++ b/consensus/XDPoS/XDPoS.go @@ -60,7 +60,7 @@ type XDPoS struct { signingTxsCache *lru.Cache // Share Channel - WaitPeriodCh chan int // Miner wait Period Channel + MinePeriodCh chan int // Miner wait Period Channel // Trading and lending service GetXDCXService func() utils.TradingService @@ -97,7 +97,7 @@ func New(chainConfig *params.ChainConfig, db ethdb.Database) *XDPoS { log.Info("xdc config loading", "config", config) - waitPeriodCh := make(chan int) + minePeriodCh := make(chan int) // Allocate the snapshot caches and create the engine signingTxsCache, _ := lru.New(utils.BlockSignersCacheLimit) @@ -106,11 +106,11 @@ func New(chainConfig *params.ChainConfig, db ethdb.Database) *XDPoS { config: config, db: db, - WaitPeriodCh: waitPeriodCh, + MinePeriodCh: minePeriodCh, signingTxsCache: signingTxsCache, EngineV1: engine_v1.New(chainConfig, db), - EngineV2: engine_v2.New(chainConfig, db, waitPeriodCh), + EngineV2: engine_v2.New(chainConfig, db, minePeriodCh), } } @@ -124,7 +124,7 @@ func NewFaker(db ethdb.Database, chainConfig *params.ChainConfig) *XDPoS { conf = chainConfig.XDPoS } - waitPeriodCh := make(chan int) + minePeriodCh := make(chan int) // Allocate the snapshot caches and create the engine signingTxsCache, _ := lru.New(utils.BlockSignersCacheLimit) @@ -133,14 +133,14 @@ func NewFaker(db ethdb.Database, chainConfig *params.ChainConfig) *XDPoS { config: conf, db: db, - WaitPeriodCh: waitPeriodCh, + MinePeriodCh: minePeriodCh, GetXDCXService: func() utils.TradingService { return nil }, GetLendingService: func() utils.LendingService { return nil }, signingTxsCache: signingTxsCache, EngineV1: engine_v1.NewFaker(db, chainConfig), - EngineV2: engine_v2.New(chainConfig, db, waitPeriodCh), + EngineV2: engine_v2.New(chainConfig, db, minePeriodCh), } return fakeEngine } diff --git a/consensus/XDPoS/engines/engine_v1/engine.go b/consensus/XDPoS/engines/engine_v1/engine.go index 98b38607f0..3f3c7d559a 100644 --- a/consensus/XDPoS/engines/engine_v1/engine.go +++ b/consensus/XDPoS/engines/engine_v1/engine.go @@ -30,9 +30,9 @@ import ( const ( // timeout waiting for M1 - waitPeriod = 10 + minePeriod = 10 // timeout for checkpoint. - waitPeriodCheckpoint = 20 + minePeriodCheckpoint = 20 ) // XDPoS is the delegated-proof-of-stake consensus engine proposed to support the @@ -62,7 +62,9 @@ type XDPoS_v1 struct { HookGetSignersFromContract func(blockHash common.Hash) ([]common.Address, error) } -/* V1 Block +/* + V1 Block + SignerFn is a signer callback function to request a hash to be signed by a backing account. type SignerFn func(accounts.Account, []byte) ([]byte, error) @@ -409,11 +411,11 @@ func (x *XDPoS_v1) YourTurn(chain consensus.ChainReader, parent *types.Header, s return false, nil } h := utils.Hop(len, preIndex, curIndex) - gap := waitPeriod * int64(h) + gap := minePeriod * int64(h) // Check nearest checkpoint block in hop range. nearest := x.config.Epoch - (parent.Number.Uint64() % x.config.Epoch) if uint64(h) >= nearest { - gap = waitPeriodCheckpoint * int64(h) + gap = minePeriodCheckpoint * int64(h) } log.Info("Distance from the parent block", "seconds", gap, "hops", h) waitedTime := time.Now().Unix() - parent.Time.Int64() diff --git a/consensus/XDPoS/engines/engine_v2/engine.go b/consensus/XDPoS/engines/engine_v2/engine.go index 0ff6aa9c18..8366e9a899 100644 --- a/consensus/XDPoS/engines/engine_v2/engine.go +++ b/consensus/XDPoS/engines/engine_v2/engine.go @@ -42,7 +42,7 @@ type XDPoS_v2 struct { signLock sync.RWMutex // Protects the signer fields BroadcastCh chan interface{} - waitPeriodCh chan int + minePeriodCh chan int timeoutWorker *countdown.CountdownTimer // Timer to generate broadcast timeout msg if threashold reached timeoutCount int // number of timeout being sent @@ -66,7 +66,7 @@ type XDPoS_v2 struct { votePoolCollectionTime time.Time } -func New(chainConfig *params.ChainConfig, db ethdb.Database, waitPeriodCh chan int) *XDPoS_v2 { +func New(chainConfig *params.ChainConfig, db ethdb.Database, minePeriodCh chan int) *XDPoS_v2 { config := chainConfig.XDPoS // Setup timeoutTimer duration := time.Duration(config.V2.CurrentConfig.TimeoutPeriod) * time.Second @@ -93,7 +93,7 @@ func New(chainConfig *params.ChainConfig, db ethdb.Database, waitPeriodCh chan i epochSwitches: epochSwitches, timeoutWorker: timeoutTimer, BroadcastCh: make(chan interface{}), - waitPeriodCh: waitPeriodCh, + minePeriodCh: minePeriodCh, timeoutPool: timeoutPool, votePool: votePool, @@ -139,7 +139,7 @@ func (x *XDPoS_v2) UpdateParams(header *types.Header) { // avoid deadlock go func() { - x.waitPeriodCh <- x.config.V2.CurrentConfig.WaitPeriod + x.minePeriodCh <- x.config.V2.CurrentConfig.MinePeriod }() } @@ -229,10 +229,10 @@ func (x *XDPoS_v2) initial(chain consensus.ChainReader, header *types.Header) er } // Initial timeout - log.Info("[initial] miner wait period", "period", x.config.V2.CurrentConfig.WaitPeriod) + log.Info("[initial] miner wait period", "period", x.config.V2.CurrentConfig.MinePeriod) // avoid deadlock go func() { - x.waitPeriodCh <- x.config.V2.CurrentConfig.WaitPeriod + x.minePeriodCh <- x.config.V2.CurrentConfig.MinePeriod }() // Kick-off the countdown timer diff --git a/consensus/tests/engine_v2_tests/initial_test.go b/consensus/tests/engine_v2_tests/initial_test.go index 438fdf8b76..70d3f9e95b 100644 --- a/consensus/tests/engine_v2_tests/initial_test.go +++ b/consensus/tests/engine_v2_tests/initial_test.go @@ -43,8 +43,8 @@ func TestInitialFirstV2Block(t *testing.T) { assert.Equal(t, uint64(450), snap.Number) // Test Running channels - waitPeriod := <-adaptor.WaitPeriodCh - assert.Equal(t, params.TestXDPoSMockChainConfig.XDPoS.V2.CurrentConfig.WaitPeriod, waitPeriod) + minePeriod := <-adaptor.MinePeriodCh + assert.Equal(t, params.TestXDPoSMockChainConfig.XDPoS.V2.CurrentConfig.MinePeriod, minePeriod) t.Logf("Waiting %d secs for timeout to happen", params.TestXDPoSMockChainConfig.XDPoS.V2.CurrentConfig.TimeoutPeriod) timeoutMsg := <-adaptor.EngineV2.BroadcastCh diff --git a/miner/worker.go b/miner/worker.go index 2acbc6a869..76b5562b00 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -267,11 +267,11 @@ func (self *worker) update() { defer self.chainSideSub.Unsubscribe() // timeout waiting for v1 inital value - waitPeriod := 2 - WaitPeriodCh := self.engine.(*XDPoS.XDPoS).WaitPeriodCh - defer close(WaitPeriodCh) + minePeriod := 2 + MinePeriodCh := self.engine.(*XDPoS.XDPoS).MinePeriodCh + defer close(MinePeriodCh) - timeout := time.NewTimer(time.Duration(waitPeriod) * time.Second) + timeout := time.NewTimer(time.Duration(minePeriod) * time.Second) c := make(chan struct{}) finish := make(chan struct{}) defer close(finish) @@ -290,21 +290,21 @@ func (self *worker) update() { for { // A real event arrived, process interesting content select { - case v := <-WaitPeriodCh: + case v := <-MinePeriodCh: log.Info("[worker] update wait period", "period", v) - waitPeriod = v - timeout.Reset(time.Duration(waitPeriod) * time.Second) + minePeriod = v + timeout.Reset(time.Duration(minePeriod) * time.Second) case <-c: if atomic.LoadInt32(&self.mining) == 1 { self.commitNewWork() } - timeout.Reset(time.Duration(waitPeriod) * time.Second) + timeout.Reset(time.Duration(minePeriod) * time.Second) // Handle ChainHeadEvent case <-self.chainHeadCh: self.commitNewWork() - timeout.Reset(time.Duration(waitPeriod) * time.Second) + timeout.Reset(time.Duration(minePeriod) * time.Second) // Handle ChainSideEvent case ev := <-self.chainSideCh: diff --git a/params/config.go b/params/config.go index 583828643a..9cc45024e4 100644 --- a/params/config.go +++ b/params/config.go @@ -45,7 +45,6 @@ var ( CertThreshold: 73, // based on masternode is 108 TimeoutSyncThreshold: 3, TimeoutPeriod: 60, - WaitPeriod: 10, MinePeriod: 10, }, 9999999999: { @@ -53,7 +52,6 @@ var ( CertThreshold: 73, // based on masternode is 108 TimeoutSyncThreshold: 3, TimeoutPeriod: 60, - WaitPeriod: 10, MinePeriod: 10, }, } @@ -64,7 +62,6 @@ var ( CertThreshold: 3, TimeoutSyncThreshold: 2, TimeoutPeriod: 4, - WaitPeriod: 1, MinePeriod: 2, }, } @@ -75,7 +72,6 @@ var ( CertThreshold: 73, // based on masternode is 108 TimeoutSyncThreshold: 5, TimeoutPeriod: 10, - WaitPeriod: 2, MinePeriod: 2, }, } @@ -86,7 +82,6 @@ var ( CertThreshold: 3, TimeoutSyncThreshold: 2, TimeoutPeriod: 4, - WaitPeriod: 1, MinePeriod: 2, }, 10: { @@ -94,7 +89,6 @@ var ( CertThreshold: 5, TimeoutSyncThreshold: 2, TimeoutPeriod: 4, - WaitPeriod: 2, MinePeriod: 3, }, 899: { @@ -102,8 +96,7 @@ var ( CertThreshold: 5, TimeoutSyncThreshold: 4, TimeoutPeriod: 5, - WaitPeriod: 2, - MinePeriod: 3, + MinePeriod: 2, }, } @@ -341,7 +334,6 @@ type V2 struct { type V2Config struct { SwitchRound uint64 `json:"switchRound"` // v1 to v2 switch block number - WaitPeriod int `json:"waitPeriod"` // Miner wait period to check mine event MinePeriod int `json:"minePeriod"` // Miner mine period to mine a block TimeoutSyncThreshold int `json:"timeoutSyncThreshold"` // send syncInfo after number of timeout TimeoutPeriod int `json:"timeoutPeriod"` // Duration in ms