github.com/shrimpyuk/bor@v0.2.15-0.20220224151350-fb4ec6020bae/internal/cli/server/config_test.go (about)

     1  package server
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestConfigDefault(t *testing.T) {
    12  	// the default config should work out of the box
    13  	config := DefaultConfig()
    14  	assert.NoError(t, config.loadChain())
    15  
    16  	_, err := config.buildNode()
    17  	assert.NoError(t, err)
    18  
    19  	_, err = config.buildEth(nil)
    20  	assert.NoError(t, err)
    21  }
    22  
    23  func TestConfigMerge(t *testing.T) {
    24  	c0 := &Config{
    25  		Chain:    "0",
    26  		Snapshot: true,
    27  		Whitelist: map[string]string{
    28  			"a": "b",
    29  		},
    30  		TxPool: &TxPoolConfig{
    31  			LifeTime: 5 * time.Second,
    32  		},
    33  		P2P: &P2PConfig{
    34  			Discovery: &P2PDiscovery{
    35  				StaticNodes: []string{
    36  					"a",
    37  				},
    38  			},
    39  		},
    40  	}
    41  	c1 := &Config{
    42  		Chain: "1",
    43  		Whitelist: map[string]string{
    44  			"b": "c",
    45  		},
    46  		P2P: &P2PConfig{
    47  			MaxPeers: 10,
    48  			Discovery: &P2PDiscovery{
    49  				StaticNodes: []string{
    50  					"b",
    51  				},
    52  			},
    53  		},
    54  	}
    55  	expected := &Config{
    56  		Chain:    "1",
    57  		Snapshot: true,
    58  		Whitelist: map[string]string{
    59  			"a": "b",
    60  			"b": "c",
    61  		},
    62  		TxPool: &TxPoolConfig{
    63  			LifeTime: 5 * time.Second,
    64  		},
    65  		P2P: &P2PConfig{
    66  			MaxPeers: 10,
    67  			Discovery: &P2PDiscovery{
    68  				StaticNodes: []string{
    69  					"a",
    70  					"b",
    71  				},
    72  			},
    73  		},
    74  	}
    75  	assert.NoError(t, c0.Merge(c1))
    76  	assert.Equal(t, c0, expected)
    77  }
    78  
    79  func TestConfigLoadFile(t *testing.T) {
    80  	readFile := func(path string) {
    81  		config, err := readConfigFile(path)
    82  		assert.NoError(t, err)
    83  		assert.Equal(t, config, &Config{
    84  			DataDir: "./data",
    85  			Whitelist: map[string]string{
    86  				"a": "b",
    87  			},
    88  			P2P: &P2PConfig{
    89  				MaxPeers: 30,
    90  			},
    91  			TxPool: &TxPoolConfig{
    92  				LifeTime: time.Duration(1 * time.Second),
    93  			},
    94  			Gpo: &GpoConfig{
    95  				MaxPrice: big.NewInt(100),
    96  			},
    97  			Sealer: &SealerConfig{},
    98  			Cache:  &CacheConfig{},
    99  		})
   100  	}
   101  
   102  	// read file in hcl format
   103  	t.Run("hcl", func(t *testing.T) {
   104  		readFile("./testdata/simple.hcl")
   105  	})
   106  	// read file in json format
   107  	t.Run("json", func(t *testing.T) {
   108  		readFile("./testdata/simple.json")
   109  	})
   110  }
   111  
   112  var dummyEnodeAddr = "enode://0cb82b395094ee4a2915e9714894627de9ed8498fb881cec6db7c65e8b9a5bd7f2f25cc84e71e89d0947e51c76e85d0847de848c7782b13c0255247a6758178c@44.232.55.71:30303"
   113  
   114  func TestConfigBootnodesDefault(t *testing.T) {
   115  	t.Run("EmptyBootnodes", func(t *testing.T) {
   116  		// if no bootnodes are specific, we use the ones from the genesis chain
   117  		config := DefaultConfig()
   118  		assert.NoError(t, config.loadChain())
   119  
   120  		cfg, err := config.buildNode()
   121  		assert.NoError(t, err)
   122  		assert.NotEmpty(t, cfg.P2P.BootstrapNodes)
   123  	})
   124  	t.Run("NotEmptyBootnodes", func(t *testing.T) {
   125  		// if bootnodes specific, DO NOT load the genesis bootnodes
   126  		config := DefaultConfig()
   127  		config.P2P.Discovery.Bootnodes = []string{dummyEnodeAddr}
   128  
   129  		cfg, err := config.buildNode()
   130  		assert.NoError(t, err)
   131  		assert.Len(t, cfg.P2P.BootstrapNodes, 1)
   132  	})
   133  }