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 12 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: 11 additions & 8 deletions app/test/prepare_proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import (
"bytes"
"testing"

"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/app/encoding"
"github.com/celestiaorg/celestia-app/testutil"
"github.com/celestiaorg/celestia-app/x/payment/types"
"github.com/celestiaorg/nmt/namespace"
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -17,6 +13,11 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/pkg/consts"
core "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/app/encoding"
"github.com/celestiaorg/celestia-app/testutil"
"github.com/celestiaorg/celestia-app/x/payment/types"
)

func TestPrepareProposal(t *testing.T) {
Expand All @@ -25,6 +26,7 @@ func TestPrepareProposal(t *testing.T) {
encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...)

testApp := testutil.SetupTestAppWithGenesisValSet(t)
allSquareSizes := []uint64{2, 4, 8, 16, 32, 64, 128}

type test struct {
input abci.RequestPrepareProposal
Expand All @@ -34,15 +36,15 @@ func TestPrepareProposal(t *testing.T) {

firstNS := []byte{2, 2, 2, 2, 2, 2, 2, 2}
firstMessage := bytes.Repeat([]byte{4}, 512)
firstRawTx := generateRawTx(t, encCfg.TxConfig, firstNS, firstMessage, signer, 2, 4, 8, 16)
firstRawTx := generateRawTx(t, encCfg.TxConfig, firstNS, firstMessage, signer, allSquareSizes...)
rootulp marked this conversation as resolved.
Show resolved Hide resolved

secondNS := []byte{1, 1, 1, 1, 1, 1, 1, 1}
secondMessage := []byte{2}
secondRawTx := generateRawTx(t, encCfg.TxConfig, secondNS, secondMessage, signer, 2, 4, 8, 16)
secondRawTx := generateRawTx(t, encCfg.TxConfig, secondNS, secondMessage, signer, allSquareSizes...)

thirdNS := []byte{3, 3, 3, 3, 3, 3, 3, 3}
thirdMessage := []byte{1}
thirdRawTx := generateRawTx(t, encCfg.TxConfig, thirdNS, thirdMessage, signer, 2, 4, 8, 16)
thirdRawTx := generateRawTx(t, encCfg.TxConfig, thirdNS, thirdMessage, signer, allSquareSizes...)

tests := []test{
{
Expand Down Expand Up @@ -109,6 +111,7 @@ func TestPrepareMessagesWithReservedNamespaces(t *testing.T) {
encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...)

signer := testutil.GenerateKeyringSigner(t, testAccName)
allSquareSizes := []uint64{2, 4, 8, 16, 32, 64, 128}

type test struct {
name string
Expand All @@ -126,7 +129,7 @@ func TestPrepareMessagesWithReservedNamespaces(t *testing.T) {
}

for _, tt := range tests {
tx := generateRawTx(t, encCfg.TxConfig, tt.namespace, []byte{1}, signer, 2, 4, 8, 16)
tx := generateRawTx(t, encCfg.TxConfig, tt.namespace, []byte{1}, signer, allSquareSizes...)
input := abci.RequestPrepareProposal{
BlockData: &core.Data{
Txs: [][]byte{tx},
Expand Down
24 changes: 13 additions & 11 deletions app/test/split_shares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"bytes"
"testing"

"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/app/encoding"
shares "github.com/celestiaorg/celestia-app/pkg/shares"
"github.com/celestiaorg/celestia-app/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/pkg/consts"
"github.com/tendermint/tendermint/pkg/da"
core "github.com/tendermint/tendermint/proto/tendermint/types"
coretypes "github.com/tendermint/tendermint/types"

"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/app/encoding"
shares "github.com/celestiaorg/celestia-app/pkg/shares"
"github.com/celestiaorg/celestia-app/testutil"
)

func TestSplitShares(t *testing.T) {
Expand All @@ -26,18 +27,20 @@ func TestSplitShares(t *testing.T) {
}

signer := testutil.GenerateKeyringSigner(t, testAccName)
allSquareSizes := []uint64{2, 4, 8, 16, 32, 64, 128}
invalidSquareSizes := []uint64{2, 8, 16, 32, 64, 128} // missing square size: 4

firstNS := []byte{2, 2, 2, 2, 2, 2, 2, 2}
firstMessage := bytes.Repeat([]byte{4}, 512)
firstRawTx := generateRawTx(t, encCfg.TxConfig, firstNS, firstMessage, signer, 2, 4, 8)
firstRawTx := generateRawTx(t, encCfg.TxConfig, firstNS, firstMessage, signer, allSquareSizes...)

secondNS := []byte{1, 1, 1, 1, 1, 1, 1, 1}
secondMessage := []byte{2}
secondRawTx := generateRawTx(t, encCfg.TxConfig, secondNS, secondMessage, signer, 2, 4, 8)
secondRawTx := generateRawTx(t, encCfg.TxConfig, secondNS, secondMessage, signer, allSquareSizes...)

thirdNS := []byte{3, 3, 3, 3, 3, 3, 3, 3}
thirdMessage := []byte{1}
thirdRawTx := generateRawTx(t, encCfg.TxConfig, thirdNS, thirdMessage, signer, 2, 8)
thirdRawTx := generateRawTx(t, encCfg.TxConfig, thirdNS, thirdMessage, signer, invalidSquareSizes...)

tests := []test{
{
Expand Down Expand Up @@ -65,17 +68,16 @@ func TestSplitShares(t *testing.T) {
data: &core.Data{
Txs: [][]byte{firstRawTx, secondRawTx, thirdRawTx},
},
expectedTxCount: 3,
expectedTxCount: 2,
},
{
// calculate the square using the same txs but using a square size
// of 16, this should remove all of the txs as they weren't signed
// over for that square size
// of 16
squareSize: 16,
data: &core.Data{
Txs: [][]byte{firstRawTx, secondRawTx, thirdRawTx},
},
expectedTxCount: 0,
expectedTxCount: 2,
},
}

Expand Down
1 change: 1 addition & 0 deletions x/payment/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ var (
ErrTxNamespace = sdkerrors.Register(ModuleName, 11119, "cannot use transaction namespace ID")
ErrEvidenceNamespace = sdkerrors.Register(ModuleName, 11120, "cannot use evidence namespace ID")
ErrNoMessageShareCommitments = sdkerrors.Register(ModuleName, 11121, "no message share commitments")
ErrInvalidShareCommitments = sdkerrors.Register(ModuleName, 11122, "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 @@ -428,7 +428,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
56 changes: 56 additions & 0 deletions x/payment/types/wirepayfordata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
fmt "fmt"
"sort"

"github.com/celestiaorg/nmt/namespace"
sdkclient "github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -102,6 +103,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 @@ -122,9 +133,54 @@ func (msg *MsgWirePayForData) ValidateBasic() error {
return ErrNoMessageShareCommitments
}

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 square sizes expected for this message size.
func (msg *MsgWirePayForData) ValidateAllSquareSizesCommitedTo() error {
allSquareSizes := AllSquareSizes(int(msg.MessageSize))
committedSquareSizes := msg.committedSquareSizes()

if len(allSquareSizes) != len(committedSquareSizes) {
return ErrInvalidShareCommitments.Wrapf("length of all square sizes: %v must equal length of committed square sizes: %v", len(allSquareSizes), len(committedSquareSizes))
}

sort.Slice(allSquareSizes, func(i, j int) bool { return allSquareSizes[i] < allSquareSizes[j] })
rootulp marked this conversation as resolved.
Show resolved Hide resolved
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 !isEqual(allSquareSizes, committedSquareSizes) {
return ErrInvalidShareCommitments.Wrapf("all square sizes: %v, committed square sizes: %v", allSquareSizes, committedSquareSizes)
}
return nil
}

// isEqual returns true if the given uint64 slices are equal
func isEqual(a, b []uint64) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
rootulp marked this conversation as resolved.
Show resolved Hide resolved

// 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:]

// pfd that signed over no squares
noMessageShareCommitments := validWirePayForData(t)
Expand Down Expand Up @@ -104,6 +108,11 @@ func TestWirePayForData_ValidateBasic(t *testing.T) {
msg: noMessageShareCommitments,
wantErr: ErrNoMessageShareCommitments,
},
{
name: "missing commitment for one square size",
msg: missingCommitmentForOneSquareSize,
wantErr: ErrInvalidShareCommitments,
},
}

for _, tt := range tests {
Expand Down