From 2e2557a2d004006e72be463d45a3b4a04c397cd2 Mon Sep 17 00:00:00 2001 From: Jerry Date: Tue, 24 May 2022 13:08:45 -0700 Subject: [PATCH] Enable prealloc check and improve address comparison --- .golangci.yml | 2 +- consensus/bor/api.go | 2 +- consensus/bor/bor.go | 19 ++++++++++--------- consensus/bor/merkle.go | 2 +- consensus/bor/snapshot.go | 3 +-- consensus/bor/validator_set.go | 2 +- internal/cli/attach.go | 5 +++-- 7 files changed, 18 insertions(+), 17 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index daea4e1e0b..b063984061 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -38,7 +38,7 @@ linters: - noctx #- nosprintfhostport # TODO: do we use IPv6? - paralleltest - # - prealloc + - prealloc - predeclared #- promlinter #- revive diff --git a/consensus/bor/api.go b/consensus/bor/api.go index 361e439bf7..364fc448b8 100644 --- a/consensus/bor/api.go +++ b/consensus/bor/api.go @@ -61,7 +61,7 @@ type difficultiesKV struct { } func rankMapDifficulties(values map[common.Address]uint64) []difficultiesKV { - var ss []difficultiesKV + ss := make([]difficultiesKV, 0, len(values)) for k, v := range values { ss = append(ss, difficultiesKV{k, v}) } diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index ada601f838..62336e73f0 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -472,10 +472,9 @@ func (c *Bor) verifyCascadingFields(chain consensus.ChainHeaderReader, header *t // nolint: gocognit 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 - var ( - headers []*types.Header - snap *Snapshot - ) + var snap *Snapshot + + headers := make([]*types.Header, 0, 16) //nolint:govet for snap == nil { @@ -549,6 +548,8 @@ func (c *Bor) snapshot(chain consensus.ChainHeaderReader, number uint64, hash co number, hash = number-1, header.ParentHash } + log.Info("Snapshot has been found in %d headers depth.", len(headers)) + // check if snapshot is nil if snap == nil { 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 // 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) if err != nil { return err @@ -1146,7 +1147,7 @@ func (c *Bor) fetchAndCommitSpan( } // get validators bytes - var validators []MinimalVal + validators := make([]MinimalVal, 0, len(heimdallSpan.ValidatorSet.Validators)) for _, val := range heimdallSpan.ValidatorSet.Validators { validators = append(validators, val.MinimalVal()) } @@ -1158,7 +1159,7 @@ func (c *Bor) fetchAndCommitSpan( } // get producers bytes - var producers []MinimalVal + producers := make([]MinimalVal, 0, len(heimdallSpan.SelectedProducers)) for _, val := range heimdallSpan.SelectedProducers { producers = append(producers, val.MinimalVal()) } @@ -1401,7 +1402,7 @@ func applyMessage( func validatorContains(a []*Validator, x *Validator) (*Validator, bool) { for _, n := range a { - if bytes.Equal(n.Address.Bytes(), x.Address.Bytes()) { + if n.Address == x.Address { return n, true } } @@ -1413,7 +1414,7 @@ func getUpdatedValidatorSet(oldValidatorSet *ValidatorSet, newVals []*Validator) v := oldValidatorSet oldVals := v.Validators - var changes []*Validator + changes := make([]*Validator, 0, len(oldVals)) for _, ov := range oldVals { if f, ok := validatorContains(newVals, ov); ok { diff --git a/consensus/bor/merkle.go b/consensus/bor/merkle.go index 5c61eb4a12..ef1b4eb87e 100644 --- a/consensus/bor/merkle.go +++ b/consensus/bor/merkle.go @@ -40,7 +40,7 @@ func convertTo32(input []byte) (output [32]byte) { } func convert(input []([32]byte)) [][]byte { - var output [][]byte + output := make([][]byte, 0, len(input)) for _, in := range input { newInput := make([]byte, len(in[:])) diff --git a/consensus/bor/snapshot.go b/consensus/bor/snapshot.go index ef92cc99b4..606c28340b 100644 --- a/consensus/bor/snapshot.go +++ b/consensus/bor/snapshot.go @@ -1,7 +1,6 @@ package bor import ( - "bytes" "encoding/json" 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 func (s *Snapshot) Difficulty(signer common.Address) uint64 { // if signer is empty - if bytes.Equal(signer.Bytes(), common.Address{}.Bytes()) { + if signer == (common.Address{}) { return 1 } diff --git a/consensus/bor/validator_set.go b/consensus/bor/validator_set.go index 892891bbda..ee14ef2cb1 100644 --- a/consensus/bor/validator_set.go +++ b/consensus/bor/validator_set.go @@ -255,7 +255,7 @@ func (vals *ValidatorSet) GetByAddress(address common.Address) (index int, val * idx := sort.Search(len(vals.Validators), func(i int) bool { 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() } diff --git a/internal/cli/attach.go b/internal/cli/attach.go index 1aa888c12b..134a282180 100644 --- a/internal/cli/attach.go +++ b/internal/cli/attach.go @@ -182,9 +182,10 @@ func (c *AttachCommand) makeConsolePreloads() []string { return nil } // 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)) }