From 26c9134f356b2ba1fe512fb9812ff1a87529fb7e Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Mon, 20 Apr 2020 09:56:18 +0400 Subject: [PATCH] lite2: verify ConsensusHash in rpc client ______ For contributor use: - [ ] Wrote tests - [ ] Updated CHANGELOG_PENDING.md - [ ] Linked to Github issue with discussion and accepted design OR link to spec that describes this work. - [ ] Updated relevant documentation (`docs/`) and code comments - [x] Re-reviewed `Files changed` in the Github PR explorer --- lite2/rpc/client.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lite2/rpc/client.go b/lite2/rpc/client.go index abd15adc2..da42a747a 100644 --- a/lite2/rpc/client.go +++ b/lite2/rpc/client.go @@ -156,7 +156,24 @@ func (c *Client) ConsensusState() (*ctypes.ResultConsensusState, error) { } func (c *Client) ConsensusParams(height *int64) (*ctypes.ResultConsensusParams, error) { - return c.next.ConsensusParams(height) + res, err := c.next.ConsensusParams(height) + if err != nil { + return nil, err + } + + // Update the light client if we're behind. + h, err := c.updateLiteClientIfNeededTo(res.BlockHeight) + if err != nil { + return nil, err + } + + // Verify hash. + if cH, tH := res.ConsensusParams.Hash(), h.ConsensusHash; !bytes.Equal(cH, tH) { + return nil, fmt.Errorf("params hash %X does not match trusted hash %X", + cH, tH) + } + + return res, nil } func (c *Client) Health() (*ctypes.ResultHealth, error) {