--stop-on-error

This commit is contained in:
Roy Crihfield 2024-05-20 07:37:06 +08:00
parent 2edf257db7
commit a5911fcd6a
4 changed files with 22 additions and 1 deletions

View File

@ -21,6 +21,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cerc-io/tx-spammer/pkg/auto"
)
@ -39,6 +40,12 @@ Support standard, optimism L2, optimism L1 to L2, and EIP1559 transactions`,
},
}
func init() {
autoSendCmd.PersistentFlags().Bool("stop-on-error", true,
"stop service when SendTransaction returns an error")
viper.BindPFlag(auto.SpammerStopOnError, autoSendCmd.PersistentFlags().Lookup("stop-on-error"))
}
func autoSend() {
config, err := auto.NewConfig()
if err != nil {

View File

@ -51,6 +51,9 @@ func init() {
// Config holds all the parameters for the auto tx spammer
type Config struct {
// Whether to stop service on any error from tx generation or sending
StopOnSendError bool
// HTTP client for sending transactions
RpcClient *rpc.Client
EthClient *ethclient.Client
@ -130,6 +133,8 @@ type BlobTxConfig struct {
}
func NewConfig() (*Config, error) {
stopOnError := viper.GetBool(SpammerStopOnError)
// Initialize rpc client
httpPathStr := viper.GetString(ethHttpPath)
if httpPathStr == "" {
@ -207,6 +212,7 @@ func NewConfig() (*Config, error) {
// Assemble and return
return &Config{
StopOnSendError: stopOnError,
RpcClient: rpcClient,
EthClient: ethClient,
SenderKeys: keys,

View File

@ -19,6 +19,8 @@ package auto
import "github.com/spf13/viper"
const (
SPAMMER_STOP_ON_ERROR = "SPAMMER_STOP_ON_ERROR"
// env variables
ETH_KEY_DIR_PATH = "ETH_KEY_DIR_PATH"
ETH_HTTP_PATH = "ETH_HTTP_PATH"
@ -55,6 +57,8 @@ const (
ETH_BLOBTX_BLOB_COUNT = "ETH_BLOBTX_BLOB_COUNT"
// toml bindings
SpammerStopOnError = "service.stopOnError"
ethKeyDirPath = "eth.keyDirPath"
ethHttpPath = "eth.httpPath"
ethContractAddrPath = "eth.contractAddrPath"
@ -91,6 +95,8 @@ const (
)
func bindEnv() {
viper.BindEnv(SpammerStopOnError, SPAMMER_STOP_ON_ERROR)
viper.BindEnv(ethKeyDirPath, ETH_KEY_DIR_PATH)
viper.BindEnv(ethHttpPath, ETH_HTTP_PATH)
viper.BindEnv(ethContractAddrPath, ETH_CONTRACT_ADDR_PATH)

View File

@ -75,7 +75,9 @@ func (s *Spammer) Loop(quitChan <-chan bool) (<-chan bool, error) {
go func() {
for err := range sendErrChan {
logrus.Errorf("tx sending error: %v", err)
recoverClose(genQuit)
if s.config.StopOnSendError {
recoverClose(genQuit)
}
}
}()