Skip to content

Commit

Permalink
refactor(blob)!: replace SubmitOptions with a GasPrice parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
distractedm1nd committed Jan 17, 2024
1 parent 36205cc commit 99d8e04
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 58 deletions.
32 changes: 26 additions & 6 deletions blob/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import (
"context"
"errors"
"fmt"
"math"
"sync"

"cosmossdk.io/math"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/types"
auth "github.com/cosmos/cosmos-sdk/x/auth/types"
logging "github.com/ipfs/go-log/v2"

"github.com/celestiaorg/celestia-app/pkg/appconsts"
"github.com/celestiaorg/celestia-app/pkg/shares"
blobtypes "github.com/celestiaorg/celestia-app/x/blob/types"

"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/share"
Expand All @@ -23,11 +27,21 @@ var (
log = logging.Logger("blob")
)

// GasPrice represents the amount to be paid per gas unit. Fee is set by
// multiplying GasPrice by GasLimit, which is determined by the blob sizes.
type GasPrice float64

// DefaultGasPrice returns the default gas price, letting node automatically
// determine the Fee based on the passed blob sizes.
func DefaultGasPrice() GasPrice {
return -1.0
}

// Submitter is an interface that allows submitting blobs to the celestia-core. It is used to
// avoid a circular dependency between the blob and the state package, since the state package needs
// the blob.Blob type for this signature.
type Submitter interface {
SubmitPayForBlob(ctx context.Context, fee math.Int, gasLim uint64, blobs []*Blob) (*types.TxResponse, error)
SubmitPayForBlob(ctx context.Context, fee sdkmath.Int, gasLim uint64, blobs []*Blob) (*types.TxResponse, error)
}

type Service struct {
Expand Down Expand Up @@ -66,15 +80,21 @@ func DefaultSubmitOptions() *SubmitOptions {
}
}

// Submit sends PFB transaction and reports the height in which it was included.
// Submit sends PFB transaction and reports the height at which it was included.
// Allows sending multiple Blobs atomically synchronously.
// Uses default wallet registered on the Node.
// Handles gas estimation and fee calculation.
func (s *Service) Submit(ctx context.Context, blobs []*Blob, options *SubmitOptions) (uint64, error) {
func (s *Service) Submit(ctx context.Context, blobs []*Blob, gasPrice GasPrice) (uint64, error) {
log.Debugw("submitting blobs", "amount", len(blobs))

if options == nil {
options = DefaultSubmitOptions()
options := DefaultSubmitOptions()
if gasPrice > 0 {
blobSizes := make([]uint32, len(blobs))
for i, blob := range blobs {
blobSizes[i] = uint32(len(blob.Data))
}
options.GasLimit = blobtypes.EstimateGas(blobSizes, appconsts.DefaultGasPerBlobByte, auth.DefaultTxSizeCostPerByte)
options.Fee = types.NewInt(int64(math.Ceil(float64(gasPrice) * float64(options.GasLimit)))).Int64()
}

resp, err := s.blobSubmitter.SubmitPayForBlob(ctx, types.NewInt(options.Fee), options.GasLimit, blobs)
Expand Down
8 changes: 4 additions & 4 deletions nodebuilder/blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Module interface {
// Submit sends Blobs and reports the height in which they were included.
// Allows sending multiple Blobs atomically synchronously.
// Uses default wallet registered on the Node.
Submit(_ context.Context, _ []*blob.Blob, _ *blob.SubmitOptions) (height uint64, _ error)
Submit(_ context.Context, _ []*blob.Blob, _ blob.GasPrice) (height uint64, _ error)
// Get retrieves the blob by commitment under the given namespace and height.
Get(_ context.Context, height uint64, _ share.Namespace, _ blob.Commitment) (*blob.Blob, error)
// GetAll returns all blobs at the given height under the given namespaces.
Expand All @@ -30,16 +30,16 @@ type Module interface {

type API struct {
Internal struct {
Submit func(context.Context, []*blob.Blob, *blob.SubmitOptions) (uint64, error) `perm:"write"`
Submit func(context.Context, []*blob.Blob, blob.GasPrice) (uint64, error) `perm:"write"`
Get func(context.Context, uint64, share.Namespace, blob.Commitment) (*blob.Blob, error) `perm:"read"`
GetAll func(context.Context, uint64, []share.Namespace) ([]*blob.Blob, error) `perm:"read"`
GetProof func(context.Context, uint64, share.Namespace, blob.Commitment) (*blob.Proof, error) `perm:"read"`
Included func(context.Context, uint64, share.Namespace, *blob.Proof, blob.Commitment) (bool, error) `perm:"read"`
}
}

func (api *API) Submit(ctx context.Context, blobs []*blob.Blob, options *blob.SubmitOptions) (uint64, error) {
return api.Internal.Submit(ctx, blobs, options)
func (api *API) Submit(ctx context.Context, blobs []*blob.Blob, gasPrice blob.GasPrice) (uint64, error) {
return api.Internal.Submit(ctx, blobs, gasPrice)
}

func (api *API) Get(
Expand Down
27 changes: 8 additions & 19 deletions nodebuilder/blob/cmd/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import (
var (
base64Flag bool

fee int64
gasLimit uint64
gasPrice float64
)

func init() {
Expand All @@ -37,23 +36,13 @@ func init() {
"printed blob's data as a base64 string",
)

submitCmd.PersistentFlags().Int64Var(
&fee,
"fee",
-1,
"specifies fee (in utia) for blob submission.\n"+
submitCmd.PersistentFlags().Float64Var(
&gasPrice,
"gas.price",
float64(blob.DefaultGasPrice()),
"specifies gas price (in utia) for blob submission.\n"+
"Fee will be automatically calculated if negative value is passed [optional]",
)

submitCmd.PersistentFlags().Uint64Var(
&gasLimit,
"gas.limit",
0,
"sets the amount of gas that is consumed during blob submission [optional]",
)

// unset the default value to avoid users confusion
submitCmd.PersistentFlags().Lookup("fee").DefValue = "0"
}

var Cmd = &cobra.Command{
Expand Down Expand Up @@ -135,7 +124,7 @@ var submitCmd = &cobra.Command{
Short: "Submit the blob at the given namespace.\n" +
"Note:\n" +
"* only one blob is allowed to submit through the RPC.\n" +
"* fee and gas.limit params will be calculated automatically if they are not provided as arguments",
"* fee and gas limit params will be calculated automatically based on the gas.price parameter.\n",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := cmdnode.ParseClientFromCtx(cmd.Context())
if err != nil {
Expand All @@ -156,7 +145,7 @@ var submitCmd = &cobra.Command{
height, err := client.Blob.Submit(
cmd.Context(),
[]*blob.Blob{parsedBlob},
&blob.SubmitOptions{Fee: fee, GasLimit: gasLimit},
blob.GasPrice(gasPrice),
)

response := struct {
Expand Down
5 changes: 2 additions & 3 deletions nodebuilder/blob/mocks/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions nodebuilder/das/mocks/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions nodebuilder/fraud/mocks/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions nodebuilder/header/mocks/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions nodebuilder/node/mocks/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions nodebuilder/share/mocks/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions nodebuilder/state/mocks/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion nodebuilder/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestBlobRPC(t *testing.T) {
)
require.NoError(t, err)

height, err := rpcClient.Blob.Submit(ctx, []*blob.Blob{newBlob}, nil)
height, err := rpcClient.Blob.Submit(ctx, []*blob.Blob{newBlob}, blob.DefaultGasPrice())
require.NoError(t, err)
require.True(t, height != 0)
}
Expand Down
6 changes: 3 additions & 3 deletions nodebuilder/tests/blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestBlobModule(t *testing.T) {
fullClient := getAdminClient(ctx, fullNode, t)
lightClient := getAdminClient(ctx, lightNode, t)

height, err := fullClient.Blob.Submit(ctx, blobs, nil)
height, err := fullClient.Blob.Submit(ctx, blobs, blob.DefaultGasPrice())
require.NoError(t, err)

_, err = fullClient.Header.WaitForHeight(ctx, height)
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestBlobModule(t *testing.T) {
)
require.NoError(t, err)

height, err := fullClient.Blob.Submit(ctx, []*blob.Blob{b, b}, nil)
height, err := fullClient.Blob.Submit(ctx, []*blob.Blob{b, b}, blob.DefaultGasPrice())
require.NoError(t, err)

_, err = fullClient.Header.WaitForHeight(ctx, height)
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestBlobModule(t *testing.T) {
// different pfbs.
name: "Submit the same blob in different pfb",
doFn: func(t *testing.T) {
h, err := fullClient.Blob.Submit(ctx, []*blob.Blob{blobs[0]}, nil)
h, err := fullClient.Blob.Submit(ctx, []*blob.Blob{blobs[0]}, blob.DefaultGasPrice())
require.NoError(t, err)

_, err = fullClient.Header.WaitForHeight(ctx, h)
Expand Down
3 changes: 1 addition & 2 deletions share/availability/mocks/availability.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions share/mocks/getter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 99d8e04

Please sign in to comment.