Skip to content

Commit

Permalink
Remove gas values from the config
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos committed Sep 16, 2023
1 parent 3eb1857 commit db1c052
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 66 deletions.
44 changes: 25 additions & 19 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ import (

const (
configFlagName = "config"
defaultRemote = "http://127.0.0.1:26657"
envPrefix = "GNO_FAUCET"
)

var (
defaultGasFee = "1000000ugnot"
defaultGasWanted = "100000"
defaultRemote = "http://127.0.0.1:26657"
)

var remoteRegex = regexp.MustCompile(`^https?://[a-z\d.-]+(:\d+)?(?:/[a-z\d]+)*$`)

// faucetCfg wraps the faucet
Expand All @@ -36,6 +41,8 @@ type faucetCfg struct {

faucetConfigPath string
remote string
gasFee string
gasWanted string
}

// newRootCmd creates the root faucet command
Expand Down Expand Up @@ -83,13 +90,6 @@ func (c *faucetCfg) registerRootFlags(fs *flag.FlagSet) {
"the IP:PORT URL for the faucet server",
)

fs.StringVar(
&c.remote,
"remote",
defaultRemote,
"the JSON-RPC URL of the Gno chain",
)

fs.StringVar(
&c.config.ChainID,
"chain-id",
Expand Down Expand Up @@ -119,17 +119,17 @@ func (c *faucetCfg) registerRootFlags(fs *flag.FlagSet) {
)

fs.StringVar(
&c.config.GasFee,
&c.gasFee,
"gas-fee",
config.DefaultGasFee,
"the static gas fee for the transaction",
defaultGasFee,
"the static gas fee for the transaction. Format: <AMOUNT>ugnot",
)

fs.StringVar(
&c.config.GasWanted,
&c.gasWanted,
"gas-wanted",
config.DefaultGasWanted,
"the static gas wanted for the transaction",
defaultGasWanted,
"the static gas wanted for the transaction. Format: <AMOUNT>ugnot",
)

fs.StringVar(
Expand All @@ -138,6 +138,13 @@ func (c *faucetCfg) registerRootFlags(fs *flag.FlagSet) {
"",
"the path to the faucet TOML configuration, if any",
)

fs.StringVar(
&c.remote,
"remote",
defaultRemote,
"the JSON-RPC URL of the Gno chain",
)
}

// exec executes the faucet root command
Expand All @@ -156,18 +163,17 @@ func (c *faucetCfg) exec(_ context.Context, _ []string) error {
// It is worth noting that this is temporary,
// and will be removed once gas estimation is enabled
// on Gno.land
gasFee, err := std.ParseCoin(c.config.GasFee)
gasFee, err := std.ParseCoin(c.gasFee)
if err != nil {
return fmt.Errorf("invalid gas fee, %w", err)
}

gasWanted, err := strconv.ParseInt(c.config.GasWanted, 10, 64)
gasWanted, err := strconv.ParseInt(c.gasWanted, 10, 64)
if err != nil {
return fmt.Errorf("invalid gas wanted, %w", err)
}

// Validate the remote URL
// validate the remote address
// Validate the remote address
if !remoteRegex.MatchString(c.remote) {
return errors.New("invalid remote address")
}
Expand All @@ -182,7 +188,7 @@ func (c *faucetCfg) exec(_ context.Context, _ []string) error {
// static gas estimation
f, err := faucet.NewFaucet(
static.New(gasFee, gasWanted),
tm2Client.NewClient(defaultRemote),
tm2Client.NewClient(c.remote),
faucet.WithLogger(newCommandLogger(logger)),
faucet.WithConfig(c.config),
)
Expand Down
25 changes: 0 additions & 25 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ const (
DefaultListenAddress = "0.0.0.0:8545"
DefaultChainID = "dev"
DefaultSendAmount = "1000000ugnot"
DefaultGasFee = "1000000ugnot"
DefaultGasWanted = "100000"
//nolint:lll // Mnemonic is naturally long
DefaultMnemonic = "source bonus chronic canvas draft south burst lottery vacant surface solve popular case indicate oppose farm nothing bullet exhibit title speed wink action roast"
DefaultNumAccounts = uint64(1)
Expand All @@ -23,16 +21,13 @@ var (
ErrInvalidListenAddress = errors.New("invalid listen address")
ErrInvalidChainID = errors.New("invalid chain ID")
ErrInvalidSendAmount = errors.New("invalid send amount")
ErrInvalidGasFee = errors.New("invalid gas fee")
ErrInvalidGasWanted = errors.New("invalid gas wanted")
ErrInvalidMnemonic = errors.New("invalid mnemonic")
ErrInvalidNumAccounts = errors.New("invalid number of faucet accounts")
)

var (
listenAddressRegex = regexp.MustCompile(`^\d{1,3}(\.\d{1,3}){3}:\d+$`)
amountRegex = regexp.MustCompile(`^\d+ugnot$`)
numberRegex = regexp.MustCompile(`^\d+$`)
)

// Config defines the base-level Faucet configuration
Expand All @@ -54,14 +49,6 @@ type Config struct {
// Format should be: <AMOUNT>ugnot
SendAmount string `toml:"send_amount"`

// The static gas fee for the transaction.
// Format should be: <AMOUNT>ugnot
GasFee string `toml:"gas_fee"`

// The static gas wanted for the transaction.
// Format should be: <AMOUNT>
GasWanted string `toml:"gas_wanted"`

// The number of faucet accounts,
// based on the mnemonic (account 0, index x)
NumAccounts uint64 `toml:"num_accounts"`
Expand All @@ -73,8 +60,6 @@ func DefaultConfig() *Config {
ListenAddress: DefaultListenAddress,
ChainID: DefaultChainID,
SendAmount: DefaultSendAmount,
GasFee: DefaultGasFee,
GasWanted: DefaultGasWanted,
Mnemonic: DefaultMnemonic,
NumAccounts: DefaultNumAccounts,
CORSConfig: DefaultCORSConfig(),
Expand All @@ -98,16 +83,6 @@ func ValidateConfig(config *Config) error {
return ErrInvalidSendAmount
}

// validate the gas fee
if !amountRegex.MatchString(config.GasFee) {
return ErrInvalidGasFee
}

// validate the gas wanted
if !numberRegex.MatchString(config.GasWanted) {
return ErrInvalidGasWanted
}

// validate the mnemonic is bip39-compliant
if !bip39.IsMnemonicValid(config.Mnemonic) {
return fmt.Errorf("%w, %s", ErrInvalidMnemonic, config.Mnemonic)
Expand Down
18 changes: 0 additions & 18 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,6 @@ func TestConfig_ValidateConfig(t *testing.T) {
assert.ErrorIs(t, ValidateConfig(cfg), ErrInvalidSendAmount)
})

t.Run("invalid gas fee", func(t *testing.T) {
t.Parallel()

cfg := DefaultConfig()
cfg.GasFee = "1000goo" // invalid denom

assert.ErrorIs(t, ValidateConfig(cfg), ErrInvalidGasFee)
})

t.Run("invalid gas wanted", func(t *testing.T) {
t.Parallel()

cfg := DefaultConfig()
cfg.GasWanted = "totally a number" // invalid number

assert.ErrorIs(t, ValidateConfig(cfg), ErrInvalidGasWanted)
})

t.Run("invalid mnemonic", func(t *testing.T) {
t.Parallel()

Expand Down
8 changes: 4 additions & 4 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestFaucet_Serve_ValidRequests(t *testing.T) {
t.Parallel()

var (
gasFee = std.MustParseCoin(config.DefaultGasFee)
gasFee = std.MustParseCoin("1ugnot")
sendAmount = std.MustParseCoins(config.DefaultSendAmount)
)

Expand Down Expand Up @@ -294,7 +294,7 @@ func TestFaucet_Serve_InvalidRequests(t *testing.T) {
t.Parallel()

var (
gasFee = std.MustParseCoin(config.DefaultGasFee)
gasFee = std.MustParseCoin("1ugnot")
sendAmount = std.MustParseCoins(config.DefaultSendAmount)
)

Expand Down Expand Up @@ -523,7 +523,7 @@ func TestFaucet_Serve_MalformedRequests(t *testing.T) {
cfg.ListenAddress = fmt.Sprintf("127.0.0.1:%d", getFreePort(t))

f, err := NewFaucet(
static.New(std.MustParseCoin(config.DefaultGasFee), 100000),
static.New(std.MustParseCoin("1ugnot"), 100000),
&mockClient{},
WithConfig(cfg),
)
Expand Down Expand Up @@ -568,7 +568,7 @@ func TestFaucet_Serve_NoFundedAccounts(t *testing.T) {
t.Parallel()

var (
gasFee = std.MustParseCoin(config.DefaultGasFee)
gasFee = std.MustParseCoin("1ugnot")
sendAmount = std.MustParseCoins(config.DefaultSendAmount)
)

Expand Down

0 comments on commit db1c052

Please sign in to comment.