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 5 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
11 changes: 2 additions & 9 deletions app/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -18,7 +17,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
Expand Down Expand Up @@ -58,8 +56,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)
evan-forbes marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
continue
}
Expand Down Expand Up @@ -116,9 +115,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
}
evan-forbes marked this conversation as resolved.
Show resolved Hide resolved
177 changes: 177 additions & 0 deletions flags/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package flags

import (
"fmt"
"strconv"

"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"
evan-forbes marked this conversation as resolved.
Show resolved Hide resolved

// 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", "<host>:<port> 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", "<host>:<port> 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, 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
}
}
11 changes: 8 additions & 3 deletions x/payment/client/cli/wirepayformessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
evan-forbes marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
pfmMsg, err := types.NewWirePayForMessage(namespace, message, squareSize)

evan-forbes marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
Expand Down
16 changes: 15 additions & 1 deletion x/payment/client/testutil/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down