From 1fecce24665e6fb65fa52cbdd32e2c727c8e5b00 Mon Sep 17 00:00:00 2001 From: toanngo Date: Fri, 4 Feb 2022 23:55:26 -0800 Subject: [PATCH 01/25] add new flags --- flags/flags.go | 177 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 flags/flags.go diff --git a/flags/flags.go b/flags/flags.go new file mode 100644 index 0000000000..71301f53d4 --- /dev/null +++ b/flags/flags.go @@ -0,0 +1,177 @@ +package flags + +import ( + "fmt" + "strconv" + + "github.com/spf13/cobra" + tmcli "github.com/tendermint/tendermint/libs/cli" + + "github.com/cosmos/cosmos-sdk/crypto/keyring" +) + +const ( + // DefaultGasAdjustment is applied to gas estimates to avoid tx execution + // failures due to state changes that might occur between the tx simulation + // and the actual run. + DefaultGasAdjustment = 1.0 + DefaultGasLimit = 200000 + GasFlagAuto = "auto" + + // DefaultKeyringBackend + DefaultKeyringBackend = keyring.BackendOS + + // BroadcastBlock defines a tx broadcasting mode where the client waits for + // the tx to be committed in a block. + BroadcastBlock = "block" + // BroadcastSync defines a tx broadcasting mode where the client waits for + // a CheckTx execution response only. + BroadcastSync = "sync" + // BroadcastAsync defines a tx broadcasting mode where the client returns + // immediately. + BroadcastAsync = "async" + + // SignModeDirect is the value of the --sign-mode flag for SIGN_MODE_DIRECT + SignModeDirect = "direct" + // SignModeLegacyAminoJSON is the value of the --sign-mode flag for SIGN_MODE_LEGACY_AMINO_JSON + SignModeLegacyAminoJSON = "amino-json" + // SignModeDirectAux is the value of the --sign-mode flag for SIGN_MODE_DIRECT_AUX + SignModeDirectAux = "direct-aux" +) + +// List of CLI flags +const ( + FlagHome = tmcli.HomeFlag + FlagKeyringDir = "keyring-dir" + FlagUseLedger = "ledger" + FlagChainID = "chain-id" + FlagNode = "node" + FlagHeight = "height" + FlagGasAdjustment = "gas-adjustment" + FlagFrom = "from" + FlagName = "name" + FlagAccountNumber = "account-number" + FlagSequence = "sequence" + FlagNote = "note" + FlagFees = "fees" + FlagGas = "gas" + FlagGasPrices = "gas-prices" + FlagBroadcastMode = "broadcast-mode" + FlagDryRun = "dry-run" + FlagGenerateOnly = "generate-only" + FlagOffline = "offline" + FlagOutputDocument = "output-document" // inspired by wget -O + FlagSkipConfirmation = "yes" + FlagProve = "prove" + FlagKeyringBackend = "keyring-backend" + FlagPage = "page" + FlagLimit = "limit" + FlagSignMode = "sign-mode" + FlagPageKey = "page-key" + FlagOffset = "offset" + FlagCountTotal = "count-total" + FlagTimeoutHeight = "timeout-height" + FlagKeyAlgorithm = "algo" + FlagFeePayer = "fee-payer" + FlagFeeGranter = "fee-granter" + FlagReverse = "reverse" + FlagTip = "tip" + FlagAux = "aux" + FlagTipper = "tipper" + FlagSquareSize = "square-size" + + // Tendermint logging flags + FlagLogLevel = "log_level" + FlagLogFormat = "log_format" +) + +// LineBreak can be included in a command list to provide a blank line +// to help with readability +var LineBreak = &cobra.Command{Run: func(*cobra.Command, []string) {}} + +// AddQueryFlagsToCmd adds common flags to a module query command. +func AddQueryFlagsToCmd(cmd *cobra.Command) { + cmd.Flags().String(FlagNode, "tcp://localhost:26657", ": to Tendermint RPC interface for this chain") + cmd.Flags().Int64(FlagHeight, 0, "Use a specific height to query state at (this can error if the node is pruning state)") + cmd.Flags().StringP(tmcli.OutputFlag, "o", "text", "Output format (text|json)") + + cmd.MarkFlagRequired(FlagChainID) +} + +// AddTxFlagsToCmd adds common flags to a module tx command. +func AddTxFlagsToCmd(cmd *cobra.Command) { + cmd.Flags().StringP(tmcli.OutputFlag, "o", "json", "Output format (text|json)") + cmd.Flags().String(FlagKeyringDir, "", "The client Keyring directory; if omitted, the default 'home' directory will be used") + cmd.Flags().String(FlagFrom, "", "Name or address of private key with which to sign") + cmd.Flags().Uint64P(FlagAccountNumber, "a", 0, "The account number of the signing account (offline mode only)") + cmd.Flags().Uint64P(FlagSequence, "s", 0, "The sequence number of the signing account (offline mode only)") + cmd.Flags().String(FlagNote, "", "Note to add a description to the transaction (previously --memo)") + cmd.Flags().String(FlagFees, "", "Fees to pay along with transaction; eg: 10uatom") + cmd.Flags().String(FlagGasPrices, "", "Gas prices in decimal format to determine the transaction fee (e.g. 0.1uatom)") + cmd.Flags().String(FlagNode, "tcp://localhost:26657", ": to tendermint rpc interface for this chain") + cmd.Flags().Bool(FlagUseLedger, false, "Use a connected Ledger device") + cmd.Flags().Float64(FlagGasAdjustment, DefaultGasAdjustment, "adjustment factor to be multiplied against the estimate returned by the tx simulation; if the gas limit is set manually this flag is ignored ") + cmd.Flags().StringP(FlagBroadcastMode, "b", BroadcastSync, "Transaction broadcasting mode (sync|async|block)") + cmd.Flags().Bool(FlagDryRun, false, "ignore the --gas flag and perform a simulation of a transaction, but don't broadcast it") + cmd.Flags().Bool(FlagGenerateOnly, false, "Build an unsigned transaction and write it to STDOUT (when enabled, the local Keybase is not accessible)") + cmd.Flags().Bool(FlagOffline, false, "Offline mode (does not allow any online functionality)") + cmd.Flags().BoolP(FlagSkipConfirmation, "y", false, "Skip tx broadcasting prompt confirmation") + cmd.Flags().String(FlagKeyringBackend, DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test|memory)") + cmd.Flags().String(FlagSignMode, "", "Choose sign mode (direct|amino-json|direct-aux), this is an advanced feature") + cmd.Flags().Uint64(FlagTimeoutHeight, 0, "Set a block timeout height to prevent the tx from being committed past a certain height") + cmd.Flags().String(FlagFeePayer, "", "Fee payer pays fees for the transaction instead of deducting from the signer") + cmd.Flags().String(FlagFeeGranter, "", "Fee granter grants fees for the transaction") + cmd.Flags().String(FlagTip, "", "Tip is the amount that is going to be transferred to the fee payer on the target chain. This flag is only valid when used with --aux") + cmd.Flags().String(FlagTipper, "", "Tipper will pay for tips for executing the tx on the target chain. This flag is only valid when used with --aux") + cmd.Flags().Bool(FlagAux, false, "Generate aux signer data instead of sending a tx") + cmd.Flags().Uint64(FlagSquareSize, 16, "Square size") + + // --gas can accept integers and "auto" + cmd.Flags().String(FlagGas, "", fmt.Sprintf("gas limit to set per-transaction; set to %q to calculate sufficient gas automatically (default %d)", GasFlagAuto, DefaultGasLimit)) +} + +// AddPaginationFlagsToCmd adds common pagination flags to cmd +func AddPaginationFlagsToCmd(cmd *cobra.Command, query string) { + cmd.Flags().Uint64(FlagPage, 1, fmt.Sprintf("pagination page of %s to query for. This sets offset to a multiple of limit", query)) + cmd.Flags().String(FlagPageKey, "", fmt.Sprintf("pagination page-key of %s to query for", query)) + cmd.Flags().Uint64(FlagOffset, 0, fmt.Sprintf("pagination offset of %s to query for", query)) + cmd.Flags().Uint64(FlagLimit, 100, fmt.Sprintf("pagination limit of %s to query for", query)) + cmd.Flags().Bool(FlagCountTotal, false, fmt.Sprintf("count total number of records in %s to query for", query)) + cmd.Flags().Bool(FlagReverse, false, "results are sorted in descending order") +} + +// GasSetting encapsulates the possible values passed through the --gas flag. +type GasSetting struct { + Simulate bool + Gas uint64 +} + +func (v *GasSetting) String() string { + if v.Simulate { + return GasFlagAuto + } + + return strconv.FormatUint(v.Gas, 10) +} + +// ParseGasSetting parses a string gas value. The value may either be 'auto', +// which indicates a transaction should be executed in simulate mode to +// automatically find a sufficient gas value, or a string integer. It returns an +// error if a string integer is provided which cannot be parsed. +func ParseGasSetting(gasStr string) (GasSetting, error) { + switch gasStr { + case "": + return GasSetting{false, DefaultGasLimit}, nil + + case GasFlagAuto: + return GasSetting{true, 0}, nil + + default: + gas, err := strconv.ParseUint(gasStr, 10, 64) + if err != nil { + return GasSetting{}, fmt.Errorf("gas must be either integer or %s", GasFlagAuto) + } + + return GasSetting{false, gas}, nil + } +} \ No newline at end of file From 41f931905d5e495eca0bbe23f1c073e036bef9bf Mon Sep 17 00:00:00 2001 From: toanngo Date: Sat, 5 Feb 2022 02:35:58 -0800 Subject: [PATCH 02/25] add celestia flags --- flags/flags.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flags/flags.go b/flags/flags.go index 71301f53d4..4464a2362b 100644 --- a/flags/flags.go +++ b/flags/flags.go @@ -6,7 +6,7 @@ import ( "github.com/spf13/cobra" tmcli "github.com/tendermint/tendermint/libs/cli" - + "github.com/tendermint/tendermint/pkg/consts" "github.com/cosmos/cosmos-sdk/crypto/keyring" ) @@ -124,7 +124,7 @@ func AddTxFlagsToCmd(cmd *cobra.Command) { cmd.Flags().String(FlagTip, "", "Tip is the amount that is going to be transferred to the fee payer on the target chain. This flag is only valid when used with --aux") cmd.Flags().String(FlagTipper, "", "Tipper will pay for tips for executing the tx on the target chain. This flag is only valid when used with --aux") cmd.Flags().Bool(FlagAux, false, "Generate aux signer data instead of sending a tx") - cmd.Flags().Uint64(FlagSquareSize, 16, "Square size") + cmd.Flags().Uint64(FlagSquareSize, consts.MaxSquareSize, "Specify the square size") // --gas can accept integers and "auto" cmd.Flags().String(FlagGas, "", fmt.Sprintf("gas limit to set per-transaction; set to %q to calculate sufficient gas automatically (default %d)", GasFlagAuto, DefaultGasLimit)) From ecec3f09bdc5ea4424f5c87e4480db3ebd3b57c5 Mon Sep 17 00:00:00 2001 From: toanngo Date: Sat, 5 Feb 2022 02:48:16 -0800 Subject: [PATCH 03/25] use square size in cli wirepayformessage --- x/payment/client/cli/wirepayformessage.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/x/payment/client/cli/wirepayformessage.go b/x/payment/client/cli/wirepayformessage.go index f54666b5f1..bb564de8e6 100644 --- a/x/payment/client/cli/wirepayformessage.go +++ b/x/payment/client/cli/wirepayformessage.go @@ -6,11 +6,11 @@ import ( "fmt" "github.com/spf13/cobra" - "github.com/tendermint/tendermint/pkg/consts" + //"github.com/tendermint/tendermint/pkg/consts" "github.com/celestiaorg/celestia-app/x/payment/types" "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/celestiaorg/celestia-app/flags" "github.com/cosmos/cosmos-sdk/client/tx" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -52,7 +52,12 @@ func CmdWirePayForMessage() *cobra.Command { } // create the MsgPayForMessage - pfmMsg, err := types.NewWirePayForMessage(namespace, message, consts.MaxSquareSize) + squareSize, err := cmd.Flags().GetUint64("square-size") + if err != nil { + return err + } + pfmMsg, err := types.NewWirePayForMessage(namespace, message, squareSize) + if err != nil { return err } From de95c7801b34d6ed31022123602eefcef0ca316c Mon Sep 17 00:00:00 2001 From: toanngo Date: Sun, 6 Feb 2022 17:45:15 -0800 Subject: [PATCH 04/25] add square size in app --- app/abci.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/app/abci.go b/app/abci.go index fd4a45c78e..46fd7761c0 100644 --- a/app/abci.go +++ b/app/abci.go @@ -18,7 +18,6 @@ import ( // performing basic validation for the incoming txs, and by cleanly separating // share messages from transactions func (app *App) PreprocessTxs(txs abci.RequestPreprocessTxs) abci.ResponsePreprocessTxs { - squareSize := app.SquareSize() shareCounter := uint64(0) var shareMsgs []*core.Message var processedTxs [][]byte @@ -58,8 +57,9 @@ func (app *App) PreprocessTxs(txs abci.RequestPreprocessTxs) abci.ResponsePrepro continue } + squareSize := wireMsg.MessageShareCommitment[0].K // parse wire message and create a single message - coreMsg, unsignedPFM, sig, err := types.ProcessWirePayForMessage(wireMsg, app.SquareSize()) + coreMsg, unsignedPFM, sig, err := types.ProcessWirePayForMessage(wireMsg, squareSize) if err != nil { continue } @@ -116,9 +116,3 @@ func hasWirePayForMessage(tx sdk.Tx) bool { } return false } - -// SquareSize returns the current square size. Currently, the square size is -// hardcoded. todo(evan): don't hardcode the square size -func (app *App) SquareSize() uint64 { - return consts.MaxSquareSize -} From ff0637eb8f317d8c162a9bfae3ebe2fa3e81253b Mon Sep 17 00:00:00 2001 From: toanngo Date: Sun, 6 Feb 2022 18:15:02 -0800 Subject: [PATCH 05/25] update test for arbitrary square size --- app/abci.go | 1 - x/payment/client/testutil/integration_test.go | 16 +++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/abci.go b/app/abci.go index 46fd7761c0..1e64c30bef 100644 --- a/app/abci.go +++ b/app/abci.go @@ -9,7 +9,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth/signing" abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/pkg/consts" core "github.com/tendermint/tendermint/proto/tendermint/types" coretypes "github.com/tendermint/tendermint/types" ) diff --git a/x/payment/client/testutil/integration_test.go b/x/payment/client/testutil/integration_test.go index 7e728f7e93..5540bd4135 100644 --- a/x/payment/client/testutil/integration_test.go +++ b/x/payment/client/testutil/integration_test.go @@ -5,7 +5,8 @@ import ( "testing" "time" - "github.com/cosmos/cosmos-sdk/client/flags" + //"github.com/cosmos/cosmos-sdk/client/flags" + "github.com/celestiaorg/celestia-app/flags" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/suite" @@ -82,6 +83,19 @@ func (s *IntegrationTestSuite) TestSubmitWirePayForMessage() { }, false, 0, &sdk.TxResponse{}, }, + { + "valid transaction square size 64", + []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", flags.FlagSquareSize, "64"), + }, + false, 0, &sdk.TxResponse{}, + }, } for _, tc := range testCases { From 962b4071a0d16741afaec8eee6bc0c3e1998fbaf Mon Sep 17 00:00:00 2001 From: toanngo Date: Tue, 8 Feb 2022 21:17:39 -0800 Subject: [PATCH 06/25] refactor flags --- flags/flags.go | 166 +----------------- x/payment/client/cli/wirepayformessage.go | 7 +- x/payment/client/testutil/integration_test.go | 6 +- 3 files changed, 10 insertions(+), 169 deletions(-) diff --git a/flags/flags.go b/flags/flags.go index 4464a2362b..af5c0b0561 100644 --- a/flags/flags.go +++ b/flags/flags.go @@ -1,177 +1,17 @@ package flags import ( - "fmt" - "strconv" + "github.com/cosmos/cosmos-sdk/client/flags" "github.com/spf13/cobra" - tmcli "github.com/tendermint/tendermint/libs/cli" "github.com/tendermint/tendermint/pkg/consts" - "github.com/cosmos/cosmos-sdk/crypto/keyring" -) - -const ( - // DefaultGasAdjustment is applied to gas estimates to avoid tx execution - // failures due to state changes that might occur between the tx simulation - // and the actual run. - DefaultGasAdjustment = 1.0 - DefaultGasLimit = 200000 - GasFlagAuto = "auto" - - // DefaultKeyringBackend - DefaultKeyringBackend = keyring.BackendOS - - // BroadcastBlock defines a tx broadcasting mode where the client waits for - // the tx to be committed in a block. - BroadcastBlock = "block" - // BroadcastSync defines a tx broadcasting mode where the client waits for - // a CheckTx execution response only. - BroadcastSync = "sync" - // BroadcastAsync defines a tx broadcasting mode where the client returns - // immediately. - BroadcastAsync = "async" - - // SignModeDirect is the value of the --sign-mode flag for SIGN_MODE_DIRECT - SignModeDirect = "direct" - // SignModeLegacyAminoJSON is the value of the --sign-mode flag for SIGN_MODE_LEGACY_AMINO_JSON - SignModeLegacyAminoJSON = "amino-json" - // SignModeDirectAux is the value of the --sign-mode flag for SIGN_MODE_DIRECT_AUX - SignModeDirectAux = "direct-aux" ) // List of CLI flags -const ( - FlagHome = tmcli.HomeFlag - FlagKeyringDir = "keyring-dir" - FlagUseLedger = "ledger" - FlagChainID = "chain-id" - FlagNode = "node" - FlagHeight = "height" - FlagGasAdjustment = "gas-adjustment" - FlagFrom = "from" - FlagName = "name" - FlagAccountNumber = "account-number" - FlagSequence = "sequence" - FlagNote = "note" - FlagFees = "fees" - FlagGas = "gas" - FlagGasPrices = "gas-prices" - FlagBroadcastMode = "broadcast-mode" - FlagDryRun = "dry-run" - FlagGenerateOnly = "generate-only" - FlagOffline = "offline" - FlagOutputDocument = "output-document" // inspired by wget -O - FlagSkipConfirmation = "yes" - FlagProve = "prove" - FlagKeyringBackend = "keyring-backend" - FlagPage = "page" - FlagLimit = "limit" - FlagSignMode = "sign-mode" - FlagPageKey = "page-key" - FlagOffset = "offset" - FlagCountTotal = "count-total" - FlagTimeoutHeight = "timeout-height" - FlagKeyAlgorithm = "algo" - FlagFeePayer = "fee-payer" - FlagFeeGranter = "fee-granter" - FlagReverse = "reverse" - FlagTip = "tip" - FlagAux = "aux" - FlagTipper = "tipper" - FlagSquareSize = "square-size" - - // Tendermint logging flags - FlagLogLevel = "log_level" - FlagLogFormat = "log_format" -) - -// LineBreak can be included in a command list to provide a blank line -// to help with readability -var LineBreak = &cobra.Command{Run: func(*cobra.Command, []string) {}} - -// AddQueryFlagsToCmd adds common flags to a module query command. -func AddQueryFlagsToCmd(cmd *cobra.Command) { - cmd.Flags().String(FlagNode, "tcp://localhost:26657", ": to Tendermint RPC interface for this chain") - cmd.Flags().Int64(FlagHeight, 0, "Use a specific height to query state at (this can error if the node is pruning state)") - cmd.Flags().StringP(tmcli.OutputFlag, "o", "text", "Output format (text|json)") - - cmd.MarkFlagRequired(FlagChainID) -} +const FlagSquareSize = "square-size" // AddTxFlagsToCmd adds common flags to a module tx command. func AddTxFlagsToCmd(cmd *cobra.Command) { - cmd.Flags().StringP(tmcli.OutputFlag, "o", "json", "Output format (text|json)") - cmd.Flags().String(FlagKeyringDir, "", "The client Keyring directory; if omitted, the default 'home' directory will be used") - cmd.Flags().String(FlagFrom, "", "Name or address of private key with which to sign") - cmd.Flags().Uint64P(FlagAccountNumber, "a", 0, "The account number of the signing account (offline mode only)") - cmd.Flags().Uint64P(FlagSequence, "s", 0, "The sequence number of the signing account (offline mode only)") - cmd.Flags().String(FlagNote, "", "Note to add a description to the transaction (previously --memo)") - cmd.Flags().String(FlagFees, "", "Fees to pay along with transaction; eg: 10uatom") - cmd.Flags().String(FlagGasPrices, "", "Gas prices in decimal format to determine the transaction fee (e.g. 0.1uatom)") - cmd.Flags().String(FlagNode, "tcp://localhost:26657", ": to tendermint rpc interface for this chain") - cmd.Flags().Bool(FlagUseLedger, false, "Use a connected Ledger device") - cmd.Flags().Float64(FlagGasAdjustment, DefaultGasAdjustment, "adjustment factor to be multiplied against the estimate returned by the tx simulation; if the gas limit is set manually this flag is ignored ") - cmd.Flags().StringP(FlagBroadcastMode, "b", BroadcastSync, "Transaction broadcasting mode (sync|async|block)") - cmd.Flags().Bool(FlagDryRun, false, "ignore the --gas flag and perform a simulation of a transaction, but don't broadcast it") - cmd.Flags().Bool(FlagGenerateOnly, false, "Build an unsigned transaction and write it to STDOUT (when enabled, the local Keybase is not accessible)") - cmd.Flags().Bool(FlagOffline, false, "Offline mode (does not allow any online functionality)") - cmd.Flags().BoolP(FlagSkipConfirmation, "y", false, "Skip tx broadcasting prompt confirmation") - cmd.Flags().String(FlagKeyringBackend, DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test|memory)") - cmd.Flags().String(FlagSignMode, "", "Choose sign mode (direct|amino-json|direct-aux), this is an advanced feature") - cmd.Flags().Uint64(FlagTimeoutHeight, 0, "Set a block timeout height to prevent the tx from being committed past a certain height") - cmd.Flags().String(FlagFeePayer, "", "Fee payer pays fees for the transaction instead of deducting from the signer") - cmd.Flags().String(FlagFeeGranter, "", "Fee granter grants fees for the transaction") - cmd.Flags().String(FlagTip, "", "Tip is the amount that is going to be transferred to the fee payer on the target chain. This flag is only valid when used with --aux") - cmd.Flags().String(FlagTipper, "", "Tipper will pay for tips for executing the tx on the target chain. This flag is only valid when used with --aux") - cmd.Flags().Bool(FlagAux, false, "Generate aux signer data instead of sending a tx") + flags.AddTxFlagsToCmd(cmd) cmd.Flags().Uint64(FlagSquareSize, consts.MaxSquareSize, "Specify the square size") - - // --gas can accept integers and "auto" - cmd.Flags().String(FlagGas, "", fmt.Sprintf("gas limit to set per-transaction; set to %q to calculate sufficient gas automatically (default %d)", GasFlagAuto, DefaultGasLimit)) -} - -// AddPaginationFlagsToCmd adds common pagination flags to cmd -func AddPaginationFlagsToCmd(cmd *cobra.Command, query string) { - cmd.Flags().Uint64(FlagPage, 1, fmt.Sprintf("pagination page of %s to query for. This sets offset to a multiple of limit", query)) - cmd.Flags().String(FlagPageKey, "", fmt.Sprintf("pagination page-key of %s to query for", query)) - cmd.Flags().Uint64(FlagOffset, 0, fmt.Sprintf("pagination offset of %s to query for", query)) - cmd.Flags().Uint64(FlagLimit, 100, fmt.Sprintf("pagination limit of %s to query for", query)) - cmd.Flags().Bool(FlagCountTotal, false, fmt.Sprintf("count total number of records in %s to query for", query)) - cmd.Flags().Bool(FlagReverse, false, "results are sorted in descending order") -} - -// GasSetting encapsulates the possible values passed through the --gas flag. -type GasSetting struct { - Simulate bool - Gas uint64 -} - -func (v *GasSetting) String() string { - if v.Simulate { - return GasFlagAuto - } - - return strconv.FormatUint(v.Gas, 10) -} - -// ParseGasSetting parses a string gas value. The value may either be 'auto', -// which indicates a transaction should be executed in simulate mode to -// automatically find a sufficient gas value, or a string integer. It returns an -// error if a string integer is provided which cannot be parsed. -func ParseGasSetting(gasStr string) (GasSetting, error) { - switch gasStr { - case "": - return GasSetting{false, DefaultGasLimit}, nil - - case GasFlagAuto: - return GasSetting{true, 0}, nil - - default: - gas, err := strconv.ParseUint(gasStr, 10, 64) - if err != nil { - return GasSetting{}, fmt.Errorf("gas must be either integer or %s", GasFlagAuto) - } - - return GasSetting{false, gas}, nil - } } \ No newline at end of file diff --git a/x/payment/client/cli/wirepayformessage.go b/x/payment/client/cli/wirepayformessage.go index bb564de8e6..1627089116 100644 --- a/x/payment/client/cli/wirepayformessage.go +++ b/x/payment/client/cli/wirepayformessage.go @@ -10,7 +10,8 @@ import ( "github.com/celestiaorg/celestia-app/x/payment/types" "github.com/cosmos/cosmos-sdk/client" - "github.com/celestiaorg/celestia-app/flags" + "github.com/cosmos/cosmos-sdk/client/flags" + cflags "github.com/celestiaorg/celestia-app/flags" "github.com/cosmos/cosmos-sdk/client/tx" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -52,7 +53,7 @@ func CmdWirePayForMessage() *cobra.Command { } // create the MsgPayForMessage - squareSize, err := cmd.Flags().GetUint64("square-size") + squareSize, err := cmd.Flags().GetUint64(cflags.FlagSquareSize) if err != nil { return err } @@ -106,7 +107,7 @@ func CmdWirePayForMessage() *cobra.Command { }, } - flags.AddTxFlagsToCmd(cmd) + cflags.AddTxFlagsToCmd(cmd) return cmd } diff --git a/x/payment/client/testutil/integration_test.go b/x/payment/client/testutil/integration_test.go index 5540bd4135..80da2f3b57 100644 --- a/x/payment/client/testutil/integration_test.go +++ b/x/payment/client/testutil/integration_test.go @@ -5,8 +5,8 @@ import ( "testing" "time" - //"github.com/cosmos/cosmos-sdk/client/flags" - "github.com/celestiaorg/celestia-app/flags" + "github.com/cosmos/cosmos-sdk/client/flags" + cflags "github.com/celestiaorg/celestia-app/flags" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/suite" @@ -92,7 +92,7 @@ func (s *IntegrationTestSuite) TestSubmitWirePayForMessage() { 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", flags.FlagSquareSize, "64"), + fmt.Sprintf("--%s=%s", cflags.FlagSquareSize, "64"), }, false, 0, &sdk.TxResponse{}, }, From 8b1ec7f7706c0186b30fc94f193f82c24b3abbd0 Mon Sep 17 00:00:00 2001 From: toanngo Date: Thu, 10 Feb 2022 17:12:52 -0800 Subject: [PATCH 07/25] shorten flags file --- flags/flags.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flags/flags.go b/flags/flags.go index af5c0b0561..56e6f8e9d0 100644 --- a/flags/flags.go +++ b/flags/flags.go @@ -8,10 +8,10 @@ import ( ) // List of CLI flags -const FlagSquareSize = "square-size" +const FlagSquareSizes = "square-sizes" // AddTxFlagsToCmd adds common flags to a module tx command. func AddTxFlagsToCmd(cmd *cobra.Command) { flags.AddTxFlagsToCmd(cmd) - cmd.Flags().Uint64(FlagSquareSize, consts.MaxSquareSize, "Specify the square size") + cmd.Flags().UintSlice(FlagSquareSizes, []uint{consts.MaxSquareSize, 128, 64}, "Specify the square size") } \ No newline at end of file From e5f4b35955dd36b756c672c5fddcff9620000dd8 Mon Sep 17 00:00:00 2001 From: toanngo Date: Thu, 10 Feb 2022 17:13:39 -0800 Subject: [PATCH 08/25] update nwpfm to receive list of square sizes --- x/payment/client/cli/wirepayformessage.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/x/payment/client/cli/wirepayformessage.go b/x/payment/client/cli/wirepayformessage.go index 1627089116..c3ce576543 100644 --- a/x/payment/client/cli/wirepayformessage.go +++ b/x/payment/client/cli/wirepayformessage.go @@ -53,11 +53,16 @@ func CmdWirePayForMessage() *cobra.Command { } // create the MsgPayForMessage - squareSize, err := cmd.Flags().GetUint64(cflags.FlagSquareSize) + squareSizes, err := cmd.Flags().GetUintSlice(cflags.FlagSquareSizes) if err != nil { return err } - pfmMsg, err := types.NewWirePayForMessage(namespace, message, squareSize) + + squareSizes64 := make([]uint64, len(squareSizes)) + for i := range squareSizes { + squareSizes64[i] = uint64(squareSizes[i]) + } + pfmMsg, err := types.NewWirePayForMessage(namespace, message, squareSizes64...) if err != nil { return err From f18fc52a30632824304e163865550ee259063ec6 Mon Sep 17 00:00:00 2001 From: toanngo Date: Thu, 10 Feb 2022 17:14:12 -0800 Subject: [PATCH 09/25] add a new test case for list of square sizes --- x/payment/client/testutil/integration_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/payment/client/testutil/integration_test.go b/x/payment/client/testutil/integration_test.go index 80da2f3b57..552bbc3e29 100644 --- a/x/payment/client/testutil/integration_test.go +++ b/x/payment/client/testutil/integration_test.go @@ -84,7 +84,7 @@ func (s *IntegrationTestSuite) TestSubmitWirePayForMessage() { false, 0, &sdk.TxResponse{}, }, { - "valid transaction square size 64", + "valid transaction list of square sizes", []string{ hexNS, hexMsg, @@ -92,7 +92,7 @@ func (s *IntegrationTestSuite) TestSubmitWirePayForMessage() { 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", cflags.FlagSquareSize, "64"), + fmt.Sprintf("--%s=%s", cflags.FlagSquareSizes, "256,128,64"), }, false, 0, &sdk.TxResponse{}, }, From 8f3056390b75b2d0e897d5a96ab849e1e844e8b8 Mon Sep 17 00:00:00 2001 From: toanngo Date: Thu, 10 Feb 2022 17:19:18 -0800 Subject: [PATCH 10/25] add back square size --- app/abci.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/abci.go b/app/abci.go index 1e64c30bef..5e986a5f03 100644 --- a/app/abci.go +++ b/app/abci.go @@ -9,6 +9,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth/signing" abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/pkg/consts" core "github.com/tendermint/tendermint/proto/tendermint/types" coretypes "github.com/tendermint/tendermint/types" ) @@ -17,6 +18,7 @@ import ( // performing basic validation for the incoming txs, and by cleanly separating // share messages from transactions func (app *App) PreprocessTxs(txs abci.RequestPreprocessTxs) abci.ResponsePreprocessTxs { + squareSize := app.SquareSize() shareCounter := uint64(0) var shareMsgs []*core.Message var processedTxs [][]byte @@ -58,7 +60,7 @@ func (app *App) PreprocessTxs(txs abci.RequestPreprocessTxs) abci.ResponsePrepro squareSize := wireMsg.MessageShareCommitment[0].K // parse wire message and create a single message - coreMsg, unsignedPFM, sig, err := types.ProcessWirePayForMessage(wireMsg, squareSize) + coreMsg, unsignedPFM, sig, err := types.ProcessWirePayForMessage(wireMsg, app.SquareSize()) if err != nil { continue } @@ -115,3 +117,9 @@ func hasWirePayForMessage(tx sdk.Tx) bool { } return false } + +// SquareSize returns the current square size. Currently, the square size is +// hardcoded. todo(evan): don't hardcode the square size +func (app *App) SquareSize() uint64 { + return consts.MaxSquareSize +} \ No newline at end of file From c71313c346e0a31a06eb01f494c791b972d1b8e7 Mon Sep 17 00:00:00 2001 From: toanngo Date: Thu, 10 Feb 2022 17:21:18 -0800 Subject: [PATCH 11/25] unchange abci --- app/abci.go | 1 - 1 file changed, 1 deletion(-) diff --git a/app/abci.go b/app/abci.go index 5e986a5f03..591c850f56 100644 --- a/app/abci.go +++ b/app/abci.go @@ -58,7 +58,6 @@ func (app *App) PreprocessTxs(txs abci.RequestPreprocessTxs) abci.ResponsePrepro continue } - squareSize := wireMsg.MessageShareCommitment[0].K // parse wire message and create a single message coreMsg, unsignedPFM, sig, err := types.ProcessWirePayForMessage(wireMsg, app.SquareSize()) if err != nil { From 78b9fd4d9da61d7e59f2608928a73ca858072b29 Mon Sep 17 00:00:00 2001 From: toanngo Date: Thu, 10 Feb 2022 19:58:58 -0800 Subject: [PATCH 12/25] refactor parse uint to uint64 --- x/payment/client/cli/wirepayformessage.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/x/payment/client/cli/wirepayformessage.go b/x/payment/client/cli/wirepayformessage.go index c3ce576543..228fcf55aa 100644 --- a/x/payment/client/cli/wirepayformessage.go +++ b/x/payment/client/cli/wirepayformessage.go @@ -58,10 +58,7 @@ func CmdWirePayForMessage() *cobra.Command { return err } - squareSizes64 := make([]uint64, len(squareSizes)) - for i := range squareSizes { - squareSizes64[i] = uint64(squareSizes[i]) - } + squareSizes64 := parseSquareSizes(squareSizes) pfmMsg, err := types.NewWirePayForMessage(namespace, message, squareSizes64...) if err != nil { @@ -116,3 +113,11 @@ func CmdWirePayForMessage() *cobra.Command { return cmd } + +func parseSquareSizes(squareSizes []uint) []uint64 { + squareSizes64 := make([]uint64, len(squareSizes)) + for i := range squareSizes { + squareSizes64[i] = uint64(squareSizes[i]) + } + return squareSizes64 +} \ No newline at end of file From bb2c8dbe37463e1ff0a041d07859c2db38d8fd74 Mon Sep 17 00:00:00 2001 From: toanngo Date: Thu, 10 Feb 2022 20:40:34 -0800 Subject: [PATCH 13/25] add power of 2 check --- x/payment/types/wirepayformessage.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/x/payment/types/wirepayformessage.go b/x/payment/types/wirepayformessage.go index 5ad1a0761d..2ea77dc357 100644 --- a/x/payment/types/wirepayformessage.go +++ b/x/payment/types/wirepayformessage.go @@ -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 @@ -106,6 +109,10 @@ func (msg *MsgWirePayForMessage) ValidateBasic() error { return err } + if !powerOf2(commit.K) { + return fmt.Errorf("invalid square size, the size must be power of 2: %d", commit.K) + } + if string(calculatedCommit) != string(commit.ShareCommitment) { return fmt.Errorf("invalid commit for square size %d", commit.K) } @@ -206,3 +213,13 @@ func ProcessWirePayForMessage(msg *MsgWirePayForMessage, squareSize uint64) (*tm return &coreMsg, pfm, shareCommit.Signature, nil } + + +// Check if number is power of 2 +func powerOf2(size uint64) bool { + if size & (size-1) == 0 { + return true + } else { + return false + } +} \ No newline at end of file From ae9f46621c87649e99c1f9035b54e68b6f0e76e8 Mon Sep 17 00:00:00 2001 From: toanngo Date: Thu, 10 Feb 2022 20:40:54 -0800 Subject: [PATCH 14/25] add test case for invalid square size --- x/payment/client/testutil/integration_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/x/payment/client/testutil/integration_test.go b/x/payment/client/testutil/integration_test.go index 552bbc3e29..3bd6056b97 100644 --- a/x/payment/client/testutil/integration_test.go +++ b/x/payment/client/testutil/integration_test.go @@ -96,6 +96,19 @@ func (s *IntegrationTestSuite) TestSubmitWirePayForMessage() { }, 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", cflags.FlagSquareSizes, "256,123,64"), + }, + true, 0, &sdk.TxResponse{}, + }, } for _, tc := range testCases { From b08447ba70d2c2f3b69746c9fa2f0204a66ec873 Mon Sep 17 00:00:00 2001 From: toanngo Date: Thu, 10 Feb 2022 20:50:56 -0800 Subject: [PATCH 15/25] update description flags --- flags/flags.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flags/flags.go b/flags/flags.go index 56e6f8e9d0..2f017f84b4 100644 --- a/flags/flags.go +++ b/flags/flags.go @@ -13,5 +13,5 @@ const FlagSquareSizes = "square-sizes" // AddTxFlagsToCmd adds common flags to a module tx command. func AddTxFlagsToCmd(cmd *cobra.Command) { flags.AddTxFlagsToCmd(cmd) - cmd.Flags().UintSlice(FlagSquareSizes, []uint{consts.MaxSquareSize, 128, 64}, "Specify the square size") + cmd.Flags().UintSlice(FlagSquareSizes, []uint{consts.MaxSquareSize, 128, 64}, "Specify the square sizes, must be power of 2") } \ No newline at end of file From 08ea1979f9e00eda46eb73db9e5a056b59f13888 Mon Sep 17 00:00:00 2001 From: toanngo Date: Fri, 11 Feb 2022 18:19:34 -0800 Subject: [PATCH 16/25] move and update powerof2 func --- x/payment/types/payformessage.go | 9 +++++++++ x/payment/types/wirepayformessage.go | 10 ---------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/x/payment/types/payformessage.go b/x/payment/types/payformessage.go index 10b298fd22..a12dcb51e6 100644 --- a/x/payment/types/payformessage.go +++ b/x/payment/types/payformessage.go @@ -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 { + return true + } else { + return false + } +} diff --git a/x/payment/types/wirepayformessage.go b/x/payment/types/wirepayformessage.go index 2ea77dc357..8d4ae491bf 100644 --- a/x/payment/types/wirepayformessage.go +++ b/x/payment/types/wirepayformessage.go @@ -212,14 +212,4 @@ func ProcessWirePayForMessage(msg *MsgWirePayForMessage, squareSize uint64) (*tm } return &coreMsg, pfm, shareCommit.Signature, nil -} - - -// Check if number is power of 2 -func powerOf2(size uint64) bool { - if size & (size-1) == 0 { - return true - } else { - return false - } } \ No newline at end of file From d3cca6fab979f42f8ccd5e2c7d89fc0098291941 Mon Sep 17 00:00:00 2001 From: toanngo Date: Fri, 11 Feb 2022 18:20:19 -0800 Subject: [PATCH 17/25] update powerOf2 func --- x/payment/types/payformessage.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/payment/types/payformessage.go b/x/payment/types/payformessage.go index a12dcb51e6..10e8a2a868 100644 --- a/x/payment/types/payformessage.go +++ b/x/payment/types/payformessage.go @@ -229,7 +229,7 @@ func nextPowerOf2(v uint64) uint64 { // Check if number is power of 2 func powerOf2(v uint64) bool { - if v & (v-1) == 0 { + if v & (v-1) == 0 && v != 0 { return true } else { return false From 5498a551089919065e4165797bbb937768de9def Mon Sep 17 00:00:00 2001 From: toanngo Date: Fri, 11 Feb 2022 18:20:38 -0800 Subject: [PATCH 18/25] add test to powerOf2 --- x/payment/types/payformessage_test.go | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/x/payment/types/payformessage_test.go b/x/payment/types/payformessage_test.go index e071c7bd8e..a7b4cbc3a5 100644 --- a/x/payment/types/payformessage_test.go +++ b/x/payment/types/payformessage_test.go @@ -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) { From c94130c272ac0e4bfa0b1d2431a1e8217c2b1a40 Mon Sep 17 00:00:00 2001 From: toanngo Date: Sun, 13 Feb 2022 09:33:51 -0800 Subject: [PATCH 19/25] delete gap --- x/payment/client/cli/wirepayformessage.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/x/payment/client/cli/wirepayformessage.go b/x/payment/client/cli/wirepayformessage.go index 228fcf55aa..5d2beaac98 100644 --- a/x/payment/client/cli/wirepayformessage.go +++ b/x/payment/client/cli/wirepayformessage.go @@ -57,10 +57,8 @@ func CmdWirePayForMessage() *cobra.Command { if err != nil { return err } - squareSizes64 := parseSquareSizes(squareSizes) pfmMsg, err := types.NewWirePayForMessage(namespace, message, squareSizes64...) - if err != nil { return err } From 33c7791e612ba46727a35d5765355c8dda4e82da Mon Sep 17 00:00:00 2001 From: toanngo Date: Sun, 13 Feb 2022 09:35:56 -0800 Subject: [PATCH 20/25] move check power of 2 first --- x/payment/types/wirepayformessage.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/x/payment/types/wirepayformessage.go b/x/payment/types/wirepayformessage.go index 8d4ae491bf..ff1eac534b 100644 --- a/x/payment/types/wirepayformessage.go +++ b/x/payment/types/wirepayformessage.go @@ -104,15 +104,15 @@ 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 } - if !powerOf2(commit.K) { - return fmt.Errorf("invalid square size, the size must be power of 2: %d", commit.K) - } - if string(calculatedCommit) != string(commit.ShareCommitment) { return fmt.Errorf("invalid commit for square size %d", commit.K) } From 2d580818a8d3c0d98789028906abde9f2b2af3e6 Mon Sep 17 00:00:00 2001 From: toanngo Date: Sun, 13 Feb 2022 09:53:20 -0800 Subject: [PATCH 21/25] move flag to wirepayformessage --- flags/flags.go | 17 ----------------- x/payment/client/cli/wirepayformessage.go | 7 +++++-- 2 files changed, 5 insertions(+), 19 deletions(-) delete mode 100644 flags/flags.go diff --git a/flags/flags.go b/flags/flags.go deleted file mode 100644 index 2f017f84b4..0000000000 --- a/flags/flags.go +++ /dev/null @@ -1,17 +0,0 @@ -package flags - -import ( - "github.com/cosmos/cosmos-sdk/client/flags" - - "github.com/spf13/cobra" - "github.com/tendermint/tendermint/pkg/consts" -) - -// List of CLI flags -const FlagSquareSizes = "square-sizes" - -// AddTxFlagsToCmd adds common flags to a module tx command. -func AddTxFlagsToCmd(cmd *cobra.Command) { - flags.AddTxFlagsToCmd(cmd) - cmd.Flags().UintSlice(FlagSquareSizes, []uint{consts.MaxSquareSize, 128, 64}, "Specify the square sizes, must be power of 2") -} \ No newline at end of file diff --git a/x/payment/client/cli/wirepayformessage.go b/x/payment/client/cli/wirepayformessage.go index 5d2beaac98..b3867a3324 100644 --- a/x/payment/client/cli/wirepayformessage.go +++ b/x/payment/client/cli/wirepayformessage.go @@ -6,7 +6,7 @@ import ( "fmt" "github.com/spf13/cobra" - //"github.com/tendermint/tendermint/pkg/consts" + "github.com/tendermint/tendermint/pkg/consts" "github.com/celestiaorg/celestia-app/x/payment/types" "github.com/cosmos/cosmos-sdk/client" @@ -16,6 +16,8 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +const FlagSquareSizes = "square-sizes" + func CmdWirePayForMessage() *cobra.Command { cmd := &cobra.Command{ Use: "payForMessage [hexNamespace] [hexMessage]", @@ -107,7 +109,8 @@ func CmdWirePayForMessage() *cobra.Command { }, } - cflags.AddTxFlagsToCmd(cmd) + flags.AddTxFlagsToCmd(cmd) + cmd.Flags().UintSlice(FlagSquareSizes, []uint{consts.MaxSquareSize, 128, 64}, "Specify the square sizes, must be power of 2") return cmd } From 27928a7b87ba207274e01f4ee558513dd1ec88b1 Mon Sep 17 00:00:00 2001 From: toanngo Date: Sun, 13 Feb 2022 10:03:13 -0800 Subject: [PATCH 22/25] move const FlagSquareSizes to wirepayformessage --- x/payment/client/cli/wirepayformessage.go | 3 +-- x/payment/client/testutil/integration_test.go | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/x/payment/client/cli/wirepayformessage.go b/x/payment/client/cli/wirepayformessage.go index b3867a3324..5257270473 100644 --- a/x/payment/client/cli/wirepayformessage.go +++ b/x/payment/client/cli/wirepayformessage.go @@ -11,7 +11,6 @@ import ( "github.com/celestiaorg/celestia-app/x/payment/types" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" - cflags "github.com/celestiaorg/celestia-app/flags" "github.com/cosmos/cosmos-sdk/client/tx" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -55,7 +54,7 @@ func CmdWirePayForMessage() *cobra.Command { } // create the MsgPayForMessage - squareSizes, err := cmd.Flags().GetUintSlice(cflags.FlagSquareSizes) + squareSizes, err := cmd.Flags().GetUintSlice(FlagSquareSizes) if err != nil { return err } diff --git a/x/payment/client/testutil/integration_test.go b/x/payment/client/testutil/integration_test.go index 3bd6056b97..93d274ca3a 100644 --- a/x/payment/client/testutil/integration_test.go +++ b/x/payment/client/testutil/integration_test.go @@ -6,7 +6,6 @@ import ( "time" "github.com/cosmos/cosmos-sdk/client/flags" - cflags "github.com/celestiaorg/celestia-app/flags" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/suite" @@ -92,7 +91,7 @@ func (s *IntegrationTestSuite) TestSubmitWirePayForMessage() { 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", cflags.FlagSquareSizes, "256,128,64"), + fmt.Sprintf("--%s=%s", paycli.FlagSquareSizes, "256,128,64"), }, false, 0, &sdk.TxResponse{}, }, @@ -105,7 +104,7 @@ func (s *IntegrationTestSuite) TestSubmitWirePayForMessage() { 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", cflags.FlagSquareSizes, "256,123,64"), + fmt.Sprintf("--%s=%s", paycli.FlagSquareSizes, "256,123,64"), }, true, 0, &sdk.TxResponse{}, }, From 6da8dc918cb6473000f637c8d782c0ddaeef0d75 Mon Sep 17 00:00:00 2001 From: toanngo Date: Sun, 13 Feb 2022 11:29:19 -0800 Subject: [PATCH 23/25] add test for invalid square size in pfm ValidateBasic --- x/payment/types/payformessage_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/x/payment/types/payformessage_test.go b/x/payment/types/payformessage_test.go index a7b4cbc3a5..94095da67b 100644 --- a/x/payment/types/payformessage_test.go +++ b/x/payment/types/payformessage_test.go @@ -288,6 +288,10 @@ func TestWirePayForMessage_ValidateBasic(t *testing.T) { badCommitMsg := validWirePayForMessage(t) badCommitMsg.MessageShareCommitment[0].ShareCommitment = []byte{1, 2, 3, 4} + // pfm that has a wrong square size + badSquareSizeMsg := validWirePayForMessage(t) + badSquareSizeMsg.MessageShareCommitment[0].K = 15 + tests := []test{ { name: "valid msg", @@ -323,6 +327,12 @@ func TestWirePayForMessage_ValidateBasic(t *testing.T) { expectErr: true, errStr: "invalid commit for square size", }, + { + name: "bad square size", + msg: badSquareSizeMsg, + expectErr: true, + errStr: fmt.Sprintf("invalid square size, the size must be power of 2: %d", badSquareSizeMsg.MessageShareCommitment[0].K), + }, } for _, tt := range tests { From 28bb0c457bad6cd51555cc3306eafc23cdd65cb4 Mon Sep 17 00:00:00 2001 From: toanngo Date: Sun, 13 Feb 2022 11:41:11 -0800 Subject: [PATCH 24/25] add 1 more test where change square size to another power of 2 number --- x/payment/types/payformessage_test.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/x/payment/types/payformessage_test.go b/x/payment/types/payformessage_test.go index 94095da67b..d009bfe770 100644 --- a/x/payment/types/payformessage_test.go +++ b/x/payment/types/payformessage_test.go @@ -288,9 +288,13 @@ func TestWirePayForMessage_ValidateBasic(t *testing.T) { badCommitMsg := validWirePayForMessage(t) badCommitMsg.MessageShareCommitment[0].ShareCommitment = []byte{1, 2, 3, 4} - // pfm that has a wrong square size + // 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 = 15 + badSquareSizeMsg.MessageShareCommitment[0].K = 128 tests := []test{ { @@ -328,10 +332,16 @@ func TestWirePayForMessage_ValidateBasic(t *testing.T) { errStr: "invalid commit for square size", }, { - name: "bad 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 square size, the size must be power of 2: %d", badSquareSizeMsg.MessageShareCommitment[0].K), + errStr: fmt.Sprintf("invalid commit for square size %d", badSquareSizeMsg.MessageShareCommitment[0].K), }, } From 80b52ab8c3d3117a4f8eb656f6db5ee152ac7374 Mon Sep 17 00:00:00 2001 From: toanngo Date: Sun, 13 Feb 2022 20:01:32 -0800 Subject: [PATCH 25/25] change squaresize to 4 to pass the test --- x/payment/types/payformessage_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/payment/types/payformessage_test.go b/x/payment/types/payformessage_test.go index d009bfe770..24dd9f49bb 100644 --- a/x/payment/types/payformessage_test.go +++ b/x/payment/types/payformessage_test.go @@ -294,7 +294,7 @@ func TestWirePayForMessage_ValidateBasic(t *testing.T) { // pfm that has a different power of 2 square size badSquareSizeMsg := validWirePayForMessage(t) - badSquareSizeMsg.MessageShareCommitment[0].K = 128 + badSquareSizeMsg.MessageShareCommitment[0].K = 4 tests := []test{ { @@ -428,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 {