From 9cce148d834f29136912cd2251680e36e45a4ba7 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Sat, 24 Aug 2024 22:03:10 +0300 Subject: [PATCH] *: use maps.Equal/slices.Equal where appropriate Testing for equality has never been easier. Signed-off-by: Roman Khimov --- pkg/config/application_config.go | 6 ++--- pkg/config/protocol_config.go | 40 ++++++-------------------------- pkg/services/oracle/nodes.go | 15 ++++-------- pkg/util/bitfield/bitfield.go | 10 +------- 4 files changed, 14 insertions(+), 57 deletions(-) diff --git a/pkg/config/application_config.go b/pkg/config/application_config.go index fb9ba2827..17a74389a 100644 --- a/pkg/config/application_config.go +++ b/pkg/config/application_config.go @@ -43,10 +43,8 @@ func (a *ApplicationConfiguration) EqualsButServices(o *ApplicationConfiguration oCp := slices.Clone(o.P2P.Addresses) sort.Strings(aCp) sort.Strings(oCp) - for i := range aCp { - if aCp[i] != oCp[i] { - return false - } + if !slices.Equal(aCp, oCp) { + return false } if a.P2P.AttemptConnPeers != o.P2P.AttemptConnPeers || a.P2P.BroadcastFactor != o.P2P.BroadcastFactor || diff --git a/pkg/config/protocol_config.go b/pkg/config/protocol_config.go index fdf0422d5..cf5ad78cc 100644 --- a/pkg/config/protocol_config.go +++ b/pkg/config/protocol_config.go @@ -3,6 +3,8 @@ package config import ( "errors" "fmt" + "maps" + "slices" "sort" "time" @@ -231,40 +233,12 @@ func (p *ProtocolConfiguration) Equals(o *ProtocolConfiguration) bool { p.TimePerBlock != o.TimePerBlock || p.ValidatorsCount != o.ValidatorsCount || p.VerifyTransactions != o.VerifyTransactions || - len(p.CommitteeHistory) != len(o.CommitteeHistory) || - len(p.Hardforks) != len(o.Hardforks) || - len(p.SeedList) != len(o.SeedList) || - len(p.StandbyCommittee) != len(o.StandbyCommittee) || - len(p.ValidatorsHistory) != len(o.ValidatorsHistory) { + !maps.Equal(p.CommitteeHistory, o.CommitteeHistory) || + !maps.Equal(p.Hardforks, o.Hardforks) || + !slices.Equal(p.SeedList, o.SeedList) || + !slices.Equal(p.StandbyCommittee, o.StandbyCommittee) || + !maps.Equal(p.ValidatorsHistory, o.ValidatorsHistory) { return false } - for k, v := range p.CommitteeHistory { - vo, ok := o.CommitteeHistory[k] - if !ok || v != vo { - return false - } - } - for k, v := range p.Hardforks { - vo, ok := o.Hardforks[k] - if !ok || v != vo { - return false - } - } - for i := range p.SeedList { - if p.SeedList[i] != o.SeedList[i] { - return false - } - } - for i := range p.StandbyCommittee { - if p.StandbyCommittee[i] != o.StandbyCommittee[i] { - return false - } - } - for k, v := range p.ValidatorsHistory { - vo, ok := o.ValidatorsHistory[k] - if !ok || v != vo { - return false - } - } return true } diff --git a/pkg/services/oracle/nodes.go b/pkg/services/oracle/nodes.go index 0331de62d..e720bd8f2 100644 --- a/pkg/services/oracle/nodes.go +++ b/pkg/services/oracle/nodes.go @@ -1,6 +1,8 @@ package oracle import ( + "slices" + "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/smartcontract" @@ -13,17 +15,8 @@ func (o *Oracle) UpdateOracleNodes(oracleNodes keys.PublicKeys) { o.accMtx.Lock() defer o.accMtx.Unlock() - old := o.oracleNodes - if isEqual := len(old) == len(oracleNodes); isEqual { - for i := range old { - if !old[i].Equal(oracleNodes[i]) { - isEqual = false - break - } - } - if isEqual { - return - } + if slices.EqualFunc(o.oracleNodes, oracleNodes, (*keys.PublicKey).Equal) { + return } var acc *wallet.Account diff --git a/pkg/util/bitfield/bitfield.go b/pkg/util/bitfield/bitfield.go index 1bd082a0d..ee5f84fb8 100644 --- a/pkg/util/bitfield/bitfield.go +++ b/pkg/util/bitfield/bitfield.go @@ -51,15 +51,7 @@ func (f Field) And(m Field) { // Equals compares two Fields and returns true if they're equal. func (f Field) Equals(o Field) bool { - if len(f) != len(o) { - return false - } - for i := range f { - if f[i] != o[i] { - return false - } - } - return true + return slices.Equal(f, o) } // IsSubset returns true when f is a subset of o (only has bits set that are