diff options
author | Mission Liao <mission.liao@dexon.org> | 2018-09-18 09:07:52 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-18 09:07:52 +0800 |
commit | 81cf96477127a2da8843d544802027c7061a9c06 (patch) | |
tree | 70c06aec5df92df1dd93b605cc7afdbd2b8b0347 /core/ticker.go | |
parent | 8a908e98279d7e80978cd412057eddd4a6bbf06c (diff) | |
download | dexon-consensus-81cf96477127a2da8843d544802027c7061a9c06.tar dexon-consensus-81cf96477127a2da8843d544802027c7061a9c06.tar.gz dexon-consensus-81cf96477127a2da8843d544802027c7061a9c06.tar.bz2 dexon-consensus-81cf96477127a2da8843d544802027c7061a9c06.tar.lz dexon-consensus-81cf96477127a2da8843d544802027c7061a9c06.tar.xz dexon-consensus-81cf96477127a2da8843d544802027c7061a9c06.tar.zst dexon-consensus-81cf96477127a2da8843d544802027c7061a9c06.zip |
core: remove ticker parameter from NewConsensus
- remove BlockProposingInterval, it's replaced
by lambda.
- remove ticker parameter of NewConsensus,
ticker would be derived from governance.
- leave a backdoor to hook the construction
of ticker.
- move 'Lambda' config from to consensus.
Diffstat (limited to 'core/ticker.go')
-rw-r--r-- | core/ticker.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/core/ticker.go b/core/ticker.go new file mode 100644 index 0000000..dffd676 --- /dev/null +++ b/core/ticker.go @@ -0,0 +1,58 @@ +// Copyright 2018 The dexon-consensus-core Authors +// This file is part of the dexon-consensus-core library. +// +// The dexon-consensus-core library is free software: you can redistribute it +// and/or modify it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation, either version 3 of the License, +// or (at your option) any later version. +// +// The dexon-consensus-core library is distributed in the hope that it will be +// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser +// General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the dexon-consensus-core library. If not, see +// <http://www.gnu.org/licenses/>. + +package core + +import "time" + +// defaultTicker is a wrapper to implement ticker interface based on +// time.Ticker. +type defaultTicker struct { + ticker *time.Ticker +} + +// newDefaultTicker constructs an defaultTicker instance by giving an interval. +func newDefaultTicker(lambda time.Duration) *defaultTicker { + return &defaultTicker{ticker: time.NewTicker(lambda)} +} + +// Tick implements Tick method of ticker interface. +func (t *defaultTicker) Tick() <-chan time.Time { + return t.ticker.C +} + +// Stop implements Stop method of ticker interface. +func (t *defaultTicker) Stop() { + t.ticker.Stop() +} + +// newTicker is a helper to setup a ticker by giving an Governance. If +// the governace object implements a ticker generator, a ticker from that +// generator would be returned, else constructs a default one. +func newTicker(gov Governance) (t Ticker) { + type tickerGenerator interface { + NewTicker() Ticker + } + + if gen, ok := gov.(tickerGenerator); ok { + t = gen.NewTicker() + } + if t == nil { + t = newDefaultTicker(gov.GetLambda()) + } + return +} |