From 357bc768829cf5797c1e0756e647e7703e02129a Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Sat, 24 Aug 2024 13:07:53 +0300 Subject: [PATCH] *: use slices.Concat where appropriate Signed-off-by: Roman Khimov --- cli/nep_test/nep17_test.go | 4 ++-- pkg/core/interop/storage/find.go | 3 ++- pkg/core/mpt/helpers.go | 10 ++++++---- pkg/core/mpt/trie.go | 3 ++- pkg/core/mpt/trie_store.go | 3 ++- pkg/core/storage/store.go | 5 ++--- 6 files changed, 16 insertions(+), 12 deletions(-) diff --git a/cli/nep_test/nep17_test.go b/cli/nep_test/nep17_test.go index e7aa29393..65a980244 100644 --- a/cli/nep_test/nep17_test.go +++ b/cli/nep_test/nep17_test.go @@ -4,6 +4,7 @@ import ( "io" "math/big" "path/filepath" + "slices" "strconv" "strings" "testing" @@ -133,8 +134,7 @@ func TestNEP17Transfer(t *testing.T) { } t.Run("missing receiver", func(t *testing.T) { - as := append([]string{}, args[:8]...) - as = append(as, args[10:]...) + as := slices.Concat(args[:8], args[10:]) e.In.WriteString("one\r") e.RunWithErrorCheck(t, `Required flag "to" not set`, as...) e.In.Reset() diff --git a/pkg/core/interop/storage/find.go b/pkg/core/interop/storage/find.go index dacf5a83f..e153d6f00 100644 --- a/pkg/core/interop/storage/find.go +++ b/pkg/core/interop/storage/find.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "slices" "github.com/nspcc-dev/neo-go/pkg/core/interop" "github.com/nspcc-dev/neo-go/pkg/core/storage" @@ -61,7 +62,7 @@ func (s *Iterator) Value() stackitem.Item { } key := s.curr.Key if s.opts&FindRemovePrefix == 0 { - key = append(bytes.Clone(s.prefix), key...) + key = slices.Concat(s.prefix, key) } if s.opts&FindKeysOnly != 0 { return stackitem.NewByteArray(key) diff --git a/pkg/core/mpt/helpers.go b/pkg/core/mpt/helpers.go index 27167c593..fcf3b076c 100644 --- a/pkg/core/mpt/helpers.go +++ b/pkg/core/mpt/helpers.go @@ -1,6 +1,10 @@ package mpt -import "github.com/nspcc-dev/neo-go/pkg/util" +import ( + "slices" + + "github.com/nspcc-dev/neo-go/pkg/util" +) // lcp returns the longest common prefix of a and b. // Note: it does no allocations. @@ -85,9 +89,7 @@ func GetChildrenPaths(path []byte, node Node) map[util.Uint256][][]byte { } case *ExtensionNode: if n.next.Type() == HashT { - cPath := make([]byte, len(path)+len(n.key)) - copy(cPath, path) - copy(cPath[len(path):], n.key) + cPath := slices.Concat(path, n.key) res[n.next.Hash()] = [][]byte{cPath} } default: diff --git a/pkg/core/mpt/trie.go b/pkg/core/mpt/trie.go index 0d86d16f0..f0e807c0b 100644 --- a/pkg/core/mpt/trie.go +++ b/pkg/core/mpt/trie.go @@ -5,6 +5,7 @@ import ( "encoding/binary" "errors" "fmt" + "slices" "github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/io" @@ -616,7 +617,7 @@ func (t *Trie) Find(prefix, from []byte, maxNum int) ([]storage.KeyValue, error) if leaf, ok := node.(*LeafNode); ok { if from == nil || !bytes.Equal(pathToNode, from) { // (*Billet).traverse includes `from` path into result if so. Need to filter out manually. res = append(res, storage.KeyValue{ - Key: append(bytes.Clone(prefix), pathToNode...), + Key: slices.Concat(prefix, pathToNode), Value: bytes.Clone(leaf.value), }) count++ diff --git a/pkg/core/mpt/trie_store.go b/pkg/core/mpt/trie_store.go index 924ea7811..3ab800088 100644 --- a/pkg/core/mpt/trie_store.go +++ b/pkg/core/mpt/trie_store.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "fmt" + "slices" "github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/util" @@ -98,7 +99,7 @@ func (m *TrieStore) Seek(rng storage.SeekRange, f func(k, v []byte) bool) { if leaf, ok := node.(*LeafNode); ok { // (*Billet).traverse includes `from` path into the result if so. It's OK for Seek, so shouldn't be filtered out. kv := storage.KeyValue{ - Key: append(bytes.Clone(rng.Prefix), pathToNode...), // Do not cut prefix. + Key: slices.Concat(rng.Prefix, pathToNode), // Do not cut prefix. Value: bytes.Clone(leaf.value), } return !f(kv.Key, kv.Value) // Should return whether to stop. diff --git a/pkg/core/storage/store.go b/pkg/core/storage/store.go index e21804004..3ee884467 100644 --- a/pkg/core/storage/store.go +++ b/pkg/core/storage/store.go @@ -3,6 +3,7 @@ package storage import ( "errors" "fmt" + "slices" "github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig" "github.com/nspcc-dev/neo-go/pkg/core/storage/dboper" @@ -105,10 +106,8 @@ type ( func seekRangeToPrefixes(sr SeekRange) *util.Range { var ( rang *util.Range - start = make([]byte, len(sr.Prefix)+len(sr.Start)) + start = slices.Concat(sr.Prefix, sr.Start) ) - copy(start, sr.Prefix) - copy(start[len(sr.Prefix):], sr.Start) if !sr.Backwards { rang = util.BytesPrefix(sr.Prefix)