Skip to content

Commit

Permalink
lite2: correctly return the results of the "latest" block (#4931)
Browse files Browse the repository at this point in the history
Closes #4837

- `/block_results`

  before:

  failed to update light client to 7: failed to obtain the header #7: signed header not found

  after:

  We can't return the latest block results because we won't be able to
  prove them. Return the results for the previous block instead.

- /block_results?height=X`

  no changes
  • Loading branch information
melekes committed Jun 2, 2020
1 parent da924fc commit 8ab0a4c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
8 changes: 4 additions & 4 deletions cmd/tendermint/commands/lite.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ Example:
start a fresh instance:
lite cosmoshub-3 -p 52.57.29.196:26657 -w public-seed-node.cosmoshub.certus.one:26657
lite cosmoshub-3 -p http://52.57.29.196:26657 -w http://public-seed-node.cosmoshub.certus.one:26657
--height 962118 --hash 28B97BE9F6DE51AC69F70E0B7BFD7E5C9CD1A595B7DC31AFF27C50D4948020CD
continue from latest state:
lite cosmoshub-3 -p 52.57.29.196:26657 -w public-seed-node.cosmoshub.certus.one:26657
lite cosmoshub-3 -p http://52.57.29.196:26657 -w http://public-seed-node.cosmoshub.certus.one:26657
`,
RunE: runProxy,
Args: cobra.ExactArgs(1),
Example: `lite cosmoshub-3 -p 52.57.29.196:26657 -w public-seed-node.cosmoshub.certus.one:26657
Example: `lite cosmoshub-3 -p http://52.57.29.196:26657 -w http://public-seed-node.cosmoshub.certus.one:26657
--height 962118 --hash 28B97BE9F6DE51AC69F70E0B7BFD7E5C9CD1A595B7DC31AFF27C50D4948020CD`,
}

Expand Down Expand Up @@ -102,7 +102,7 @@ func runProxy(cmd *cobra.Command, args []string) error {

db, err := dbm.NewGoLevelDB("lite-client-db", home)
if err != nil {
return fmt.Errorf("new goleveldb: %w", err)
return fmt.Errorf("can't create a db: %w", err)
}

var c *lite.Client
Expand Down
21 changes: 18 additions & 3 deletions lite2/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,23 @@ func (c *Client) BlockByHash(hash []byte) (*ctypes.ResultBlock, error) {
return res, nil
}

// BlockResults returns the block results for the given height. If no height is
// provided, the results of the block preceding the latest are returned.
func (c *Client) BlockResults(height *int64) (*ctypes.ResultBlockResults, error) {
res, err := c.next.BlockResults(height)
var h int64
if height == nil {
res, err := c.next.Status()
if err != nil {
return nil, fmt.Errorf("can't get latest height: %w", err)
}
// Can't return the latest block results here because we won't be able to
// prove them. Return the results for the previous block instead.
h = res.SyncInfo.LatestBlockHeight - 1
} else {
h = *height
}

res, err := c.next.BlockResults(&h)
if err != nil {
return nil, err
}
Expand All @@ -314,14 +329,14 @@ func (c *Client) BlockResults(height *int64) (*ctypes.ResultBlockResults, error)
}

// Update the light client if we're behind.
h, err := c.updateLiteClientIfNeededTo(res.Height + 1)
trustedHeader, err := c.updateLiteClientIfNeededTo(h + 1)
if err != nil {
return nil, err
}

// Verify block results.
results := types.NewResults(res.TxsResults)
if rH, tH := results.Hash(), h.LastResultsHash; !bytes.Equal(rH, tH) {
if rH, tH := results.Hash(), trustedHeader.LastResultsHash; !bytes.Equal(rH, tH) {
return nil, fmt.Errorf("last results %X does not match with trusted last results %X",
rH, tH)
}
Expand Down

0 comments on commit 8ab0a4c

Please sign in to comment.