From f71061b2af6b867a5d43dcde85943c0ea690c5c9 Mon Sep 17 00:00:00 2001 From: AnkushinDaniil Date: Wed, 31 Jul 2024 16:52:57 +0300 Subject: [PATCH 1/5] slices, for loop copy, min/max update --- blockchain/blockchain_test.go | 3 ++- core/block_test.go | 3 +-- core/state.go | 6 ++---- core/transaction.go | 6 ++---- db/buckets_test.go | 3 ++- db/buffered_transaction.go | 3 ++- db/pebble/iterator.go | 8 ++++---- l1/l1_pkg_test.go | 1 - migration/migration.go | 3 ++- rpc/block_test.go | 2 -- rpc/events_test.go | 3 ++- sync/sync.go | 6 +----- upgrader/upgrader_test.go | 1 - vm/trace.go | 12 ++++++------ vm/vm.go | 4 ++-- 15 files changed, 28 insertions(+), 36 deletions(-) diff --git a/blockchain/blockchain_test.go b/blockchain/blockchain_test.go index c0c3666a91..51c753339c 100644 --- a/blockchain/blockchain_test.go +++ b/blockchain/blockchain_test.go @@ -3,6 +3,7 @@ package blockchain_test import ( "context" "fmt" + "slices" "testing" "time" @@ -515,7 +516,7 @@ func TestEvents(t *testing.T) { for i := 0; i < len(allEvents)+1; i++ { gotEvents, lastToken, err = filter.Events(lastToken, chunkSize) require.NoError(t, err) - accEvents = append(accEvents, gotEvents...) + accEvents = slices.Concat(accEvents, gotEvents) if lastToken == nil { break } diff --git a/core/block_test.go b/core/block_test.go index 81cac95e5b..55a09fd171 100644 --- a/core/block_test.go +++ b/core/block_test.go @@ -171,8 +171,7 @@ func TestBlockHash(t *testing.T) { }, } - for _, testcase := range tests { - tc := testcase + for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { t.Parallel() client := feeder.NewTestClient(t, &tc.chain) diff --git a/core/state.go b/core/state.go index 96b5c3548a..aee9d44a50 100644 --- a/core/state.go +++ b/core/state.go @@ -6,7 +6,7 @@ import ( "errors" "fmt" "runtime" - "sort" + "slices" "github.com/NethermindEth/juno/core/crypto" "github.com/NethermindEth/juno/core/felt" @@ -400,9 +400,7 @@ func (s *State) updateContractStorages(stateTrie *trie.Trie, diffs map[felt.Felt for key := range diffs { keys = append(keys, key) } - sort.SliceStable(keys, func(i, j int) bool { - return len(diffs[keys[i]]) > len(diffs[keys[j]]) - }) + slices.SortStableFunc(keys, func(a, b felt.Felt) int { return a.Cmp(&b) }) // update per-contract storage Tries concurrently contractUpdaters := pool.NewWithResults[*bufferedTransactionWithAddress]().WithErrors().WithMaxGoroutines(runtime.GOMAXPROCS(0)) diff --git a/core/transaction.go b/core/transaction.go index 194a7b9ae7..a534e5e408 100644 --- a/core/transaction.go +++ b/core/transaction.go @@ -570,8 +570,7 @@ func l1HandlerTransactionHash(l *L1HandlerTransaction, n *utils.Network) (*felt. } func deployAccountTransactionHash(d *DeployAccountTransaction, n *utils.Network) (*felt.Felt, error) { - callData := []*felt.Felt{d.ClassHash, d.ContractAddressSalt} - callData = append(callData, d.ConstructorCallData...) + callData := slices.Concat([]*felt.Felt{d.ClassHash, d.ContractAddressSalt}, d.ConstructorCallData) // There is no version 0 for deploy account switch { case d.Version.Is(1): @@ -698,9 +697,8 @@ func ParseBlockVersion(protocolVersion string) (*semver.Version, error) { } sep := "." - digits := strings.Split(protocolVersion, sep) // pad with 3 zeros in case version has less than 3 digits - digits = append(digits, []string{"0", "0", "0"}...) + digits := slices.Concat(strings.Split(protocolVersion, sep), []string{"0", "0", "0"}) // get first 3 digits only return semver.NewVersion(strings.Join(digits[:3], sep)) diff --git a/db/buckets_test.go b/db/buckets_test.go index b93ec88431..38ca188765 100644 --- a/db/buckets_test.go +++ b/db/buckets_test.go @@ -1,6 +1,7 @@ package db_test import ( + "slices" "testing" "github.com/NethermindEth/juno/db" @@ -22,7 +23,7 @@ func TestKey(t *testing.T) { for _, k := range keys { t.Run(string(rune(len(k))), func(t *testing.T) { expectedKey := []byte{byte(db.StateTrie)} - expectedKey = append(expectedKey, k...) + expectedKey = slices.Concat(expectedKey, k) assert.Equal(t, expectedKey, db.StateTrie.Key(k)) }) } diff --git a/db/buffered_transaction.go b/db/buffered_transaction.go index d708633cfe..959b02d47a 100644 --- a/db/buffered_transaction.go +++ b/db/buffered_transaction.go @@ -2,6 +2,7 @@ package db import ( "errors" + "slices" ) // BufferedTransaction buffers the updates in the memory to be later flushed to the underlying Transaction @@ -35,7 +36,7 @@ func (t *BufferedTransaction) Commit() error { // Set : see db.Transaction.Set func (t *BufferedTransaction) Set(key, val []byte) error { valueCopy := make([]byte, 0, len(val)) - t.updates[string(key)] = append(valueCopy, val...) + t.updates[string(key)] = slices.Concat(valueCopy, val) return nil } diff --git a/db/pebble/iterator.go b/db/pebble/iterator.go index e28f83197f..471b186fdb 100644 --- a/db/pebble/iterator.go +++ b/db/pebble/iterator.go @@ -1,6 +1,8 @@ package pebble import ( + "slices" + "github.com/NethermindEth/juno/db" "github.com/cockroachdb/pebble" ) @@ -23,8 +25,7 @@ func (i *iterator) Key() []byte { if key == nil { return nil } - buf := make([]byte, len(key)) - copy(buf, key) + buf := slices.Clone(key) return buf } @@ -34,8 +35,7 @@ func (i *iterator) Value() ([]byte, error) { if err != nil || val == nil { return nil, err } - buf := make([]byte, len(val)) - copy(buf, val) + buf := slices.Clone(val) return buf, nil } diff --git a/l1/l1_pkg_test.go b/l1/l1_pkg_test.go index 4e377cce12..eefa2e3aa1 100644 --- a/l1/l1_pkg_test.go +++ b/l1/l1_pkg_test.go @@ -330,7 +330,6 @@ func TestClient(t *testing.T) { } for _, tt := range tests { - tt := tt t.Run(tt.description, func(t *testing.T) { t.Parallel() diff --git a/migration/migration.go b/migration/migration.go index 3e39c1831f..2c04251391 100644 --- a/migration/migration.go +++ b/migration/migration.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "runtime" + "slices" "sync" "github.com/NethermindEth/juno/adapters/sn2core" @@ -551,7 +552,7 @@ func migrateTrieNodesFromBitsetToTrieKey(target db.Bucket) BucketMigratorDoFunc } orgKeyPrefix[0] = byte(db.Temporary) // move the node to temporary bucket - return txn.Set(append(orgKeyPrefix, keyBuffer.Bytes()...), tempBuf.Bytes()) + return txn.Set(slices.Concat(orgKeyPrefix, keyBuffer.Bytes()), tempBuf.Bytes()) } } diff --git a/rpc/block_test.go b/rpc/block_test.go index f778ac7350..d7a02e348b 100644 --- a/rpc/block_test.go +++ b/rpc/block_test.go @@ -52,7 +52,6 @@ func TestBlockId(t *testing.T) { }, } for name, test := range tests { - test := test t.Run(name, func(t *testing.T) { t.Parallel() var blockID rpc.BlockID @@ -81,7 +80,6 @@ func TestBlockId(t *testing.T) { }, } for name, test := range failingTests { - test := test t.Run(name, func(t *testing.T) { t.Parallel() var blockID rpc.BlockID diff --git a/rpc/events_test.go b/rpc/events_test.go index 3db08d233e..492c0f6d56 100644 --- a/rpc/events_test.go +++ b/rpc/events_test.go @@ -6,6 +6,7 @@ import ( "io" "net" "net/http/httptest" + "slices" "testing" "time" @@ -120,7 +121,7 @@ func TestEvents(t *testing.T) { for i := 0; i < len(allEvents)+1; i++ { events, err := handler.Events(args) require.Nil(t, err) - accEvents = append(accEvents, events.Events...) + accEvents = slices.Concat(accEvents, events.Events) args.ContinuationToken = events.ContinuationToken if args.ContinuationToken == "" { break diff --git a/sync/sync.go b/sync/sync.go index 99f3d3dd2b..a2e4ac0bf0 100644 --- a/sync/sync.go +++ b/sync/sync.go @@ -298,11 +298,7 @@ func (s *Synchronizer) syncBlocks(syncCtx context.Context) { } func maxWorkers() int { - m, mProcs := 16, runtime.GOMAXPROCS(0) - if mProcs > m { - return m - } - return mProcs + return min(16, runtime.GOMAXPROCS(0)) //nolint:mnd } func (s *Synchronizer) setupWorkers() (*stream.Stream, *stream.Stream) { diff --git a/upgrader/upgrader_test.go b/upgrader/upgrader_test.go index 213eed0b5e..f2893f2781 100644 --- a/upgrader/upgrader_test.go +++ b/upgrader/upgrader_test.go @@ -102,7 +102,6 @@ func TestUpgrader(t *testing.T) { } for description, test := range tests { - test := test t.Run(description, func(t *testing.T) { t.Parallel() srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { diff --git a/vm/trace.go b/vm/trace.go index f17257ff41..5ee830a259 100644 --- a/vm/trace.go +++ b/vm/trace.go @@ -150,7 +150,7 @@ func (t *TransactionTrace) RevertReason() string { func (t *TransactionTrace) AllEvents() []OrderedEvent { events := make([]OrderedEvent, 0) for _, invocation := range t.allInvocations() { - events = append(events, invocation.allEvents()...) + events = slices.Concat(events, invocation.allEvents()) } return events } @@ -158,7 +158,7 @@ func (t *TransactionTrace) AllEvents() []OrderedEvent { func (t *TransactionTrace) AllMessages() []OrderedL2toL1Message { messages := make([]OrderedL2toL1Message, 0) for _, invocation := range t.allInvocations() { - messages = append(messages, invocation.allMessages()...) + messages = slices.Concat(messages, invocation.allMessages()) } return messages } @@ -181,17 +181,17 @@ type FunctionInvocation struct { func (invocation *FunctionInvocation) allEvents() []OrderedEvent { events := make([]OrderedEvent, 0) for i := range invocation.Calls { - events = append(events, invocation.Calls[i].allEvents()...) + events = slices.Concat(events, invocation.Calls[i].allEvents()) } - return append(events, invocation.Events...) + return slices.Concat(events, invocation.Events) } func (invocation *FunctionInvocation) allMessages() []OrderedL2toL1Message { messages := make([]OrderedL2toL1Message, 0) for i := range invocation.Calls { - messages = append(messages, invocation.Calls[i].allMessages()...) + messages = slices.Concat(messages, invocation.Calls[i].Messages) } - return append(messages, invocation.Messages...) + return slices.Concat(messages, invocation.Messages) } type ExecuteInvocation struct { diff --git a/vm/vm.go b/vm/vm.go index ba952ee1f7..a55d3e297c 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -344,7 +344,7 @@ func (v *vm) Execute(txns []core.Transaction, declaredClasses []core.Class, paid } func marshalTxnsAndDeclaredClasses(txns []core.Transaction, declaredClasses []core.Class) (json.RawMessage, json.RawMessage, error) { //nolint:lll - txnJSONs := []json.RawMessage{} + txnJSONs := make([]json.RawMessage, 0, len(txns)) for _, txn := range txns { txnJSON, err := marshalTxn(txn) if err != nil { @@ -353,7 +353,7 @@ func marshalTxnsAndDeclaredClasses(txns []core.Transaction, declaredClasses []co txnJSONs = append(txnJSONs, txnJSON) } - classJSONs := []json.RawMessage{} + classJSONs := make([]json.RawMessage, 0, len(declaredClasses)) for _, declaredClass := range declaredClasses { declaredClassJSON, cErr := marshalClassInfo(declaredClass) if cErr != nil { From b527563fdeead7d72b5e3f30c730fc43aba1e7da Mon Sep 17 00:00:00 2001 From: AnkushinDaniil Date: Wed, 31 Jul 2024 18:54:27 +0300 Subject: [PATCH 2/5] append revert --- blockchain/blockchain_test.go | 3 +-- core/transaction.go | 4 ++-- db/buckets_test.go | 3 +-- db/buffered_transaction.go | 3 +-- grpc/handlers.go | 5 ++--- migration/migration.go | 3 +-- rpc/events_test.go | 3 +-- vm/trace.go | 12 ++++++------ 8 files changed, 15 insertions(+), 21 deletions(-) diff --git a/blockchain/blockchain_test.go b/blockchain/blockchain_test.go index 51c753339c..c0c3666a91 100644 --- a/blockchain/blockchain_test.go +++ b/blockchain/blockchain_test.go @@ -3,7 +3,6 @@ package blockchain_test import ( "context" "fmt" - "slices" "testing" "time" @@ -516,7 +515,7 @@ func TestEvents(t *testing.T) { for i := 0; i < len(allEvents)+1; i++ { gotEvents, lastToken, err = filter.Events(lastToken, chunkSize) require.NoError(t, err) - accEvents = slices.Concat(accEvents, gotEvents) + accEvents = append(accEvents, gotEvents...) if lastToken == nil { break } diff --git a/core/transaction.go b/core/transaction.go index a534e5e408..b08eb5b6f9 100644 --- a/core/transaction.go +++ b/core/transaction.go @@ -570,7 +570,7 @@ func l1HandlerTransactionHash(l *L1HandlerTransaction, n *utils.Network) (*felt. } func deployAccountTransactionHash(d *DeployAccountTransaction, n *utils.Network) (*felt.Felt, error) { - callData := slices.Concat([]*felt.Felt{d.ClassHash, d.ContractAddressSalt}, d.ConstructorCallData) + callData := append([]*felt.Felt{d.ClassHash, d.ContractAddressSalt}, d.ConstructorCallData...) // There is no version 0 for deploy account switch { case d.Version.Is(1): @@ -698,7 +698,7 @@ func ParseBlockVersion(protocolVersion string) (*semver.Version, error) { sep := "." // pad with 3 zeros in case version has less than 3 digits - digits := slices.Concat(strings.Split(protocolVersion, sep), []string{"0", "0", "0"}) + digits := append(strings.Split(protocolVersion, sep), []string{"0", "0", "0"}...) // get first 3 digits only return semver.NewVersion(strings.Join(digits[:3], sep)) diff --git a/db/buckets_test.go b/db/buckets_test.go index 38ca188765..b93ec88431 100644 --- a/db/buckets_test.go +++ b/db/buckets_test.go @@ -1,7 +1,6 @@ package db_test import ( - "slices" "testing" "github.com/NethermindEth/juno/db" @@ -23,7 +22,7 @@ func TestKey(t *testing.T) { for _, k := range keys { t.Run(string(rune(len(k))), func(t *testing.T) { expectedKey := []byte{byte(db.StateTrie)} - expectedKey = slices.Concat(expectedKey, k) + expectedKey = append(expectedKey, k...) assert.Equal(t, expectedKey, db.StateTrie.Key(k)) }) } diff --git a/db/buffered_transaction.go b/db/buffered_transaction.go index 959b02d47a..d708633cfe 100644 --- a/db/buffered_transaction.go +++ b/db/buffered_transaction.go @@ -2,7 +2,6 @@ package db import ( "errors" - "slices" ) // BufferedTransaction buffers the updates in the memory to be later flushed to the underlying Transaction @@ -36,7 +35,7 @@ func (t *BufferedTransaction) Commit() error { // Set : see db.Transaction.Set func (t *BufferedTransaction) Set(key, val []byte) error { valueCopy := make([]byte, 0, len(val)) - t.updates[string(key)] = slices.Concat(valueCopy, val) + t.updates[string(key)] = append(valueCopy, val...) return nil } diff --git a/grpc/handlers.go b/grpc/handlers.go index d063ee9bc2..6e77e3c00a 100644 --- a/grpc/handlers.go +++ b/grpc/handlers.go @@ -5,7 +5,6 @@ import ( "bytes" "context" "fmt" - "slices" "github.com/Masterminds/semver/v3" "github.com/NethermindEth/juno/db" @@ -94,13 +93,13 @@ func (h Handler) handleTxCursor( switch cur.Op { case gen.Op_SEEK: - key := slices.Concat(cur.BucketName, cur.K) + key := append(cur.BucketName, cur.K...) if it.Seek(key) { responsePair.K = it.Key() responsePair.V, err = it.Value() } case gen.Op_SEEK_EXACT: - key := slices.Concat(cur.BucketName, cur.K) + key := append(cur.BucketName, cur.K...) if it.Seek(key) && bytes.Equal(it.Key(), key) { responsePair.K = it.Key() responsePair.V, err = it.Value() diff --git a/migration/migration.go b/migration/migration.go index 2c04251391..3e39c1831f 100644 --- a/migration/migration.go +++ b/migration/migration.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "runtime" - "slices" "sync" "github.com/NethermindEth/juno/adapters/sn2core" @@ -552,7 +551,7 @@ func migrateTrieNodesFromBitsetToTrieKey(target db.Bucket) BucketMigratorDoFunc } orgKeyPrefix[0] = byte(db.Temporary) // move the node to temporary bucket - return txn.Set(slices.Concat(orgKeyPrefix, keyBuffer.Bytes()), tempBuf.Bytes()) + return txn.Set(append(orgKeyPrefix, keyBuffer.Bytes()...), tempBuf.Bytes()) } } diff --git a/rpc/events_test.go b/rpc/events_test.go index 492c0f6d56..3db08d233e 100644 --- a/rpc/events_test.go +++ b/rpc/events_test.go @@ -6,7 +6,6 @@ import ( "io" "net" "net/http/httptest" - "slices" "testing" "time" @@ -121,7 +120,7 @@ func TestEvents(t *testing.T) { for i := 0; i < len(allEvents)+1; i++ { events, err := handler.Events(args) require.Nil(t, err) - accEvents = slices.Concat(accEvents, events.Events) + accEvents = append(accEvents, events.Events...) args.ContinuationToken = events.ContinuationToken if args.ContinuationToken == "" { break diff --git a/vm/trace.go b/vm/trace.go index 5ee830a259..04c95898d6 100644 --- a/vm/trace.go +++ b/vm/trace.go @@ -150,7 +150,7 @@ func (t *TransactionTrace) RevertReason() string { func (t *TransactionTrace) AllEvents() []OrderedEvent { events := make([]OrderedEvent, 0) for _, invocation := range t.allInvocations() { - events = slices.Concat(events, invocation.allEvents()) + events = append(events, invocation.allEvents()...) } return events } @@ -158,7 +158,7 @@ func (t *TransactionTrace) AllEvents() []OrderedEvent { func (t *TransactionTrace) AllMessages() []OrderedL2toL1Message { messages := make([]OrderedL2toL1Message, 0) for _, invocation := range t.allInvocations() { - messages = slices.Concat(messages, invocation.allMessages()) + messages = append(messages, invocation.allMessages()...) } return messages } @@ -181,17 +181,17 @@ type FunctionInvocation struct { func (invocation *FunctionInvocation) allEvents() []OrderedEvent { events := make([]OrderedEvent, 0) for i := range invocation.Calls { - events = slices.Concat(events, invocation.Calls[i].allEvents()) + events = append(events, invocation.Calls[i].allEvents()...) } - return slices.Concat(events, invocation.Events) + return append(events, invocation.Events...) } func (invocation *FunctionInvocation) allMessages() []OrderedL2toL1Message { messages := make([]OrderedL2toL1Message, 0) for i := range invocation.Calls { - messages = slices.Concat(messages, invocation.Calls[i].Messages) + messages = append(messages, invocation.Calls[i].Messages...) } - return slices.Concat(messages, invocation.Messages) + return append(messages, invocation.Messages...) } type ExecuteInvocation struct { From 6cc2fcde0b0d0bc21df63166b1bd958a40129e34 Mon Sep 17 00:00:00 2001 From: AnkushinDaniil Date: Wed, 31 Jul 2024 18:57:09 +0300 Subject: [PATCH 3/5] typo fixed --- vm/trace.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/trace.go b/vm/trace.go index 04c95898d6..f17257ff41 100644 --- a/vm/trace.go +++ b/vm/trace.go @@ -189,7 +189,7 @@ func (invocation *FunctionInvocation) allEvents() []OrderedEvent { func (invocation *FunctionInvocation) allMessages() []OrderedL2toL1Message { messages := make([]OrderedL2toL1Message, 0) for i := range invocation.Calls { - messages = append(messages, invocation.Calls[i].Messages...) + messages = append(messages, invocation.Calls[i].allMessages()...) } return append(messages, invocation.Messages...) } From ec38b80331598d5917c1f015f94da794b2598a54 Mon Sep 17 00:00:00 2001 From: AnkushinDaniil Date: Thu, 1 Aug 2024 16:42:47 +0300 Subject: [PATCH 4/5] linker fixed --- grpc/handlers.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/grpc/handlers.go b/grpc/handlers.go index 6e77e3c00a..d063ee9bc2 100644 --- a/grpc/handlers.go +++ b/grpc/handlers.go @@ -5,6 +5,7 @@ import ( "bytes" "context" "fmt" + "slices" "github.com/Masterminds/semver/v3" "github.com/NethermindEth/juno/db" @@ -93,13 +94,13 @@ func (h Handler) handleTxCursor( switch cur.Op { case gen.Op_SEEK: - key := append(cur.BucketName, cur.K...) + key := slices.Concat(cur.BucketName, cur.K) if it.Seek(key) { responsePair.K = it.Key() responsePair.V, err = it.Value() } case gen.Op_SEEK_EXACT: - key := append(cur.BucketName, cur.K...) + key := slices.Concat(cur.BucketName, cur.K) if it.Seek(key) && bytes.Equal(it.Key(), key) { responsePair.K = it.Key() responsePair.V, err = it.Value() From fae2c27d8c2a8cc4ac65b9465f7364516933f39d Mon Sep 17 00:00:00 2001 From: AnkushinDaniil Date: Mon, 5 Aug 2024 12:06:30 +0300 Subject: [PATCH 5/5] update --- core/state.go | 3 ++- core/transaction.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/core/state.go b/core/state.go index aee9d44a50..1518ca49ce 100644 --- a/core/state.go +++ b/core/state.go @@ -7,6 +7,7 @@ import ( "fmt" "runtime" "slices" + "sort" "github.com/NethermindEth/juno/core/crypto" "github.com/NethermindEth/juno/core/felt" @@ -400,7 +401,7 @@ func (s *State) updateContractStorages(stateTrie *trie.Trie, diffs map[felt.Felt for key := range diffs { keys = append(keys, key) } - slices.SortStableFunc(keys, func(a, b felt.Felt) int { return a.Cmp(&b) }) + slices.SortStableFunc(keys, func(a, b felt.Felt) int { return len(diffs[a]) - len(diffs[b]) }) // update per-contract storage Tries concurrently contractUpdaters := pool.NewWithResults[*bufferedTransactionWithAddress]().WithErrors().WithMaxGoroutines(runtime.GOMAXPROCS(0)) diff --git a/core/transaction.go b/core/transaction.go index b08eb5b6f9..5ab0a9cd9b 100644 --- a/core/transaction.go +++ b/core/transaction.go @@ -697,8 +697,9 @@ func ParseBlockVersion(protocolVersion string) (*semver.Version, error) { } sep := "." + digits := strings.Split(protocolVersion, sep) // pad with 3 zeros in case version has less than 3 digits - digits := append(strings.Split(protocolVersion, sep), []string{"0", "0", "0"}...) + digits = append(digits, []string{"0", "0", "0"}...) // get first 3 digits only return semver.NewVersion(strings.Join(digits[:3], sep))