From f37d07db3dc7d5848f82a65c933b9be820976e0c Mon Sep 17 00:00:00 2001 From: Jaynti Kanani <jdkanani@gmail.com> Date: Fri, 17 May 2019 23:25:56 +0530 Subject: [PATCH] update bor consensus --- cmd/puppeth/wizard_genesis.go | 2 +- consensus/bor/bor.go | 25 +++++++++++++++---------- consensus/bor/snapshot.go | 6 +++--- params/config.go | 2 +- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/cmd/puppeth/wizard_genesis.go b/cmd/puppeth/wizard_genesis.go index a26139b00..61666fda2 100644 --- a/cmd/puppeth/wizard_genesis.go +++ b/cmd/puppeth/wizard_genesis.go @@ -107,7 +107,7 @@ func (w *wizard) makeGenesis() { genesis.Difficulty = big.NewInt(1) genesis.GasLimit = 10000000 genesis.Config.Bor = ¶ms.BorConfig{ - BlockInterval: 1, + Period: 1, ProducerInterval: 60, Epoch: 30000, } diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index 738054979..fd2fbb0ac 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -183,8 +183,8 @@ func encodeSigHeader(w io.Writer, header *types.Header) { // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty // that a new block should have based on the previous blocks in the chain and the // current signer. -func CalcDifficulty(snap *Snapshot, signer common.Address) *big.Int { - if snap.inturn(snap.Number+1, signer) { +func CalcDifficulty(snap *Snapshot, signer common.Address, producerPeriod uint64) *big.Int { + if snap.inturn(snap.Number+1, signer, producerPeriod) { return new(big.Int).Set(diffInTurn) } return new(big.Int).Set(diffNoTurn) @@ -356,7 +356,7 @@ func (c *Bor) verifyCascadingFields(chain consensus.ChainReader, header *types.H if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash { return consensus.ErrUnknownAncestor } - if parent.Time+c.config.BlockInterval > header.Time { + if parent.Time+c.config.Period > header.Time { return ErrInvalidTimestamp } // Retrieve the snapshot needed to verify this header and cache it @@ -500,13 +500,13 @@ func (c *Bor) verifySeal(chain consensus.ChainReader, header *types.Header, pare if recent == signer { // Signer is among recents, only fail if the current block doesn't shift it out if limit := uint64(len(snap.Signers)/2 + 1); seen > number-limit { - return errRecentlySigned + // return errRecentlySigned } } } // Ensure that the difficulty corresponds to the turn-ness of the signer if !c.fakeDiff { - inturn := snap.inturn(header.Number.Uint64(), signer) + inturn := snap.inturn(header.Number.Uint64(), signer, c.config.ProducerInterval) if inturn && header.Difficulty.Cmp(diffInTurn) != 0 { return errWrongDifficulty } @@ -514,6 +514,7 @@ func (c *Bor) verifySeal(chain consensus.ChainReader, header *types.Header, pare return errWrongDifficulty } } + return nil } @@ -552,7 +553,7 @@ func (c *Bor) Prepare(chain consensus.ChainReader, header *types.Header) error { c.lock.RUnlock() } // Set the correct difficulty - header.Difficulty = CalcDifficulty(snap, c.signer) + header.Difficulty = CalcDifficulty(snap, c.signer, c.config.ProducerInterval) // Ensure the extra data has all it's components if len(header.Extra) < extraVanity { @@ -575,7 +576,7 @@ func (c *Bor) Prepare(chain consensus.ChainReader, header *types.Header) error { if parent == nil { return consensus.ErrUnknownAncestor } - header.Time = parent.Time + c.config.BlockInterval + header.Time = parent.Time + c.config.Period if header.Time < uint64(time.Now().Unix()) { header.Time = uint64(time.Now().Unix()) } @@ -622,7 +623,7 @@ func (c *Bor) Seal(chain consensus.ChainReader, block *types.Block, results chan return errUnknownBlock } // For 0-period chains, refuse to seal empty blocks (no reward but would spin sealing) - if c.config.BlockInterval == 0 && len(block.Transactions()) == 0 { + if c.config.Period == 0 && len(block.Transactions()) == 0 { log.Info("Sealing paused, waiting for transactions") return nil } @@ -645,7 +646,7 @@ func (c *Bor) Seal(chain consensus.ChainReader, block *types.Block, results chan // Signer is among recents, only wait if the current block doesn't shift it out if limit := uint64(len(snap.Signers)/2 + 1); number < limit || seen > number-limit { log.Info("Signed recently, must wait for others") - return nil + // return nil } } } @@ -687,7 +688,11 @@ func (c *Bor) Seal(chain consensus.ChainReader, block *types.Block, results chan // that a new block should have based on the previous blocks in the chain and the // current signer. func (c *Bor) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int { - return big.NewInt(0) + snap, err := c.snapshot(chain, parent.Number.Uint64(), parent.Hash(), nil) + if err != nil { + return nil + } + return CalcDifficulty(snap, c.signer, c.config.ProducerInterval) } // SealHash returns the hash of a block prior to it being sealed. diff --git a/consensus/bor/snapshot.go b/consensus/bor/snapshot.go index b2bdba088..a9148558c 100644 --- a/consensus/bor/snapshot.go +++ b/consensus/bor/snapshot.go @@ -218,7 +218,7 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) { } for _, recent := range snap.Recents { if recent == signer { - return nil, errRecentlySigned + // return nil, errRecentlySigned } } snap.Recents[number] = signer @@ -303,10 +303,10 @@ func (s *Snapshot) signers() []common.Address { } // inturn returns if a signer at a given block height is in-turn or not. -func (s *Snapshot) inturn(number uint64, signer common.Address) bool { +func (s *Snapshot) inturn(number uint64, signer common.Address, producerPeriod uint64) bool { signers, offset := s.signers(), 0 for offset < len(signers) && signers[offset] != signer { offset++ } - return (number % uint64(len(signers))) == uint64(offset) + return ((number / producerPeriod) % uint64(len(signers))) == uint64(offset) } diff --git a/params/config.go b/params/config.go index 9d95f160d..afa222263 100644 --- a/params/config.go +++ b/params/config.go @@ -228,7 +228,7 @@ func (c *CliqueConfig) String() string { // BorConfig is the consensus engine configs for Matic bor based sealing. type BorConfig struct { - BlockInterval uint64 `json:"blockInterval"` // Number of seconds between blocks to enforce + Period uint64 `json:"period"` // Number of seconds between blocks to enforce ProducerInterval uint64 `json:"producerInterval"` // Number of seconds between change in block producer interval to enforce Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint } -- GitLab