From 915b3a1e8be383a3ade355dbb6c8bf7a00c649d6 Mon Sep 17 00:00:00 2001 From: jcstein <46639943+jcstein@users.noreply.github.com> Date: Mon, 24 Mar 2025 13:37:54 -0400 Subject: [PATCH 1/5] Apply gas price and gas limit customization for txsim --- test/cmd/txsim/cli.go | 12 ++++++++++++ test/txsim/account.go | 37 +++++++++++++++++++++++++++++++++---- test/txsim/run.go | 22 ++++++++++++++++++++++ 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/test/cmd/txsim/cli.go b/test/cmd/txsim/cli.go index 06ee0c6d13..d500478da5 100644 --- a/test/cmd/txsim/cli.go +++ b/test/cmd/txsim/cli.go @@ -42,6 +42,8 @@ var ( useFeegrant, suppressLogs bool upgradeSchedule string blobShareVersion int + gasLimit uint64 + gasPrice float64 ) func main() { @@ -177,6 +179,14 @@ account that can act as the master account. The command runs until all sequences opts.SuppressLogs() } + if gasLimit > 0 { + opts.WithGasLimit(gasLimit) + } + + if gasPrice > 0 { + opts.WithGasPrice(gasPrice) + } + encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) err = txsim.Run( cmd.Context(), @@ -217,6 +227,8 @@ func flags() *flag.FlagSet { flags.BoolVar(&useFeegrant, "feegrant", false, "use the feegrant module to pay for fees") flags.BoolVar(&suppressLogs, "suppressLogs", false, "disable logging") flags.IntVar(&blobShareVersion, "blob-share-version", -1, "optionally specify a share version to use for the blob sequences") + flags.Uint64Var(&gasLimit, "gas-limit", 0, "custom gas limit to use for transactions (0 = auto-estimate)") + flags.Float64Var(&gasPrice, "gas-price", 0, "custom gas price to use for transactions (0 = use default)") return flags } diff --git a/test/txsim/account.go b/test/txsim/account.go index 9796f60b04..722921b10d 100644 --- a/test/txsim/account.go +++ b/test/txsim/account.go @@ -32,6 +32,8 @@ type AccountManager struct { encCfg encoding.Config pollTime time.Duration useFeegrant bool + gasLimit uint64 + gasPrice float64 // to protect from concurrent writes to the map mtx sync.Mutex @@ -225,11 +227,29 @@ func (am *AccountManager) Submit(ctx context.Context, op Operation) error { opts := make([]user.TxOption, 0) if op.GasLimit == 0 { - opts = append(opts, user.SetGasLimit(DefaultGasLimit), user.SetFee(defaultFee)) + if am.gasLimit > 0 { + // Use the custom gas limit set on the AccountManager + opts = append(opts, user.SetGasLimit(am.gasLimit)) + + // Calculate fee based on gas price + var gasPrice float64 + if am.gasPrice > 0 { + gasPrice = am.gasPrice + } else { + gasPrice = appconsts.DefaultMinGasPrice + } + opts = append(opts, user.SetFee(uint64(math.Ceil(float64(am.gasLimit)*gasPrice)))) + } else { + // Use the default gas limit + opts = append(opts, user.SetGasLimit(DefaultGasLimit), user.SetFee(defaultFee)) + } } else { opts = append(opts, user.SetGasLimit(op.GasLimit)) if op.GasPrice > 0 { opts = append(opts, user.SetFee(uint64(math.Ceil(float64(op.GasLimit)*op.GasPrice)))) + } else if am.gasPrice > 0 { + // Use custom gas price set on AccountManager if available + opts = append(opts, user.SetFee(uint64(math.Ceil(float64(op.GasLimit)*am.gasPrice)))) } else { opts = append(opts, user.SetFee(uint64(math.Ceil(float64(op.GasLimit)*appconsts.DefaultMinGasPrice)))) } @@ -409,9 +429,18 @@ func (am *AccountManager) updateHeight(ctx context.Context) (uint64, error) { } func (am *AccountManager) nextAccountName() string { - am.mtx.Lock() - defer am.mtx.Unlock() - return accountName(len(am.pending) + am.accountIndex) + am.accountIndex++ + return accountName(am.accountIndex) +} + +// SetGasLimit sets a custom gas limit to be used for all transactions +func (am *AccountManager) SetGasLimit(gasLimit uint64) { + am.gasLimit = gasLimit +} + +// SetGasPrice sets a custom gas price to be used for all transactions +func (am *AccountManager) SetGasPrice(gasPrice float64) { + am.gasPrice = gasPrice } type account struct { diff --git a/test/txsim/run.go b/test/txsim/run.go index 65be8a9fd8..3d50ebeb59 100644 --- a/test/txsim/run.go +++ b/test/txsim/run.go @@ -66,6 +66,16 @@ func Run( return err } + // Set custom gas limit if provided + if opts.gasLimit > 0 { + manager.SetGasLimit(opts.gasLimit) + } + + // Set custom gas price if provided + if opts.gasPrice > 0 { + manager.SetGasPrice(opts.gasPrice) + } + // Initialize each of the sequences by allowing them to allocate accounts. for _, sequence := range sequences { sequence.Init(ctx, manager.conn, manager.AllocateAccounts, r, opts.useFeeGrant) @@ -132,6 +142,8 @@ type Options struct { pollTime time.Duration useFeeGrant bool suppressLogger bool + gasLimit uint64 + gasPrice float64 } func (o *Options) Fill() { @@ -174,6 +186,16 @@ func (o *Options) WithPollTime(pollTime time.Duration) *Options { return o } +func (o *Options) WithGasLimit(gasLimit uint64) *Options { + o.gasLimit = gasLimit + return o +} + +func (o *Options) WithGasPrice(gasPrice float64) *Options { + o.gasPrice = gasPrice + return o +} + // buildGrpcConn applies the config if the handshake succeeds; otherwise, it falls back to an insecure connection. func buildGrpcConn(grpcEndpoint string, config *tls.Config) (*grpc.ClientConn, error) { netConn, err := net.Dial("tcp", grpcEndpoint) From 36d2c38486f3494a6426ea38fb5a481f7a454153 Mon Sep 17 00:00:00 2001 From: jcstein <46639943+jcstein@users.noreply.github.com> Date: Mon, 24 Mar 2025 17:44:58 -0400 Subject: [PATCH 2/5] refactor(txsim): improve fee calculation structure and fix linter issues --- test/txsim/account.go | 60 +++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/test/txsim/account.go b/test/txsim/account.go index 722921b10d..991cb8b40e 100644 --- a/test/txsim/account.go +++ b/test/txsim/account.go @@ -226,33 +226,43 @@ func (am *AccountManager) Submit(ctx context.Context, op Operation) error { } opts := make([]user.TxOption, 0) - if op.GasLimit == 0 { - if am.gasLimit > 0 { - // Use the custom gas limit set on the AccountManager - opts = append(opts, user.SetGasLimit(am.gasLimit)) - - // Calculate fee based on gas price - var gasPrice float64 - if am.gasPrice > 0 { - gasPrice = am.gasPrice - } else { - gasPrice = appconsts.DefaultMinGasPrice + var gasLimit, fee uint64 + var gasPrice float64 + + // Determine gas limit + switch { + case op.GasLimit > 0: + gasLimit = op.GasLimit + case am.gasLimit > 0: + gasLimit = am.gasLimit + default: + gasLimit = DefaultGasLimit + fee = defaultFee + } + + // Set gas limit + if gasLimit > 0 { + opts = append(opts, user.SetGasLimit(gasLimit)) + } + + // Determine gas price and calculate fee if not already set + if fee == 0 { + switch { + case op.GasPrice > 0: + gasPrice = op.GasPrice + case am.gasPrice > 0: + gasPrice = am.gasPrice + if gasPrice < appconsts.DefaultMinGasPrice { + log.Warn(). + Float64("gas_price", gasPrice). + Float64("default_min_gas_price", appconsts.DefaultMinGasPrice). + Msg("custom gas price is lower than default minimum") } - opts = append(opts, user.SetFee(uint64(math.Ceil(float64(am.gasLimit)*gasPrice)))) - } else { - // Use the default gas limit - opts = append(opts, user.SetGasLimit(DefaultGasLimit), user.SetFee(defaultFee)) - } - } else { - opts = append(opts, user.SetGasLimit(op.GasLimit)) - if op.GasPrice > 0 { - opts = append(opts, user.SetFee(uint64(math.Ceil(float64(op.GasLimit)*op.GasPrice)))) - } else if am.gasPrice > 0 { - // Use custom gas price set on AccountManager if available - opts = append(opts, user.SetFee(uint64(math.Ceil(float64(op.GasLimit)*am.gasPrice)))) - } else { - opts = append(opts, user.SetFee(uint64(math.Ceil(float64(op.GasLimit)*appconsts.DefaultMinGasPrice)))) + default: + gasPrice = appconsts.DefaultMinGasPrice } + fee = uint64(math.Ceil(float64(gasLimit) * gasPrice)) + opts = append(opts, user.SetFee(fee)) } if am.useFeegrant { From a71af690d82df86406738487af31a3b538ea0882 Mon Sep 17 00:00:00 2001 From: jcstein <46639943+jcstein@users.noreply.github.com> Date: Tue, 25 Mar 2025 11:06:00 -0400 Subject: [PATCH 3/5] refactor(txsim): improve gas limit, price and fee calculation logic - Separate gas limit, price and fee calculation into clear steps\n- Remove redundant default fee calculation\n- Improve code readability and maintainability --- test/txsim/account.go | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/test/txsim/account.go b/test/txsim/account.go index 991cb8b40e..2ffa2ed6b9 100644 --- a/test/txsim/account.go +++ b/test/txsim/account.go @@ -229,7 +229,7 @@ func (am *AccountManager) Submit(ctx context.Context, op Operation) error { var gasLimit, fee uint64 var gasPrice float64 - // Determine gas limit + // Step 1: Determine gas limit switch { case op.GasLimit > 0: gasLimit = op.GasLimit @@ -237,7 +237,6 @@ func (am *AccountManager) Submit(ctx context.Context, op Operation) error { gasLimit = am.gasLimit default: gasLimit = DefaultGasLimit - fee = defaultFee } // Set gas limit @@ -245,22 +244,24 @@ func (am *AccountManager) Submit(ctx context.Context, op Operation) error { opts = append(opts, user.SetGasLimit(gasLimit)) } - // Determine gas price and calculate fee if not already set - if fee == 0 { - switch { - case op.GasPrice > 0: - gasPrice = op.GasPrice - case am.gasPrice > 0: - gasPrice = am.gasPrice - if gasPrice < appconsts.DefaultMinGasPrice { - log.Warn(). - Float64("gas_price", gasPrice). - Float64("default_min_gas_price", appconsts.DefaultMinGasPrice). - Msg("custom gas price is lower than default minimum") - } - default: - gasPrice = appconsts.DefaultMinGasPrice + // Step 2: Determine gas price + switch { + case op.GasPrice > 0: + gasPrice = op.GasPrice + case am.gasPrice > 0: + gasPrice = am.gasPrice + if gasPrice < appconsts.DefaultMinGasPrice { + log.Warn(). + Float64("gas_price", gasPrice). + Float64("default_min_gas_price", appconsts.DefaultMinGasPrice). + Msg("custom gas price is lower than default minimum") } + default: + gasPrice = appconsts.DefaultMinGasPrice + } + + // Step 3: Calculate fee + if fee == 0 { fee = uint64(math.Ceil(float64(gasLimit) * gasPrice)) opts = append(opts, user.SetFee(fee)) } From 9657d317bf72172719c4eaebf07b1b51baa0ac1c Mon Sep 17 00:00:00 2001 From: evan-forbes Date: Wed, 16 Apr 2025 05:24:54 -0500 Subject: [PATCH 4/5] chore: fix linter --- test/txsim/account.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/txsim/account.go b/test/txsim/account.go index 90e607c914..991494c7cc 100644 --- a/test/txsim/account.go +++ b/test/txsim/account.go @@ -26,8 +26,6 @@ import ( "github.com/celestiaorg/celestia-app/v4/pkg/user" ) -const defaultFee = DefaultGasLimit * appconsts.DefaultMinGasPrice - type AccountManager struct { keys keyring.Keyring conn *grpc.ClientConn From 4c8a9f2d605f4da2a1d41ccdbcfc59234e392c85 Mon Sep 17 00:00:00 2001 From: Callum Waters Date: Wed, 16 Apr 2025 13:46:32 +0200 Subject: [PATCH 5/5] use maketestconfig --- test/cmd/txsim/cli.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cmd/txsim/cli.go b/test/cmd/txsim/cli.go index a7380f7df1..edac87ea64 100644 --- a/test/cmd/txsim/cli.go +++ b/test/cmd/txsim/cli.go @@ -188,7 +188,7 @@ account that can act as the master account. The command runs until all sequences opts.WithGasPrice(gasPrice) } - encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) + encCfg := encoding.MakeTestConfig(app.ModuleEncodingRegisters...) err = txsim.Run( cmd.Context(), grpcEndpoint,