|
| 1 | +// SPDX-License-Identifier: BUSL-1.1 |
| 2 | +// |
| 3 | +// Copyright (C) 2025, Berachain Foundation. All rights reserved. |
| 4 | +// Use of this software is governed by the Business Source License included |
| 5 | +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. |
| 6 | +// |
| 7 | +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY |
| 8 | +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER |
| 9 | +// VERSIONS OF THE LICENSED WORK. |
| 10 | +// |
| 11 | +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF |
| 12 | +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF |
| 13 | +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). |
| 14 | +// |
| 15 | +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON |
| 16 | +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, |
| 17 | +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF |
| 18 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND |
| 19 | +// TITLE. |
| 20 | + |
| 21 | +package state_test |
| 22 | + |
| 23 | +import ( |
| 24 | + "testing" |
| 25 | + |
| 26 | + "cosmossdk.io/collections" |
| 27 | + "cosmossdk.io/log" |
| 28 | + "cosmossdk.io/store" |
| 29 | + sdkmetrics "cosmossdk.io/store/metrics" |
| 30 | + storetypes "cosmossdk.io/store/types" |
| 31 | + "github.com/berachain/beacon-kit/config/spec" |
| 32 | + ctypes "github.com/berachain/beacon-kit/consensus-types/types" |
| 33 | + "github.com/berachain/beacon-kit/node-core/components/metrics" |
| 34 | + "github.com/berachain/beacon-kit/primitives/common" |
| 35 | + "github.com/berachain/beacon-kit/primitives/math" |
| 36 | + "github.com/berachain/beacon-kit/state-transition/core/state" |
| 37 | + "github.com/berachain/beacon-kit/storage" |
| 38 | + "github.com/berachain/beacon-kit/storage/beacondb" |
| 39 | + "github.com/berachain/beacon-kit/storage/db" |
| 40 | + dbm "github.com/cosmos/cosmos-db" |
| 41 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 42 | + "github.com/stretchr/testify/require" |
| 43 | +) |
| 44 | + |
| 45 | +func TestStateProtect(t *testing.T) { |
| 46 | + t.Parallel() |
| 47 | + |
| 48 | + db, err := db.OpenDB("", dbm.MemDBBackend) |
| 49 | + require.NoError(t, err) |
| 50 | + |
| 51 | + cs, errSpec := spec.MainnetChainSpec() |
| 52 | + require.NoError(t, errSpec) |
| 53 | + |
| 54 | + var ( |
| 55 | + nopLog = log.NewNopLogger() |
| 56 | + nopMetrics = sdkmetrics.NewNoOpMetrics() |
| 57 | + ) |
| 58 | + |
| 59 | + cms := store.NewCommitMultiStore(db, nopLog, nopMetrics) |
| 60 | + cms.MountStoreWithDB(testStoreKey, storetypes.StoreTypeIAVL, nil) |
| 61 | + require.NoError(t, cms.LoadLatestVersion()) |
| 62 | + |
| 63 | + backendStoreService := &storage.KVStoreService{Key: testStoreKey} |
| 64 | + kvStore := beacondb.New(backendStoreService) |
| 65 | + |
| 66 | + ms := cms.CacheMultiStore() |
| 67 | + sdkCtx := sdk.NewContext(ms, true, nopLog) |
| 68 | + originalState := state.NewBeaconStateFromDB( |
| 69 | + kvStore.WithContext(sdkCtx), |
| 70 | + cs, |
| 71 | + sdkCtx.Logger(), |
| 72 | + metrics.NewNoOpTelemetrySink(), |
| 73 | + ) |
| 74 | + |
| 75 | + protectingState := originalState.Protect(sdkCtx) |
| 76 | + |
| 77 | + // 1- set an attribute in the original state and show |
| 78 | + // that value is carried over the protecting state |
| 79 | + wantSlot := math.Slot(1234) |
| 80 | + require.NoError(t, originalState.SetSlot(wantSlot)) |
| 81 | + |
| 82 | + gotSlot, err := protectingState.GetSlot() |
| 83 | + require.NoError(t, err) |
| 84 | + require.Equal(t, wantSlot, gotSlot) |
| 85 | + |
| 86 | + // 2- Show that modifying the protecting state |
| 87 | + // does not affect the original state |
| 88 | + wantFork := &ctypes.Fork{ |
| 89 | + PreviousVersion: common.Version{0x11, 0x22, 0x33, 0x44}, |
| 90 | + CurrentVersion: common.Version{0xff, 0xff, 0xff, 0xff}, |
| 91 | + Epoch: math.Epoch(1234), |
| 92 | + } |
| 93 | + require.NoError(t, protectingState.SetFork(wantFork)) |
| 94 | + |
| 95 | + _, err = originalState.GetFork() |
| 96 | + require.ErrorIs(t, err, collections.ErrNotFound) |
| 97 | + |
| 98 | + // 3- Show that changes made to original state after protection |
| 99 | + // are carried over the protecting state |
| 100 | + wantEthIdx := uint64(1987) |
| 101 | + require.NoError(t, originalState.SetEth1DepositIndex(wantEthIdx)) |
| 102 | + |
| 103 | + gotEthIdx, err := protectingState.GetEth1DepositIndex() |
| 104 | + require.NoError(t, err) |
| 105 | + require.Equal(t, wantEthIdx, gotEthIdx) |
| 106 | +} |
| 107 | + |
| 108 | +var testStoreKey = storetypes.NewKVStoreKey("test-stateDB") |
0 commit comments