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

api: fix inclusion proof verification flake #956

Merged
merged 2 commits into from
Aug 9, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 49 additions & 16 deletions pkg/api/trillian_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ type Response struct {
getConsistencyProofResult *trillian.GetConsistencyProofResponse
}

func unmarshalLogRoot(logRoot []byte) (types.LogRootV1, error) {
var root types.LogRootV1
if err := root.UnmarshalBinary(logRoot); err != nil {
return types.LogRootV1{}, err
}
return root, nil
}

func (t *TrillianClient) root() (types.LogRootV1, error) {
rqst := &trillian.GetLatestSignedLogRootRequest{
LogId: t.logID,
Expand All @@ -77,11 +85,7 @@ func (t *TrillianClient) root() (types.LogRootV1, error) {
if err != nil {
return types.LogRootV1{}, err
}
var root types.LogRootV1
if err := root.UnmarshalBinary(resp.SignedLogRoot.LogRoot); err != nil {
return types.LogRootV1{}, err
}
return root, nil
return unmarshalLogRoot(resp.SignedLogRoot.LogRoot)
}

func (t *TrillianClient) addLeaf(byteValue []byte) *Response {
Expand Down Expand Up @@ -210,8 +214,15 @@ func (t *TrillianClient) getLeafAndProofByIndex(index int64) *Response {
ctx, cancel := context.WithTimeout(t.context, 20*time.Second)
defer cancel()

root, err := t.root()
if err != nil {
rootResp := t.getLatest(0)
if rootResp.err != nil {
return &Response{
status: status.Code(rootResp.err),
err: rootResp.err,
}
}
var root types.LogRootV1
if err := root.UnmarshalBinary(rootResp.getLatestResult.SignedLogRoot.LogRoot); err != nil {
return &Response{
status: status.Code(err),
err: err,
asraa marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -232,21 +243,36 @@ func (t *TrillianClient) getLeafAndProofByIndex(index int64) *Response {
err: err,
}
}
return &Response{
status: status.Code(err),
err: err,
getLeafAndProofResult: &trillian.GetEntryAndProofResponse{
Proof: resp.Proof,
Leaf: resp.Leaf,
SignedLogRoot: rootResp.getLatestResult.SignedLogRoot,
},
}
}

return &Response{
status: status.Code(err),
err: err,
getLeafAndProofResult: resp,
status: status.Code(err),
err: err,
}
}

func (t *TrillianClient) getProofByHash(hashValue []byte) *Response {
ctx, cancel := context.WithTimeout(t.context, 20*time.Second)
defer cancel()

root, err := t.root()
if err != nil {
rootResp := t.getLatest(0)
if rootResp.err != nil {
return &Response{
status: status.Code(rootResp.err),
err: rootResp.err,
}
}
var root types.LogRootV1
if err := root.UnmarshalBinary(rootResp.getLatestResult.SignedLogRoot.LogRoot); err != nil {
asraa marked this conversation as resolved.
Show resolved Hide resolved
return &Response{
status: status.Code(err),
err: err,
Expand All @@ -263,20 +289,27 @@ func (t *TrillianClient) getProofByHash(hashValue []byte) *Response {
if resp != nil {
v := client.NewLogVerifier(rfc6962.DefaultHasher)
for _, proof := range resp.Proof {

if err := v.VerifyInclusionByHash(&root, hashValue, proof); err != nil {
return &Response{
status: status.Code(err),
err: err,
}
}
}
// Return an inclusion proof response with the requested
return &Response{
status: status.Code(err),
err: err,
getProofResult: &trillian.GetInclusionProofByHashResponse{
Proof: resp.Proof,
SignedLogRoot: rootResp.getLatestResult.SignedLogRoot,
},
}
}

return &Response{
status: status.Code(err),
err: err,
getProofResult: resp,
status: status.Code(err),
err: err,
}
}

Expand Down