github.com/koko1123/flow-go-1@v0.29.6/storage/badger/computation_result_test.go (about) 1 package badger_test 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 10 "github.com/dgraph-io/badger/v3" 11 12 "github.com/koko1123/flow-go-1/engine/execution" 13 "github.com/koko1123/flow-go-1/ledger" 14 "github.com/koko1123/flow-go-1/ledger/common/pathfinder" 15 "github.com/koko1123/flow-go-1/ledger/complete" 16 "github.com/koko1123/flow-go-1/model/flow" 17 bstorage "github.com/koko1123/flow-go-1/storage/badger" 18 "github.com/koko1123/flow-go-1/utils/unittest" 19 ) 20 21 func TestUpsertAndRetrieveComputationResult(t *testing.T) { 22 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 23 expected := generateComputationResult(t) 24 crStorage := bstorage.NewComputationResultUploadStatus(db) 25 crId := expected.ExecutableBlock.ID() 26 27 // True case - upsert 28 testUploadStatus := true 29 err := crStorage.Upsert(crId, testUploadStatus) 30 require.NoError(t, err) 31 32 actualUploadStatus, err := crStorage.ByID(crId) 33 require.NoError(t, err) 34 35 assert.Equal(t, testUploadStatus, actualUploadStatus) 36 37 // False case - update 38 testUploadStatus = false 39 err = crStorage.Upsert(crId, testUploadStatus) 40 require.NoError(t, err) 41 42 actualUploadStatus, err = crStorage.ByID(crId) 43 require.NoError(t, err) 44 45 assert.Equal(t, testUploadStatus, actualUploadStatus) 46 }) 47 } 48 49 func TestRemoveComputationResults(t *testing.T) { 50 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 51 t.Run("Remove ComputationResult", func(t *testing.T) { 52 expected := generateComputationResult(t) 53 crId := expected.ExecutableBlock.ID() 54 crStorage := bstorage.NewComputationResultUploadStatus(db) 55 56 testUploadStatus := true 57 err := crStorage.Upsert(crId, testUploadStatus) 58 require.NoError(t, err) 59 60 _, err = crStorage.ByID(crId) 61 require.NoError(t, err) 62 63 err = crStorage.Remove(crId) 64 require.NoError(t, err) 65 66 _, err = crStorage.ByID(crId) 67 assert.Error(t, err) 68 }) 69 }) 70 } 71 72 func TestListComputationResults(t *testing.T) { 73 unittest.RunWithBadgerDB(t, func(db *badger.DB) { 74 t.Run("List all ComputationResult with given status", func(t *testing.T) { 75 expected := [...]*execution.ComputationResult{ 76 generateComputationResult(t), 77 generateComputationResult(t), 78 } 79 crStorage := bstorage.NewComputationResultUploadStatus(db) 80 81 // Store a list of ComputationResult instances first 82 expectedIDs := make(map[string]bool, 0) 83 for _, cr := range expected { 84 crId := cr.ExecutableBlock.ID() 85 expectedIDs[crId.String()] = true 86 err := crStorage.Upsert(crId, true) 87 require.NoError(t, err) 88 } 89 // Add in entries with non-targeted status 90 unexpected := [...]*execution.ComputationResult{ 91 generateComputationResult(t), 92 generateComputationResult(t), 93 } 94 for _, cr := range unexpected { 95 crId := cr.ExecutableBlock.ID() 96 err := crStorage.Upsert(crId, false) 97 require.NoError(t, err) 98 } 99 100 // Get the list of IDs for stored instances 101 crIDs, err := crStorage.GetIDsByUploadStatus(true) 102 require.NoError(t, err) 103 104 crIDsStrMap := make(map[string]bool, 0) 105 for _, crID := range crIDs { 106 crIDsStrMap[crID.String()] = true 107 } 108 109 assert.True(t, reflect.DeepEqual(crIDsStrMap, expectedIDs)) 110 }) 111 }) 112 } 113 114 // Generate ComputationResult for testing purposes 115 func generateComputationResult(t *testing.T) *execution.ComputationResult { 116 117 update1, err := ledger.NewUpdate( 118 ledger.State(unittest.StateCommitmentFixture()), 119 []ledger.Key{ 120 ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(3, []byte{33})}), 121 ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(1, []byte{11})}), 122 ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(2, []byte{1, 1}), ledger.NewKeyPart(3, []byte{2, 5})}), 123 }, 124 []ledger.Value{ 125 []byte{21, 37}, 126 nil, 127 []byte{3, 3, 3, 3, 3}, 128 }, 129 ) 130 require.NoError(t, err) 131 132 trieUpdate1, err := pathfinder.UpdateToTrieUpdate(update1, complete.DefaultPathFinderVersion) 133 require.NoError(t, err) 134 135 update2, err := ledger.NewUpdate( 136 ledger.State(unittest.StateCommitmentFixture()), 137 []ledger.Key{}, 138 []ledger.Value{}, 139 ) 140 require.NoError(t, err) 141 142 trieUpdate2, err := pathfinder.UpdateToTrieUpdate(update2, complete.DefaultPathFinderVersion) 143 require.NoError(t, err) 144 145 update3, err := ledger.NewUpdate( 146 ledger.State(unittest.StateCommitmentFixture()), 147 []ledger.Key{ 148 ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(9, []byte{6})}), 149 }, 150 []ledger.Value{ 151 []byte{21, 37}, 152 }, 153 ) 154 require.NoError(t, err) 155 156 trieUpdate3, err := pathfinder.UpdateToTrieUpdate(update3, complete.DefaultPathFinderVersion) 157 require.NoError(t, err) 158 159 update4, err := ledger.NewUpdate( 160 ledger.State(unittest.StateCommitmentFixture()), 161 []ledger.Key{ 162 ledger.NewKey([]ledger.KeyPart{ledger.NewKeyPart(9, []byte{6})}), 163 }, 164 []ledger.Value{ 165 []byte{21, 37}, 166 }, 167 ) 168 require.NoError(t, err) 169 170 trieUpdate4, err := pathfinder.UpdateToTrieUpdate(update4, complete.DefaultPathFinderVersion) 171 require.NoError(t, err) 172 173 return &execution.ComputationResult{ 174 ExecutableBlock: unittest.ExecutableBlockFixture([][]flow.Identifier{ 175 {unittest.IdentifierFixture()}, 176 {unittest.IdentifierFixture()}, 177 {unittest.IdentifierFixture()}, 178 }), 179 StateSnapshots: nil, 180 StateCommitments: []flow.StateCommitment{ 181 unittest.StateCommitmentFixture(), 182 unittest.StateCommitmentFixture(), 183 unittest.StateCommitmentFixture(), 184 unittest.StateCommitmentFixture(), 185 }, 186 Proofs: nil, 187 Events: []flow.EventsList{ 188 { 189 unittest.EventFixture("what", 0, 0, unittest.IdentifierFixture(), 2), 190 unittest.EventFixture("ever", 0, 1, unittest.IdentifierFixture(), 22), 191 }, 192 {}, 193 { 194 unittest.EventFixture("what", 2, 0, unittest.IdentifierFixture(), 2), 195 unittest.EventFixture("ever", 2, 1, unittest.IdentifierFixture(), 22), 196 unittest.EventFixture("ever", 2, 2, unittest.IdentifierFixture(), 2), 197 unittest.EventFixture("ever", 2, 3, unittest.IdentifierFixture(), 22), 198 }, 199 {}, // system chunk events 200 }, 201 EventsHashes: nil, 202 ServiceEvents: nil, 203 TransactionResults: []flow.TransactionResult{ 204 { 205 TransactionID: unittest.IdentifierFixture(), 206 ErrorMessage: "", 207 ComputationUsed: 23, 208 }, 209 { 210 TransactionID: unittest.IdentifierFixture(), 211 ErrorMessage: "fail", 212 ComputationUsed: 1, 213 }, 214 }, 215 TransactionResultIndex: []int{1, 1, 2, 2}, 216 TrieUpdates: []*ledger.TrieUpdate{ 217 trieUpdate1, 218 trieUpdate2, 219 trieUpdate3, 220 trieUpdate4, 221 }, 222 } 223 }