Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Revert the millisecond changes for block timestamp (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos authored Feb 2, 2022
1 parent 2e2fbbb commit 43fb2a6
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 33 deletions.
2 changes: 1 addition & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
8 changes: 4 additions & 4 deletions command/helper/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -84,7 +84,7 @@ func DefaultConfig() *Config {
Consensus: map[string]interface{}{},
LogLevel: "INFO",
RestoreFile: "",
BlockTime: defaultBlockTime, // default block time in miliseconds
BlockTime: defaultBlockTime,
}
}

Expand Down
2 changes: 1 addition & 1 deletion command/server/server_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down
2 changes: 1 addition & 1 deletion consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 14 additions & 11 deletions consensus/ibft/ibft.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)))
Expand Down
2 changes: 1 addition & 1 deletion consensus/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...),
}
}
Expand Down
12 changes: 0 additions & 12 deletions consensus/utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package consensus

import (
"time"

"github.com/0xPolygon/polygon-edge/types"
"github.com/0xPolygon/polygon-edge/types/buildroot"
)
Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
}

Expand Down

0 comments on commit 43fb2a6

Please sign in to comment.