From 00aa98419aaad08b846a8eef62b1824979b5da9a Mon Sep 17 00:00:00 2001 From: ptrus Date: Tue, 31 Mar 2020 16:14:01 +0200 Subject: [PATCH] go/runtime/client: use histroy for GetBlock(latest) --- go/roothash/api/history.go | 3 +++ go/runtime/client/client.go | 11 ++++++----- go/runtime/history/history.go | 17 +++++++++++++++++ go/runtime/history/history_test.go | 12 ++++++++++++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/go/roothash/api/history.go b/go/roothash/api/history.go index f467ae5c1d2..c7538f048b5 100644 --- a/go/roothash/api/history.go +++ b/go/roothash/api/history.go @@ -32,4 +32,7 @@ type BlockHistory interface { // GetBlock returns the block at a specific round. GetBlock(ctx context.Context, round uint64) (*block.Block, error) + + // GetLatestBlock returns the block at latest round. + GetLatestBlock(ctx context.Context) (*block.Block, error) } diff --git a/go/runtime/client/client.go b/go/runtime/client/client.go index 9cb52a6d8ca..26f8fee4325 100644 --- a/go/runtime/client/client.go +++ b/go/runtime/client/client.go @@ -216,16 +216,17 @@ func (c *runtimeClient) WatchBlocks(ctx context.Context, runtimeID common.Namesp // Implements api.RuntimeClient. func (c *runtimeClient) GetBlock(ctx context.Context, request *api.GetBlockRequest) (*block.Block, error) { - if request.Round == api.RoundLatest { - return c.common.consensus.RootHash().GetLatestBlock(ctx, request.RuntimeID, consensus.HeightLatest) - } - rt, err := c.common.runtimeRegistry.GetRuntime(request.RuntimeID) if err != nil { return nil, err } - return rt.History().GetBlock(ctx, request.Round) + switch request.Round { + case api.RoundLatest: + return rt.History().GetLatestBlock(ctx) + default: + return rt.History().GetBlock(ctx, request.Round) + } } func (c *runtimeClient) getTxnTree(blk *block.Block) *transaction.Tree { diff --git a/go/runtime/history/history.go b/go/runtime/history/history.go index 08b95243b08..4634fae4a0f 100644 --- a/go/runtime/history/history.go +++ b/go/runtime/history/history.go @@ -76,6 +76,10 @@ func (h *nopHistory) GetBlock(ctx context.Context, round uint64) (*block.Block, return nil, errNopHistory } +func (h *nopHistory) GetLatestBlock(ctx context.Context) (*block.Block, error) { + return nil, errNopHistory +} + func (h *nopHistory) Pruner() Pruner { pruner, _ := NewNonePruner()(nil) return pruner @@ -144,6 +148,19 @@ func (h *runtimeHistory) GetBlock(ctx context.Context, round uint64) (*block.Blo return annBlk.Block, nil } +func (h *runtimeHistory) GetLatestBlock(ctx context.Context) (*block.Block, error) { + meta, err := h.db.metadata() + if err != nil { + return nil, err + } + annBlk, err := h.db.getBlock(meta.LastRound) + if err != nil { + return nil, err + } + + return annBlk.Block, nil +} + func (h *runtimeHistory) Pruner() Pruner { return h.pruner } diff --git a/go/runtime/history/history_test.go b/go/runtime/history/history_test.go index 1936833b8c5..a6b30fba620 100644 --- a/go/runtime/history/history_test.go +++ b/go/runtime/history/history_test.go @@ -41,6 +41,10 @@ func TestHistory(t *testing.T) { require.Error(err, "GetBlock should fail for non-indexed block") require.Equal(roothash.ErrNotFound, err) + _, err = history.GetLatestBlock(context.Background()) + require.Error(err, "GetLatestBlock should fail for no indexed block") + require.Equal(roothash.ErrNotFound, err) + err = history.ConsensusCheckpoint(42) require.NoError(err, "ConsensusCheckpoint") err = history.ConsensusCheckpoint(40) @@ -82,6 +86,10 @@ func TestHistory(t *testing.T) { require.NoError(err, "GetBlock") require.Equal(&putBlk, gotBlk, "GetBlock should return the correct block") + gotLatestBlk, err := history.GetLatestBlock(context.Background()) + require.NoError(err, "GetLatestBlock") + require.Equal(&putBlk, gotLatestBlk, "GetLatestBlock should return the correct block") + // Close history and try to reopen and continue. history.Close() @@ -102,6 +110,10 @@ func TestHistory(t *testing.T) { gotBlk, err = history.GetBlock(context.Background(), 10) require.NoError(err, "GetBlock") require.Equal(&putBlk, gotBlk, "GetBlock should return the correct block") + + gotLatestBlk, err = history.GetLatestBlock(context.Background()) + require.NoError(err, "GetLatestBlock") + require.Equal(&putBlk, gotLatestBlk, "GetLatestBlock should return the correct block") } type testPruneHandler struct {