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

Add square sizes flag to the payForMessage cli command #202

Merged
merged 25 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1fecce2
add new flags
toanngosy Feb 5, 2022
41f9319
add celestia flags
toanngosy Feb 5, 2022
ecec3f0
use square size in cli wirepayformessage
toanngosy Feb 5, 2022
de95c78
add square size in app
toanngosy Feb 7, 2022
ff0637e
update test for arbitrary square size
toanngosy Feb 7, 2022
962b407
refactor flags
toanngosy Feb 9, 2022
8b1ec7f
shorten flags file
toanngosy Feb 11, 2022
e5f4b35
update nwpfm to receive list of square sizes
toanngosy Feb 11, 2022
f18fc52
add a new test case for list of square sizes
toanngosy Feb 11, 2022
8f30563
add back square size
toanngosy Feb 11, 2022
c71313c
unchange abci
toanngosy Feb 11, 2022
78b9fd4
refactor parse uint to uint64
toanngosy Feb 11, 2022
bb2c8db
add power of 2 check
toanngosy Feb 11, 2022
ae9f466
add test case for invalid square size
toanngosy Feb 11, 2022
b08447b
update description flags
toanngosy Feb 11, 2022
08ea197
move and update powerof2 func
toanngosy Feb 12, 2022
d3cca6f
update powerOf2 func
toanngosy Feb 12, 2022
5498a55
add test to powerOf2
toanngosy Feb 12, 2022
c94130c
delete gap
toanngosy Feb 13, 2022
33c7791
move check power of 2 first
toanngosy Feb 13, 2022
2d58081
move flag to wirepayformessage
toanngosy Feb 13, 2022
27928a7
move const FlagSquareSizes to wirepayformessage
toanngosy Feb 13, 2022
6da8dc9
add test for invalid square size in pfm ValidateBasic
toanngosy Feb 13, 2022
28bb0c4
add 1 more test where change square size to another power of 2 number
toanngosy Feb 13, 2022
80b52ab
change squaresize to 4 to pass the test
toanngosy Feb 14, 2022
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
2 changes: 1 addition & 1 deletion app/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,4 @@ func hasWirePayForMessage(tx sdk.Tx) bool {
// hardcoded. todo(evan): don't hardcode the square size
func (app *App) SquareSize() uint64 {
return consts.MaxSquareSize
}
}
18 changes: 17 additions & 1 deletion x/payment/client/cli/wirepayformessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

const FlagSquareSizes = "square-sizes"

func CmdWirePayForMessage() *cobra.Command {
cmd := &cobra.Command{
Use: "payForMessage [hexNamespace] [hexMessage]",
Expand Down Expand Up @@ -52,7 +54,12 @@ func CmdWirePayForMessage() *cobra.Command {
}

// create the MsgPayForMessage
pfmMsg, err := types.NewWirePayForMessage(namespace, message, consts.MaxSquareSize)
squareSizes, err := cmd.Flags().GetUintSlice(FlagSquareSizes)
if err != nil {
return err
}
squareSizes64 := parseSquareSizes(squareSizes)
pfmMsg, err := types.NewWirePayForMessage(namespace, message, squareSizes64...)
if err != nil {
return err
}
Expand Down Expand Up @@ -102,6 +109,15 @@ func CmdWirePayForMessage() *cobra.Command {
}

flags.AddTxFlagsToCmd(cmd)
cmd.Flags().UintSlice(FlagSquareSizes, []uint{consts.MaxSquareSize, 128, 64}, "Specify the square sizes, must be power of 2")

return cmd
}

func parseSquareSizes(squareSizes []uint) []uint64 {
squareSizes64 := make([]uint64, len(squareSizes))
for i := range squareSizes {
squareSizes64[i] = uint64(squareSizes[i])
}
return squareSizes64
}
26 changes: 26 additions & 0 deletions x/payment/client/testutil/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@ func (s *IntegrationTestSuite) TestSubmitWirePayForMessage() {
},
false, 0, &sdk.TxResponse{},
},
{
"valid transaction list of square sizes",
[]string{
hexNS,
hexMsg,
fmt.Sprintf("--from=%s", username),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(2))).String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", paycli.FlagSquareSizes, "256,128,64"),
},
false, 0, &sdk.TxResponse{},
},
{
"invalid transaction list of square sizes",
[]string{
hexNS,
hexMsg,
fmt.Sprintf("--from=%s", username),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(2))).String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", paycli.FlagSquareSizes, "256,123,64"),
},
true, 0, &sdk.TxResponse{},
},
}

for _, tc := range testCases {
Expand Down
9 changes: 9 additions & 0 deletions x/payment/types/payformessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,12 @@ func nextPowerOf2(v uint64) uint64 {
// return the next lowest power
return v / 2
}

// Check if number is power of 2
func powerOf2(v uint64) bool {
if v & (v-1) == 0 && v != 0 {
return true
} else {
return false
}
}
59 changes: 58 additions & 1 deletion x/payment/types/payformessage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,43 @@ func TestNextPowerOf2(t *testing.T) {
}
}

func TestPowerOf2 (t *testing.T) {
type test struct {
input uint64
expected bool
}
tests := []test {
{
input: 1,
expected: true,
},
{
input: 2,
expected: true,
},
{
input: 256,
expected: true,
},
{
input: 3,
expected: false,
},
{
input: 79,
expected: false,
},
{
input: 0,
expected: false,
},
}
for _, tt := range tests {
res := powerOf2(tt.input)
assert.Equal(t, tt.expected, res)
}
}

// TestCreateCommit only shows if something changed, it doesn't actually show
// the commit is being created correctly todo(evan): fix me.
func TestCreateCommitment(t *testing.T) {
Expand Down Expand Up @@ -251,6 +288,14 @@ func TestWirePayForMessage_ValidateBasic(t *testing.T) {
badCommitMsg := validWirePayForMessage(t)
badCommitMsg.MessageShareCommitment[0].ShareCommitment = []byte{1, 2, 3, 4}

// pfm that has invalid square size (not power of 2)
invalidSquareSizeMsg := validWirePayForMessage(t)
invalidSquareSizeMsg.MessageShareCommitment[0].K = 15

// pfm that has a different power of 2 square size
badSquareSizeMsg := validWirePayForMessage(t)
badSquareSizeMsg.MessageShareCommitment[0].K = 4

tests := []test{
{
name: "valid msg",
Expand Down Expand Up @@ -286,6 +331,18 @@ func TestWirePayForMessage_ValidateBasic(t *testing.T) {
expectErr: true,
errStr: "invalid commit for square size",
},
{
name: "invalid square size",
msg: invalidSquareSizeMsg,
expectErr: true,
errStr: fmt.Sprintf("invalid square size, the size must be power of 2: %d", invalidSquareSizeMsg.MessageShareCommitment[0].K),
},
{
name: "wrong but valid square size",
msg: badSquareSizeMsg,
expectErr: true,
errStr: fmt.Sprintf("invalid commit for square size %d", badSquareSizeMsg.MessageShareCommitment[0].K),
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -371,7 +428,7 @@ func TestProcessMessage(t *testing.T) {
func validWirePayForMessage(t *testing.T) *MsgWirePayForMessage {
msg, err := NewWirePayForMessage(
[]byte{1, 2, 3, 4, 5, 6, 7, 8},
bytes.Repeat([]byte{1}, 1000),
bytes.Repeat([]byte{1}, 2000),
16, 32, 64,
)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion x/payment/types/wirepayformessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func NewWirePayForMessage(namespace, message []byte, sizes ...uint64) (*MsgWireP

// generate the share commitments
for i, size := range sizes {
if !powerOf2(size) {
return nil, fmt.Errorf("Invalid square size, the size must be power of 2: %d", size)
}
commit, err := CreateCommitment(size, namespace, message)
if err != nil {
return nil, err
Expand Down Expand Up @@ -101,6 +104,10 @@ func (msg *MsgWirePayForMessage) ValidateBasic() error {

for _, commit := range msg.MessageShareCommitment {
// check that each commit is valid
if !powerOf2(commit.K) {
return fmt.Errorf("invalid square size, the size must be power of 2: %d", commit.K)
}

calculatedCommit, err := CreateCommitment(commit.K, msg.GetMessageNameSpaceId(), msg.Message)
if err != nil {
return err
Expand Down Expand Up @@ -205,4 +212,4 @@ func ProcessWirePayForMessage(msg *MsgWirePayForMessage, squareSize uint64) (*tm
}

return &coreMsg, pfm, shareCommit.Signature, nil
}
}