diff --git a/celestia/celestia.go b/celestia/celestia.go index 0299017..9d7ac5c 100644 --- a/celestia/celestia.go +++ b/celestia/celestia.go @@ -4,6 +4,7 @@ import ( "context" "encoding/binary" "log" + "math" "strings" "github.com/celestiaorg/celestia-app/pkg/appconsts" @@ -12,6 +13,8 @@ import ( "github.com/celestiaorg/celestia-node/blob" "github.com/celestiaorg/celestia-node/share" "github.com/celestiaorg/nmt" + sdktypes "github.com/cosmos/cosmos-sdk/types" + auth "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/rollkit/go-da" ) @@ -74,12 +77,21 @@ func (c *CelestiaDA) Commit(daBlobs []da.Blob) ([]da.Commitment, error) { } // Submit submits the Blobs to Data Availability layer. -func (c *CelestiaDA) Submit(daBlobs []da.Blob) ([]da.ID, []da.Proof, error) { +func (c *CelestiaDA) Submit(daBlobs []da.Blob, gasPrice float64) ([]da.ID, []da.Proof, error) { blobs, commitments, err := c.blobsAndCommitments(daBlobs) if err != nil { return nil, nil, err } - height, err := c.client.Blob.Submit(c.ctx, blobs, blob.DefaultSubmitOptions()) + options := blob.DefaultSubmitOptions() + if gasPrice >= 0 { + blobSizes := make([]uint32, len(blobs)) + for i, blob := range blobs { + blobSizes[i] = uint32(len(blob.Data)) + } + options.GasLimit = types.EstimateGas(blobSizes, appconsts.DefaultGasPerBlobByte, auth.DefaultTxSizeCostPerByte) + options.Fee = sdktypes.NewInt(int64(math.Ceil(gasPrice * float64(options.GasLimit)))).Int64() + } + height, err := c.client.Blob.Submit(c.ctx, blobs, options) if err != nil { return nil, nil, err } diff --git a/celestia/celestia_test.go b/celestia/celestia_test.go index 013a74c..ac4a26f 100644 --- a/celestia/celestia_test.go +++ b/celestia/celestia_test.go @@ -79,7 +79,7 @@ func TestCelestiaDA(t *testing.T) { }) t.Run("Submit_empty", func(t *testing.T) { - blobs, proofs, err := m.Submit(nil) + blobs, proofs, err := m.Submit(nil, -1) assert.NoError(t, err) assert.Equal(t, 0, len(blobs)) assert.Equal(t, 0, len(proofs)) @@ -118,7 +118,14 @@ func TestCelestiaDA(t *testing.T) { }) t.Run("Submit_existing", func(t *testing.T) { - blobs, proofs, err := m.Submit([]Blob{[]byte{0x00, 0x01, 0x02}}) + blobs, proofs, err := m.Submit([]Blob{[]byte{0x00, 0x01, 0x02}}, -1) + assert.NoError(t, err) + assert.Equal(t, 1, len(blobs)) + assert.Equal(t, 1, len(proofs)) + }) + + t.Run("Submit_existing_with_gasprice", func(t *testing.T) { + blobs, proofs, err := m.Submit([]Blob{[]byte{0x00, 0x01, 0x02}}, 0.5) assert.NoError(t, err) assert.Equal(t, 1, len(blobs)) assert.Equal(t, 1, len(proofs)) diff --git a/celestia/mock.go b/celestia/mock.go index 7c72220..81ef106 100644 --- a/celestia/mock.go +++ b/celestia/mock.go @@ -17,7 +17,7 @@ type MockBlobAPI struct { } // Submit mocks the blob.Submit method -func (m *MockBlobAPI) Submit(context.Context, []*blob.Blob, *blob.SubmitOptions) (uint64, error) { +func (m *MockBlobAPI) Submit(ctx context.Context, blobs []*blob.Blob, options *blob.SubmitOptions) (uint64, error) { m.height += 1 return m.height, nil } diff --git a/go.mod b/go.mod index 9ac1c1d..b2680e9 100644 --- a/go.mod +++ b/go.mod @@ -8,12 +8,13 @@ require ( github.com/celestiaorg/celestia-app v1.6.0 github.com/celestiaorg/celestia-node v0.12.2 github.com/celestiaorg/nmt v0.20.0 + github.com/cosmos/cosmos-sdk v0.46.14 github.com/cristalhq/jwt v1.2.0 github.com/filecoin-project/go-jsonrpc v0.3.1 github.com/ipfs/go-log/v2 v2.5.1 github.com/mitchellh/go-homedir v1.1.0 github.com/ory/dockertest/v3 v3.10.0 - github.com/rollkit/go-da v0.0.0-20231225164956-a3533025ce47 + github.com/rollkit/go-da v0.1.0 github.com/spf13/cobra v1.8.0 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.4 @@ -69,7 +70,6 @@ require ( github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/cosmos/btcutil v1.0.5 // indirect github.com/cosmos/cosmos-proto v1.0.0-alpha8 // indirect - github.com/cosmos/cosmos-sdk v0.46.14 // indirect github.com/cosmos/cosmos-sdk/api v0.1.0 // indirect github.com/cosmos/go-bip39 v1.0.0 // indirect github.com/cosmos/gogoproto v1.4.11 // indirect diff --git a/go.sum b/go.sum index 02d43a9..6e80867 100644 --- a/go.sum +++ b/go.sum @@ -2102,8 +2102,8 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= -github.com/rollkit/go-da v0.0.0-20231225164956-a3533025ce47 h1:aprTK4O2aqZ/Mb1pqrxH6EdKs331oJEyf5OROpN1o1Q= -github.com/rollkit/go-da v0.0.0-20231225164956-a3533025ce47/go.mod h1:cy1LA9kCyCJHgszKkTh9hJn816l5Oa87GMA2c1imfqA= +github.com/rollkit/go-da v0.1.0 h1:FAEMTNF8mTsPuiUgYt2dQSMzw8iYPjiWq7692CS2mbY= +github.com/rollkit/go-da v0.1.0/go.mod h1:Kef0XI5ecEKd3TXzI8S+9knAUJnZg0svh2DuXoCsPlM= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/rs/cors v1.9.0 h1:l9HGsTsHJcvW14Nk7J9KFz8bzeAWXn3CG6bgt7LsrAE= github.com/rs/cors v1.9.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= diff --git a/specs/src/celestia-da.md b/specs/src/celestia-da.md index d954328..f14f066 100644 --- a/specs/src/celestia-da.md +++ b/specs/src/celestia-da.md @@ -48,10 +48,14 @@ The implementation calls `blob.CreateCommitments` which does not call any RPC me Submit submits blobs and returns their ids and proofs. -The implementation calls [blob.Submit] RPC method with `DefaultSubmitOptions` on the Celestia Node API. +The implementation calls [blob.Submit] RPC method with `DefaultSubmitOptions` on the Celestia Node API if `gasPrice` is greater than or equal to zero. `DefaultSubmitOptions` uses default values for `Fee` and `GasLimit`. +If `gasPrice` is less than zero, then it uses `app types` to `EstimateGas` based on the blob sizes and updates `GasLimit` and `Fee` on the `SubmitOptions` accordingly. + +This way the client increase the `gasPrice` to increase the fee for the transaction or use the default by passing a negative `gasPrice`. + ### Validate Validate validates blob ids and proofs and returns whether they are included.