update bor consensus

This commit is contained in:
Jaynti Kanani 2019-05-17 23:25:56 +05:30
parent 84828e7c94
commit f37d07db3d
No known key found for this signature in database
GPG key ID: 4396982C976BAE5E
4 changed files with 20 additions and 15 deletions

View file

@ -107,7 +107,7 @@ func (w *wizard) makeGenesis() {
genesis.Difficulty = big.NewInt(1)
genesis.GasLimit = 10000000
genesis.Config.Bor = &params.BorConfig{
BlockInterval: 1,
Period: 1,
ProducerInterval: 60,
Epoch: 30000,
}

View file

@ -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.

View file

@ -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)
}

View file

@ -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
}