diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index 219aa5620c7457a9a3854254060a99fbcb5a13e5..f2d8e43772448ce3d001ab049e5acc5c3fa5079b 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -206,16 +206,9 @@ func CalcDifficulty(snap *Snapshot, signer common.Address, epoch uint64) *big.In // CalcProducerDelay is the producer delay algorithm based on block time. func CalcProducerDelay(snap *Snapshot, signer common.Address, period uint64, epoch uint64, producerDelay uint64) uint64 { - // lastSigner := snap.Recents[snap.Number] - // proposer := snap.ValidatorSet.GetProposer() - // if block is epoch start block, proposer will be inturn signer if (snap.Number+1)%epoch == 0 { return producerDelay - - // if block is not epoch block, last block signer will be inturn - // } else if bytes.Compare(lastSigner.Bytes(), signer.Bytes()) != 0 { - // return producerDelay } return period @@ -473,7 +466,10 @@ func (c *Bor) snapshot(chain consensus.ChainReader, number uint64, hash common.H hash := checkpoint.Hash() // get validators and current span - validators, _ := c.GetCurrentValidators(number, number+1) + validators, err := c.GetCurrentValidators(number, number+1) + if err != nil { + return nil, err + } // new snap shot snap = newSnapshot(c.config, c.signatures, number, hash, validators, c.ethAPI) @@ -672,13 +668,13 @@ func (c *Bor) Finalize(chain consensus.ChainReader, header *types.Header, state // check and commit span if err := c.checkAndCommitSpan(state, header, cx); err != nil { fmt.Println("Error while committing span", err) - // return nil, err + return } // commit statees if err := c.CommitStates(state, header, cx); err != nil { fmt.Println("Error while committing states", err) - // return nil, err + return } } @@ -698,7 +694,7 @@ func (c *Bor) FinalizeAndAssemble(chain consensus.ChainReader, header *types.Hea err := c.checkAndCommitSpan(state, header, cx) if err != nil { fmt.Println("Error while committing span", err) - // return nil, err + return nil, err } // commit statees @@ -990,7 +986,7 @@ func (c *Bor) checkAndCommitSpan( header *types.Header, chain core.ChainContext, ) error { - var pending bool = false + pending := false var span *Span = nil var wg sync.WaitGroup wg.Add(1) @@ -1007,8 +1003,6 @@ func (c *Bor) checkAndCommitSpan( wg.Wait() - fmt.Println("Span", span.ID, span.StartBlock, span.EndBlock, "number", header.Number, "needToCommitSpan", c.needToCommitSpan(span, header)) - // commit span if there is new span pending or span is ending or end block is not set if pending || c.needToCommitSpan(span, header) { err := c.commitSpan(span, state, header, chain) diff --git a/consensus/bor/snapshot.go b/consensus/bor/snapshot.go index 8b91360e92f43de979b32e29fec8cf9e382addb3..41a844eaa5c0c117c6fd8861ab53c77a4e6af508 100644 --- a/consensus/bor/snapshot.go +++ b/consensus/bor/snapshot.go @@ -118,50 +118,6 @@ func (s *Snapshot) copy() *Snapshot { return cpy } -// // validVote returns whether it makes sense to cast the specified vote in the -// // given snapshot context (e.g. don't try to add an already authorized signer). -// func (s *Snapshot) validVote(address common.Address, authorize bool) bool { -// _, signer := s.Signers[address] -// return (signer && !authorize) || (!signer && authorize) -// } - -// // cast adds a new vote into the tally. -// func (s *Snapshot) cast(address common.Address, authorize bool) bool { -// // Ensure the vote is meaningful -// if !s.validVote(address, authorize) { -// return false -// } -// // Cast the vote into an existing or new tally -// if old, ok := s.Tally[address]; ok { -// old.Votes++ -// s.Tally[address] = old -// } else { -// s.Tally[address] = Tally{Authorize: authorize, Votes: 1} -// } -// return true -// } - -// // uncast removes a previously cast vote from the tally. -// func (s *Snapshot) uncast(address common.Address, authorize bool) bool { -// // If there's no tally, it's a dangling vote, just drop -// tally, ok := s.Tally[address] -// if !ok { -// return false -// } -// // Ensure we only revert counted votes -// if tally.Authorize != authorize { -// return false -// } -// // Otherwise revert the vote -// if tally.Votes > 1 { -// tally.Votes-- -// s.Tally[address] = tally -// } else { -// delete(s.Tally, address) -// } -// return true -// } - func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) { // Allow passing in no headers for cleaner code if len(headers) == 0 { @@ -220,9 +176,6 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) { validators := snap.ValidatorSet.Validators // proposer will be the last signer if block is not epoch block proposer := snap.ValidatorSet.GetProposer().Address - // if number%s.config.Sprint != 0 { - // proposer = snap.Recents[number-1] - // } proposerIndex, _ := snap.ValidatorSet.GetByAddress(proposer) signerIndex, _ := snap.ValidatorSet.GetByAddress(signer) limit := len(validators) - (len(validators)/2 + 1) @@ -268,11 +221,6 @@ func (s *Snapshot) inturn(number uint64, signer common.Address, epoch uint64) ui proposer := s.ValidatorSet.GetProposer().Address totalValidators := len(validators) - // proposer will be the last signer if block is not epoch block - // proposer := snap.ValidatorSet.GetProposer().Address - // if number%epoch != 0 { - // proposer = snap.Recents[number-1] - // } proposerIndex, _ := s.ValidatorSet.GetByAddress(proposer) signerIndex, _ := s.ValidatorSet.GetByAddress(signer) @@ -283,21 +231,4 @@ func (s *Snapshot) inturn(number uint64, signer common.Address, epoch uint64) ui } return uint64(totalValidators - (tempIndex - proposerIndex)) - - // signers, offset := s.signers(), 0 - // for offset < len(signers) && signers[offset] != signer { - // offset++ - // } - // return ((number / producerPeriod) % uint64(len(signers))) == uint64(offset) - - // // if block is epoch start block, proposer will be inturn signer - // if s.Number%epoch == 0 { - // if bytes.Compare(proposer.Address.Bytes(), signer.Bytes()) == 0 { - // return true - // } - // // if block is not epoch block, last block signer will be inturn - // } else if bytes.Compare(lastSigner.Bytes(), signer.Bytes()) == 0 { - // return false - // } - // return false } diff --git a/consensus/bor/span.go b/consensus/bor/span.go index 6da0df7ea7353fe9446258735488f9e43191cd6d..2fd0cf10792ddf6c5fecf9078bfdedf5378e1757 100644 --- a/consensus/bor/span.go +++ b/consensus/bor/span.go @@ -7,7 +7,7 @@ type Span struct { EndBlock uint64 `json:"end_block" yaml:"end_block"` } -// HeimdallSpan represents +// HeimdallSpan represents span from heimdall APIs type HeimdallSpan struct { Span ValidatorSet ValidatorSet `json:"validator_set" yaml:"validator_set"`