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

chore!: improve start/end row proof validation #1476

Draft
wants to merge 4 commits into
base: v0.34.x-celestia
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions types/row_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ func (rp RowProof) Validate(root []byte) error {
if len(rp.Proofs) != len(rp.RowRoots) {
return fmt.Errorf("the number of proofs %d must equal the number of row roots %d", len(rp.Proofs), len(rp.RowRoots))
}
if len(rp.Proofs) == 0 {
return fmt.Errorf("empty proofs")
}
firstProofIndex := rp.Proofs[0].Index
for i := 1; i < len(rp.Proofs); i++ {
if rp.Proofs[0].Total != rp.Proofs[i].Total {
return errors.New("proofs should have the same total")
}
if rp.Proofs[i].Index != firstProofIndex+1 {
return errors.New("proof indexes are not sequential")
}
firstProofIndex++
}
if int64(rp.StartRow) != rp.Proofs[0].Index {
return fmt.Errorf("invalid start row")
}
if int64(rp.EndRow) != rp.Proofs[len(rp.Proofs)-1].Index {
return fmt.Errorf("invalid end row")
}
if !rp.VerifyProof(root) {
return errors.New("row proof failed to verify")
}
Expand Down
40 changes: 40 additions & 0 deletions types/row_proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,46 @@ func TestRowProofValidate(t *testing.T) {
root: root,
wantErr: true,
},
{
name: "proof with different total",
rp: func() RowProof {
proof := validRowProof()
proof.Proofs[0].Total += 1
return proof
}(),
root: root,
wantErr: true,
},
{
name: "proof with inconsequential indexes",
rp: func() RowProof {
proof := validRowProof()
proof.Proofs[0].Index -= 1
return proof
}(),
root: root,
wantErr: true,
},
{
name: "invalid start row",
rp: func() RowProof {
proof := validRowProof()
proof.StartRow += 1
return proof
}(),
root: root,
wantErr: true,
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: this test could be improved by verifying that the error is fmt.Errorf("invalid start/end row") in case some other validation check is erroring before the expected error is encountered.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that's fine as long as we're attesting that some error happens. Changing this would mean refactoring the whole test + it's not something that's being done consistently in this repo, most tests are just wanting an error.

If you feel strongly about it, I can refactor, no issue

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not blocking on it. It's just a nice to have

},
{
name: "invalid end row",
rp: func() RowProof {
proof := validRowProof()
proof.EndRow += 1
return proof
}(),
root: root,
wantErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
Loading