Skip to content

Commit

Permalink
Merge pull request onflow#5125 from onflow/leo/in-memory-store-pruned…
Browse files Browse the repository at this point in the history
…-height

[Storehouse] Improve InMemoryRegisterStore's IsBlockExecuted method
  • Loading branch information
zhangchiqing authored Dec 11, 2023
2 parents 3f18e8b + 76ffa80 commit 3048504
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
21 changes: 19 additions & 2 deletions engine/execution/storehouse/in_memory_register_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,18 @@ func (s *InMemoryRegisterStore) IsBlockExecuted(height uint64, blockID flow.Iden
defer s.RUnlock()

// finalized and executed blocks are pruned
if height <= s.prunedHeight {
// so if the height is below pruned height, in memory register store is not sure if
// it's executed or not
if height < s.prunedHeight {
return false, fmt.Errorf("below pruned height")
}

// if the height is the pruned height, then it's executed only if the blockID is the prunedID
// since the pruned block must be finalized and executed.
if height == s.prunedHeight {
return blockID == s.prunedID, nil
}

_, ok := s.registersByBlockID[blockID]
return ok, nil
}
Expand All @@ -260,9 +268,18 @@ func (s *InMemoryRegisterStore) findFinalizedFork(height uint64, blockID flow.Id
s.RLock()
defer s.RUnlock()

if height <= s.prunedHeight {
if height < s.prunedHeight {
return nil, fmt.Errorf("cannot find finalized fork at height %d, it is pruned (prunedHeight: %v)", height, s.prunedHeight)
}

if height == s.prunedHeight {
if blockID != s.prunedID {
return nil, fmt.Errorf("cannot find finalized fork at height %d, it is pruned (prunedHeight: %v, prunedID: %v)", height, s.prunedHeight, s.prunedID)
}

return nil, nil
}

prunedHeight := height
block := blockID

Expand Down
62 changes: 62 additions & 0 deletions engine/execution/storehouse/in_memory_register_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,68 @@ func TestInMemoryRegisterStore(t *testing.T) {
require.Equal(t, regB.Value, valB)
})

t.Run("IsBlockExecuted", func(t *testing.T) {
t.Parallel()
pruned := uint64(10)
lastID := unittest.IdentifierFixture()
store := NewInMemoryRegisterStore(pruned, lastID)

height := pruned + 1 // above the pruned pruned
blockID := unittest.IdentifierFixture()
reg := unittest.RegisterEntryFixture()
err := store.SaveRegisters(
height,
blockID,
lastID,
flow.RegisterEntries{reg},
)
require.NoError(t, err)

// above the pruned height and is executed
executed, err := store.IsBlockExecuted(height, blockID)
require.NoError(t, err)
require.True(t, executed)

// above the pruned height, and is not executed
executed, err = store.IsBlockExecuted(pruned+1, unittest.IdentifierFixture())
require.NoError(t, err)
require.False(t, executed)

executed, err = store.IsBlockExecuted(pruned+2, unittest.IdentifierFixture())
require.NoError(t, err)
require.False(t, executed)

// below the pruned height
_, err = store.IsBlockExecuted(pruned-1, unittest.IdentifierFixture())
require.Error(t, err)

// equal to the pruned height and is the pruned block
executed, err = store.IsBlockExecuted(pruned, lastID)
require.NoError(t, err)
require.True(t, executed)

// equal to the pruned height, but is not the pruned block
executed, err = store.IsBlockExecuted(pruned, unittest.IdentifierFixture())
require.NoError(t, err)
require.False(t, executed)

// prune a new block
require.NoError(t, store.Prune(height, blockID))
// equal to the pruned height and is the pruned block
executed, err = store.IsBlockExecuted(height, blockID)
require.NoError(t, err)
require.True(t, executed)

// equal to the pruned height, but is not the pruned block
executed, err = store.IsBlockExecuted(height, unittest.IdentifierFixture())
require.NoError(t, err)
require.False(t, executed)

// below the pruned height
_, err = store.IsBlockExecuted(pruned, lastID)
require.Error(t, err)
})

// 5. Given A(X: 1, Y: 2), GetRegister(A, X) should return 1, GetRegister(A, X) should return 2
t.Run("GetRegistersOK", func(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit 3048504

Please sign in to comment.