From 43fb2a6805711919bdb828d6e7873f01eb2ff2b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20=C5=BDivkovi=C4=87?= Date: Wed, 2 Feb 2022 11:22:32 +0100 Subject: [PATCH] Revert the millisecond changes for block timestamp (#399) --- blockchain/blockchain.go | 2 +- command/helper/config.go | 8 ++++---- command/server/server_command.go | 2 +- consensus/consensus.go | 2 +- consensus/ibft/ibft.go | 25 ++++++++++++++----------- consensus/metrics.go | 2 +- consensus/utils.go | 12 ------------ server/config.go | 4 ++-- 8 files changed, 24 insertions(+), 33 deletions(-) diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index bda9a6ecf1..314d5e7313 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -646,7 +646,7 @@ func (b *Blockchain) WriteBlock(block *types.Block) error { if prevHeader, ok := b.GetHeaderByNumber(header.Number - 1); ok { diff := header.Timestamp - prevHeader.Timestamp - logArgs = append(logArgs, "generation_time_in_miliseconds", diff) + logArgs = append(logArgs, "generation_time_in_seconds", diff) } b.logger.Info("new block", logArgs...) diff --git a/command/helper/config.go b/command/helper/config.go index 1d3d6486dc..6b5dd58b11 100644 --- a/command/helper/config.go +++ b/command/helper/config.go @@ -35,7 +35,7 @@ type Config struct { Join string `json:"join_addr"` Consensus map[string]interface{} `json:"consensus"` RestoreFile string `json:"restore_file"` - BlockTime uint64 `json:"block_time_ms"` // block time im miliseconds + BlockTime uint64 `json:"block_time_s"` } // Telemetry holds the config details for metric services. @@ -60,8 +60,8 @@ type TxPool struct { MaxSlots uint64 `json:"max_slots"` } -// variable defining default BlockTime parameter in miliseconds -const defaultBlockTime uint64 = 2000 +// minimum block generation time in seconds +const defaultBlockTime uint64 = 2 // DefaultConfig returns the default server configuration func DefaultConfig() *Config { @@ -84,7 +84,7 @@ func DefaultConfig() *Config { Consensus: map[string]interface{}{}, LogLevel: "INFO", RestoreFile: "", - BlockTime: defaultBlockTime, // default block time in miliseconds + BlockTime: defaultBlockTime, } } diff --git a/command/server/server_command.go b/command/server/server_command.go index ec20092f24..3b858aa637 100644 --- a/command/server/server_command.go +++ b/command/server/server_command.go @@ -232,7 +232,7 @@ func (c *ServerCommand) DefineFlags() { } c.FlagMap["block-time"] = helper.FlagDescriptor{ - Description: fmt.Sprintf("Sets block time in miliseconds. Default: %d", helper.DefaultConfig().BlockTime), + Description: fmt.Sprintf("Sets block time in seconds. Default: %ds", helper.DefaultConfig().BlockTime), Arguments: []string{ "BLOCK_TIME", }, diff --git a/consensus/consensus.go b/consensus/consensus.go index 96bc71f19d..c1270a0597 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -65,7 +65,7 @@ type ConsensusParams struct { Logger hclog.Logger Metrics *Metrics SecretsManager secrets.SecretsManager - BlockTime uint64 // block time im miliseconds + BlockTime uint64 } // Factory is the factory function to create a discovery backend diff --git a/consensus/ibft/ibft.go b/consensus/ibft/ibft.go index 1ca31e39b5..88e9d8e3b4 100644 --- a/consensus/ibft/ibft.go +++ b/consensus/ibft/ibft.go @@ -97,7 +97,7 @@ type Ibft struct { mechanism ConsensusMechanism // IBFT ConsensusMechanism used (PoA / PoS) - blockTime uint64 // Configurable consensus blocktime in miliseconds + blockTime time.Duration // Minimum block generation time in seconds } // Define the type of the IBFT consensus @@ -247,7 +247,7 @@ func Factory( sealing: params.Seal, metrics: params.Metrics, secretsManager: params.SecretsManager, - blockTime: params.BlockTime, + blockTime: time.Duration(params.BlockTime) * time.Second, } // Initialize the mechanism @@ -613,14 +613,14 @@ func (i *Ibft) buildBlock(snap *Snapshot, parent *types.Header) (*types.Block, e // to preserve go backward compatibility as time.UnixMili is available as of go 17 // set the timestamp - parentTime := consensus.MilliToUnix(parent.Timestamp) - headerTime := parentTime.Add(time.Duration(i.blockTime) * time.Millisecond) + parentTime := time.Unix(int64(parent.Timestamp), 0) + headerTime := parentTime.Add(i.blockTime) if headerTime.Before(time.Now()) { headerTime = time.Now() } - header.Timestamp = consensus.UnixToMilli(headerTime) + header.Timestamp = uint64(headerTime.Unix()) // we need to include in the extra field the current set of validators putIbftExtraValidators(header, snap.Set) @@ -805,7 +805,7 @@ func (i *Ibft) runAcceptState() { // start new round } // calculate how much time do we have to wait to mine the block - delay := time.Until(consensus.MilliToUnix(i.state.block.Header.Timestamp)) + delay := time.Until(time.Unix(int64(i.state.block.Header.Timestamp), 0)) select { case <-time.After(delay): @@ -979,12 +979,15 @@ func (i *Ibft) runValidateState() { func (i *Ibft) updateMetrics(block *types.Block) { // get previous header prvHeader, _ := i.blockchain.GetHeaderByNumber(block.Number() - 1) - // calculate difference between previous and current header timestamps - // diff := time.Unix(int64(block.Header.Timestamp),0).Sub(time.Unix(int64(prvHeader.Timestamp),0)) - diff := consensus.MilliToUnix(block.Header.Timestamp).Sub(consensus.MilliToUnix(prvHeader.Timestamp)) + parentTime := time.Unix(int64(prvHeader.Timestamp), 0) + headerTime := time.Unix(int64(block.Header.Timestamp), 0) - // update block_interval metric - i.metrics.BlockInterval.Set(float64(diff.Milliseconds())) + //Update the block interval metric + if block.Number() > 1 { + i.metrics.BlockInterval.Set( + headerTime.Sub(parentTime).Seconds(), + ) + } //Update the Number of transactions in the block metric i.metrics.NumTxs.Set(float64(len(block.Body().Transactions))) diff --git a/consensus/metrics.go b/consensus/metrics.go index 382b7cb3fd..63dc680fcb 100644 --- a/consensus/metrics.go +++ b/consensus/metrics.go @@ -52,7 +52,7 @@ func GetPrometheusMetrics(namespace string, labelsWithValues ...string) *Metrics Namespace: namespace, Subsystem: "consensus", Name: "block_interval", - Help: "Time between current block and the previous block in miliseconds.", + Help: "Time between current block and the previous block in seconds.", }, labels).With(labelsWithValues...), } } diff --git a/consensus/utils.go b/consensus/utils.go index a3a363ace8..77d267c55f 100644 --- a/consensus/utils.go +++ b/consensus/utils.go @@ -1,8 +1,6 @@ package consensus import ( - "time" - "github.com/0xPolygon/polygon-edge/types" "github.com/0xPolygon/polygon-edge/types/buildroot" ) @@ -40,13 +38,3 @@ func BuildBlock(params BuildBlockParams) *types.Block { Transactions: txs, } } - -// MilliToUnix returns the local Time corresponding to the given Unix time m milliseconds since January 1, 1970 UTC. -func MilliToUnix(m uint64) time.Time { - return time.Unix(0, int64(m)*1e6) -} - -// UnixToMilli returns uint64 value for miliseconds -func UnixToMilli(t time.Time) uint64 { - return uint64(t.UnixNano() / 1e6) -} diff --git a/server/config.go b/server/config.go index e9e73b9d0f..4e41771277 100644 --- a/server/config.go +++ b/server/config.go @@ -10,7 +10,7 @@ import ( const DefaultGRPCPort int = 9632 const DefaultJSONRPCPort int = 8545 -const DefaultBlockTime = 2000 // in milliseconds +const DefaultBlockTime = 2 // in seconds // Config is used to parametrize the minimal client type Config struct { @@ -38,7 +38,7 @@ func DefaultConfig() *Config { Network: network.DefaultConfig(), Telemetry: &Telemetry{PrometheusAddr: nil}, SecretsManager: nil, - BlockTime: DefaultBlockTime, // default block time in milliseconds + BlockTime: DefaultBlockTime, } }