rename some config variables

This commit is contained in:
Jianrong 2021-11-03 19:57:22 +11:00
parent 1d48ed7d06
commit 521b703207
4 changed files with 34 additions and 35 deletions

View file

@ -36,7 +36,7 @@ func (t *CountdownTimer) StopTimer() {
// Reset will start the countdown timer if it's already stopped, or simply reset the countdown time back to the defual `duration`
func (t *CountdownTimer) Reset() {
if !t.getInitilisedValue() {
if !t.isInitilised() {
t.setInitilised(true)
go t.startTimer()
} else {
@ -76,7 +76,7 @@ func (t *CountdownTimer) setInitilised(value bool) {
t.initilised = value
}
func (t *CountdownTimer) getInitilisedValue() bool {
func (t *CountdownTimer) isInitilised() bool {
t.lock.Lock()
defer t.lock.Unlock()
return t.initilised

View file

@ -1,7 +1,6 @@
package countdown
import (
"fmt"
"testing"
"time"
@ -20,7 +19,7 @@ func TestCountdownWillCallback(t *testing.T) {
countdown.OnTimeoutFn = OnTimeoutFn
countdown.Reset()
<-called
fmt.Println("Times up, successfully called OnTimeoutFn")
t.Log("Times up, successfully called OnTimeoutFn")
}
func TestCountdownShouldReset(t *testing.T) {
@ -33,10 +32,10 @@ func TestCountdownShouldReset(t *testing.T) {
countdown := NewCountDown(5000 * time.Millisecond)
countdown.OnTimeoutFn = OnTimeoutFn
// Check countdown did not start
assert.False(t, countdown.getInitilisedValue())
assert.False(t, countdown.isInitilised())
countdown.Reset()
// Now the countdown should already started
assert.True(t, countdown.getInitilisedValue())
assert.True(t, countdown.isInitilised())
expectedCalledTime := time.Now().Add(9000 * time.Millisecond)
resetTimer := time.NewTimer(4000 * time.Millisecond)
@ -46,8 +45,8 @@ firstReset:
case <-called:
if time.Now().After(expectedCalledTime) {
// Make sure the countdown runs forever
assert.True(t, countdown.getInitilisedValue())
fmt.Println("Correctly reset the countdown once")
assert.True(t, countdown.isInitilised())
t.Log("Correctly reset the countdown once")
} else {
t.Fatalf("Countdown did not reset correctly first time")
}
@ -58,14 +57,14 @@ firstReset:
}
// Now the countdown is paused after calling the callback function, let's reset it again
assert.True(t, countdown.getInitilisedValue())
assert.True(t, countdown.isInitilised())
expectedTimeAfterReset := time.Now().Add(5000 * time.Millisecond)
countdown.Reset()
<-called
// Always initilised
assert.True(t, countdown.getInitilisedValue())
assert.True(t, countdown.isInitilised())
if time.Now().After(expectedTimeAfterReset) {
fmt.Println("Correctly reset the countdown second time")
t.Log("Correctly reset the countdown second time")
} else {
t.Fatalf("Countdown did not reset correctly second time")
}
@ -81,13 +80,13 @@ func TestCountdownShouldBeAbleToStop(t *testing.T) {
countdown := NewCountDown(5000 * time.Millisecond)
countdown.OnTimeoutFn = OnTimeoutFn
// Check countdown did not start
assert.False(t, countdown.getInitilisedValue())
assert.False(t, countdown.isInitilised())
countdown.Reset()
// Now the countdown should already started
assert.True(t, countdown.getInitilisedValue())
assert.True(t, countdown.isInitilised())
// Try manually stop the timer before it triggers the callback
stopTimer := time.NewTimer(4000 * time.Millisecond)
<-stopTimer.C
countdown.StopTimer()
assert.False(t, countdown.getInitilisedValue())
assert.False(t, countdown.isInitilised())
}

View file

@ -22,7 +22,7 @@ type XDPoS_v2 struct {
func New(config *params.XDPoSConfig, db ethdb.Database) *XDPoS_v2 {
// Setup Timer
duration := time.Duration(config.ConsensusV2Config.TimeoutWorkerDuration) * time.Millisecond
duration := time.Duration(config.V2.TimeoutWorkerDuration) * time.Millisecond
timer := countdown.NewCountDown(duration)
engine := &XDPoS_v2{
@ -41,7 +41,7 @@ func NewFaker(db ethdb.Database, config *params.XDPoSConfig) *XDPoS_v2 {
// Set any missing consensus parameters to their defaults
conf := config
// Setup Timer
duration := time.Duration(config.ConsensusV2Config.TimeoutWorkerDuration) * time.Millisecond
duration := time.Duration(config.V2.TimeoutWorkerDuration) * time.Millisecond
timer := countdown.NewCountDown(duration)
// Allocate the snapshot caches and create the engine

View file

@ -35,7 +35,7 @@ var (
)
var (
XDPoSV2Config = &ConsensusV2Config{
XDPoSV2Config = &V2{
TimeoutWorkerDuration: 50000,
}
@ -55,7 +55,7 @@ var (
RewardCheckpoint: 900,
Gap: 5,
FoudationWalletAddr: common.HexToAddress("0x0000000000000000000000000000000000000068"),
ConsensusV2Config: *XDPoSV2Config,
V2: *XDPoSV2Config,
},
}
@ -102,9 +102,9 @@ var (
ByzantiumBlock: big.NewInt(1035301),
ConstantinopleBlock: nil,
XDPoS: &XDPoSConfig{
Period: 15,
Epoch: 30000,
ConsensusV2Config: *XDPoSV2Config,
Period: 15,
Epoch: 30000,
V2: *XDPoSV2Config,
},
}
@ -123,11 +123,11 @@ var (
AllXDPoSProtocolChanges = &ChainConfig{big.NewInt(89), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, &XDPoSConfig{Period: 0, Epoch: 30000}}
AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}, nil}
TestXDPoSChanConfig = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, &XDPoSConfig{Period: 2, Epoch: 900, Reward: 250, RewardCheckpoint: 900, Gap: 890, FoudationWalletAddr: common.HexToAddress("0x0000000000000000000000000000000000000068"), ConsensusV2Config: *XDPoSV2Config}}
TestXDPoSChanConfig = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, &XDPoSConfig{Period: 2, Epoch: 900, Reward: 250, RewardCheckpoint: 900, Gap: 890, FoudationWalletAddr: common.HexToAddress("0x0000000000000000000000000000000000000068"), V2: *XDPoSV2Config}}
// XDPoS config in use for v1 engine only
TestXDPoSMockChainConfig = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, &XDPoSConfig{Epoch: 900, Gap: 450, SkipValidation: true, ConsensusV2Config: *XDPoSV2Config}}
TestXDPoSMockChainConfig = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, &XDPoSConfig{Epoch: 900, Gap: 450, SkipValidation: true, V2: *XDPoSV2Config}}
// XDPoS config with v2 engine after block 10
TestXDPoSMockChainConfigWithV2Engine = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, &XDPoSConfig{Epoch: 900, Gap: 450, SkipValidation: true, V2ConsensusBlockNumber: big.NewInt(10), ConsensusV2Config: *XDPoSV2Config}}
TestXDPoSMockChainConfigWithV2Engine = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, &XDPoSConfig{Epoch: 900, Gap: 450, SkipValidation: true, XDPoSV2Block: big.NewInt(10), V2: *XDPoSV2Config}}
TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, nil}
TestRules = TestChainConfig.Rules(new(big.Int))
@ -183,18 +183,18 @@ func (c *CliqueConfig) String() string {
// XDPoSConfig is the consensus engine configs for delegated-proof-of-stake based sealing.
type XDPoSConfig struct {
Period uint64 `json:"period"` // Number of seconds between blocks to enforce
Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint
Reward uint64 `json:"reward"` // Block reward - unit Ether
RewardCheckpoint uint64 `json:"rewardCheckpoint"` // Checkpoint block for calculate rewards.
Gap uint64 `json:"gap"` // Gap time preparing for the next epoch
FoudationWalletAddr common.Address `json:"foudationWalletAddr"` // Foundation Address Wallet
SkipValidation bool //Skip Block Validation for testing purpose
V2ConsensusBlockNumber *big.Int
ConsensusV2Config ConsensusV2Config
Period uint64 `json:"period"` // Number of seconds between blocks to enforce
Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint
Reward uint64 `json:"reward"` // Block reward - unit Ether
RewardCheckpoint uint64 `json:"rewardCheckpoint"` // Checkpoint block for calculate rewards.
Gap uint64 `json:"gap"` // Gap time preparing for the next epoch
FoudationWalletAddr common.Address `json:"foudationWalletAddr"` // Foundation Address Wallet
SkipValidation bool //Skip Block Validation for testing purpose
XDPoSV2Block *big.Int
V2 V2
}
type ConsensusV2Config struct {
type V2 struct {
TimeoutWorkerDuration int64 `json:"TimeoutWorkerDuration"` // Duration in ms
}
@ -208,7 +208,7 @@ ConsensusVersion will return the consensus version to use for the provided block
TODO: It's a dummy value for now until the 2.0 consensus engine is fully implemented.
*/
func (c *XDPoSConfig) BlockConsensusVersion(num *big.Int) string {
if c.V2ConsensusBlockNumber != nil && num.Cmp(c.V2ConsensusBlockNumber) > 0 {
if c.XDPoSV2Block != nil && num.Cmp(c.XDPoSV2Block) > 0 {
return ConsensusEngineVersion2
}
return ConsensusEngineVersion1