From 90ef054c7e0188d5a0fc834ec44145acb6373386 Mon Sep 17 00:00:00 2001 From: Fridrik Asmundsson Date: Thu, 19 Dec 2024 15:30:00 +0000 Subject: [PATCH 01/14] fix linter errors --- cmd/beacond/defaults.go | 4 --- node-core/components/depinject.go | 37 --------------------------- node-core/components/signer/signer.go | 2 +- scripts/build/linting.mk | 4 +-- 4 files changed, 3 insertions(+), 44 deletions(-) delete mode 100644 node-core/components/depinject.go diff --git a/cmd/beacond/defaults.go b/cmd/beacond/defaults.go index e70745e508..956cfe185c 100644 --- a/cmd/beacond/defaults.go +++ b/cmd/beacond/defaults.go @@ -24,7 +24,6 @@ import ( "github.com/berachain/beacon-kit/node-core/components" ) -//nolint:funlen // happens func DefaultComponents() []any { c := []any{ components.ProvideAttributesFactory[ @@ -97,9 +96,6 @@ func DefaultComponents() []any { *KVStore, *Logger, *StorageBackend, ], - // TODO Hacks - components.ProvideKVStoreService, - components.ProvideKVStoreKey, } c = append(c, components.ProvideNodeAPIServer[*Logger, NodeAPIContext], diff --git a/node-core/components/depinject.go b/node-core/components/depinject.go deleted file mode 100644 index 3d94b530af..0000000000 --- a/node-core/components/depinject.go +++ /dev/null @@ -1,37 +0,0 @@ -// SPDX-License-Identifier: BUSL-1.1 -// -// Copyright (C) 2024, Berachain Foundation. All rights reserved. -// Use of this software is governed by the Business Source License included -// in the LICENSE file of this repository and at www.mariadb.com/bsl11. -// -// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY -// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER -// VERSIONS OF THE LICENSED WORK. -// -// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF -// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF -// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). -// -// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON -// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, -// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND -// TITLE. - -package components - -import ( - "cosmossdk.io/core/store" - "cosmossdk.io/store/types" - "github.com/cosmos/cosmos-sdk/runtime" -) - -var storeKey = types.NewKVStoreKey("beacon") - -func ProvideKVStoreKey() *types.KVStoreKey { - return storeKey -} - -func ProvideKVStoreService(storeKey *types.KVStoreKey) store.KVStoreService { - return runtime.NewKVStoreService(storeKey) -} diff --git a/node-core/components/signer/signer.go b/node-core/components/signer/signer.go index 2375a5d22b..f85ff4a9a5 100644 --- a/node-core/components/signer/signer.go +++ b/node-core/components/signer/signer.go @@ -85,7 +85,7 @@ func (f BLSSigner) VerifySignature( ) error { pk, err := bls12381.NewPublicKeyFromCompressedBytes(pubKey[:]) if err != nil { - return fmt.Errorf("verifying signature: %s", err) + return fmt.Errorf("verifying signature: %w", err) } if !pk.VerifySignature(msg, signature[:]) { return ErrInvalidSignature diff --git a/scripts/build/linting.mk b/scripts/build/linting.mk index 3664b09790..74a4b03a77 100644 --- a/scripts/build/linting.mk +++ b/scripts/build/linting.mk @@ -15,14 +15,14 @@ lint: ## run all configured linters # TODO: Remove GODEBUG override once: https://github.com/golang/go/issues/68877 is resolved. golangci: @echo "--> Running linter on all modules" - (GODEBUG=gotypesalias=0 go run github.com/golangci/golangci-lint/cmd/golangci-lint run --config $(ROOT_DIR)/.golangci.yaml --timeout=10m --concurrency 8) || exit 1; + (GODEBUG=gotypesalias=0 go run github.com/golangci/golangci-lint/cmd/golangci-lint --build-tags bls12381 run --config $(ROOT_DIR)/.golangci.yaml --timeout=10m --concurrency 8) || exit 1; @printf "All modules complete\n" # TODO: Remove GODEBUG override once: https://github.com/golang/go/issues/68877 is resolved. golangci-fix: @echo "--> Running linter with fixes on all modules" - (GODEBUG=gotypesalias=0 go run github.com/golangci/golangci-lint/cmd/golangci-lint run --config $(ROOT_DIR)/.golangci.yaml --timeout=10m --fix --concurrency 8) || exit 1; + (GODEBUG=gotypesalias=0 go run github.com/golangci/golangci-lint/cmd/golangci-lint --build-tags bls12381 run --config $(ROOT_DIR)/.golangci.yaml --timeout=10m --fix --concurrency 8) || exit 1; @printf "All modules complete\n" ################# From ffa7c89409159e36276f24b61267deb6d0f1bb03 Mon Sep 17 00:00:00 2001 From: Fridrik Asmundsson Date: Thu, 19 Dec 2024 15:42:32 +0000 Subject: [PATCH 02/14] Add bls12381 build tag to tests --- scripts/build/testing.mk | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/scripts/build/testing.mk b/scripts/build/testing.mk index 2606ab8bc3..633970f591 100644 --- a/scripts/build/testing.mk +++ b/scripts/build/testing.mk @@ -251,35 +251,36 @@ SHORT_FUZZ_TIME=10s MEDIUM_FUZZ_TIME=30s LONG_FUZZ_TIME=3m + test: @$(MAKE) test-unit test-forge-fuzz test-unit: ## run golang unit tests @echo "Running unit tests..." @go list -f '{{.Dir}}/...' -m | xargs \ - go test -race + go test --tags bls12381 -race test-unit-cover: ## run golang unit tests with coverage @echo "Running unit tests with coverage..." @go list -f '{{.Dir}}/...' -m | xargs \ - go test -race -coverprofile=test-unit-cover.txt + go test --tags bls12381 -race -coverprofile=test-unit-cover.txt test-unit-bench: ## run golang unit benchmarks @echo "Running unit tests with benchmarks..." @go list -f '{{.Dir}}/...' -m | xargs \ - go test -bench=. -run=^$ -benchmem + go test --tags bls12381 -bench=. -run=^$ -benchmem # On MacOS, if there is a linking issue on the fuzz tests, # use the old linker with flags -ldflags=-extldflags=-Wl,-ld_classic test-unit-fuzz: ## run fuzz tests @echo "Running fuzz tests with coverage..." - go test -run ^FuzzPayloadIDCacheBasic -fuzztime=${SHORT_FUZZ_TIME} github.com/berachain/beacon-kit/payload/cache - go test -run ^FuzzPayloadIDInvalidInput -fuzztime=${SHORT_FUZZ_TIME} github.com/berachain/beacon-kit/payload/cache - go test -run ^FuzzPayloadIDCacheConcurrency -fuzztime=${SHORT_FUZZ_TIME} github.com/berachain/beacon-kit/payload/cache - go test -run ^FuzzHashTreeRoot -fuzztime=${MEDIUM_FUZZ_TIME} github.com/berachain/beacon-kit/primitives/merkle + go test --tags bls12381 -run ^FuzzPayloadIDCacheBasic -fuzztime=${SHORT_FUZZ_TIME} github.com/berachain/beacon-kit/payload/cache + go test --tags bls12381 -run ^FuzzPayloadIDInvalidInput -fuzztime=${SHORT_FUZZ_TIME} github.com/berachain/beacon-kit/payload/cache + go test --tags bls12381 -run ^FuzzPayloadIDCacheConcurrency -fuzztime=${SHORT_FUZZ_TIME} github.com/berachain/beacon-kit/payload/cache + go test --tags bls12381 -run ^FuzzHashTreeRoot -fuzztime=${MEDIUM_FUZZ_TIME} github.com/berachain/beacon-kit/primitives/merkle test-e2e: ## run e2e tests @$(MAKE) build-docker VERSION=kurtosis-local test-e2e-no-build test-e2e-no-build: - go test -timeout 0 -tags e2e,bls12381 ./testing/e2e/. -v \ No newline at end of file + go test --tags bls12381 -timeout 0 -tags e2e,bls12381 ./testing/e2e/. -v \ No newline at end of file From 74a587fda077ca8ea79c35ee7dcc770cb79eff7d Mon Sep 17 00:00:00 2001 From: Fridrik Asmundsson Date: Thu, 19 Dec 2024 15:50:07 +0000 Subject: [PATCH 03/14] run go mod tidy --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 73db790ba1..9c33e49797 100644 --- a/go.mod +++ b/go.mod @@ -48,7 +48,6 @@ require ( github.com/spf13/cobra v1.8.1 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.19.0 - github.com/supranational/blst v0.3.13 github.com/umbracle/fastrlp v0.1.0 go.uber.org/automaxprocs v1.6.0 go.uber.org/nilaway v0.0.0-20241010202415-ba14292918d8 @@ -418,6 +417,7 @@ require ( github.com/stoewer/go-strcase v1.3.0 // indirect github.com/stretchr/objx v0.5.2 // indirect github.com/subosito/gotenv v1.6.0 // indirect + github.com/supranational/blst v0.3.13 // indirect github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect github.com/tdakkota/asciicheck v0.2.0 // indirect github.com/tendermint/go-amino v0.16.0 // indirect From 08a7dd24c14c4ade5eaea06ebfd0ffffa9407e08 Mon Sep 17 00:00:00 2001 From: Fridrik Asmundsson Date: Thu, 19 Dec 2024 16:08:49 +0000 Subject: [PATCH 04/14] upgrade docker go version --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 623a4d1f18..93c438993b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,7 @@ ### Stage 0 - Build Arguments ### ####################################################### -ARG GO_VERSION=1.23.0 +ARG GO_VERSION=1.23.4 ARG RUNNER_IMAGE=alpine:3.20 ARG BUILD_TAGS="netgo,muslc,blst,bls12381,pebbledb" ARG NAME=beacond From 189f8746016282ec81d44ae2cc3e014667e30197 Mon Sep 17 00:00:00 2001 From: Fridrik Asmundsson Date: Thu, 19 Dec 2024 16:41:26 +0000 Subject: [PATCH 05/14] add back ProvideKVStore/Service --- cmd/beacond/defaults.go | 3 +++ node-core/components/depinject.go | 37 +++++++++++++++++++++++++++++++ scripts/build/linting.mk | 4 ++-- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 node-core/components/depinject.go diff --git a/cmd/beacond/defaults.go b/cmd/beacond/defaults.go index 956cfe185c..a268c4571e 100644 --- a/cmd/beacond/defaults.go +++ b/cmd/beacond/defaults.go @@ -96,6 +96,9 @@ func DefaultComponents() []any { *KVStore, *Logger, *StorageBackend, ], + // TODO Hacks + components.ProvideKVStoreService, + components.ProvideKVStoreKey, } c = append(c, components.ProvideNodeAPIServer[*Logger, NodeAPIContext], diff --git a/node-core/components/depinject.go b/node-core/components/depinject.go new file mode 100644 index 0000000000..3d94b530af --- /dev/null +++ b/node-core/components/depinject.go @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package components + +import ( + "cosmossdk.io/core/store" + "cosmossdk.io/store/types" + "github.com/cosmos/cosmos-sdk/runtime" +) + +var storeKey = types.NewKVStoreKey("beacon") + +func ProvideKVStoreKey() *types.KVStoreKey { + return storeKey +} + +func ProvideKVStoreService(storeKey *types.KVStoreKey) store.KVStoreService { + return runtime.NewKVStoreService(storeKey) +} diff --git a/scripts/build/linting.mk b/scripts/build/linting.mk index 74a4b03a77..8df57fcab2 100644 --- a/scripts/build/linting.mk +++ b/scripts/build/linting.mk @@ -53,7 +53,7 @@ license-fix: nilaway: @echo "--> Running nilaway" - (go run go.uber.org/nilaway/cmd/nilaway -exclude-errors-in-files "geth-primitives/deposit/" -v ./...) || exit 1; + (go run go.uber.org/nilaway/cmd/nilaway --tags bls12381 -exclude-errors-in-files "geth-primitives/deposit/" -v ./...) || exit 1; @printf "Nilaway check complete\n" ################# @@ -62,7 +62,7 @@ nilaway: gosec: @echo "--> Running gosec" - @go run github.com/cosmos/gosec/v2/cmd/gosec -exclude G702 ./... + @go run github.com/cosmos/gosec/v2/cmd/gosec -tags bls12381 -exclude G702 ./... ################# # slither # From 619a5ffed1806218c51588641e2e2b42e827127c Mon Sep 17 00:00:00 2001 From: Fridrik Asmundsson Date: Thu, 19 Dec 2024 16:45:23 +0000 Subject: [PATCH 06/14] remove unneded tag --- scripts/build/testing.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build/testing.mk b/scripts/build/testing.mk index 633970f591..c035ded162 100644 --- a/scripts/build/testing.mk +++ b/scripts/build/testing.mk @@ -283,4 +283,4 @@ test-e2e: ## run e2e tests @$(MAKE) build-docker VERSION=kurtosis-local test-e2e-no-build test-e2e-no-build: - go test --tags bls12381 -timeout 0 -tags e2e,bls12381 ./testing/e2e/. -v \ No newline at end of file + go test -timeout 0 -tags e2e,bls12381 ./testing/e2e/. -v \ No newline at end of file From b1b28c727669da44fb05feb4e713b6cab24792e8 Mon Sep 17 00:00:00 2001 From: aBear Date: Thu, 19 Dec 2024 18:01:17 +0100 Subject: [PATCH 07/14] fix unit tests --- cmd/beacond/defaults.go | 3 + node-core/components/depinject.go | 113 +++++++++++++++++++++++++++++ state-transition/core/core_test.go | 20 ++++- storage/beacondb/registry_test.go | 24 +++++- 4 files changed, 152 insertions(+), 8 deletions(-) create mode 100644 node-core/components/depinject.go diff --git a/cmd/beacond/defaults.go b/cmd/beacond/defaults.go index 956cfe185c..a268c4571e 100644 --- a/cmd/beacond/defaults.go +++ b/cmd/beacond/defaults.go @@ -96,6 +96,9 @@ func DefaultComponents() []any { *KVStore, *Logger, *StorageBackend, ], + // TODO Hacks + components.ProvideKVStoreService, + components.ProvideKVStoreKey, } c = append(c, components.ProvideNodeAPIServer[*Logger, NodeAPIContext], diff --git a/node-core/components/depinject.go b/node-core/components/depinject.go new file mode 100644 index 0000000000..dffb76b199 --- /dev/null +++ b/node-core/components/depinject.go @@ -0,0 +1,113 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package components + +import ( + "context" + + "cosmossdk.io/core/store" + storetypes "cosmossdk.io/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +//nolint:gochecknoglobals // storeKey is a singleton. +var storeKey = storetypes.NewKVStoreKey("beacon") + +func ProvideKVStoreKey() *storetypes.KVStoreKey { + return storeKey +} + +func ProvideKVStoreService( + storeKey *storetypes.KVStoreKey, +) store.KVStoreService { + // skips modules that have no store + return kvStoreService{key: storeKey} +} + +func NewKVStoreService(storeKey *storetypes.KVStoreKey) store.KVStoreService { + return &kvStoreService{key: storeKey} +} + +type kvStoreService struct { + key *storetypes.KVStoreKey +} + +func (k kvStoreService) OpenKVStore(ctx context.Context) store.KVStore { + return NewKVStore(sdk.UnwrapSDKContext(ctx).KVStore(k.key)) +} + +// CoreKVStore is a wrapper of Core/Store kvstore interface +// Remove after https://github.com/cosmos/cosmos-sdk/issues/14714 is closed. +type coreKVStore struct { + kvStore storetypes.KVStore +} + +// NewKVStore returns a wrapper of Core/Store kvstore interface +// Remove once store migrates to core/store kvstore interface. +func NewKVStore(store storetypes.KVStore) store.KVStore { + return coreKVStore{kvStore: store} +} + +// Get returns nil iff key doesn't exist. Errors on nil key. +func (store coreKVStore) Get(key []byte) ([]byte, error) { + return store.kvStore.Get(key), nil +} + +// Has checks if a key exists. Errors on nil key. +func (store coreKVStore) Has(key []byte) (bool, error) { + return store.kvStore.Has(key), nil +} + +// Set sets the key. Errors on nil key or value. +func (store coreKVStore) Set(key, value []byte) error { + store.kvStore.Set(key, value) + return nil +} + +// Delete deletes the key. Errors on nil key. +func (store coreKVStore) Delete(key []byte) error { + store.kvStore.Delete(key) + return nil +} + +// Iterator iterates over a domain of keys in ascending order. End is exclusive. +// Start must be less than end, or the Iterator is invalid. +// Iterator must be closed by caller. +// To iterate over entire domain, use store.Iterator(nil, nil) +// CONTRACT: No writes may happen within a domain while an iterator exists over +// it. +// Exceptionally allowed for cachekv.Store, safe to write in the modules. +func (store coreKVStore) Iterator(start, end []byte) (store.Iterator, error) { + return store.kvStore.Iterator(start, end), nil +} + +// ReverseIterator iterates over a domain of keys in descending order. End is +// exclusive. +// Start must be less than end, or the Iterator is invalid. +// Iterator must be closed by caller. +// CONTRACT: No writes may happen within a domain while an iterator exists over +// it. +// Exceptionally allowed for cachekv.Store, safe to write in the modules. +func (store coreKVStore) ReverseIterator( + start, end []byte, +) (store.Iterator, error) { + return store.kvStore.ReverseIterator(start, end), nil +} diff --git a/state-transition/core/core_test.go b/state-transition/core/core_test.go index f11c3ff04c..b162287eae 100644 --- a/state-transition/core/core_test.go +++ b/state-transition/core/core_test.go @@ -21,9 +21,11 @@ package core_test import ( + "context" "fmt" "testing" + corestore "cosmossdk.io/core/store" "cosmossdk.io/log" "cosmossdk.io/store" "cosmossdk.io/store/metrics" @@ -46,7 +48,6 @@ import ( depositstore "github.com/berachain/beacon-kit/storage/deposit" "github.com/berachain/beacon-kit/storage/encoding" dbm "github.com/cosmos/cosmos-db" - "github.com/cosmos/cosmos-sdk/runtime" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" @@ -70,6 +71,17 @@ type ( ] ) +type testKVStoreService struct { + ctx sdk.Context +} + +func (kvs *testKVStoreService) OpenKVStore(context.Context) corestore.KVStore { + //nolint:contextcheck // fine with tests + return components.NewKVStore( + sdk.UnwrapSDKContext(kvs.ctx).KVStore(testStoreKey), + ) +} + var ( testStoreKey = storetypes.NewKVStoreKey("state-transition-tests") testCodec = &encoding.SSZInterfaceCodec[*types.ExecutionPayloadHeader]{} @@ -96,12 +108,12 @@ func initTestStores() (*beacondb.KVStore, *depositstore.KVStore, error) { return nil, nil, fmt.Errorf("failed to load latest version: %w", err) } - sdkCtx := sdk.NewContext(cms, true, nopLog) - testStoreService := runtime.NewKVStoreService(testStoreKey) + ctx := sdk.NewContext(cms, true, nopLog) + testStoreService := &testKVStoreService{ctx: ctx} return beacondb.New( testStoreService, testCodec, - ).WithContext(sdkCtx), + ), depositstore.NewStore(testStoreService, nopLog), nil } diff --git a/storage/beacondb/registry_test.go b/storage/beacondb/registry_test.go index 0856c50a70..e73632168a 100644 --- a/storage/beacondb/registry_test.go +++ b/storage/beacondb/registry_test.go @@ -21,25 +21,38 @@ package beacondb_test import ( + "context" "fmt" "testing" + corestore "cosmossdk.io/core/store" "cosmossdk.io/log" "cosmossdk.io/store" "cosmossdk.io/store/metrics" storetypes "cosmossdk.io/store/types" "github.com/berachain/beacon-kit/consensus-types/types" + "github.com/berachain/beacon-kit/node-core/components" "github.com/berachain/beacon-kit/primitives/bytes" "github.com/berachain/beacon-kit/primitives/math" "github.com/berachain/beacon-kit/storage/beacondb" "github.com/berachain/beacon-kit/storage/db" "github.com/berachain/beacon-kit/storage/encoding" dbm "github.com/cosmos/cosmos-db" - "github.com/cosmos/cosmos-sdk/runtime" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" ) +type testKVStoreService struct { + ctx sdk.Context +} + +func (kvs *testKVStoreService) OpenKVStore(context.Context) corestore.KVStore { + //nolint:contextcheck // fine with tests + return components.NewKVStore( + sdk.UnwrapSDKContext(kvs.ctx).KVStore(testStoreKey), + ) +} + var ( testStoreKey = storetypes.NewKVStoreKey("storage-tests") testCodec = &encoding.SSZInterfaceCodec[*types.ExecutionPayloadHeader]{} @@ -205,9 +218,12 @@ func initTestStore() (*beacondb.KVStore, error) { return nil, fmt.Errorf("failed to load latest version: %w", err) } - sdkCtx := sdk.NewContext(cms, true, nopLog) + ctx := sdk.NewContext(cms, true, nopLog) + testStoreService := &testKVStoreService{ + ctx: ctx, + } return beacondb.New( - runtime.NewKVStoreService(testStoreKey), + testStoreService, testCodec, - ).WithContext(sdkCtx), nil + ), nil } From d1ee33af86c1122799c99935b525096ae93b2263 Mon Sep 17 00:00:00 2001 From: Fridrik Asmundsson Date: Thu, 19 Dec 2024 17:04:48 +0000 Subject: [PATCH 08/14] address lint error --- cmd/beacond/defaults.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/beacond/defaults.go b/cmd/beacond/defaults.go index a268c4571e..b44249ed37 100644 --- a/cmd/beacond/defaults.go +++ b/cmd/beacond/defaults.go @@ -24,6 +24,7 @@ import ( "github.com/berachain/beacon-kit/node-core/components" ) +//nolint:funlen func DefaultComponents() []any { c := []any{ components.ProvideAttributesFactory[ From af410faddeb771a11d54e1d1afea22a5eb54fbc1 Mon Sep 17 00:00:00 2001 From: Fridrik Asmundsson Date: Thu, 19 Dec 2024 17:20:45 +0000 Subject: [PATCH 09/14] fix gosec lint --- scripts/build/linting.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build/linting.mk b/scripts/build/linting.mk index 8df57fcab2..819c8afa53 100644 --- a/scripts/build/linting.mk +++ b/scripts/build/linting.mk @@ -62,7 +62,7 @@ nilaway: gosec: @echo "--> Running gosec" - @go run github.com/cosmos/gosec/v2/cmd/gosec -tags bls12381 -exclude G702 ./... + @go run github.com/cosmos/gosec/v2/cmd/gosec -tags bls12381 -exclude-dir node-core/components/signer -exclude G702 ./... ################# # slither # From d1c950b5efdd7f8d62047dbad6318ba5df92516d Mon Sep 17 00:00:00 2001 From: Fridrik Asmundsson Date: Thu, 19 Dec 2024 18:20:59 +0000 Subject: [PATCH 10/14] fix test-e2 --- testing/e2e/e2e_staking_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/testing/e2e/e2e_staking_test.go b/testing/e2e/e2e_staking_test.go index 0b27384572..98fc1c2aa8 100644 --- a/testing/e2e/e2e_staking_test.go +++ b/testing/e2e/e2e_staking_test.go @@ -29,6 +29,7 @@ import ( "github.com/berachain/beacon-kit/geth-primitives/deposit" "github.com/berachain/beacon-kit/primitives/common" "github.com/berachain/beacon-kit/testing/e2e/config" + "github.com/cometbft/cometbft/crypto/bls12381" "github.com/ethereum/go-ethereum/accounts/abi/bind" gethcommon "github.com/ethereum/go-ethereum/common" coretypes "github.com/ethereum/go-ethereum/core/types" @@ -102,10 +103,16 @@ func (s *BeaconKitE2ESuite) TestDepositRobustness() { case 0: pubkey, err = client.GetPubKey(s.Ctx()) s.Require().NoError(err) + pk, err := bls12381.NewPublicKeyFromBytes(pubkey[:]) + s.Require().NoError(err) + pubkey = pk.Compress() s.Require().Len(pubkey, 48) case 1: pubkey, err = client2.GetPubKey(s.Ctx()) s.Require().NoError(err) + pk, err := bls12381.NewPublicKeyFromBytes(pubkey[:]) + s.Require().NoError(err) + pubkey = pk.Compress() s.Require().Len(pubkey, 48) default: pubkey = make([]byte, 48) From 7fb4868dcbe153705b3dd4da97cf393e690febab Mon Sep 17 00:00:00 2001 From: Fridrik Asmundsson Date: Thu, 19 Dec 2024 20:21:43 +0000 Subject: [PATCH 11/14] hack: Prevent node from starting up with invalid timeout_commit of 0 Since newest cometbft defaults to timeout_commit of 0 we always default to that in our config.toml file as this code is generated by cosmossdk init function handler. To fix this properly I need to figure out how to pass in the config as context value to the cobra.command as a viper instance. Until then putting this error here as to preventing us forgetting to set it to the correct time_commit (which I assume will be 1sec) --- cli/commands/server/start.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cli/commands/server/start.go b/cli/commands/server/start.go index fb08d9e748..613f77e8d1 100644 --- a/cli/commands/server/start.go +++ b/cli/commands/server/start.go @@ -23,6 +23,7 @@ package server import ( "context" + "fmt" pruningtypes "cosmossdk.io/store/pruning/types" types "github.com/berachain/beacon-kit/cli/commands/server/types" @@ -106,6 +107,10 @@ custom: allow pruning options to be manually specified through 'pruning-keep-rec RunE: func(cmd *cobra.Command, _ []string) error { logger := clicontext.GetLoggerFromCmd[LoggerT](cmd) cfg := clicontext.GetConfigFromCmd(cmd) + if cfg.Consensus.TimeoutCommit == 0 { + return fmt.Errorf("please edit your config.toml file and set timeout_commit to 1s") + } + v := clicontext.GetViperFromCmd(cmd) _, err := GetPruningOptionsFromFlags(v) if err != nil { From 59ad0b24b0c316446107abf443a6eb6544649b06 Mon Sep 17 00:00:00 2001 From: Fridrik Asmundsson Date: Thu, 19 Dec 2024 20:48:33 +0000 Subject: [PATCH 12/14] simplify pubkey[:] to pubkey --- testing/e2e/e2e_staking_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testing/e2e/e2e_staking_test.go b/testing/e2e/e2e_staking_test.go index 98fc1c2aa8..dc3f594497 100644 --- a/testing/e2e/e2e_staking_test.go +++ b/testing/e2e/e2e_staking_test.go @@ -103,14 +103,14 @@ func (s *BeaconKitE2ESuite) TestDepositRobustness() { case 0: pubkey, err = client.GetPubKey(s.Ctx()) s.Require().NoError(err) - pk, err := bls12381.NewPublicKeyFromBytes(pubkey[:]) + pk, err := bls12381.NewPublicKeyFromBytes(pubkey) s.Require().NoError(err) pubkey = pk.Compress() s.Require().Len(pubkey, 48) case 1: pubkey, err = client2.GetPubKey(s.Ctx()) s.Require().NoError(err) - pk, err := bls12381.NewPublicKeyFromBytes(pubkey[:]) + pk, err := bls12381.NewPublicKeyFromBytes(pubkey) s.Require().NoError(err) pubkey = pk.Compress() s.Require().Len(pubkey, 48) From a040a60bb700e4c454756a997cd1479d19743940 Mon Sep 17 00:00:00 2001 From: Fridrik Asmundsson Date: Thu, 19 Dec 2024 21:00:40 +0000 Subject: [PATCH 13/14] disable nilaway on CI until build tags are supported --- .github/workflows/pipeline.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index f1d0376830..7e5de28d1c 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -63,7 +63,6 @@ jobs: - "lint" - "slither" - "gosec" - - "nilaway" - "markdownlint" - "generate-check" - "tidy-sync-check" From 4eedd80e3ce456612325c5fb424ab9883a412e94 Mon Sep 17 00:00:00 2001 From: Fridrik Asmundsson Date: Thu, 19 Dec 2024 21:07:34 +0000 Subject: [PATCH 14/14] fix minor lints --- cli/commands/server/start.go | 4 ++-- testing/e2e/e2e_staking_test.go | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/cli/commands/server/start.go b/cli/commands/server/start.go index 613f77e8d1..19fe168c90 100644 --- a/cli/commands/server/start.go +++ b/cli/commands/server/start.go @@ -23,7 +23,7 @@ package server import ( "context" - "fmt" + "errors" pruningtypes "cosmossdk.io/store/pruning/types" types "github.com/berachain/beacon-kit/cli/commands/server/types" @@ -108,7 +108,7 @@ custom: allow pruning options to be manually specified through 'pruning-keep-rec logger := clicontext.GetLoggerFromCmd[LoggerT](cmd) cfg := clicontext.GetConfigFromCmd(cmd) if cfg.Consensus.TimeoutCommit == 0 { - return fmt.Errorf("please edit your config.toml file and set timeout_commit to 1s") + return errors.New("please edit your config.toml file and set timeout_commit to 1s") } v := clicontext.GetViperFromCmd(cmd) diff --git a/testing/e2e/e2e_staking_test.go b/testing/e2e/e2e_staking_test.go index dc3f594497..f7d15813e5 100644 --- a/testing/e2e/e2e_staking_test.go +++ b/testing/e2e/e2e_staking_test.go @@ -99,18 +99,20 @@ func (s *BeaconKitE2ESuite) TestDepositRobustness() { // Create a deposit transaction. Use the default validators' pubkeys // if exists, otherwise pubkey is a random 48 byte slice. var pubkey []byte + var pk *bls12381.PubKey + switch i { case 0: pubkey, err = client.GetPubKey(s.Ctx()) s.Require().NoError(err) - pk, err := bls12381.NewPublicKeyFromBytes(pubkey) + pk, err = bls12381.NewPublicKeyFromBytes(pubkey) s.Require().NoError(err) pubkey = pk.Compress() s.Require().Len(pubkey, 48) case 1: pubkey, err = client2.GetPubKey(s.Ctx()) s.Require().NoError(err) - pk, err := bls12381.NewPublicKeyFromBytes(pubkey) + pk, err = bls12381.NewPublicKeyFromBytes(pubkey) s.Require().NoError(err) pubkey = pk.Compress() s.Require().Len(pubkey, 48)