Skip to content

Commit

Permalink
*: Convert slices to arrays instead of copy where possible (#2885)
Browse files Browse the repository at this point in the history
  • Loading branch information
carpawell committed Jul 9, 2024
2 parents e6eeda1 + 35ca2f1 commit 3c616d4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 57 deletions.
2 changes: 1 addition & 1 deletion cmd/neofs-adm/internal/modules/morph/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func dumpNetworkConfig(cmd *cobra.Command, _ []string) error {
netmapEpochKey, netmapInnerRingCandidateFeeKey,
netmapMaxObjectSizeKey, netmapWithdrawFeeKey:
nbuf := make([]byte, 8)
copy(nbuf[:], v)
copy(nbuf, v)
n := binary.LittleEndian.Uint64(nbuf)
_, _ = tw.Write([]byte(fmt.Sprintf("%s:\t%d (int)\n", k, n)))
case netmapEigenTrustAlphaKey:
Expand Down
66 changes: 21 additions & 45 deletions cmd/neofs-adm/internal/modules/morph/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"errors"
"fmt"
"os"
"sort"

"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
Expand Down Expand Up @@ -59,6 +57,11 @@ func dumpContainers(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("invalid filename: %w", err)
}

requestedIDs, err := getCIDs(cmd)
if err != nil {
return err
}

c, err := getN3Client(viper.GetViper())
if err != nil {
return fmt.Errorf("can't create N3 client: %w", err)
Expand All @@ -71,25 +74,12 @@ func dumpContainers(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("unable to get contaract hash: %w", err)
}

cids, err := getContainersList(inv, ch)
if err != nil {
return fmt.Errorf("%w: %w", errInvalidContainerResponse, err)
}

isOK, err := getCIDFilterFunc(cmd)
if err != nil {
return err
}

var containers []*Container
b := smartcontract.NewBuilder()
for _, id := range cids {
if !isOK(id) {
continue
}
for id := range requestedIDs {
b.Reset()
b.InvokeMethod(ch, "get", id)
b.InvokeMethod(ch, "eACL", id)
b.InvokeMethod(ch, "get", id[:])
b.InvokeMethod(ch, "eACL", id[:])

script, err := b.Script()
if err != nil {
Expand Down Expand Up @@ -191,19 +181,20 @@ func restoreContainers(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("can't parse dump file: %w", err)
}

isOK, err := getCIDFilterFunc(cmd)
requested, err := getCIDs(cmd)
if err != nil {
return err
}

var id cid.ID
b := smartcontract.NewBuilder()
for _, cnt := range containers {
hv := hash.Sha256(cnt.Value)
if !isOK(hv[:]) {
id.FromBinary(cnt.Value)
if _, ok := requested[id]; !ok {
continue
}
b.Reset()
b.InvokeMethod(ch, "get", hv.BytesBE())
b.InvokeMethod(ch, "get", id[:])

script, err := b.Script()
if err != nil {
Expand All @@ -223,8 +214,6 @@ func restoreContainers(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("%w: %w", errInvalidContainerResponse, err)
}
if len(old.Value) != 0 {
var id cid.ID
id.SetSHA256(hv)
cmd.Printf("Container %s is already deployed.\n", id)
continue
}
Expand Down Expand Up @@ -353,33 +342,20 @@ func (c *EACL) FromStackItem(item stackitem.Item) error {
return nil
}

// getCIDFilterFunc returns filtering function for container IDs.
// Raw byte slices are used because it works with structures returned
// from contract.
func getCIDFilterFunc(cmd *cobra.Command) (func([]byte) bool, error) {
// returns set of cid.ID specified via the 'cid' command flag. Function allows
// duplicates.
func getCIDs(cmd *cobra.Command) (map[cid.ID]struct{}, error) {
rawIDs, err := cmd.Flags().GetStringSlice(containerIDsFlag)
if err != nil {
return nil, err
}
if len(rawIDs) == 0 {
return func([]byte) bool { return true }, nil
}

var id cid.ID
res := make(map[cid.ID]struct{}, len(rawIDs))
for i := range rawIDs {
err := new(cid.ID).DecodeString(rawIDs[i])
if err != nil {
if err = id.DecodeString(rawIDs[i]); err != nil {
return nil, fmt.Errorf("can't parse CID %s: %w", rawIDs[i], err)
}
res[id] = struct{}{}
}
sort.Strings(rawIDs)
return func(rawID []byte) bool {
var v [32]byte
copy(v[:], rawID)

var id cid.ID
id.SetSHA256(v)
idStr := id.EncodeToString()
n := sort.Search(len(rawIDs), func(i int) bool { return rawIDs[i] >= idStr })
return n < len(rawIDs) && rawIDs[n] == idStr
}, nil
return res, nil
}
6 changes: 2 additions & 4 deletions cmd/neofs-node/reputation/intermediate/contract.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package intermediate

import (
"bytes"
"crypto/ecdsa"
"fmt"

Expand Down Expand Up @@ -80,11 +81,8 @@ func (fw FinalWriter) WriteIntermediateTrust(t eigentrust.IterationTrust) error
apiTrust.SetValue(t.Value().Float64())
apiTrust.SetPeer(t.Peer())

var managerPublicKey [33]byte
copy(managerPublicKey[:], fw.pubKey)

var apiMangerPeerID apireputation.PeerID
apiMangerPeerID.SetPublicKey(managerPublicKey[:])
apiMangerPeerID.SetPublicKey(bytes.Clone(fw.pubKey))

var gTrust apireputation.GlobalTrust
gTrust.SetTrust(apiTrust)
Expand Down
6 changes: 2 additions & 4 deletions pkg/local_object_storage/metabase/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,7 @@ func TestDB_SelectPayloadHash(t *testing.T) {
require.NoError(t, err)

cs, _ := raw1.PayloadChecksum()
var payloadHash [sha256.Size]byte
copy(payloadHash[:], cs.Value())
payloadHash := [sha256.Size]byte(cs.Value())

fs := objectSDK.SearchFilters{}
fs.AddPayloadHashFilter(objectSDK.MatchStringEqual, payloadHash)
Expand Down Expand Up @@ -444,8 +443,7 @@ func TestDB_SelectWithSlowFilters(t *testing.T) {

t.Run("object with TZHash", func(t *testing.T) {
cs, _ := raw1.PayloadHomomorphicHash()
var homoHash [tz.Size]byte
copy(homoHash[:], cs.Value())
homoHash := [tz.Size]byte(cs.Value())

fs := objectSDK.SearchFilters{}
fs.AddHomomorphicHashFilter(objectSDK.MatchStringEqual, homoHash)
Expand Down
4 changes: 1 addition & 3 deletions pkg/services/object_manager/storagegroup/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ func CollectMembers(r objutil.ObjectSource, cnr cid.ID, members []oid.ID, calcHo
}

var cs checksum.Checksum
tzHash := [64]byte{}
copy(tzHash[:], sumHash)
cs.SetTillichZemor(tzHash)
cs.SetTillichZemor([tz.Size]byte(sumHash))

sg.SetValidationDataHash(cs)
}
Expand Down

0 comments on commit 3c616d4

Please sign in to comment.