@@ -180,6 +180,45 @@ func TestIndexProtocolKVStore_MissingLock(t *testing.T) {
180180 })
181181 require .Error (t , err )
182182 require .Contains (t , err .Error (), storage .LockInsertBlock )
183+ })
184+ }
185+
186+ // TestInsertProtocolKVStore_WrongLock tests that InsertProtocolKVStore fails when holding wrong locks.
187+ func TestInsertProtocolKVStore_WrongLock (t * testing.T ) {
188+ dbtest .RunWithDB (t , func (t * testing.T , db storage.DB ) {
189+ lockManager := storage .NewTestingLockManager ()
190+ expected := & flow.PSKeyValueStoreData {
191+ Version : 2 ,
192+ Data : unittest .RandomBytes (32 ),
193+ }
194+
195+ kvStoreStateID := unittest .IdentifierFixture ()
183196
197+ // Test with LockFinalizeBlock (wrong lock)
198+ err := unittest .WithLock (t , lockManager , storage .LockFinalizeBlock , func (lctx lockctx.Context ) error {
199+ return db .WithReaderBatchWriter (func (rw storage.ReaderBatchWriter ) error {
200+ return operation .InsertProtocolKVStore (lctx , rw , kvStoreStateID , expected )
201+ })
202+ })
203+ require .Error (t , err )
204+ require .Contains (t , err .Error (), storage .LockInsertBlock )
205+ })
206+ }
207+
208+ // TestIndexProtocolKVStore_WrongLock tests that IndexProtocolKVStore fails when holding wrong locks.
209+ func TestIndexProtocolKVStore_WrongLock (t * testing.T ) {
210+ dbtest .RunWithDB (t , func (t * testing.T , db storage.DB ) {
211+ lockManager := storage .NewTestingLockManager ()
212+ kvStoreStateID := unittest .IdentifierFixture ()
213+ blockID := unittest .IdentifierFixture ()
214+
215+ // Test with LockFinalizeBlock (wrong lock)
216+ err := unittest .WithLock (t , lockManager , storage .LockFinalizeBlock , func (lctx lockctx.Context ) error {
217+ return db .WithReaderBatchWriter (func (rw storage.ReaderBatchWriter ) error {
218+ return operation .IndexProtocolKVStore (lctx , rw , blockID , kvStoreStateID )
219+ })
220+ })
221+ require .Error (t , err )
222+ require .Contains (t , err .Error (), storage .LockInsertBlock )
184223 })
185224}
0 commit comments