code.vegaprotocol.io/vega@v0.79.0/core/snapshot/databases/metadata/in_memory_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 metadata_test 17 18 import ( 19 "testing" 20 21 "code.vegaprotocol.io/vega/core/snapshot/databases/metadata" 22 23 tmtypes "github.com/cometbft/cometbft/abci/types" 24 "github.com/stretchr/testify/assert" 25 "github.com/stretchr/testify/require" 26 ) 27 28 func TestInMemoryDatabase(t *testing.T) { 29 t.Run("Saving and loading snapshot metadata succeeds", testInMemoryDatabaseSavingAndLoadingSnapshotMetadataSucceeds) 30 t.Run("Finding a version by block height succeeds", testInMemoryDatabaseFindingVersionByBlockHeightSucceeds) 31 t.Run("Removing a version succeeds", testInMemoryDatabaseRemovingVersionSucceeds) 32 } 33 34 func testInMemoryDatabaseSavingAndLoadingSnapshotMetadataSucceeds(t *testing.T) { 35 db := metadata.NewInMemoryDatabase() 36 defer func() { 37 // Ensure closing does not have problem of any kind. 38 require.NoError(t, db.Close()) 39 }() 40 41 snapshotV1 := &tmtypes.Snapshot{Height: 1, Format: 2, Chunks: 7, Hash: []byte{1, 2}, Metadata: []byte{1}} 42 snapshotV2 := &tmtypes.Snapshot{Height: 2, Format: 2, Chunks: 7, Hash: []byte{2, 2}, Metadata: []byte{2}} 43 44 // Verifying a new database is empty. 45 assert.True(t, db.IsEmpty(), "the database should not contain data") 46 47 // Saving 2 snapshots to verify they are properly saved, and do not 48 // override each other. 49 require.NoError(t, db.Save(1, snapshotV1)) 50 require.NoError(t, db.Save(2, snapshotV2)) 51 52 // Verifying the database correctly states it's not empty when not. 53 assert.False(t, db.IsEmpty(), "the database should contain data") 54 55 // Verify both snapshot can be retrieve and match the originals. 56 loadedSnapshotV1, err := db.Load(1) 57 require.NoError(t, err) 58 assert.Equal(t, snapshotV1, loadedSnapshotV1) 59 60 loadedSnapshotV2, err := db.Load(2) 61 require.NoError(t, err) 62 assert.Equal(t, snapshotV2, loadedSnapshotV2) 63 64 // Removing the snapshots from the database. 65 require.NoError(t, db.Clear()) 66 67 // Verifying the database correctly states it's empty when is. 68 assert.True(t, db.IsEmpty(), "the database should not contain data") 69 70 // Verify both snapshot can no longer be retrieved from the database. 71 loadedSnapshotV1AfterClear, err := db.Load(1) 72 assert.Error(t, err) 73 assert.Nil(t, loadedSnapshotV1AfterClear) 74 75 loadedSnapshotV2AfterClear, err := db.Load(2) 76 assert.Error(t, err) 77 assert.Nil(t, loadedSnapshotV2AfterClear) 78 } 79 80 func testInMemoryDatabaseFindingVersionByBlockHeightSucceeds(t *testing.T) { 81 db := metadata.NewInMemoryDatabase() 82 defer func() { 83 // Ensure closing does not have problem of any kind. 84 require.NoError(t, db.Close()) 85 }() 86 87 snapshotV1 := &tmtypes.Snapshot{Height: 1, Format: 2, Chunks: 7, Hash: []byte{1, 2}, Metadata: []byte{1}} 88 snapshotV2 := &tmtypes.Snapshot{Height: 2, Format: 2, Chunks: 7, Hash: []byte{2, 2}, Metadata: []byte{2}} 89 90 // Saving 2 snapshots. 91 require.NoError(t, db.Save(1, snapshotV1)) 92 require.NoError(t, db.Save(2, snapshotV2)) 93 94 // Looking for a height that has no match. 95 versionNotFound, err := db.FindVersionByBlockHeight(3) 96 97 require.NoError(t, err) 98 assert.Equal(t, int64(-1), versionNotFound) 99 100 // Looking for a height that has no match. 101 versionFound, err := db.FindVersionByBlockHeight(2) 102 103 require.NoError(t, err) 104 assert.Equal(t, int64(2), versionFound) 105 } 106 107 func testInMemoryDatabaseRemovingVersionSucceeds(t *testing.T) { 108 db := metadata.NewInMemoryDatabase() 109 defer func() { 110 // Ensure closing does not have problem of any kind. 111 require.NoError(t, db.Close()) 112 }() 113 114 snapshotV1 := &tmtypes.Snapshot{Height: 1, Format: 2, Chunks: 7, Hash: []byte{1, 2}, Metadata: []byte{1}} 115 snapshotV2 := &tmtypes.Snapshot{Height: 2, Format: 2, Chunks: 7, Hash: []byte{2, 3}, Metadata: []byte{2}} 116 snapshotV3 := &tmtypes.Snapshot{Height: 3, Format: 2, Chunks: 7, Hash: []byte{3, 4}, Metadata: []byte{3}} 117 snapshotV4 := &tmtypes.Snapshot{Height: 4, Format: 2, Chunks: 7, Hash: []byte{4, 5}, Metadata: []byte{4}} 118 snapshotV5 := &tmtypes.Snapshot{Height: 5, Format: 2, Chunks: 7, Hash: []byte{5, 6}, Metadata: []byte{5}} 119 120 // Saving 2 snapshots. 121 require.NoError(t, db.Save(1, snapshotV1)) 122 require.NoError(t, db.Save(2, snapshotV2)) 123 require.NoError(t, db.Save(3, snapshotV3)) 124 require.NoError(t, db.Save(4, snapshotV4)) 125 require.NoError(t, db.Save(5, snapshotV5)) 126 127 // Deleting first snapshot 128 require.NoError(t, db.Delete(1)) 129 130 // Looking for a height that has no match. 131 snapshotNotFound, err := db.Load(1) 132 133 require.Error(t, err) 134 assert.Nil(t, snapshotNotFound) 135 136 // Deleting first snapshot 137 require.NoError(t, db.DeleteRange(2, 5)) 138 139 expectedDeletion := []int64{2, 3, 4} 140 for deletedVersion := range expectedDeletion { 141 snapshotNotFound, err = db.Load(int64(deletedVersion)) 142 143 require.Error(t, err) 144 assert.Nilf(t, snapshotNotFound, "Version %d should have been deleted", deletedVersion) 145 } 146 147 // Looking for a height that has no match. 148 versionFound, err := db.FindVersionByBlockHeight(5) 149 150 require.NoError(t, err) 151 assert.Equal(t, int64(5), versionFound) 152 }