Skip to content

Commit

Permalink
Obsolete block time flag from server command (#1365)
Browse files Browse the repository at this point in the history
* refactor(server): remove block time configuration from raw config and server params (#1363)

The block time configuration was removed from the raw config and server params. The initBlockTime function was also removed from the server params.

* Resolve block time from consensus engine parameters

* Provide zero value for block time for dummy and dev consensuses

* Move DefaultBlockTime to IBFT E2E tests

* Make sure that block time is not less than 1 second

---------

Co-authored-by: Flavor Town <117117716+flavor-town@users.noreply.github.com>
  • Loading branch information
Stefan-Ethernal and flavor-town committed Apr 7, 2023
1 parent 3765811 commit 5d4c9d9
Show file tree
Hide file tree
Showing 19 changed files with 167 additions and 49 deletions.
1 change: 1 addition & 0 deletions command/genesis/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ func (p *genesisParams) initIBFTEngineMap(ibftType fork.IBFTType) {
string(server.IBFTConsensus): map[string]interface{}{
fork.KeyType: ibftType,
fork.KeyValidatorType: p.ibftValidatorType,
fork.KeyBlockTime: p.blockTime,
ibft.KeyEpochSize: p.epochSize,
},
}
Expand Down
3 changes: 2 additions & 1 deletion command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi/artifact"
"github.com/0xPolygon/polygon-edge/helper/common"

"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/command"
Expand Down Expand Up @@ -88,7 +89,7 @@ func (p *genesisParams) generatePolyBftChainConfig(o command.OutputFormatter) er

polyBftConfig := &polybft.PolyBFTConfig{
InitialValidatorSet: manifest.GenesisValidators,
BlockTime: p.blockTime,
BlockTime: common.Duration{Duration: p.blockTime},
EpochSize: p.epochSize,
SprintSize: p.sprintSize,
EpochReward: p.epochReward,
Expand Down
1 change: 1 addition & 0 deletions command/ibft/switch/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ func appendIBFTForks(
Type: ibftType,
ValidatorType: validatorType,
From: common.JSONNumber{Value: from},
BlockTime: lastFork.BlockTime,
}

switch ibftType {
Expand Down
5 changes: 0 additions & 5 deletions command/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type Config struct {
TxPool *TxPool `json:"tx_pool" yaml:"tx_pool"`
LogLevel string `json:"log_level" yaml:"log_level"`
RestoreFile string `json:"restore_file" yaml:"restore_file"`
BlockTime uint64 `json:"block_time_s" yaml:"block_time_s"`
Headers *Headers `json:"headers" yaml:"headers"`
LogFilePath string `json:"log_to" yaml:"log_to"`
JSONRPCBatchRequestLimit uint64 `json:"json_rpc_batch_request_limit" yaml:"json_rpc_batch_request_limit"`
Expand Down Expand Up @@ -65,9 +64,6 @@ type Headers struct {
}

const (
// DefaultBlockTime minimum block generation time in seconds
DefaultBlockTime uint64 = 2

// BlockTimeMultiplierForTimeout Multiplier to get IBFT timeout from block time
// timeout is calculated when IBFT timeout is not specified
BlockTimeMultiplierForTimeout uint64 = 5
Expand Down Expand Up @@ -111,7 +107,6 @@ func DefaultConfig() *Config {
},
LogLevel: "INFO",
RestoreFile: "",
BlockTime: DefaultBlockTime,
Headers: &Headers{
AccessControlAllowOrigins: []string{"*"},
},
Expand Down
13 changes: 0 additions & 13 deletions command/server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
)

var (
errInvalidBlockTime = errors.New("invalid block time specified")
errDataDirectoryUndefined = errors.New("data directory not defined")
)

Expand Down Expand Up @@ -50,10 +49,6 @@ func (p *serverParams) initRawParams() error {
return err
}

if err := p.initBlockTime(); err != nil {
return err
}

if p.isDevMode {
p.initDevMode()
}
Expand All @@ -66,14 +61,6 @@ func (p *serverParams) initRawParams() error {
return p.initAddresses()
}

func (p *serverParams) initBlockTime() error {
if p.rawConfig.BlockTime < 1 {
return errInvalidBlockTime
}

return nil
}

func (p *serverParams) initDataDirLocation() error {
if p.rawConfig.DataDir == "" {
return errDataDirectoryUndefined
Expand Down
2 changes: 0 additions & 2 deletions command/server/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const (
blockGasTargetFlag = "block-gas-target"
secretsConfigFlag = "secrets-config"
restoreFlag = "restore"
blockTimeFlag = "block-time"
devIntervalFlag = "dev-interval"
devFlag = "dev"
corsOriginFlag = "access-control-allow-origins"
Expand Down Expand Up @@ -179,7 +178,6 @@ func (p *serverParams) generateConfig() *server.Config {
MaxAccountEnqueued: p.rawConfig.TxPool.MaxAccountEnqueued,
SecretsManager: p.secretsConfig,
RestoreFile: p.getRestoreFilePath(),
BlockTime: p.rawConfig.BlockTime,
LogLevel: hclog.LevelFromString(p.rawConfig.LogLevel),
JSONLogFormat: p.rawConfig.JSONLogFormat,
LogFilePath: p.logFileLocation,
Expand Down
7 changes: 0 additions & 7 deletions command/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,6 @@ func setFlags(cmd *cobra.Command) {
"maximum number of enqueued transactions per account",
)

cmd.Flags().Uint64Var(
&params.rawConfig.BlockTime,
blockTimeFlag,
defaultConfig.BlockTime,
"minimum block time in seconds (at least 1s)",
)

cmd.Flags().StringArrayVar(
&params.corsAllowedOrigins,
corsOriginFlag,
Expand Down
26 changes: 21 additions & 5 deletions consensus/ibft/fork/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ const (
KeyType = "type"
KeyTypes = "types"
KeyValidatorType = "validator_type"
KeyBlockTime = "blockTime"
)

var (
ErrUndefinedIBFTConfig = errors.New("IBFT config is not defined")
errInvalidBlockTime = errors.New("invalid block time provided")
)

// IBFT Fork represents setting in params.engine.ibft of genesis.json
Expand All @@ -26,6 +28,7 @@ type IBFTFork struct {
Deployment *common.JSONNumber `json:"deployment,omitempty"`
From common.JSONNumber `json:"from"`
To *common.JSONNumber `json:"to,omitempty"`
BlockTime *common.Duration `json:"blockTime,omitempty"`

// PoA
Validators validators.Validators `json:"validators,omitempty"`
Expand All @@ -42,6 +45,7 @@ func (f *IBFTFork) UnmarshalJSON(data []byte) error {
Deployment *common.JSONNumber `json:"deployment,omitempty"`
From common.JSONNumber `json:"from"`
To *common.JSONNumber `json:"to,omitempty"`
BlockTime *common.Duration `json:"blockTime,omitempty"`
Validators interface{} `json:"validators,omitempty"`
MaxValidatorCount *common.JSONNumber `json:"maxValidatorCount,omitempty"`
MinValidatorCount *common.JSONNumber `json:"minValidatorCount,omitempty"`
Expand All @@ -55,6 +59,7 @@ func (f *IBFTFork) UnmarshalJSON(data []byte) error {
f.Deployment = raw.Deployment
f.From = raw.From
f.To = raw.To
f.BlockTime = raw.BlockTime
f.MaxValidatorCount = raw.MaxValidatorCount
f.MinValidatorCount = raw.MinValidatorCount

Expand All @@ -74,11 +79,7 @@ func (f *IBFTFork) UnmarshalJSON(data []byte) error {
return err
}

if err := json.Unmarshal(validatorsBytes, f.Validators); err != nil {
return err
}

return nil
return json.Unmarshal(validatorsBytes, f.Validators)
}

// GetIBFTForks returns IBFT fork configurations from chain config
Expand All @@ -98,13 +99,28 @@ func GetIBFTForks(ibftConfig map[string]interface{}) (IBFTForks, error) {
}
}

var blockTime *common.Duration = nil

blockTimeGeneric, ok := ibftConfig[KeyBlockTime]
if ok {
blockTimeRaw, err := json.Marshal(blockTimeGeneric)
if err != nil {
return nil, errInvalidBlockTime
}

if err := json.Unmarshal(blockTimeRaw, &blockTime); err != nil {
return nil, errInvalidBlockTime
}
}

return IBFTForks{
{
Type: typ,
Deployment: nil,
ValidatorType: validatorType,
From: common.JSONNumber{Value: 0},
To: nil,
BlockTime: blockTime,
},
}, nil
}
Expand Down
6 changes: 5 additions & 1 deletion consensus/ibft/fork/fork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"testing"
"time"

"github.com/0xPolygon/polygon-edge/helper/common"
testHelper "github.com/0xPolygon/polygon-edge/helper/tests"
Expand Down Expand Up @@ -62,6 +63,7 @@ func TestIBFTForkUnmarshalJSON(t *testing.T) {
data: fmt.Sprintf(`{
"type": "%s",
"validator_type": "%s",
"blockTime": %d,
"validators": [
{
"Address": "%s"
Expand All @@ -72,8 +74,9 @@ func TestIBFTForkUnmarshalJSON(t *testing.T) {
],
"from": %d,
"to": %d
}`,
}`,
PoA, validators.ECDSAValidatorType,
3000000000,
types.StringToAddress("1"), types.StringToAddress("2"),
16, 20,
),
Expand All @@ -86,6 +89,7 @@ func TestIBFTForkUnmarshalJSON(t *testing.T) {
validators.NewECDSAValidator(types.StringToAddress("1")),
validators.NewECDSAValidator(types.StringToAddress("2")),
),
BlockTime: &common.Duration{Duration: 3 * time.Second},
MaxValidatorCount: nil,
MinValidatorCount: nil,
},
Expand Down
2 changes: 1 addition & 1 deletion consensus/polybft/consensus_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (c *consensusRuntime) FSM() error {
parent,
types.Address(c.config.Key.Address()),
c.config.txPool,
c.config.PolyBFTConfig.BlockTime,
c.config.PolyBFTConfig.BlockTime.Duration,
c.logger,
)

Expand Down
3 changes: 2 additions & 1 deletion consensus/polybft/consensus_runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
bls "github.com/0xPolygon/polygon-edge/consensus/polybft/signer"
"github.com/0xPolygon/polygon-edge/consensus/polybft/wallet"
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/helper/common"
"github.com/0xPolygon/polygon-edge/types"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -447,7 +448,7 @@ func Test_NewConsensusRuntime(t *testing.T) {
},
EpochSize: 10,
SprintSize: 10,
BlockTime: 2 * time.Second,
BlockTime: common.Duration{Duration: 2 * time.Second},
}

validators := newTestValidators(t, 3).getPublicIdentities()
Expand Down
3 changes: 1 addition & 2 deletions consensus/polybft/polybft_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"math/big"
"os"
"path/filepath"
"time"

"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
Expand All @@ -34,7 +33,7 @@ type PolyBFTConfig struct {
SprintSize uint64 `json:"sprintSize"`

// BlockTime is target frequency of blocks production
BlockTime time.Duration `json:"blockTime"`
BlockTime common.Duration `json:"blockTime"`

// Governance is the initial governance address
Governance types.Address `json:"governance"`
Expand Down
2 changes: 0 additions & 2 deletions consensus/polybft/sc_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"math/big"
"strconv"
"testing"
"time"

"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/consensus/polybft/bitmap"
Expand Down Expand Up @@ -278,7 +277,6 @@ func TestIntegration_CommitEpoch(t *testing.T) {

polyBFTConfig := PolyBFTConfig{
InitialValidatorSet: initValidators,
BlockTime: 2 * time.Second,
EpochSize: 24 * 60 * 60 / 2,
SprintSize: 5,
EpochReward: reward,
Expand Down
11 changes: 7 additions & 4 deletions e2e/framework/testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ func (t *TestServer) GenerateGenesis() error {
args = append(args, "--premine", acct.Addr.String()+":0x"+acct.Balance.Text(16))
}

// provide block time flag
// (e2e framework expects BlockTime parameter to be provided in seconds)
if t.Config.BlockTime != 0 {
args = append(args, "--block-time",
(time.Duration(t.Config.BlockTime) * time.Second).String())
}

// add consensus flags
switch t.Config.Consensus {
case ConsensusIBFT:
Expand Down Expand Up @@ -429,10 +436,6 @@ func (t *TestServer) Start(ctx context.Context) error {
args = append(args, "--block-gas-target", *types.EncodeUint64(t.Config.BlockGasTarget))
}

if t.Config.BlockTime != 0 {
args = append(args, "--block-time", strconv.FormatUint(t.Config.BlockTime, 10))
}

if t.Config.IBFTBaseTimeout != 0 {
args = append(args, "--ibft-base-timeout", strconv.FormatUint(t.Config.IBFTBaseTimeout, 10))
}
Expand Down
7 changes: 4 additions & 3 deletions e2e/ibft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"
"time"

"github.com/0xPolygon/polygon-edge/command/server/config"
ibftSigner "github.com/0xPolygon/polygon-edge/consensus/ibft/signer"
"github.com/0xPolygon/polygon-edge/e2e/framework"
"github.com/0xPolygon/polygon-edge/helper/tests"
Expand All @@ -19,6 +18,8 @@ import (
// TestIbft_Transfer sends a transfer transaction (EOA -> EOA)
// and verifies it was mined
func TestIbft_Transfer(t *testing.T) {
const defaultBlockTime uint64 = 2

testCases := []struct {
name string
blockTime uint64
Expand All @@ -27,7 +28,7 @@ func TestIbft_Transfer(t *testing.T) {
}{
{
name: "default block time",
blockTime: config.DefaultBlockTime,
blockTime: defaultBlockTime,
ibftBaseTimeout: 0, // use default value
validatorType: validators.ECDSAValidatorType,
},
Expand All @@ -39,7 +40,7 @@ func TestIbft_Transfer(t *testing.T) {
},
{
name: "with BLS",
blockTime: config.DefaultBlockTime,
blockTime: defaultBlockTime,
ibftBaseTimeout: 0, // use default value
validatorType: validators.BLSValidatorType,
},
Expand Down
Loading

0 comments on commit 5d4c9d9

Please sign in to comment.