Merge pull request #31 from primevprotocol/ckartik/modify-chain-to-ms-timestamps

Updates timestamps to Millisecond Precision - Pre-Confirmations Decay
This commit is contained in:
Kartik Chopra 2024-03-11 14:00:54 -04:00 committed by GitHub
commit daec1d68a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 9 deletions

View file

@ -21,13 +21,14 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"github.com/ethereum/go-ethereum/metrics"
"io" "io"
"math/big" "math/big"
"math/rand" "math/rand"
"sync" "sync"
"time" "time"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
@ -52,7 +53,7 @@ const (
inmemorySnapshots = 128 // Number of recent vote snapshots to keep in memory inmemorySnapshots = 128 // Number of recent vote snapshots to keep in memory
inmemorySignatures = 4096 // Number of recent block signatures to keep in memory inmemorySignatures = 4096 // Number of recent block signatures to keep in memory
wiggleTime = 500 * time.Millisecond // Random delay (per signer) to allow concurrent signers wiggleTime = 100 * time.Millisecond // Random delay (per signer) to allow concurrent signers
) )
// Clique proof-of-authority protocol constants. // Clique proof-of-authority protocol constants.
@ -255,7 +256,7 @@ func (c *Clique) verifyHeader(chain consensus.ChainHeaderReader, header *types.H
number := header.Number.Uint64() number := header.Number.Uint64()
// Don't waste time checking blocks from the future // Don't waste time checking blocks from the future
if header.Time > uint64(time.Now().Unix()) { if header.Time > uint64(time.Now().UnixMilli()) {
return consensus.ErrFutureBlock return consensus.ErrFutureBlock
} }
// Checkpoint blocks need to enforce zero beneficiary // Checkpoint blocks need to enforce zero beneficiary
@ -346,7 +347,7 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainHeaderReader, header
if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash { if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash {
return consensus.ErrUnknownAncestor return consensus.ErrUnknownAncestor
} }
if parent.Time+c.config.PeriodMs/1000 > header.Time { if parent.Time+c.config.PeriodMs > header.Time {
return errInvalidTimestamp return errInvalidTimestamp
} }
// Verify that the gasUsed is <= gasLimit // Verify that the gasUsed is <= gasLimit
@ -575,9 +576,10 @@ func (c *Clique) Prepare(chain consensus.ChainHeaderReader, header *types.Header
if parent == nil { if parent == nil {
return consensus.ErrUnknownAncestor return consensus.ErrUnknownAncestor
} }
header.Time = parent.Time + c.config.PeriodMs/1000
if header.Time < uint64(time.Now().Unix()) { header.Time = parent.Time + c.config.PeriodMs
header.Time = uint64(time.Now().Unix()) if header.Time < uint64(time.Now().UnixMilli()) {
header.Time = uint64(time.Now().UnixMilli())
} }
return nil return nil
} }
@ -657,7 +659,7 @@ func (c *Clique) Seal(chain consensus.ChainHeaderReader, block *types.Block, res
} }
} }
// Sweet, the protocol permits us to sign the block, wait for our time // Sweet, the protocol permits us to sign the block, wait for our time
delay := time.Unix(int64(header.Time), 0).Sub(time.Now()) // nolint: gosimple delay := time.UnixMilli(int64(header.Time)).Sub(time.Now()) // nolint: gosimple
if header.Difficulty.Cmp(diffNoTurn) == 0 { if header.Difficulty.Cmp(diffNoTurn) == 0 {
// It's not our turn explicitly to sign, delay it a bit // It's not our turn explicitly to sign, delay it a bit
wiggle := time.Duration(len(snap.Signers)/2+1) * wiggleTime wiggle := time.Duration(len(snap.Signers)/2+1) * wiggleTime

View file

@ -20,7 +20,7 @@
} }
}, },
"nonce": "0x0", "nonce": "0x0",
"timestamp": "0x6546df6d", "timestamp": "0x18E0A4E0D22",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000d9cd8E5DE6d55f796D980B818D350C0746C25b97788EBABe5c3dD422Ef92Ca6714A69e2eabcE1Ee4f6ba5bca9b489de3a4ac3a57823d5d97d608feb90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000d9cd8E5DE6d55f796D980B818D350C0746C25b97788EBABe5c3dD422Ef92Ca6714A69e2eabcE1Ee4f6ba5bca9b489de3a4ac3a57823d5d97d608feb90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x1c9c380", "gasLimit": "0x1c9c380",
"difficulty": "0x1", "difficulty": "0x1",