-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5028 from onflow/leo/storehouse-loader
[Storehouse] Unfinalized blocks loader
- Loading branch information
Showing
9 changed files
with
214 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package loader | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/rs/zerolog" | ||
|
||
"github.com/onflow/flow-go/engine/execution/state" | ||
"github.com/onflow/flow-go/model/flow" | ||
"github.com/onflow/flow-go/state/protocol" | ||
"github.com/onflow/flow-go/storage" | ||
) | ||
|
||
type UnfinalizedLoader struct { | ||
log zerolog.Logger | ||
state protocol.State | ||
headers storage.Headers // see comments on getHeaderByHeight for why we need it | ||
execState state.FinalizedExecutionState | ||
} | ||
|
||
// NewUnfinalizedLoader creates a new loader that loads all unfinalized and validated blocks | ||
func NewUnfinalizedLoader( | ||
log zerolog.Logger, | ||
state protocol.State, | ||
headers storage.Headers, | ||
execState state.FinalizedExecutionState, | ||
) *UnfinalizedLoader { | ||
return &UnfinalizedLoader{ | ||
log: log.With().Str("component", "ingestion_engine_unfinalized_loader").Logger(), | ||
state: state, | ||
headers: headers, | ||
execState: execState, | ||
} | ||
} | ||
|
||
// LoadUnexecuted loads all unfinalized and validated blocks | ||
// any error returned are exceptions | ||
func (e *UnfinalizedLoader) LoadUnexecuted(ctx context.Context) ([]flow.Identifier, error) { | ||
lastExecuted := e.execState.GetHighestFinalizedExecuted() | ||
|
||
// get finalized height | ||
finalized := e.state.Final() | ||
final, err := finalized.Head() | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get finalized block: %w", err) | ||
} | ||
|
||
// TODO: dynamically bootstrapped execution node will reload blocks from | ||
unexecutedFinalized := make([]flow.Identifier, 0) | ||
|
||
// starting from the first unexecuted block, go through each unexecuted and finalized block | ||
// reload its block to execution queues | ||
// loading finalized blocks | ||
for height := lastExecuted + 1; height <= final.Height; height++ { | ||
header, err := e.getHeaderByHeight(height) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get header at height: %v, %w", height, err) | ||
} | ||
|
||
unexecutedFinalized = append(unexecutedFinalized, header.ID()) | ||
} | ||
|
||
// loaded all pending blocks | ||
pendings, err := finalized.Descendants() | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get descendants of finalized block: %w", err) | ||
} | ||
|
||
unexecuted := append(unexecutedFinalized, pendings...) | ||
|
||
e.log.Info(). | ||
Uint64("last_finalized", final.Height). | ||
Uint64("last_finalized_executed", lastExecuted). | ||
// Uint64("sealed_root_height", rootBlock.Height). | ||
// Hex("sealed_root_id", logging.Entity(rootBlock)). | ||
Int("total_finalized_unexecuted", len(unexecutedFinalized)). | ||
Int("total_unexecuted", len(unexecuted)). | ||
Msgf("finalized unexecuted blocks") | ||
|
||
return unexecuted, nil | ||
} | ||
|
||
// if the EN is dynamically bootstrapped, the finalized blocks at height range: | ||
// [ sealedRoot.Height, finalizedRoot.Height - 1] can not be retrieved from | ||
// protocol state, but only from headers | ||
func (e *UnfinalizedLoader) getHeaderByHeight(height uint64) (*flow.Header, error) { | ||
// we don't use protocol state because for dynamic boostrapped execution node | ||
// the last executed and sealed block is below the finalized root block | ||
return e.headers.ByHeight(height) | ||
} |
55 changes: 55 additions & 0 deletions
55
engine/execution/ingestion/loader/unfinalized_loader_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package loader_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/onflow/flow-go/engine/execution/ingestion" | ||
"github.com/onflow/flow-go/engine/execution/ingestion/loader" | ||
stateMock "github.com/onflow/flow-go/engine/execution/state/mock" | ||
"github.com/onflow/flow-go/model/flow" | ||
storage "github.com/onflow/flow-go/storage/mock" | ||
"github.com/onflow/flow-go/utils/unittest" | ||
"github.com/onflow/flow-go/utils/unittest/mocks" | ||
) | ||
|
||
var _ ingestion.BlockLoader = (*loader.UnfinalizedLoader)(nil) | ||
|
||
func TestLoadingUnfinalizedBlocks(t *testing.T) { | ||
ps := mocks.NewProtocolState() | ||
|
||
// Genesis <- A <- B <- C (finalized) <- D | ||
chain, result, seal := unittest.ChainFixture(5) | ||
genesis, blockA, blockB, blockC, blockD := | ||
chain[0], chain[1], chain[2], chain[3], chain[4] | ||
|
||
logChain(chain) | ||
|
||
require.NoError(t, ps.Bootstrap(genesis, result, seal)) | ||
require.NoError(t, ps.Extend(blockA)) | ||
require.NoError(t, ps.Extend(blockB)) | ||
require.NoError(t, ps.Extend(blockC)) | ||
require.NoError(t, ps.Extend(blockD)) | ||
require.NoError(t, ps.Finalize(blockC.ID())) | ||
|
||
es := new(stateMock.FinalizedExecutionState) | ||
es.On("GetHighestFinalizedExecuted").Return(genesis.Header.Height) | ||
headers := new(storage.Headers) | ||
headers.On("ByHeight", blockA.Header.Height).Return(blockA.Header, nil) | ||
headers.On("ByHeight", blockB.Header.Height).Return(blockB.Header, nil) | ||
headers.On("ByHeight", blockC.Header.Height).Return(blockC.Header, nil) | ||
|
||
loader := loader.NewUnfinalizedLoader(unittest.Logger(), ps, headers, es) | ||
|
||
unexecuted, err := loader.LoadUnexecuted(context.Background()) | ||
require.NoError(t, err) | ||
|
||
unittest.IDsEqual(t, []flow.Identifier{ | ||
blockA.ID(), | ||
blockB.ID(), | ||
blockC.ID(), | ||
blockD.ID(), | ||
}, unexecuted) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters