code.vegaprotocol.io/vega@v0.79.0/core/validators/witness_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 validators_test 17 18 import ( 19 "bytes" 20 "context" 21 "testing" 22 "time" 23 24 "code.vegaprotocol.io/vega/core/types" 25 "code.vegaprotocol.io/vega/libs/proto" 26 commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1" 27 snapshot "code.vegaprotocol.io/vega/protos/vega/snapshot/v1" 28 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/require" 31 ) 32 33 func TestSnapshot(t *testing.T) { 34 erc := getTestWitness(t) 35 defer erc.ctrl.Finish() 36 defer erc.Stop() 37 38 key := (&types.PayloadWitness{}).Key() 39 40 state1, _, err := erc.Witness.GetState(key) 41 require.Nil(t, err) 42 43 erc.top.EXPECT().IsValidator().AnyTimes().Return(true) 44 45 ctx, cancel := context.WithCancel(context.Background()) 46 res := testRes{"resource-id-1", func() error { 47 cancel() 48 return nil 49 }} 50 checkUntil := erc.startTime.Add(700 * time.Second) 51 52 cb := func(interface{}, bool) {} 53 err = erc.StartCheck(res, cb, checkUntil) 54 assert.NoError(t, err) 55 56 // wait until we've done a check 57 <-ctx.Done() 58 59 // take a snapshot after the resource has been added 60 state2, _, err := erc.Witness.GetState(key) 61 require.Nil(t, err) 62 63 // verify it has changed from before the resource 64 require.False(t, bytes.Equal(state1, state2)) 65 66 var pl snapshot.Payload 67 proto.Unmarshal(state2, &pl) 68 payload := types.PayloadFromProto(&pl) 69 70 // reload the state 71 erc2 := getTestWitness(t) 72 defer erc2.ctrl.Finish() 73 defer erc2.Stop() 74 erc2.top.EXPECT().IsValidator().AnyTimes().Return(true) 75 erc2.top.EXPECT().SelfVegaPubKey().AnyTimes().Return("1234") 76 77 _, err = erc2.LoadState(context.Background(), payload) 78 require.Nil(t, err) 79 erc2.RestoreResource(res, cb) 80 81 // expect the hash and state have been restored successfully 82 state3, _, err := erc2.GetState(key) 83 require.Nil(t, err) 84 require.True(t, bytes.Equal(state2, state3)) 85 86 // add a vote 87 pubkey := newPublicKey("1234") 88 erc2.top.EXPECT().IsValidatorVegaPubKey(pubkey.Hex()).Times(1).Return(true) 89 erc2.top.EXPECT().IsTendermintValidator(pubkey.Hex()).AnyTimes().Return(true) 90 err = erc2.AddNodeCheck(context.Background(), &commandspb.NodeVote{Reference: res.id}, pubkey) 91 92 assert.NoError(t, err) 93 94 // expect the hash/state to have changed 95 state4, _, err := erc2.GetState(key) 96 require.Nil(t, err) 97 require.False(t, bytes.Equal(state4, state3)) 98 99 // restore from the state with vote 100 proto.Unmarshal(state4, &pl) 101 payload = types.PayloadFromProto(&pl) 102 103 erc3 := getTestWitness(t) 104 defer erc3.ctrl.Finish() 105 defer erc3.Stop() 106 erc3.top.EXPECT().IsValidator().AnyTimes().Return(true) 107 erc3.top.EXPECT().SelfVegaPubKey().AnyTimes().Return("1234") 108 109 _, err = erc3.LoadState(context.Background(), payload) 110 require.Nil(t, err) 111 erc3.RestoreResource(res, cb) 112 113 state5, _, err := erc3.GetState(key) 114 require.Nil(t, err) 115 require.True(t, bytes.Equal(state5, state4)) 116 }