Decode big.int in config (#219)

This commit is contained in:
Ferran Borreguero 2021-10-26 18:09:46 +02:00 committed by GitHub
parent e9703c7e3f
commit f94c7ddd6f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 5 deletions

View file

@ -9,6 +9,7 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv" "strconv"
"strings"
"time" "time"
godebug "runtime/debug" godebug "runtime/debug"
@ -194,7 +195,8 @@ type SealerConfig struct {
GasCeil uint64 `hcl:"gas-ceil,optional"` GasCeil uint64 `hcl:"gas-ceil,optional"`
// GasPrice is the minimum gas price for mining a transaction // 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 { type JsonRPCConfig struct {
@ -256,10 +258,12 @@ type GpoConfig struct {
Percentile uint64 `hcl:"percentile,optional"` Percentile uint64 `hcl:"percentile,optional"`
// MaxPrice is an upper bound gas price // 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 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 { 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 { func (c *Config) fillTimeDurations() error {
tds := []struct { tds := []struct {
path string path string
@ -511,10 +546,14 @@ func readConfigFile(path string) (*Config, error) {
config := &Config{ config := &Config{
TxPool: &TxPoolConfig{}, TxPool: &TxPoolConfig{},
Cache: &CacheConfig{}, Cache: &CacheConfig{},
Sealer: &SealerConfig{},
} }
if err := hclsimple.DecodeFile(path, nil, config); err != nil { if err := hclsimple.DecodeFile(path, nil, config); err != nil {
return nil, fmt.Errorf("failed to decode config file '%s': %v", path, err) 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 { if err := config.fillTimeDurations(); err != nil {
return nil, err return nil, err
} }

View file

@ -1,6 +1,7 @@
package server package server
import ( import (
"math/big"
"testing" "testing"
"time" "time"
@ -81,13 +82,20 @@ func TestConfigLoadFile(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, config, &Config{ assert.Equal(t, config, &Config{
DataDir: "./data", DataDir: "./data",
Whitelist: map[string]string{
"a": "b",
},
P2P: &P2PConfig{ P2P: &P2PConfig{
MaxPeers: 30, MaxPeers: 30,
}, },
TxPool: &TxPoolConfig{ TxPool: &TxPoolConfig{
LifeTime: time.Duration(1 * time.Second), LifeTime: time.Duration(1 * time.Second),
}, },
Cache: &CacheConfig{}, Gpo: &GpoConfig{
MaxPrice: big.NewInt(100),
},
Sealer: &SealerConfig{},
Cache: &CacheConfig{},
}) })
} }

View file

@ -1,5 +1,9 @@
data-dir = "./data" data-dir = "./data"
whitelist = {
a = "b"
}
p2p { p2p {
max-peers = 30 max-peers = 30
} }
@ -7,3 +11,7 @@ p2p {
txpool { txpool {
lifetime = "1s" lifetime = "1s"
} }
gpo {
max-price = "100"
}

View file

@ -1,9 +1,15 @@
{ {
"data-dir": "./data", "data-dir": "./data",
"whitelist": {
"a": "b"
},
"p2p": { "p2p": {
"max-peers": 30 "max-peers": 30
}, },
"txpool": { "txpool": {
"lifetime": "1s" "lifetime": "1s"
},
"gpo": {
"max-price": "100"
} }
} }