code.vegaprotocol.io/vega@v0.79.0/core/netparams/snapshot_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package netparams_test
    17  
    18  import (
    19  	"testing"
    20  	"time"
    21  
    22  	"code.vegaprotocol.io/vega/core/integration/stubs"
    23  	"code.vegaprotocol.io/vega/core/netparams"
    24  	"code.vegaprotocol.io/vega/core/snapshot"
    25  	"code.vegaprotocol.io/vega/core/stats"
    26  	vgtest "code.vegaprotocol.io/vega/libs/test"
    27  	"code.vegaprotocol.io/vega/logging"
    28  	"code.vegaprotocol.io/vega/paths"
    29  
    30  	"github.com/golang/mock/gomock"
    31  	"github.com/stretchr/testify/assert"
    32  	"github.com/stretchr/testify/require"
    33  )
    34  
    35  func TestSnapshotRestoreDependentNetParams(t *testing.T) {
    36  	ctx := vgtest.VegaContext("chainid", 100)
    37  
    38  	engine1 := getTestNetParams(t)
    39  	engine1.broker.EXPECT().Send(gomock.Any()).AnyTimes()
    40  
    41  	now := time.Now()
    42  	log := logging.NewTestLogger()
    43  	timeService := stubs.NewTimeStub()
    44  	vegaPath := paths.New(t.TempDir())
    45  	timeService.SetTime(now)
    46  	statsData := stats.New(log, stats.NewDefaultConfig())
    47  	config := snapshot.DefaultConfig()
    48  
    49  	snapshotEngine1, err := snapshot.NewEngine(vegaPath, config, log, timeService, statsData.Blockchain)
    50  	require.NoError(t, err)
    51  	snapshotEngine1.AddProviders(engine1)
    52  	snapshotEngine1CloseFn := vgtest.OnlyOnce(snapshotEngine1.Close)
    53  	defer snapshotEngine1CloseFn()
    54  
    55  	require.NoError(t, snapshotEngine1.Start(ctx))
    56  
    57  	require.NoError(t, engine1.Update(ctx, netparams.MarketAuctionMinimumDuration, "1s"))
    58  	marketAuctionMinimumDurationV1, err := engine1.Get(netparams.MarketAuctionMinimumDuration)
    59  	require.NoError(t, err)
    60  	require.Equal(t, "1s", marketAuctionMinimumDurationV1)
    61  
    62  	require.NoError(t, engine1.Update(ctx, netparams.DelegationMinAmount, "100"))
    63  	delegationMinAmountV1, err := engine1.Get(netparams.DelegationMinAmount)
    64  	require.NoError(t, err)
    65  	require.Equal(t, "100", delegationMinAmountV1)
    66  
    67  	hash1, err := snapshotEngine1.SnapshotNow(ctx)
    68  	require.NoError(t, err)
    69  
    70  	require.NoError(t, engine1.Update(ctx, netparams.GovernanceProposalMarketMinClose, "2h"))
    71  
    72  	state1 := map[string][]byte{}
    73  	for _, key := range engine1.Keys() {
    74  		state, additionalProvider, err := engine1.GetState(key)
    75  		require.NoError(t, err)
    76  		assert.Empty(t, additionalProvider)
    77  		state1[key] = state
    78  	}
    79  
    80  	snapshotEngine1CloseFn()
    81  
    82  	engine2 := getTestNetParams(t)
    83  	engine2.broker.EXPECT().Send(gomock.Any()).AnyTimes()
    84  
    85  	snapshotEngine2, err := snapshot.NewEngine(vegaPath, config, log, timeService, statsData.Blockchain)
    86  	require.NoError(t, err)
    87  	defer snapshotEngine2.Close()
    88  
    89  	snapshotEngine2.AddProviders(engine2)
    90  
    91  	// This triggers the state restoration from the local snapshot.
    92  	require.NoError(t, snapshotEngine2.Start(ctx))
    93  
    94  	// Comparing the hash after restoration, to ensure it produces the same result.
    95  	hash2, _, _ := snapshotEngine2.Info()
    96  	require.Equal(t, hash1, hash2)
    97  
    98  	marketAuctionMinimumDurationV2, err := engine2.Get(netparams.MarketAuctionMinimumDuration)
    99  	require.NoError(t, err)
   100  	delegationMinAmountV2, err := engine2.Get(netparams.DelegationMinAmount)
   101  	require.NoError(t, err)
   102  
   103  	require.Equal(t, marketAuctionMinimumDurationV1, marketAuctionMinimumDurationV2)
   104  	require.Equal(t, delegationMinAmountV1, delegationMinAmountV2)
   105  
   106  	require.NoError(t, engine2.Update(ctx, netparams.GovernanceProposalMarketMinClose, "2h"))
   107  
   108  	state2 := map[string][]byte{}
   109  	for _, key := range engine2.Keys() {
   110  		state, additionalProvider, err := engine2.GetState(key)
   111  		require.NoError(t, err)
   112  		assert.Empty(t, additionalProvider)
   113  		state2[key] = state
   114  	}
   115  
   116  	for key := range state1 {
   117  		assert.Equalf(t, state1[key], state2[key], "Key %q does not have the same data", key)
   118  	}
   119  }