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` // 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() { func (t *CountdownTimer) Reset() {
if !t.getInitilisedValue() { if !t.isInitilised() {
t.setInitilised(true) t.setInitilised(true)
go t.startTimer() go t.startTimer()
} else { } else {
@ -76,7 +76,7 @@ func (t *CountdownTimer) setInitilised(value bool) {
t.initilised = value t.initilised = value
} }
func (t *CountdownTimer) getInitilisedValue() bool { func (t *CountdownTimer) isInitilised() bool {
t.lock.Lock() t.lock.Lock()
defer t.lock.Unlock() defer t.lock.Unlock()
return t.initilised return t.initilised

View file

@ -1,7 +1,6 @@
package countdown package countdown
import ( import (
"fmt"
"testing" "testing"
"time" "time"
@ -20,7 +19,7 @@ func TestCountdownWillCallback(t *testing.T) {
countdown.OnTimeoutFn = OnTimeoutFn countdown.OnTimeoutFn = OnTimeoutFn
countdown.Reset() countdown.Reset()
<-called <-called
fmt.Println("Times up, successfully called OnTimeoutFn") t.Log("Times up, successfully called OnTimeoutFn")
} }
func TestCountdownShouldReset(t *testing.T) { func TestCountdownShouldReset(t *testing.T) {
@ -33,10 +32,10 @@ func TestCountdownShouldReset(t *testing.T) {
countdown := NewCountDown(5000 * time.Millisecond) countdown := NewCountDown(5000 * time.Millisecond)
countdown.OnTimeoutFn = OnTimeoutFn countdown.OnTimeoutFn = OnTimeoutFn
// Check countdown did not start // Check countdown did not start
assert.False(t, countdown.getInitilisedValue()) assert.False(t, countdown.isInitilised())
countdown.Reset() countdown.Reset()
// Now the countdown should already started // Now the countdown should already started
assert.True(t, countdown.getInitilisedValue()) assert.True(t, countdown.isInitilised())
expectedCalledTime := time.Now().Add(9000 * time.Millisecond) expectedCalledTime := time.Now().Add(9000 * time.Millisecond)
resetTimer := time.NewTimer(4000 * time.Millisecond) resetTimer := time.NewTimer(4000 * time.Millisecond)
@ -46,8 +45,8 @@ firstReset:
case <-called: case <-called:
if time.Now().After(expectedCalledTime) { if time.Now().After(expectedCalledTime) {
// Make sure the countdown runs forever // Make sure the countdown runs forever
assert.True(t, countdown.getInitilisedValue()) assert.True(t, countdown.isInitilised())
fmt.Println("Correctly reset the countdown once") t.Log("Correctly reset the countdown once")
} else { } else {
t.Fatalf("Countdown did not reset correctly first time") 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 // 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) expectedTimeAfterReset := time.Now().Add(5000 * time.Millisecond)
countdown.Reset() countdown.Reset()
<-called <-called
// Always initilised // Always initilised
assert.True(t, countdown.getInitilisedValue()) assert.True(t, countdown.isInitilised())
if time.Now().After(expectedTimeAfterReset) { if time.Now().After(expectedTimeAfterReset) {
fmt.Println("Correctly reset the countdown second time") t.Log("Correctly reset the countdown second time")
} else { } else {
t.Fatalf("Countdown did not reset correctly second time") t.Fatalf("Countdown did not reset correctly second time")
} }
@ -81,13 +80,13 @@ func TestCountdownShouldBeAbleToStop(t *testing.T) {
countdown := NewCountDown(5000 * time.Millisecond) countdown := NewCountDown(5000 * time.Millisecond)
countdown.OnTimeoutFn = OnTimeoutFn countdown.OnTimeoutFn = OnTimeoutFn
// Check countdown did not start // Check countdown did not start
assert.False(t, countdown.getInitilisedValue()) assert.False(t, countdown.isInitilised())
countdown.Reset() countdown.Reset()
// Now the countdown should already started // 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 // Try manually stop the timer before it triggers the callback
stopTimer := time.NewTimer(4000 * time.Millisecond) stopTimer := time.NewTimer(4000 * time.Millisecond)
<-stopTimer.C <-stopTimer.C
countdown.StopTimer() 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 { func New(config *params.XDPoSConfig, db ethdb.Database) *XDPoS_v2 {
// Setup Timer // Setup Timer
duration := time.Duration(config.ConsensusV2Config.TimeoutWorkerDuration) * time.Millisecond duration := time.Duration(config.V2.TimeoutWorkerDuration) * time.Millisecond
timer := countdown.NewCountDown(duration) timer := countdown.NewCountDown(duration)
engine := &XDPoS_v2{ 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 // Set any missing consensus parameters to their defaults
conf := config conf := config
// Setup Timer // Setup Timer
duration := time.Duration(config.ConsensusV2Config.TimeoutWorkerDuration) * time.Millisecond duration := time.Duration(config.V2.TimeoutWorkerDuration) * time.Millisecond
timer := countdown.NewCountDown(duration) timer := countdown.NewCountDown(duration)
// Allocate the snapshot caches and create the engine // Allocate the snapshot caches and create the engine

View file

@ -35,7 +35,7 @@ var (
) )
var ( var (
XDPoSV2Config = &ConsensusV2Config{ XDPoSV2Config = &V2{
TimeoutWorkerDuration: 50000, TimeoutWorkerDuration: 50000,
} }
@ -55,7 +55,7 @@ var (
RewardCheckpoint: 900, RewardCheckpoint: 900,
Gap: 5, Gap: 5,
FoudationWalletAddr: common.HexToAddress("0x0000000000000000000000000000000000000068"), FoudationWalletAddr: common.HexToAddress("0x0000000000000000000000000000000000000068"),
ConsensusV2Config: *XDPoSV2Config, V2: *XDPoSV2Config,
}, },
} }
@ -102,9 +102,9 @@ var (
ByzantiumBlock: big.NewInt(1035301), ByzantiumBlock: big.NewInt(1035301),
ConstantinopleBlock: nil, ConstantinopleBlock: nil,
XDPoS: &XDPoSConfig{ XDPoS: &XDPoSConfig{
Period: 15, Period: 15,
Epoch: 30000, Epoch: 30000,
ConsensusV2Config: *XDPoSV2Config, 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}} 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} 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 // 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 // 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} 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)) 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. // XDPoSConfig is the consensus engine configs for delegated-proof-of-stake based sealing.
type XDPoSConfig struct { type XDPoSConfig struct {
Period uint64 `json:"period"` // Number of seconds between blocks to enforce Period uint64 `json:"period"` // Number of seconds between blocks to enforce
Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint
Reward uint64 `json:"reward"` // Block reward - unit Ether Reward uint64 `json:"reward"` // Block reward - unit Ether
RewardCheckpoint uint64 `json:"rewardCheckpoint"` // Checkpoint block for calculate rewards. RewardCheckpoint uint64 `json:"rewardCheckpoint"` // Checkpoint block for calculate rewards.
Gap uint64 `json:"gap"` // Gap time preparing for the next epoch Gap uint64 `json:"gap"` // Gap time preparing for the next epoch
FoudationWalletAddr common.Address `json:"foudationWalletAddr"` // Foundation Address Wallet FoudationWalletAddr common.Address `json:"foudationWalletAddr"` // Foundation Address Wallet
SkipValidation bool //Skip Block Validation for testing purpose SkipValidation bool //Skip Block Validation for testing purpose
V2ConsensusBlockNumber *big.Int XDPoSV2Block *big.Int
ConsensusV2Config ConsensusV2Config V2 V2
} }
type ConsensusV2Config struct { type V2 struct {
TimeoutWorkerDuration int64 `json:"TimeoutWorkerDuration"` // Duration in ms 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. 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 { 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 ConsensusEngineVersion2
} }
return ConsensusEngineVersion1 return ConsensusEngineVersion1