merge waitPeriod into minePeriod (#274)

* merge waitperiod into mindePeriod

* merge waitperiod into mindePeriod
This commit is contained in:
Liam 2023-05-31 23:40:50 +10:00 committed by GitHub
parent 767dfde1da
commit 2df16bbd37
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 39 deletions

View file

@ -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()

View file

@ -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
}

View file

@ -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()

View file

@ -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

View file

@ -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

View file

@ -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:

View file

@ -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