Skip to content

Commit e6a940b

Browse files
authored
fix(state/core_accessor): fix gas estimation logic (#4608)
1 parent 1a47ce5 commit e6a940b

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

state/core_access.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,6 @@ func (ca *CoreAccessor) SubmitPayForBlob(
184184
feeGrant = user.SetFeeGranter(granter)
185185
}
186186

187-
gas := cfg.GasLimit()
188-
if gas == 0 {
189-
blobSizes := make([]uint32, len(libBlobs))
190-
for i, blob := range libBlobs {
191-
blobSizes[i] = uint32(len(blob.Data()))
192-
}
193-
gas = ca.estimateGasForBlobs(blobSizes, uint32(libBlobs[0].ShareVersion()))
194-
}
195-
196187
// get tx signer account name
197188
author, err := ca.getTxAuthorAccAddress(cfg)
198189
if err != nil {
@@ -203,6 +194,14 @@ func (ca *CoreAccessor) SubmitPayForBlob(
203194
return nil, fmt.Errorf("account for signer %s not found", author)
204195
}
205196

197+
gas := cfg.GasLimit()
198+
if gas == 0 {
199+
gas, err = ca.estimateGasForBlobs(author.String(), libBlobs)
200+
if err != nil {
201+
return nil, err
202+
}
203+
}
204+
206205
gasPrice, err := ca.estimateGasPrice(ctx, cfg)
207206
if err != nil {
208207
return nil, err

state/core_access_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ func TestSubmitPayForBlob(t *testing.T) {
5656
},
5757
{
5858
name: "good blob with user provided gas and fees",
59-
blobs: []*libshare.Blob{blobbyTheBlob},
59+
blobs: []*libshare.Blob{blobbyTheBlob, blobbyTheBlob},
6060
gasPrice: 0.005,
6161
gasLim: apptypes.DefaultEstimateGas(&apptypes.MsgPayForBlobs{
62-
BlobSizes: []uint32{uint32(blobbyTheBlob.DataLen())},
63-
ShareVersions: []uint32{uint32(blobbyTheBlob.ShareVersion())},
62+
BlobSizes: []uint32{uint32(blobbyTheBlob.DataLen()), uint32(blobbyTheBlob.DataLen())},
63+
ShareVersions: []uint32{uint32(blobbyTheBlob.ShareVersion()), uint32(blobbyTheBlob.ShareVersion())},
6464
}),
6565
expErr: nil,
6666
},

state/estimate.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package state
33
import (
44
"context"
55
"errors"
6+
"fmt"
67

78
sdktypes "github.com/cosmos/cosmos-sdk/types"
89

910
apptypes "github.com/celestiaorg/celestia-app/v6/x/blob/types"
11+
libshare "github.com/celestiaorg/go-square/v3/share"
1012
)
1113

1214
var ErrGasPriceExceedsLimit = errors.New("state: estimated gas price exceeds max gas price in tx config")
@@ -66,9 +68,10 @@ func (ca *CoreAccessor) estimateGasPriceAndUsage(
6668
}
6769

6870
// estimateGasForBlobs returns a gas limit that can be applied to the `MsgPayForBlob` transactions.
69-
func (ca *CoreAccessor) estimateGasForBlobs(blobSizes []uint32, shareVersion uint32) uint64 {
70-
return apptypes.DefaultEstimateGas(&apptypes.MsgPayForBlobs{
71-
BlobSizes: blobSizes,
72-
ShareVersions: []uint32{shareVersion},
73-
})
71+
func (ca *CoreAccessor) estimateGasForBlobs(signer string, blobs []*libshare.Blob) (uint64, error) {
72+
msg, err := apptypes.NewMsgPayForBlobs(signer, 0, blobs...)
73+
if err != nil {
74+
return 0, fmt.Errorf("failed to create msg pay-for-blobs: %w", err)
75+
}
76+
return apptypes.DefaultEstimateGas(msg), nil
7477
}

0 commit comments

Comments
 (0)