Skip to content

Commit

Permalink
go/runtime/client: use histroy for GetBlock(latest)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrus committed Mar 31, 2020
1 parent 327ce4b commit 00aa984
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
3 changes: 3 additions & 0 deletions go/roothash/api/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
11 changes: 6 additions & 5 deletions go/runtime/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions go/runtime/history/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions go/runtime/history/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand All @@ -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 {
Expand Down

0 comments on commit 00aa984

Please sign in to comment.