Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lite: light client MVP #3989

Merged
merged 73 commits into from
Nov 25, 2019
Merged

lite: light client MVP #3989

merged 73 commits into from
Nov 25, 2019

Conversation

melekes
Copy link
Contributor

@melekes melekes commented Sep 16, 2019

Refs #1771

ADR: https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-044-lite-client-with-weak-subjectivity.md (incomplete - #2133)

  • Referenced an issue explaining the need for the change
  • Updated all relevant documentation in docs
  • Updated all code comments where relevant
  • Wrote tests
  • Updated CHANGELOG_PENDING.md

types/validator_set.go Outdated Show resolved Hide resolved
types/validator_set.go Show resolved Hide resolved
lite2/verifier.go Outdated Show resolved Hide resolved
lite2/verifier.go Outdated Show resolved Hide resolved
lite2/verifier.go Outdated Show resolved Hide resolved
make trustLevel an option
@codecov-io
Copy link

codecov-io commented Sep 17, 2019

Codecov Report

Merging #3989 into master will decrease coverage by 0.52%.
The diff coverage is 30.8%.

@@            Coverage Diff             @@
##           master    #3989      +/-   ##
==========================================
- Coverage   67.23%   66.71%   -0.53%     
==========================================
  Files         223      223              
  Lines       18964    19159     +195     
==========================================
+ Hits        12751    12782      +31     
- Misses       5255     5391     +136     
- Partials      958      986      +28
Impacted Files Coverage Δ
libs/common/fraction.go 0% <0%> (ø)
lite2/auto_client.go 0% <0%> (ø)
rpc/client/httpclient.go 68.56% <0%> (-1.6%) ⬇️
rpc/client/localclient.go 63.3% <0%> (-1.19%) ⬇️
lite2/rpc/proof.go 0% <0%> (ø)
rpc/client/mock/client.go 16% <0%> (-0.67%) ⬇️
lite2/rpc/client.go 0% <0%> (ø)
lite2/errors.go 100% <100%> (ø)
lite2/provider/http/http.go 33.33% <33.33%> (ø)
lite2/client.go 45.61% <45.61%> (ø)
... and 30 more

lite2/verifier.go Outdated Show resolved Hide resolved
lite2/verifier.go Outdated Show resolved Hide resolved
lite2/client.go Outdated Show resolved Hide resolved
lite2/client.go Outdated Show resolved Hide resolved
lite2/client.go Outdated Show resolved Hide resolved
lite2/verifier.go Outdated Show resolved Hide resolved
lite2/verifier.go Outdated Show resolved Hide resolved
lite2/verifier.go Outdated Show resolved Hide resolved
lite2/client.go Outdated Show resolved Hide resolved
lite2/client.go Outdated Show resolved Hide resolved
lite2/client.go Outdated Show resolved Hide resolved
lite2/verifier.go Outdated Show resolved Hide resolved
lite2/client.go Outdated Show resolved Hide resolved
lite2/client.go Outdated Show resolved Hide resolved
lite2/client.go Outdated Show resolved Hide resolved
lite2/verifier_test.go Outdated Show resolved Hide resolved
lite2/verifier_test.go Outdated Show resolved Hide resolved
lite2/verifier_test.go Outdated Show resolved Hide resolved
lite2/verifier_test.go Outdated Show resolved Hide resolved
lite2/verifier_test.go Outdated Show resolved Hide resolved
lite2/verifier_test.go Outdated Show resolved Hide resolved
lite2/verifier_test.go Outdated Show resolved Hide resolved
@Shivani912
Copy link
Contributor

I feel it'll be great if we can have error types everywhere instead of error strings. That will help standardize error handling and so we can use the same for Tendermint in Rust. Let me know :)

Copy link
Contributor

@ancazamfir ancazamfir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Anton, great work! I have some comments after going through the code once. Will take another look soon.

lite2/client.go Outdated
alternatives []provider.Provider

// Where trusted headers are stored.
trustedStore store.Store
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you comment on some properties of the store? e.g. stores all headers that have been verified. At the time of verification these headers were within the trusting period. I believe headers are not removed from the store as they become "old", i.e. get out of the trusting period.
Maybe some comments on why and how they are useful after becoming old.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe headers are not removed from the store as they become "old", i.e. get out of the trusting period.

This is a TODO. We need to only store the amount of headers necessary for fork-accountability evidence. I don't know the exact number

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add a comment once the requirements are known and implementation is adjusted accordingly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we will need to store all headers that are within trusted period

lite2/client.go Outdated Show resolved Hide resolved
return err
}

// NOTE: Verify func will check if it's expired or not.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True but maybe Client initialization should fail (? not sure myself)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good question

lite2/client.go Show resolved Hide resolved
lite2/client.go Outdated Show resolved Hide resolved
func (c *Client) VerifyHeaderAtHeight(height int64, now time.Time) error {
if c.trustedHeader.Height >= height {
return errors.Errorf("height #%d is already trusted (last: #%d)", height, c.trustedHeader.Height)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it true that it is "already trusted"? Maybe this is an older header that was never verified., (skipping case). Should we lookup the store instead?
Let's say we start with height A (trusted) and we successfully verify for height Z. So we store headers A and Z.
Now we get a request for height Q with A < Q < Z.
Is this a valid request? Or is there a requirement that VerifyHeaderAtHeight() must be called with increasing heights for both sequential and skipping case?
If it is a valid request then we should not trust Q automatically just because we trusted Z. It is possible that a verification of Q against A will fail (e.g. less than 1/3rds common validators between A and Q signed even though there were more than 1/3rds common between A and Z -> Note*)
Also, a proper solution for this "backward" case should check that A is still within trusting period and if not perform backwards-sequential verification (i.e. h, h-1, h-2, etc.) or return error if backwards-sequential is not supported.

So to summarize, if we do allow for random (not necessarily increasing in height) client header verification for skipping case then some changes are needed. If we do not, then maybe clarify this someplace.

Note* - if validators are required to change address when re-bonding then probably this is not an issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even in bisection I don't think we should ever end up looking up a header that is older than one we've already decided to trust. We should never start bisection from a 'start header' that's before one we've already decided to trust, so presumably this won't happen (but good question & merits a comment).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. We do have (potentially) many trusted headers in the store. Any new bisection call for H could start with the highest trusted header <= H. I think the code can support this with minimal changes if we ever need it.
But at least, the error message we currently have height #%d is already trusted.. should be changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have (potentially) many trusted headers in the store. Any new bisection call for H could start with the highest trusted header <= H.

I think it only makes sense with a different provider. Consider this:

a) light client performs bisection and decides to trust H#100
b) later it receives an evidence of attach (lunatic) and learns that H#100 is fabricated
c) light client exits (we should probably remove all "trusted" headers from DB at this point)
c) operator resets it with new primary source and new trusted header

lite2/client.go Outdated Show resolved Hide resolved
lite2/proxy/routes.go Outdated Show resolved Hide resolved
lite2/proxy/routes.go Outdated Show resolved Hide resolved
types/validator_set.go Outdated Show resolved Hide resolved
lite2/client.go Outdated Show resolved Hide resolved
lite2/proxy/routes.go Outdated Show resolved Hide resolved
lite2/proxy/routes.go Outdated Show resolved Hide resolved
lite2/proxy/routes.go Outdated Show resolved Hide resolved
lite2/proxy/routes.go Outdated Show resolved Hide resolved
types/validator_set.go Outdated Show resolved Hide resolved
Copy link
Contributor

@cwgoes cwgoes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated / responded to comments. I think the VerifyXYZ logic is solid, less sure about the Tendermint-side state management. Do you anticipate merging this soon @melekes or do you prefer to wait for more reviews of the state management? Either is fine, in the latter case I suppose we can target the IBC code on this branch (though a bit annoying).

lite2/client.go Show resolved Hide resolved
type Option func(*Client)

// SequentialVerification option configures the light client to sequentially
// check the headers. Note this is much slower than SkippingVerification,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes (every header, in ascending height order).

lite2/client.go Show resolved Hide resolved
// }

func (c *Client) initializeWithTrustOptions(options TrustOptions) error {
h, err := c.primary.SignedHeader(options.Height)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is initializeWithTrustOptions intended to be called multiple times?

func (c *Client) VerifyHeaderAtHeight(height int64, now time.Time) error {
if c.trustedHeader.Height >= height {
return errors.Errorf("height #%d is already trusted (last: #%d)", height, c.trustedHeader.Height)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even in bisection I don't think we should ever end up looking up a header that is older than one we've already decided to trust. We should never start bisection from a 'start header' that's before one we've already decided to trust, so presumably this won't happen (but good question & merits a comment).

Copy link
Contributor

@cwgoes cwgoes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK; this logic will need detailed, continued review but this is definitely better than the current state of affairs.

@melekes melekes marked this pull request as ready for review November 25, 2019 15:05
@melekes melekes merged commit fb8b00f into master Nov 25, 2019
@melekes melekes deleted the anton/lite-client-2 branch November 25, 2019 15:07
melekes added a commit that referenced this pull request Dec 2, 2019
@melekes melekes mentioned this pull request Dec 2, 2019
11 tasks
melekes added a commit that referenced this pull request Dec 9, 2019
in bisection func
+ replace TODO with a comment

#3989 (comment)
melekes added a commit that referenced this pull request Dec 9, 2019
because that way we avoid DB call

+ add godoc comments
+ check if there are no headers yet in AutoClient

#3989 (review)
melekes added a commit that referenced this pull request Jan 13, 2020
* rename adjusted to adjacent

Refs #3989 (comment)

* rename ErrTooMuchChange to ErrNotEnoughVotingPowerSigned

Refs #3989 (comment)

* verify commit is properly signed

* remove no longer trusted headers

* restore trustedHeader and trustedNextVals

* check trustedHeader using options

Refs #4209 (comment)

* use correct var when checking if headers are adjacent

in bisection func
+ replace TODO with a comment

#3989 (comment)

* return header in VerifyHeaderAtHeight

because that way we avoid DB call

+ add godoc comments
+ check if there are no headers yet in AutoClient

#3989 (review)

* TestVerifyAdjacentHeaders: add 2 more test-cases

+ add TestVerifyReturnsErrorIfTrustLevelIsInvalid

* lite: avoid overflow when parsing key in db store!

* lite: rename AutoClient#Err to Errs

* lite: add a test for AutoClient

* lite: fix keyPattern and call itr.Next in db store

* lite: add two tests for db store

* lite: add TestClientRemovesNoLongerTrustedHeaders

* lite: test Client#Cleanup

* lite: test restoring trustedHeader

#4209 (comment)

* lite: comment out unused code in test_helpers

* fix TestVerifyReturnsErrorIfTrustLevelIsInvalid after merge

* change defaultRemoveNoLongerTrustedHeadersPeriod

and add docs

* write more doc

* lite: uncomment testable examples

* use stdlog.Fatal to stop AutoClient tests

* make lll linter happy

* separate errors for 2 cases

- the validator set of a skipped header cannot be trusted, i.e. <1/3rd
  of h1 validator set has signed (new error, something like
  ErrNewValSetCantBeTrusted)
- the validator set is trusted but < 2/3rds has signed
  (ErrNewHeaderCantBeTrusted)

#4209 (comment)

* remove all headers (even the last one) that are outside

of the trusting period. By doing this, we avoid checking the
trustedHeader's hash in checkTrustedHeaderUsingOptions (case #1).

#4209 (comment)

* explain restoreTrustedHeaderAndNextVals better

#4209 (comment)

* add ConfirmationFunction option

for optionally prompting for user input Y/n before removing headers

Refs #4209 (comment)

* make cleaning optional

#4209 (comment)

* return error when user refused to remove headers

* check for double votes in VerifyCommitTrusting

* leave only ErrNewValSetCantBeTrusted error

to differenciate between h2Vals.VerifyCommit and
h1NextVals.VerifyCommitTrusting

* fix example tests

* remove unnecessary if condition

#4209 (comment)

It will be handled by the above switch.

* verifyCommitBasic does not depend on vals

Co-authored-by: Marko <marbar3778@yahoo.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants