From 8162e9033ddeb7622144fabc2bb6b769a01e3018 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Mon, 4 Mar 2024 21:09:36 +0300 Subject: [PATCH] *: replace slice.Copy with bytes.Clone And refactor some code a bit, don't use bytes.Clone where type-specific helpers may be used instead. Close #2907. Signed-off-by: Anna Shaleva --- cli/wallet/legacy_test.go | 12 ++++++------ pkg/core/blockchain.go | 3 +-- pkg/core/blockchain_core_test.go | 6 +++--- pkg/core/dao/dao.go | 6 +++--- pkg/core/interop/contract/account_test.go | 6 +++--- pkg/core/interop/storage/find.go | 6 +++--- pkg/core/mpt/billet.go | 3 +-- pkg/core/mpt/proof.go | 9 ++++----- pkg/core/mpt/trie.go | 9 ++++----- pkg/core/mpt/trie_store.go | 5 ++--- pkg/core/native/oracle.go | 6 +++--- pkg/core/native/std.go | 2 +- pkg/core/statesync/module_test.go | 10 +++++----- pkg/core/statesync/neotest_test.go | 8 ++++---- pkg/core/storage/boltdb_store.go | 3 +-- pkg/core/storage/memcached_store.go | 8 +++----- pkg/core/storage/memcached_store_test.go | 3 +-- pkg/core/storage/storeandbatch_test.go | 5 ++--- pkg/services/notary/notary.go | 5 ++--- pkg/services/oracle/oracle.go | 6 +++--- pkg/services/oracle/oracle_test.go | 5 ++--- pkg/services/rpcsrv/server.go | 3 +-- pkg/util/slice/array.go | 7 ------- pkg/util/slice/array_test.go | 5 +++-- pkg/vm/stackitem/item.go | 13 ++++++------- pkg/vm/vm.go | 3 +-- 26 files changed, 68 insertions(+), 89 deletions(-) diff --git a/cli/wallet/legacy_test.go b/cli/wallet/legacy_test.go index 8a80ac00c..388d04841 100644 --- a/cli/wallet/legacy_test.go +++ b/cli/wallet/legacy_test.go @@ -1,12 +1,12 @@ package wallet import ( + "bytes" "crypto/elliptic" "encoding/hex" "testing" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/stretchr/testify/require" ) @@ -36,27 +36,27 @@ func TestParseMultisigContract(t *testing.T) { testParseMultisigContract(t, s, 1, pub) }) t.Run("bad, no check multisig", func(t *testing.T) { - sBad := slice.Copy(s) + sBad := bytes.Clone(s) sBad[len(sBad)-1] ^= 0xFF testParseMultisigContract(t, sBad, 0) }) t.Run("bad, invalid number of keys", func(t *testing.T) { - sBad := slice.Copy(s) + sBad := bytes.Clone(s) sBad[len(sBad)-2] = opPush1 + 1 testParseMultisigContract(t, sBad, 0) }) t.Run("bad, invalid first instruction", func(t *testing.T) { - sBad := slice.Copy(s) + sBad := bytes.Clone(s) sBad[0] = 0xFF testParseMultisigContract(t, sBad, 0) }) t.Run("bad, invalid public key", func(t *testing.T) { - sBad := slice.Copy(s) + sBad := bytes.Clone(s) sBad[2] = 0xFF testParseMultisigContract(t, sBad, 0) }) t.Run("bad, many sigs", func(t *testing.T) { - sBad := slice.Copy(s) + sBad := bytes.Clone(s) sBad[0] = opPush1 + 1 testParseMultisigContract(t, sBad, 0) }) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index eaaa75fb9..05a18a88a 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -36,7 +36,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" "github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neo-go/pkg/vm" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neo-go/pkg/vm/vmstate" @@ -1172,7 +1171,7 @@ func (bc *Blockchain) resetTransfers(cache *dao.Simple, height uint32) error { trInfo = nil removeFollowing = false } else if removeFollowing { - cache.Store.Delete(slice.Copy(k)) + cache.Store.Delete(bytes.Clone(k)) return seekErr == nil } diff --git a/pkg/core/blockchain_core_test.go b/pkg/core/blockchain_core_test.go index 0e7f6bff5..111202829 100644 --- a/pkg/core/blockchain_core_test.go +++ b/pkg/core/blockchain_core_test.go @@ -1,6 +1,7 @@ package core import ( + "bytes" "encoding/binary" "fmt" "strings" @@ -17,7 +18,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neo-go/pkg/vm/opcode" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -175,9 +175,9 @@ func TestBlockchain_InitWithIncompleteStateJump(t *testing.T) { bPrefix := make([]byte, 1) bPrefix[0] = byte(bcSpout.dao.Version.StoragePrefix) bcSpout.dao.Store.Seek(storage.SeekRange{Prefix: bPrefix}, func(k, v []byte) bool { - key := slice.Copy(k) + key := bytes.Clone(k) key[0] = byte(tempPrefix) - value := slice.Copy(v) + value := bytes.Clone(v) batch.Put(key, value) return true }) diff --git a/pkg/core/dao/dao.go b/pkg/core/dao/dao.go index 61c24c3e8..218be9123 100644 --- a/pkg/core/dao/dao.go +++ b/pkg/core/dao/dao.go @@ -1,6 +1,7 @@ package dao import ( + "bytes" "context" "encoding/binary" "errors" @@ -19,7 +20,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" ) @@ -383,7 +383,7 @@ func (dao *Simple) DeleteStorageItem(id int32, key []byte) { // may not be copied. Seek continues iterating until false is returned from f. A requested prefix // (if any non-empty) is trimmed before passing to f. func (dao *Simple) Seek(id int32, rng storage.SeekRange, f func(k, v []byte) bool) { - rng.Prefix = slice.Copy(dao.makeStorageItemKey(id, rng.Prefix)) // f() can use dao too. + rng.Prefix = bytes.Clone(dao.makeStorageItemKey(id, rng.Prefix)) // f() can use dao too. dao.Store.Seek(rng, func(k, v []byte) bool { return f(k[len(rng.Prefix):], v) }) @@ -393,7 +393,7 @@ func (dao *Simple) Seek(id int32, rng storage.SeekRange, f func(k, v []byte) boo // starting from the point specified) to a channel and returns the channel. // Resulting keys and values may not be copied. func (dao *Simple) SeekAsync(ctx context.Context, id int32, rng storage.SeekRange) chan storage.KeyValue { - rng.Prefix = slice.Copy(dao.makeStorageItemKey(id, rng.Prefix)) + rng.Prefix = bytes.Clone(dao.makeStorageItemKey(id, rng.Prefix)) return dao.Store.SeekAsync(ctx, rng, true) } diff --git a/pkg/core/interop/contract/account_test.go b/pkg/core/interop/contract/account_test.go index e03a4ea1e..cb69de97a 100644 --- a/pkg/core/interop/contract/account_test.go +++ b/pkg/core/interop/contract/account_test.go @@ -1,6 +1,7 @@ package contract_test import ( + "bytes" "math" "math/big" "testing" @@ -15,7 +16,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/neotest/chain" "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neo-go/pkg/vm/emit" "github.com/stretchr/testify/require" ) @@ -136,13 +136,13 @@ func TestCreateAccount_HFAspidochelone(t *testing.T) { emit.Int(w.BinWriter, int64(2)) emit.Syscall(w.BinWriter, interopnames.SystemContractCreateMultisigAccount) require.NoError(t, w.Err) - multisigScript := slice.Copy(w.Bytes()) + multisigScript := bytes.Clone(w.Bytes()) w.Reset() emit.Bytes(w.BinWriter, pub.Bytes()) emit.Syscall(w.BinWriter, interopnames.SystemContractCreateStandardAccount) require.NoError(t, w.Err) - standardScript := slice.Copy(w.Bytes()) + standardScript := bytes.Clone(w.Bytes()) createAccTx := func(t *testing.T, script []byte) *transaction.Transaction { tx := e.PrepareInvocation(t, script, []neotest.Signer{e.Committee}, bc.BlockHeight()+1) diff --git a/pkg/core/interop/storage/find.go b/pkg/core/interop/storage/find.go index 8a3cc3ba5..dacf5a83f 100644 --- a/pkg/core/interop/storage/find.go +++ b/pkg/core/interop/storage/find.go @@ -1,12 +1,12 @@ package storage import ( + "bytes" "context" "fmt" "github.com/nspcc-dev/neo-go/pkg/core/interop" "github.com/nspcc-dev/neo-go/pkg/core/storage" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" ) @@ -42,7 +42,7 @@ func NewIterator(seekCh chan storage.KeyValue, prefix []byte, opts int64) *Itera return &Iterator{ seekCh: seekCh, opts: opts, - prefix: slice.Copy(prefix), + prefix: bytes.Clone(prefix), } } @@ -61,7 +61,7 @@ func (s *Iterator) Value() stackitem.Item { } key := s.curr.Key if s.opts&FindRemovePrefix == 0 { - key = append(slice.Copy(s.prefix), key...) + key = append(bytes.Clone(s.prefix), key...) } if s.opts&FindKeysOnly != 0 { return stackitem.NewByteArray(key) diff --git a/pkg/core/mpt/billet.go b/pkg/core/mpt/billet.go index cba44afa0..10acfdd19 100644 --- a/pkg/core/mpt/billet.go +++ b/pkg/core/mpt/billet.go @@ -9,7 +9,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" ) var ( @@ -230,7 +229,7 @@ func (b *Billet) traverse(curr Node, path, from []byte, process func(pathToNode return b.traverse(r, path, from, process, ignoreStorageErr, backwards) } if len(from) == 0 { - bytes := slice.Copy(curr.Bytes()) + bytes := bytes.Clone(curr.Bytes()) if process(fromNibbles(path), curr, bytes) { return curr, errStop } diff --git a/pkg/core/mpt/proof.go b/pkg/core/mpt/proof.go index 7a02ebbb9..76b4901a0 100644 --- a/pkg/core/mpt/proof.go +++ b/pkg/core/mpt/proof.go @@ -7,7 +7,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" ) // GetProof returns a proof that the key belongs to t. @@ -30,11 +29,11 @@ func (t *Trie) getProof(curr Node, path []byte, proofs *[][]byte) (Node, error) switch n := curr.(type) { case *LeafNode: if len(path) == 0 { - *proofs = append(*proofs, slice.Copy(n.Bytes())) + *proofs = append(*proofs, bytes.Clone(n.Bytes())) return n, nil } case *BranchNode: - *proofs = append(*proofs, slice.Copy(n.Bytes())) + *proofs = append(*proofs, bytes.Clone(n.Bytes())) i, path := splitPath(path) r, err := t.getProof(n.Children[i], path, proofs) if err != nil { @@ -44,7 +43,7 @@ func (t *Trie) getProof(curr Node, path []byte, proofs *[][]byte) (Node, error) return n, nil case *ExtensionNode: if bytes.HasPrefix(path, n.key) { - *proofs = append(*proofs, slice.Copy(n.Bytes())) + *proofs = append(*proofs, bytes.Clone(n.Bytes())) r, err := t.getProof(n.next, path[len(n.key):], proofs) if err != nil { return nil, err @@ -75,5 +74,5 @@ func VerifyProof(rh util.Uint256, key []byte, proofs [][]byte) ([]byte, bool) { if err != nil { return nil, false } - return slice.Copy(leaf.(*LeafNode).value), true + return bytes.Clone(leaf.(*LeafNode).value), true } diff --git a/pkg/core/mpt/trie.go b/pkg/core/mpt/trie.go index 046b074fb..8b34bac80 100644 --- a/pkg/core/mpt/trie.go +++ b/pkg/core/mpt/trie.go @@ -9,7 +9,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" ) // TrieMode is the storage mode of a trie, it affects the DB scheme. @@ -84,7 +83,7 @@ func (t *Trie) Get(key []byte) ([]byte, error) { return nil, err } t.root = r - return slice.Copy(leaf.(*LeafNode).value), nil + return bytes.Clone(leaf.(*LeafNode).value), nil } // getWithPath returns the current node with all hash nodes along the path replaced @@ -221,7 +220,7 @@ func (t *Trie) putIntoExtension(curr *ExtensionNode, path []byte, val Node) (Nod t.addRef(b.Hash(), b.bytes) if lp > 0 { - e := NewExtensionNode(slice.Copy(pref), b) + e := NewExtensionNode(bytes.Clone(pref), b) t.addRef(e.Hash(), e.bytes) return e, nil } @@ -617,8 +616,8 @@ func (t *Trie) Find(prefix, from []byte, max 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(slice.Copy(prefix), pathToNode...), - Value: slice.Copy(leaf.value), + Key: append(bytes.Clone(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 b07cc99df..7c7a158fb 100644 --- a/pkg/core/mpt/trie_store.go +++ b/pkg/core/mpt/trie_store.go @@ -7,7 +7,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" ) // TrieStore is an MPT-based storage implementation for storing and retrieving @@ -101,8 +100,8 @@ 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(slice.Copy(rng.Prefix), pathToNode...), // Do not cut prefix. - Value: slice.Copy(leaf.value), + Key: append(bytes.Clone(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/native/oracle.go b/pkg/core/native/oracle.go index d07447eaf..012fc3064 100644 --- a/pkg/core/native/oracle.go +++ b/pkg/core/native/oracle.go @@ -1,6 +1,7 @@ package native import ( + "bytes" "encoding/binary" "errors" "fmt" @@ -25,7 +26,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" ) @@ -160,7 +160,7 @@ func newOracle() *Oracle { // GetOracleResponseScript returns a script for the transaction with an oracle response. func (o *Oracle) GetOracleResponseScript() []byte { - return slice.Copy(o.oracleScript) + return bytes.Clone(o.oracleScript) } // OnPersist implements the Contract interface. @@ -395,7 +395,7 @@ func (o *Oracle) RequestInternal(ic *interop.Context, url string, filter *string if len(data) > maxUserDataLength { return ErrBigArgument } - data = slice.Copy(data) // Serialization context will be used in PutRequestInternal again. + data = bytes.Clone(data) // Serialization context will be used in PutRequestInternal again. var filterNotif stackitem.Item if filter != nil { diff --git a/pkg/core/native/std.go b/pkg/core/native/std.go index 32c17fa62..90d2c579c 100644 --- a/pkg/core/native/std.go +++ b/pkg/core/native/std.go @@ -177,7 +177,7 @@ func (s *Std) serialize(ic *interop.Context, args []stackitem.Item) stackitem.It panic(errors.New("too big item")) } - return stackitem.NewByteArray(slice.Copy(data)) // Serialization context can be reused. + return stackitem.NewByteArray(bytes.Clone(data)) // Serialization context can be reused. } func (s *Std) deserialize(_ *interop.Context, args []stackitem.Item) stackitem.Item { diff --git a/pkg/core/statesync/module_test.go b/pkg/core/statesync/module_test.go index 893dfe07e..3ead29e43 100644 --- a/pkg/core/statesync/module_test.go +++ b/pkg/core/statesync/module_test.go @@ -1,6 +1,7 @@ package statesync import ( + "bytes" "fmt" "testing" @@ -8,7 +9,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/mpt" "github.com/nspcc-dev/neo-go/pkg/core/storage" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/stretchr/testify/require" "go.uber.org/zap/zaptest" ) @@ -33,8 +33,8 @@ func TestModule_PR2019_discussion_r689629704(t *testing.T) { expectedItems []storage.KeyValue ) expectedStorage.Seek(storage.SeekRange{Prefix: []byte{byte(storage.DataMPT)}}, func(k, v []byte) bool { - key := slice.Copy(k) - value := slice.Copy(v) + key := bytes.Clone(k) + value := bytes.Clone(v) expectedItems = append(expectedItems, storage.KeyValue{ Key: key, Value: value, @@ -97,8 +97,8 @@ func TestModule_PR2019_discussion_r689629704(t *testing.T) { // Compare resulting storage items and refcounts. var actualItems []storage.KeyValue expectedStorage.Seek(storage.SeekRange{Prefix: []byte{byte(storage.DataMPT)}}, func(k, v []byte) bool { - key := slice.Copy(k) - value := slice.Copy(v) + key := bytes.Clone(k) + value := bytes.Clone(v) actualItems = append(actualItems, storage.KeyValue{ Key: key, Value: value, diff --git a/pkg/core/statesync/neotest_test.go b/pkg/core/statesync/neotest_test.go index 47836cc61..d69325a5c 100644 --- a/pkg/core/statesync/neotest_test.go +++ b/pkg/core/statesync/neotest_test.go @@ -1,6 +1,7 @@ package statesync_test import ( + "bytes" "testing" "github.com/nspcc-dev/neo-go/internal/basicchain" @@ -11,7 +12,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/neotest" "github.com/nspcc-dev/neo-go/pkg/neotest/chain" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/stretchr/testify/require" ) @@ -230,7 +230,7 @@ func TestStateSyncModule_Init(t *testing.T) { alreadyRequested[unknownHashes[0]] = struct{}{} var callbackCalled bool err := bcSpout.GetStateSyncModule().Traverse(unknownHashes[0], func(node mpt.Node, nodeBytes []byte) bool { - require.NoError(t, module.AddMPTNodes([][]byte{slice.Copy(nodeBytes)})) + require.NoError(t, module.AddMPTNodes([][]byte{bytes.Clone(nodeBytes)})) callbackCalled = true return true // add nodes one-by-one }) @@ -435,8 +435,8 @@ func TestStateSyncModule_RestoreBasicChain(t *testing.T) { fetchStorage := func(ps storage.Store, storagePrefix byte) []storage.KeyValue { var kv []storage.KeyValue ps.Seek(storage.SeekRange{Prefix: []byte{storagePrefix}}, func(k, v []byte) bool { - key := slice.Copy(k) - value := slice.Copy(v) + key := bytes.Clone(k) + value := bytes.Clone(v) if key[0] == byte(storage.STTempStorage) { key[0] = byte(storage.STStorage) } diff --git a/pkg/core/storage/boltdb_store.go b/pkg/core/storage/boltdb_store.go index fba36ad9a..f313dcd2e 100644 --- a/pkg/core/storage/boltdb_store.go +++ b/pkg/core/storage/boltdb_store.go @@ -9,7 +9,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/storage/dbconfig" "github.com/nspcc-dev/neo-go/pkg/io" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "go.etcd.io/bbolt" ) @@ -81,7 +80,7 @@ func (s *BoltDBStore) Get(key []byte) (val []byte, err error) { val = b.Get(key) // Value from Get is only valid for the lifetime of transaction, #1482 if val != nil { - val = slice.Copy(val) + val = bytes.Clone(val) } return nil }) diff --git a/pkg/core/storage/memcached_store.go b/pkg/core/storage/memcached_store.go index f91c74fc4..ad476ad3f 100644 --- a/pkg/core/storage/memcached_store.go +++ b/pkg/core/storage/memcached_store.go @@ -6,8 +6,6 @@ import ( "sort" "strings" "sync" - - "github.com/nspcc-dev/neo-go/pkg/util/slice" ) // MemCachedStore is a wrapper around persistent store that caches all changes @@ -107,7 +105,7 @@ func (s *MemCachedStore) Get(key []byte) ([]byte, error) { // Put puts new KV pair into the store. func (s *MemCachedStore) Put(key, value []byte) { newKey := string(key) - vcopy := slice.Copy(value) + vcopy := bytes.Clone(value) s.lock() put(s.chooseMap(key), newKey, vcopy) s.unlock() @@ -259,8 +257,8 @@ func performSeek(ctx context.Context, ps Store, memRes []KeyValueExists, rng See return false } kvPs := KeyValue{ - Key: slice.Copy(k), - Value: slice.Copy(v), + Key: bytes.Clone(k), + Value: bytes.Clone(v), } for { select { diff --git a/pkg/core/storage/memcached_store_test.go b/pkg/core/storage/memcached_store_test.go index 0472c9817..0f2defe65 100644 --- a/pkg/core/storage/memcached_store_test.go +++ b/pkg/core/storage/memcached_store_test.go @@ -7,7 +7,6 @@ import ( "testing" "github.com/nspcc-dev/neo-go/internal/random" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -420,7 +419,7 @@ func TestCachedSeekSorting(t *testing.T) { } var foundKVs []KeyValue ts.Seek(SeekRange{Prefix: goodPrefix}, func(k, v []byte) bool { - foundKVs = append(foundKVs, KeyValue{Key: slice.Copy(k), Value: slice.Copy(v)}) + foundKVs = append(foundKVs, KeyValue{Key: bytes.Clone(k), Value: bytes.Clone(v)}) return true }) assert.Equal(t, len(foundKVs), len(lowerKVs)+len(updatedKVs)) diff --git a/pkg/core/storage/storeandbatch_test.go b/pkg/core/storage/storeandbatch_test.go index 88724fe10..7c5f64325 100644 --- a/pkg/core/storage/storeandbatch_test.go +++ b/pkg/core/storage/storeandbatch_test.go @@ -7,7 +7,6 @@ import ( "sort" "testing" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -70,8 +69,8 @@ func testStoreSeek(t *testing.T, s Store) { actual := make([]KeyValue, 0, len(goodkvs)) s.Seek(rng, func(k, v []byte) bool { actual = append(actual, KeyValue{ - Key: slice.Copy(k), - Value: slice.Copy(v), + Key: bytes.Clone(k), + Value: bytes.Clone(v), }) if cont == nil { return true diff --git a/pkg/services/notary/notary.go b/pkg/services/notary/notary.go index 205891c27..1f75f9e45 100644 --- a/pkg/services/notary/notary.go +++ b/pkg/services/notary/notary.go @@ -20,7 +20,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/network/payload" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neo-go/pkg/vm" "github.com/nspcc-dev/neo-go/pkg/vm/opcode" "github.com/nspcc-dev/neo-go/pkg/wallet" @@ -354,8 +353,8 @@ func safeCopy(tx *transaction.Transaction) *transaction.Transaction { cp.Scripts = make([]transaction.Witness, len(tx.Scripts)) for i := range cp.Scripts { cp.Scripts[i] = transaction.Witness{ - InvocationScript: slice.Copy(tx.Scripts[i].InvocationScript), - VerificationScript: slice.Copy(tx.Scripts[i].VerificationScript), + InvocationScript: bytes.Clone(tx.Scripts[i].InvocationScript), + VerificationScript: bytes.Clone(tx.Scripts[i].VerificationScript), } } return &cp diff --git a/pkg/services/oracle/oracle.go b/pkg/services/oracle/oracle.go index b9612f4a0..0868adeda 100644 --- a/pkg/services/oracle/oracle.go +++ b/pkg/services/oracle/oracle.go @@ -1,6 +1,7 @@ package oracle import ( + "bytes" "errors" "net/http" "sync" @@ -16,7 +17,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/services/oracle/broadcaster" "github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neo-go/pkg/wallet" "go.uber.org/zap" ) @@ -277,8 +277,8 @@ drain: // UpdateNativeContract updates native oracle contract info for tx verification. func (o *Oracle) UpdateNativeContract(script, resp []byte, h util.Uint160, verifyOffset int) { - o.oracleScript = slice.Copy(script) - o.oracleResponse = slice.Copy(resp) + o.oracleScript = bytes.Clone(script) + o.oracleResponse = bytes.Clone(resp) o.oracleHash = h o.verifyOffset = verifyOffset diff --git a/pkg/services/oracle/oracle_test.go b/pkg/services/oracle/oracle_test.go index 74b33f4cf..28017ef33 100644 --- a/pkg/services/oracle/oracle_test.go +++ b/pkg/services/oracle/oracle_test.go @@ -30,7 +30,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/services/oracle" "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/stretchr/testify/assert" @@ -163,8 +162,8 @@ func TestOracle(t *testing.T) { md := nativeOracleState.Manifest.ABI.GetMethod(manifest.MethodVerify, -1) require.NotNil(t, md) oracleRespScript := native.CreateOracleResponseScript(nativeOracleH) - orc1.UpdateNativeContract(nativeOracleState.NEF.Script, slice.Copy(oracleRespScript), nativeOracleH, md.Offset) - orc2.UpdateNativeContract(nativeOracleState.NEF.Script, slice.Copy(oracleRespScript), nativeOracleH, md.Offset) + orc1.UpdateNativeContract(nativeOracleState.NEF.Script, bytes.Clone(oracleRespScript), nativeOracleH, md.Offset) + orc2.UpdateNativeContract(nativeOracleState.NEF.Script, bytes.Clone(oracleRespScript), nativeOracleH, md.Offset) cs := contracts.GetOracleContractState(t, pathToInternalContracts, validator.ScriptHash(), 0) rawManifest, err := json.Marshal(cs.Manifest) diff --git a/pkg/services/rpcsrv/server.go b/pkg/services/rpcsrv/server.go index 87db5b440..6430a2ffe 100644 --- a/pkg/services/rpcsrv/server.go +++ b/pkg/services/rpcsrv/server.go @@ -52,7 +52,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest/standard" "github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neo-go/pkg/vm" "github.com/nspcc-dev/neo-go/pkg/vm/emit" "github.com/nspcc-dev/neo-go/pkg/vm/opcode" @@ -1760,7 +1759,7 @@ func (s *Server) findStorageInternal(id int32, prefix []byte, start, take int, s } if i < end { res.Results = append(res.Results, result.KeyValue{ - Key: slice.Copy(append(prefix, k...)), // Don't strip prefix, as it is done in C#. + Key: bytes.Clone(append(prefix, k...)), // Don't strip prefix, as it is done in C#. Value: v, }) i++ diff --git a/pkg/util/slice/array.go b/pkg/util/slice/array.go index d6e13f9f9..9be387e45 100644 --- a/pkg/util/slice/array.go +++ b/pkg/util/slice/array.go @@ -22,13 +22,6 @@ func reverse(dst []byte, src []byte) { } } -// Copy copies the byte slice into new slice (make/copy). -func Copy(b []byte) []byte { - d := make([]byte, len(b)) - copy(d, b) - return d -} - // Clean wipes the data in b by filling it with zeros. func Clean(b []byte) { for i := range b { diff --git a/pkg/util/slice/array_test.go b/pkg/util/slice/array_test.go index a5b8f54d6..ae7ccd162 100644 --- a/pkg/util/slice/array_test.go +++ b/pkg/util/slice/array_test.go @@ -1,6 +1,7 @@ package slice import ( + "bytes" "testing" "github.com/stretchr/testify/require" @@ -30,7 +31,7 @@ var testCases = []struct { func TestCopyReverse(t *testing.T) { for _, tc := range testCases { - arg := Copy(tc.arr) + arg := bytes.Clone(tc.arr) require.Equal(t, tc.arr, arg) have := CopyReverse(arg) @@ -52,7 +53,7 @@ func TestCopyReverse(t *testing.T) { func TestClean(t *testing.T) { for _, tc := range testCases[1:] { // Empty one will be equal. - cp := Copy(tc.arr) + cp := bytes.Clone(tc.arr) Clean(cp) require.NotEqual(t, tc.arr, cp) } diff --git a/pkg/vm/stackitem/item.go b/pkg/vm/stackitem/item.go index 6f0f1d02b..33ff664bf 100644 --- a/pkg/vm/stackitem/item.go +++ b/pkg/vm/stackitem/item.go @@ -14,7 +14,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/crypto/hash" "github.com/nspcc-dev/neo-go/pkg/encoding/bigint" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neo-go/pkg/util/slice" ) const ( @@ -197,7 +196,7 @@ func convertPrimitive(item Item, typ Type) (Item, error) { return nil, err } if typ == BufferT { - return NewBuffer(slice.Copy(b)), nil + return NewBuffer(bytes.Clone(b)), nil } // ByteArray can't really be changed, so it's OK to reuse `b`. return NewByteArray(b), nil @@ -687,7 +686,7 @@ func (i *ByteArray) equalsLimited(s Item, limit *int) bool { // Dup implements the Item interface. func (i *ByteArray) Dup() Item { - ba := slice.Copy(*i) + ba := bytes.Clone(*i) return (*ByteArray)(&ba) } @@ -1186,7 +1185,7 @@ func (i *Buffer) Convert(typ Type) (Item, error) { case BufferT: return i, nil case ByteArrayT: - return NewByteArray(slice.Copy(*i)), nil + return NewByteArray(bytes.Clone(*i)), nil case IntegerT: if len(*i) > MaxBigIntegerSizeBits/8 { return nil, errTooBigInteger @@ -1248,12 +1247,12 @@ func deepCopy(item Item, seen map[Item]Item, asImmutable bool) Item { bi := new(big.Int).Set(it.Big()) return (*BigInteger)(bi) case *ByteArray: - return NewByteArray(slice.Copy(*it)) + return NewByteArray(bytes.Clone(*it)) case *Buffer: if asImmutable { - return NewByteArray(slice.Copy(*it)) + return NewByteArray(bytes.Clone(*it)) } - return NewBuffer(slice.Copy(*it)) + return NewBuffer(bytes.Clone(*it)) case Bool: return it case *Pointer: diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index b175998a9..c48d993ec 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -3,7 +3,6 @@ package vm import ( "crypto/elliptic" "encoding/binary" - "encoding/hex" "encoding/json" "errors" "fmt" @@ -223,7 +222,7 @@ func (v *VM) PrintOps(out io.Writer) { // if the parameter is a 20-byte value. u, err := util.Uint160DecodeBytesBE(parameter) if err == nil { - desc = fmt.Sprintf("%x (%q, %q)", parameter, address.Uint160ToString(u), "0x"+hex.EncodeToString(slice.CopyReverse(parameter))) + desc = fmt.Sprintf("%x (%q, %q)", parameter, address.Uint160ToString(u), "0x"+u.StringLE()) } else { desc = fmt.Sprintf("%x", parameter) }