code.vegaprotocol.io/vega@v0.79.0/core/checkpoint/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 checkpoint_test
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  	"time"
    22  
    23  	"code.vegaprotocol.io/vega/core/types"
    24  	"code.vegaprotocol.io/vega/libs/proto"
    25  	snapshot "code.vegaprotocol.io/vega/protos/vega/snapshot/v1"
    26  
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestCheckpointSnapshot(t *testing.T) {
    31  	ctx := context.Background()
    32  
    33  	e := getTestEngine(t)
    34  	e.OnTimeElapsedUpdate(ctx, 10*time.Second)
    35  
    36  	// This is 2022-02-04T11:50:12.655Z
    37  	now := time.Unix(0, 1643975412655000000)
    38  
    39  	// take a checkpoint so that we set the next-checkpoint time
    40  	cp, err := e.Checkpoint(ctx, now)
    41  	require.NoError(t, err)
    42  	require.Nil(t, cp)
    43  
    44  	// take a snapshot
    45  	keys := e.Keys()
    46  	data, _, err := e.GetState(keys[0])
    47  	require.NoError(t, err)
    48  
    49  	snap := &snapshot.Payload{}
    50  	err = proto.Unmarshal(data, snap)
    51  	require.Nil(t, err)
    52  
    53  	// Load the snapshot into a new engne
    54  	snapEngine := getTestEngine(t)
    55  	e.OnTimeElapsedUpdate(ctx, 10*time.Second) // netparam will get propagated into it
    56  	_, err = snapEngine.LoadState(ctx, types.PayloadFromProto(snap))
    57  	require.NoError(t, err)
    58  
    59  	// this is 2022-02-04T11:50:22.591Z, if we failed to snapshot the microseconds we would be in a position where
    60  	// restored-next-cp < now < original-next-cp
    61  	now = time.Unix(0, 1643975422591000000)
    62  
    63  	c1, err := e.Checkpoint(ctx, now)
    64  	require.NoError(t, err)
    65  	c2, err := snapEngine.Checkpoint(ctx, now)
    66  	require.NoError(t, err)
    67  
    68  	// Check that both engines do not take a checkpoint
    69  	require.Nil(t, c1)
    70  	require.Nil(t, c2)
    71  
    72  	// shuffle forward
    73  	now = now.Add(time.Second)
    74  	c1, err = e.Checkpoint(ctx, now)
    75  	require.NoError(t, err)
    76  	c2, err = snapEngine.Checkpoint(ctx, now)
    77  	require.NoError(t, err)
    78  
    79  	// Check that they both now do
    80  	require.NotNil(t, c1)
    81  	require.NotNil(t, c2)
    82  }