From f94c7ddd6f793f901e3c13f689d0f6ab112dad8f Mon Sep 17 00:00:00 2001 From: Ferran Borreguero Date: Tue, 26 Oct 2021 18:09:46 +0200 Subject: [PATCH] Decode big.int in config (#219) --- command/server/config.go | 45 +++++++++++++++++++++++++++-- command/server/config_test.go | 10 ++++++- command/server/testdata/simple.hcl | 10 ++++++- command/server/testdata/simple.json | 6 ++++ 4 files changed, 66 insertions(+), 5 deletions(-) diff --git a/command/server/config.go b/command/server/config.go index d5fbb49110..e25870a645 100644 --- a/command/server/config.go +++ b/command/server/config.go @@ -9,6 +9,7 @@ import ( "path/filepath" "runtime" "strconv" + "strings" "time" godebug "runtime/debug" @@ -194,7 +195,8 @@ type SealerConfig struct { GasCeil uint64 `hcl:"gas-ceil,optional"` // GasPrice is the minimum gas price for mining a transaction - GasPrice *big.Int `hcl:"gas-price,optional"` + GasPrice *big.Int + GasPriceRaw string `hcl:"gas-price,optional"` } type JsonRPCConfig struct { @@ -256,10 +258,12 @@ type GpoConfig struct { Percentile uint64 `hcl:"percentile,optional"` // MaxPrice is an upper bound gas price - MaxPrice *big.Int `hcl:"max-price,optional"` + MaxPrice *big.Int + MaxPriceRaw string `hcl:"max-price,optional"` // IgnorePrice is a lower bound gas price - IgnorePrice *big.Int `hcl:"ignore-price,optional"` + IgnorePrice *big.Int + IgnorePriceRaw string `hcl:"ignore-price,optional"` } type TelemetryConfig struct { @@ -473,6 +477,37 @@ func DefaultConfig() *Config { } } +func (c *Config) fillBigInt() error { + tds := []struct { + path string + td **big.Int + str *string + }{ + {"gpo.maxprice", &c.Gpo.MaxPrice, &c.Gpo.MaxPriceRaw}, + {"gpo.ignoreprice", &c.Gpo.IgnorePrice, &c.Gpo.IgnorePriceRaw}, + {"sealer.gasprice", &c.Sealer.GasPrice, &c.Sealer.GasPriceRaw}, + } + + for _, x := range tds { + if *x.str != "" { + b := new(big.Int) + + var ok bool + if strings.HasPrefix(*x.str, "0x") { + b, ok = b.SetString((*x.str)[2:], 16) + } else { + b, ok = b.SetString(*x.str, 10) + } + if !ok { + return fmt.Errorf("%s can't parse big int %s", x.path, *x.str) + } + *x.str = "" + *x.td = b + } + } + return nil +} + func (c *Config) fillTimeDurations() error { tds := []struct { path string @@ -511,10 +546,14 @@ func readConfigFile(path string) (*Config, error) { config := &Config{ TxPool: &TxPoolConfig{}, Cache: &CacheConfig{}, + Sealer: &SealerConfig{}, } if err := hclsimple.DecodeFile(path, nil, config); err != nil { return nil, fmt.Errorf("failed to decode config file '%s': %v", path, err) } + if err := config.fillBigInt(); err != nil { + return nil, err + } if err := config.fillTimeDurations(); err != nil { return nil, err } diff --git a/command/server/config_test.go b/command/server/config_test.go index 537d83be86..6383c3e003 100644 --- a/command/server/config_test.go +++ b/command/server/config_test.go @@ -1,6 +1,7 @@ package server import ( + "math/big" "testing" "time" @@ -81,13 +82,20 @@ func TestConfigLoadFile(t *testing.T) { assert.NoError(t, err) assert.Equal(t, config, &Config{ DataDir: "./data", + Whitelist: map[string]string{ + "a": "b", + }, P2P: &P2PConfig{ MaxPeers: 30, }, TxPool: &TxPoolConfig{ LifeTime: time.Duration(1 * time.Second), }, - Cache: &CacheConfig{}, + Gpo: &GpoConfig{ + MaxPrice: big.NewInt(100), + }, + Sealer: &SealerConfig{}, + Cache: &CacheConfig{}, }) } diff --git a/command/server/testdata/simple.hcl b/command/server/testdata/simple.hcl index 2be98feb42..e276960e6d 100644 --- a/command/server/testdata/simple.hcl +++ b/command/server/testdata/simple.hcl @@ -1,9 +1,17 @@ data-dir = "./data" +whitelist = { + a = "b" +} + p2p { max-peers = 30 } txpool { lifetime = "1s" -} \ No newline at end of file +} + +gpo { + max-price = "100" +} diff --git a/command/server/testdata/simple.json b/command/server/testdata/simple.json index 9dbf98f515..277f05d105 100644 --- a/command/server/testdata/simple.json +++ b/command/server/testdata/simple.json @@ -1,9 +1,15 @@ { "data-dir": "./data", + "whitelist": { + "a": "b" + }, "p2p": { "max-peers": 30 }, "txpool": { "lifetime": "1s" + }, + "gpo": { + "max-price": "100" } } \ No newline at end of file