Enable prealloc check and improve address comparison

This commit is contained in:
Jerry 2022-05-24 13:08:45 -07:00
parent 45a72bc49e
commit 2e2557a2d0
7 changed files with 18 additions and 17 deletions

View file

@ -38,7 +38,7 @@ linters:
- noctx - noctx
#- nosprintfhostport # TODO: do we use IPv6? #- nosprintfhostport # TODO: do we use IPv6?
- paralleltest - paralleltest
# - prealloc - prealloc
- predeclared - predeclared
#- promlinter #- promlinter
#- revive #- revive

View file

@ -61,7 +61,7 @@ type difficultiesKV struct {
} }
func rankMapDifficulties(values map[common.Address]uint64) []difficultiesKV { func rankMapDifficulties(values map[common.Address]uint64) []difficultiesKV {
var ss []difficultiesKV ss := make([]difficultiesKV, 0, len(values))
for k, v := range values { for k, v := range values {
ss = append(ss, difficultiesKV{k, v}) ss = append(ss, difficultiesKV{k, v})
} }

View file

@ -472,10 +472,9 @@ func (c *Bor) verifyCascadingFields(chain consensus.ChainHeaderReader, header *t
// nolint: gocognit // nolint: gocognit
func (c *Bor) snapshot(chain consensus.ChainHeaderReader, number uint64, hash common.Hash, parents []*types.Header) (*Snapshot, error) { func (c *Bor) snapshot(chain consensus.ChainHeaderReader, number uint64, hash common.Hash, parents []*types.Header) (*Snapshot, error) {
// Search for a snapshot in memory or on disk for checkpoints // Search for a snapshot in memory or on disk for checkpoints
var ( var snap *Snapshot
headers []*types.Header
snap *Snapshot headers := make([]*types.Header, 0, 16)
)
//nolint:govet //nolint:govet
for snap == nil { for snap == nil {
@ -549,6 +548,8 @@ func (c *Bor) snapshot(chain consensus.ChainHeaderReader, number uint64, hash co
number, hash = number-1, header.ParentHash number, hash = number-1, header.ParentHash
} }
log.Info("Snapshot has been found in %d headers depth.", len(headers))
// check if snapshot is nil // check if snapshot is nil
if snap == nil { if snap == nil {
return nil, fmt.Errorf("Unknown error while retrieving snapshot at block number %v", number) return nil, fmt.Errorf("Unknown error while retrieving snapshot at block number %v", number)
@ -701,7 +702,7 @@ func (c *Bor) Prepare(chain consensus.ChainHeaderReader, header *types.Header) e
var succession int var succession int
// if signer is not empty // if signer is not empty
if !bytes.Equal(c.signer.Bytes(), common.Address{}.Bytes()) { if c.signer != (common.Address{}) {
succession, err = snap.GetSignerSuccessionNumber(c.signer) succession, err = snap.GetSignerSuccessionNumber(c.signer)
if err != nil { if err != nil {
return err return err
@ -1146,7 +1147,7 @@ func (c *Bor) fetchAndCommitSpan(
} }
// get validators bytes // get validators bytes
var validators []MinimalVal validators := make([]MinimalVal, 0, len(heimdallSpan.ValidatorSet.Validators))
for _, val := range heimdallSpan.ValidatorSet.Validators { for _, val := range heimdallSpan.ValidatorSet.Validators {
validators = append(validators, val.MinimalVal()) validators = append(validators, val.MinimalVal())
} }
@ -1158,7 +1159,7 @@ func (c *Bor) fetchAndCommitSpan(
} }
// get producers bytes // get producers bytes
var producers []MinimalVal producers := make([]MinimalVal, 0, len(heimdallSpan.SelectedProducers))
for _, val := range heimdallSpan.SelectedProducers { for _, val := range heimdallSpan.SelectedProducers {
producers = append(producers, val.MinimalVal()) producers = append(producers, val.MinimalVal())
} }
@ -1401,7 +1402,7 @@ func applyMessage(
func validatorContains(a []*Validator, x *Validator) (*Validator, bool) { func validatorContains(a []*Validator, x *Validator) (*Validator, bool) {
for _, n := range a { for _, n := range a {
if bytes.Equal(n.Address.Bytes(), x.Address.Bytes()) { if n.Address == x.Address {
return n, true return n, true
} }
} }
@ -1413,7 +1414,7 @@ func getUpdatedValidatorSet(oldValidatorSet *ValidatorSet, newVals []*Validator)
v := oldValidatorSet v := oldValidatorSet
oldVals := v.Validators oldVals := v.Validators
var changes []*Validator changes := make([]*Validator, 0, len(oldVals))
for _, ov := range oldVals { for _, ov := range oldVals {
if f, ok := validatorContains(newVals, ov); ok { if f, ok := validatorContains(newVals, ov); ok {

View file

@ -40,7 +40,7 @@ func convertTo32(input []byte) (output [32]byte) {
} }
func convert(input []([32]byte)) [][]byte { func convert(input []([32]byte)) [][]byte {
var output [][]byte output := make([][]byte, 0, len(input))
for _, in := range input { for _, in := range input {
newInput := make([]byte, len(in[:])) newInput := make([]byte, len(in[:]))

View file

@ -1,7 +1,6 @@
package bor package bor
import ( import (
"bytes"
"encoding/json" "encoding/json"
lru "github.com/hashicorp/golang-lru" lru "github.com/hashicorp/golang-lru"
@ -208,7 +207,7 @@ func (s *Snapshot) signers() []common.Address {
// Difficulty returns the difficulty for a particular signer at the current snapshot number // Difficulty returns the difficulty for a particular signer at the current snapshot number
func (s *Snapshot) Difficulty(signer common.Address) uint64 { func (s *Snapshot) Difficulty(signer common.Address) uint64 {
// if signer is empty // if signer is empty
if bytes.Equal(signer.Bytes(), common.Address{}.Bytes()) { if signer == (common.Address{}) {
return 1 return 1
} }

View file

@ -255,7 +255,7 @@ func (vals *ValidatorSet) GetByAddress(address common.Address) (index int, val *
idx := sort.Search(len(vals.Validators), func(i int) bool { idx := sort.Search(len(vals.Validators), func(i int) bool {
return bytes.Compare(address.Bytes(), vals.Validators[i].Address.Bytes()) <= 0 return bytes.Compare(address.Bytes(), vals.Validators[i].Address.Bytes()) <= 0
}) })
if idx < len(vals.Validators) && bytes.Equal(vals.Validators[idx].Address.Bytes(), address.Bytes()) { if idx < len(vals.Validators) && vals.Validators[idx].Address == address {
return idx, vals.Validators[idx].Copy() return idx, vals.Validators[idx].Copy()
} }

View file

@ -182,9 +182,10 @@ func (c *AttachCommand) makeConsolePreloads() []string {
return nil return nil
} }
// Otherwise resolve absolute paths and return them // Otherwise resolve absolute paths and return them
var preloads []string splitFlags := strings.Split(c.PreloadJSFlag, ",")
preloads := make([]string, 0, len(splitFlags))
for _, file := range strings.Split(c.PreloadJSFlag, ",") { for _, file := range splitFlags {
preloads = append(preloads, strings.TrimSpace(file)) preloads = append(preloads, strings.TrimSpace(file))
} }