Skip to content

Commit

Permalink
go/runtime/client: use history for GetBlock(latest)
Browse files Browse the repository at this point in the history
Using history for all GetBlock requests prevents the case where the latest
block would already be available but not yet in history, leading to
inconsistent results compared to querying by specific block number.
  • Loading branch information
ptrus committed Mar 31, 2020
1 parent 327ce4b commit ed4ceab
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changelog/2795.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go/runtime/client: use history for GetBlock(latest)

Using history for all GetBlock requests prevents the case where the latest
block would already be available but not yet in history, leading to
inconsistent results compared to querying by specific block number.
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
4 changes: 4 additions & 0 deletions go/runtime/client/tests/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func testQuery(
require.NoError(t, err, "GetBlock")
require.EqualValues(t, 4, blk.Header.Round)

blkLatest, err := c.GetBlock(ctx, &api.GetBlockRequest{RuntimeID: runtimeID, Round: api.RoundLatest})
require.NoError(t, err, "GetBlock(RoundLatest)")
require.EqualValues(t, 4, blkLatest.Header.Round)

// Out of bounds block round.
_, err = c.GetBlock(ctx, &api.GetBlockRequest{RuntimeID: runtimeID, Round: 5})
require.Error(t, err, "GetBlock")
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 ed4ceab

Please sign in to comment.