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

Validate all relevant square sizes are committed to in MsgWirePayForData #666

Merged
merged 16 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions x/payment/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ var (
ErrTailPaddingNamespace = sdkerrors.Register(ModuleName, 11118, "cannot use tail padding namespace ID")
ErrTxNamespace = sdkerrors.Register(ModuleName, 11119, "cannot use transaction namespace ID")
ErrEvidenceNamespace = sdkerrors.Register(ModuleName, 11120, "cannot use evidence namespace ID")
ErrInvalidShareCommitments = sdkerrors.Register(ModuleName, 11121, "invalid share commitments: all relevant square sizes must be committed to")
)
2 changes: 1 addition & 1 deletion x/payment/types/payfordata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func validWirePayForData(t *testing.T) *MsgWirePayForData {
msg, err := NewWirePayForData(
[]byte{1, 2, 3, 4, 5, 6, 7, 8},
bytes.Repeat([]byte{1}, 2000),
16, 32, 64,
4, 8, 16, 32, 64, 128,
rootulp marked this conversation as resolved.
Show resolved Hide resolved
)
if err != nil {
panic(err)
Expand Down
39 changes: 39 additions & 0 deletions x/payment/types/wirepayfordata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"errors"
fmt "fmt"
"reflect"
"sort"

"github.com/celestiaorg/nmt/namespace"
sdkclient "github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -102,6 +104,16 @@ func (msg *MsgWirePayForData) ValidateBasic() error {
)
}

if err := msg.ValidateMessageShareCommitments(); err != nil {
return err
}

return nil
}

// ValidateMessageShareCommitments returns an error if the message share
// commitments are invalid.
func (msg *MsgWirePayForData) ValidateMessageShareCommitments() error {
for idx, commit := range msg.MessageShareCommitment {
// check that each commit is valid
if !powerOf2(commit.K) {
Expand All @@ -118,9 +130,36 @@ func (msg *MsgWirePayForData) ValidateBasic() error {
}
}

if err := msg.ValidateAllSquareSizesCommitedTo(); err != nil {
return err
}
return nil
}

// ValidateAllSquareSizesCommitedTo returns an error if the list of square sizes
// committed to don't match all squares sizes expected for this message size.
func (msg *MsgWirePayForData) ValidateAllSquareSizesCommitedTo() error {
allSquareSizes := AllSquareSizes(int(msg.MessageSize))
sort.Slice(allSquareSizes, func(i, j int) bool { return allSquareSizes[i] < allSquareSizes[j] })
rootulp marked this conversation as resolved.
Show resolved Hide resolved

committedSquareSizes := msg.committedSquareSizes()
sort.Slice(committedSquareSizes, func(i, j int) bool { return committedSquareSizes[i] < committedSquareSizes[j] })
rootulp marked this conversation as resolved.
Show resolved Hide resolved
rootulp marked this conversation as resolved.
Show resolved Hide resolved

if !reflect.DeepEqual(allSquareSizes, committedSquareSizes) {
rootulp marked this conversation as resolved.
Show resolved Hide resolved
return ErrInvalidShareCommitments.Wrapf("all square sizes: %v, committed square sizes: %v", allSquareSizes, committedSquareSizes)
}
return nil
}

// commitedSquareSizes returns a list of square sizes that are present in a
// message's share commitment.
func (msg *MsgWirePayForData) committedSquareSizes() (squareSizes []uint64) {
for _, commit := range msg.MessageShareCommitment {
squareSizes = append(squareSizes, commit.K)
}
return squareSizes
rootulp marked this conversation as resolved.
Show resolved Hide resolved
}

// ValidateMessageNamespaceID returns an error if the provided namespace.ID is an invalid or reserved namespace id.
func ValidateMessageNamespaceID(ns namespace.ID) error {
// ensure that the namespace id is of length == NamespaceIDSize
Expand Down
11 changes: 10 additions & 1 deletion x/payment/types/wirepayfordata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ func TestWirePayForData_ValidateBasic(t *testing.T) {

// pfd that has a different power of 2 square size
badSquareSizeMsg := validWirePayForData(t)
badSquareSizeMsg.MessageShareCommitment[0].K = 4
badSquareSizeMsg.MessageShareCommitment[0].K = 16
rootulp marked this conversation as resolved.
Show resolved Hide resolved

// pfd that signs over all squares but the first one
missingCommitmentForOneSquareSize := validWirePayForData(t)
missingCommitmentForOneSquareSize.MessageShareCommitment = missingCommitmentForOneSquareSize.MessageShareCommitment[1:]

tests := []test{
{
Expand Down Expand Up @@ -95,6 +99,11 @@ func TestWirePayForData_ValidateBasic(t *testing.T) {
msg: tailPaddingMsg,
wantErr: ErrTailPaddingNamespace,
},
{
name: "missing commitment for one square size",
msg: missingCommitmentForOneSquareSize,
wantErr: ErrInvalidShareCommitments,
},
}

for _, tt := range tests {
Expand Down