code.vegaprotocol.io/vega@v0.79.0/core/parties/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 parties_test 17 18 import ( 19 "testing" 20 "time" 21 22 "code.vegaprotocol.io/vega/core/types" 23 vgtest "code.vegaprotocol.io/vega/libs/test" 24 "code.vegaprotocol.io/vega/paths" 25 vegapb "code.vegaprotocol.io/vega/protos/vega" 26 commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1" 27 28 "github.com/stretchr/testify/assert" 29 "github.com/stretchr/testify/require" 30 ) 31 32 func TestTakingAndRestoringSnapshotSucceeds(t *testing.T) { 33 ctx := vgtest.VegaContext("chainid", 100) 34 35 vegaPath := paths.New(t.TempDir()) 36 now := time.Now() 37 38 te1 := newEngine(t) 39 snapshotEngine1 := newSnapshotEngine(t, vegaPath, now, te1.engine) 40 closeSnapshotEngine1 := vgtest.OnlyOnce(snapshotEngine1.Close) 41 defer closeSnapshotEngine1() 42 43 require.NoError(t, snapshotEngine1.Start(ctx)) 44 45 party1 := newPartyID(t) 46 47 expectPartyProfileUpdatedEvent(t, te1) 48 require.NoError(t, te1.engine.UpdateProfile(ctx, party1, &commandspb.UpdatePartyProfile{ 49 Alias: "test1", 50 Metadata: []*vegapb.Metadata{ 51 { 52 Key: "key1", 53 Value: "value1", 54 }, 55 }, 56 })) 57 58 party2 := newPartyID(t) 59 60 expectPartyProfileUpdatedEvent(t, te1) 61 require.NoError(t, te1.engine.UpdateProfile(ctx, party2, &commandspb.UpdatePartyProfile{ 62 Alias: "test2", 63 Metadata: []*vegapb.Metadata{ 64 { 65 Key: "key1", 66 Value: "value1", 67 }, 68 }, 69 })) 70 71 // Take a snapshot. 72 hash1, err := snapshotEngine1.SnapshotNow(ctx) 73 require.NoError(t, err) 74 75 party3 := newPartyID(t) 76 77 postSnapshot := func(te *testEngine) { 78 expectPartyProfileUpdatedEvent(t, te) 79 require.NoError(t, te.engine.UpdateProfile(ctx, party3, &commandspb.UpdatePartyProfile{ 80 Alias: "test3", 81 Metadata: []*vegapb.Metadata{ 82 { 83 Key: "key1", 84 Value: "value1", 85 }, 86 }, 87 })) 88 89 assertEqualProfiles(t, []types.PartyProfile{ 90 { 91 PartyID: party1, 92 Alias: "test1", 93 Metadata: map[string]string{ 94 "key1": "value1", 95 }, 96 DerivedKeys: map[string]struct{}{}, 97 }, 98 { 99 PartyID: party2, 100 Alias: "test2", 101 Metadata: map[string]string{ 102 "key1": "value1", 103 }, 104 DerivedKeys: map[string]struct{}{}, 105 }, 106 { 107 PartyID: party3, 108 Alias: "test3", 109 Metadata: map[string]string{ 110 "key1": "value1", 111 }, 112 DerivedKeys: map[string]struct{}{}, 113 }, 114 }, te.engine.ListProfiles()) 115 } 116 117 postSnapshot(te1) 118 119 state1 := map[string][]byte{} 120 for _, key := range te1.engine.Keys() { 121 state, additionalProvider, err := te1.engine.GetState(key) 122 require.NoError(t, err) 123 assert.Empty(t, additionalProvider) 124 state1[key] = state 125 } 126 127 closeSnapshotEngine1() 128 129 // Reload the engine using the previous snapshot. 130 131 te2 := newEngine(t) 132 snapshotEngine2 := newSnapshotEngine(t, vegaPath, now, te2.engine) 133 defer snapshotEngine2.Close() 134 135 // This triggers the state restoration from the local snapshot. 136 require.NoError(t, snapshotEngine2.Start(ctx)) 137 138 // Comparing the hash after restoration, to ensure it produces the same result. 139 hash2, _, _ := snapshotEngine2.Info() 140 require.Equal(t, hash1, hash2) 141 142 // Re-applying exact same steps after the snapshot is taken to see if it leads 143 // to the same state. 144 postSnapshot(te2) 145 146 state2 := map[string][]byte{} 147 for _, key := range te2.engine.Keys() { 148 state, additionalProvider, err := te2.engine.GetState(key) 149 require.NoError(t, err) 150 assert.Empty(t, additionalProvider) 151 state2[key] = state 152 } 153 154 for key := range state1 { 155 assert.Equalf(t, state1[key], state2[key], "Key %q does not have the same data", key) 156 } 157 }