lite2: fix pivot height during bisection (#4850)

This commit is contained in:
Callum Waters
2020-05-20 06:33:45 +02:00
committed by GitHub
parent 1ecc886c44
commit 1b50b4e765
2 changed files with 26 additions and 2 deletions

View File

@@ -728,8 +728,8 @@ func (c *Client) bisection(
case ErrNewValSetCantBeTrusted:
// do add another header to the end of the cache
if depth == len(headerCache)-1 {
pivotHeight := (headerCache[depth].sh.Height + trustedHeader.
Height) * bisectionNumerator / bisectionDenominator
pivotHeight := trustedHeader.Height + (headerCache[depth].sh.Height-trustedHeader.
Height)*bisectionNumerator/bisectionDenominator
interimHeader, interimVals, err := c.fetchHeaderAndValsAtHeight(pivotHeight)
if err != nil {
return err

View File

@@ -295,6 +295,30 @@ func TestClient_SkippingVerification(t *testing.T) {
}
})
}
// start from a large header to make sure that the pivot height doesn't select a height outside
// the appropriate range
veryLargeFullNode := mockp.New(GenMockNode(chainID, 100, 3, 1, bTime))
h1, err := veryLargeFullNode.SignedHeader(90)
require.NoError(t, err)
c, err := lite.NewClient(
chainID,
lite.TrustOptions{
Period: 4 * time.Hour,
Height: 90,
Hash: h1.Hash(),
},
veryLargeFullNode,
[]provider.Provider{veryLargeFullNode},
dbs.New(dbm.NewMemDB(), chainID),
lite.SkippingVerification(lite.DefaultTrustLevel),
)
require.NoError(t, err)
h, err := c.Update(bTime.Add(100 * time.Minute))
assert.NoError(t, err)
h2, err := veryLargeFullNode.SignedHeader(100)
require.NoError(t, err)
assert.Equal(t, h, h2)
}
func TestClient_Cleanup(t *testing.T) {