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

x/payment/pfd.go: Add canonical PFD constructor/signer/submittor methods #352

Merged
merged 2 commits into from
Apr 25, 2022
Merged
Changes from 1 commit
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
97 changes: 97 additions & 0 deletions x/payment/pfd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package payment

import (
"context"
"google.golang.org/grpc"

sdk "github.com/cosmos/cosmos-sdk/types"
sdk_tx "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/x/auth/signing"

"github.com/celestiaorg/celestia-app/x/payment/types"
"github.com/celestiaorg/nmt/namespace"
)

var (
// shareSizes includes all the possible share sizes of the given data
// that the signer must sign over.
shareSizes = []uint64{16, 32, 64, 128}
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would prefer not to hardcode these, but I also think its ok to merge as is, considering that we have #239 and #236


// SubmitPayForData constructs, signs and synchronously submits a PayForData
// transaction, returning a sdk.TxResponse upon submission.
func SubmitPayForData(
ctx context.Context,
signer *types.KeyringSigner,
conn *grpc.ClientConn,
nID namespace.ID,
data []byte,
gasLim uint64,
) (*sdk.TxResponse, error) {
pfd, err := BuildPayForData(ctx, signer, conn, nID, data, gasLim)
if err != nil {
return nil, err
}

signed, err := SignPayForData(signer, pfd, types.SetGasLimit(gasLim))
if err != nil {
return nil, err
}

rawTx, err := signer.EncodeTx(signed)
if err != nil {
return nil, err
}

txResp, err := types.BroadcastTx(ctx, conn, sdk_tx.BroadcastMode_BROADCAST_MODE_BLOCK, rawTx)
if err != nil {
return nil, err
}
return txResp.TxResponse, nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finally, the prophesied good api for submitting a payForData has come 🙌


// BuildPayForData constructs a PayForData transaction.
func BuildPayForData(
ctx context.Context,
signer *types.KeyringSigner,
conn *grpc.ClientConn,
nID namespace.ID,
message []byte,
gasLim uint64,
) (*types.MsgWirePayForData, error) {
// create the raw WirePayForMessage transaction
wpfd, err := types.NewWirePayForData(nID, message, shareSizes...)
if err != nil {
return nil, err
}

// query for account information necessary to sign a valid tx
err = signer.QueryAccountNumber(ctx, conn)
if err != nil {
return nil, err
}

// generate the signatures for each `MsgPayForMessage` using the `KeyringSigner`,
// then set the gas limit for the tx
gasLimOption := types.SetGasLimit(gasLim)
err = wpfd.SignShareCommitments(signer, gasLimOption)
if err != nil {
return nil, err
}

return wpfd, nil
}

// SignPayForData signs a PayForData transaction.
func SignPayForData(
signer *types.KeyringSigner,
pfd *types.MsgWirePayForData,
gasLimOption types.TxBuilderOption,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of doing only a gasLim option, do you think it makes sense to do ...types.TxBuilderOption?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evan-forbes does the gas lim get set by default if no TxBuilderOption is included for it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not 100% sure, but I don't think so

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay can you check this commit to see how i've implemented it: 931fbd5

) (signing.Tx, error) {
// Build and sign the final `WirePayForMessage` tx that now contains the signatures
// for potential `MsgPayForMessage`s
return signer.BuildSignedTx(
gasLimOption(signer.NewTxBuilder()),
pfd,
)
}