From 85244a42ea306dafef90a73405202825076e7173 Mon Sep 17 00:00:00 2001 From: Callum Waters Date: Tue, 28 Jan 2020 17:16:16 +0100 Subject: [PATCH] lite2: refactor cleanup() (#4343) * lite2: add Start method There are few reasons to do that: 1) separation of state and dynamics (some users will want to delay starting the light client; does not matter we should not allow them to create a light client object) 2) less important, but some users might not need autoUpdateRoutine and removeNoLongerTrustedHeadersRoutine routines * lite2: wait till routines are finished in Stop because they are started in Start, it feels more natural to wait for them to finish in Stop. * lite2: add TrustedValidatorSet func * refactor cleanup code * changed restore header and val function to handle negative height * reverted restoreTrustedHeaderAndNextVals() functionality Co-authored-by: Anton Kaliaev --- lite2/client.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lite2/client.go b/lite2/client.go index 1c8aca2b4..446e272f7 100644 --- a/lite2/client.go +++ b/lite2/client.go @@ -282,8 +282,6 @@ func (c *Client) checkTrustedHeaderUsingOptions(options TrustOptions) error { if c.confirmationFn(action) { // remove all the headers ( options.Height, trustedHeader.Height ] c.cleanup(options.Height + 1) - // set c.trustedHeader to one at options.Height - c.restoreTrustedHeaderAndNextVals() c.logger.Info("Rolled back to older header (newer headers were removed)", "old", options.Height) @@ -555,7 +553,7 @@ func (c *Client) Cleanup() error { return c.cleanup(0) } -// stopHeight=0 -> remove all data +// cleanup deletes all headers & validator sets between +stopHeight+ and latest height included func (c *Client) cleanup(stopHeight int64) error { // 1) Get the oldest height. oldestHeight, err := c.trustedStore.FirstSignedHeaderHeight() @@ -570,7 +568,7 @@ func (c *Client) cleanup(stopHeight int64) error { } // 3) Remove all headers and validator sets. - if stopHeight == 0 { + if stopHeight < oldestHeight { stopHeight = oldestHeight } for height := stopHeight; height <= latestHeight; height++ { @@ -583,6 +581,10 @@ func (c *Client) cleanup(stopHeight int64) error { c.trustedHeader = nil c.trustedNextVals = nil + err = c.restoreTrustedHeaderAndNextVals() + if err != nil { + return err + } return nil }