Skip to content

Commit

Permalink
convert storage.ErrNotFound to nil
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangchiqing committed Nov 10, 2023
1 parent e753171 commit 46d5140
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion engine/execution/storehouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type RegisterStore interface {
// GetRegister first try to get the register from InMemoryRegisterStore, then OnDiskRegisterStore
// It returns:
// - (value, nil) if the register value is found at the given block
// - (nil, storage.ErrNotFound) if the register is not found
// - (nil, nil) if the register is not found
// - (nil, storage.ErrHeightNotIndexed) if the height is below the first height that is indexed.
// - (nil, storehouse.ErrNotExecuted) if the block is not executed yet
// - (nil, storehouse.ErrNotExecuted) if the block is conflicting iwth finalized block
Expand Down
15 changes: 12 additions & 3 deletions engine/execution/storehouse/register_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func NewRegisterStore(
// 5. above pruned height, and is executed, but register is not updated since pruned height
// It returns:
// - (value, nil) if the register value is found at the given block
// - (nil, storage.ErrNotFound) if the register is not found
// - (nil, nil) if the register is not found
// - (nil, storage.ErrHeightNotIndexed) if the height is below the first height that is indexed.
// - (nil, storehouse.ErrNotExecuted) if the block is not executed yet
// - (nil, storehouse.ErrNotExecuted) if the block is conflicting iwth finalized block
Expand All @@ -89,7 +89,7 @@ func (r *RegisterStore) GetRegister(height uint64, blockID flow.Identifier, regi
// then it means the block is connected to the pruned block of in memory store, which is
// a finalized block and executed block, so we can get its value from on disk store.
if height > prunedError.PrunedHeight {
return r.diskStore.Get(register, prunedError.PrunedHeight)
return r.getAndConvertNotFoundErr(register, prunedError.PrunedHeight)
}

// if the block is below the pruned height, then there are two cases:
Expand All @@ -105,8 +105,17 @@ func (r *RegisterStore) GetRegister(height uint64, blockID flow.Identifier, regi
// conflicting blocks are considered as un-executed
return flow.RegisterValue{}, fmt.Errorf("getting registers from conflicting block %v at height %v: %w", blockID, height, ErrNotExecuted)
}
return r.diskStore.Get(register, height)
return r.getAndConvertNotFoundErr(register, height)
}

// getAndConvertNotFoundErr returns nil if the register is not found from storage
func (r *RegisterStore) getAndConvertNotFoundErr(register flow.RegisterID, height uint64) (flow.RegisterValue, error) {
val, err := r.diskStore.Get(register, height)
if errors.Is(err, storage.ErrNotFound) {
// FVM expects the error to be nil when register is not found
return nil, nil
}
return val, err
}

// SaveRegisters saves to InMemoryRegisterStore first, then trigger the same check as OnBlockFinalized
Expand Down
7 changes: 3 additions & 4 deletions engine/execution/storehouse/register_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/onflow/flow-go/engine/execution"
"github.com/onflow/flow-go/engine/execution/storehouse"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/storage"
"github.com/onflow/flow-go/storage/pebble"
"github.com/onflow/flow-go/utils/unittest"
)
Expand Down Expand Up @@ -69,9 +68,9 @@ func TestRegisterStoreGetRegisterFail(t *testing.T) {

// known block, unknown register
rootBlock := headerByHeight[rootHeight]
_, err = rs.GetRegister(rootHeight, rootBlock.ID(), unknownReg.Key)
require.Error(t, err)
require.ErrorIs(t, err, storage.ErrNotFound)
val, err := rs.GetRegister(rootHeight, rootBlock.ID(), unknownReg.Key)
require.NoError(t, err)
require.Nil(t, val)
})
}

Expand Down

0 comments on commit 46d5140

Please sign in to comment.