github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/config/config_test.go (about)

     1  package config_test
     2  
     3  import (
     4  	"flag"
     5  	"math/big"
     6  	"os"
     7  	"reflect"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/0xPolygon/supernets2-node/aggregator"
    13  	"github.com/0xPolygon/supernets2-node/config"
    14  	"github.com/0xPolygon/supernets2-node/config/types"
    15  	"github.com/0xPolygon/supernets2-node/log"
    16  	"github.com/0xPolygon/supernets2-node/pricegetter"
    17  	"github.com/ethereum/go-ethereum/common"
    18  	"github.com/stretchr/testify/assert"
    19  	"github.com/stretchr/testify/require"
    20  	"github.com/urfave/cli/v2"
    21  )
    22  
    23  func Test_Defaults(t *testing.T) {
    24  	tcs := []struct {
    25  		path          string
    26  		expectedValue interface{}
    27  	}{
    28  		{
    29  			path:          "Log.Environment",
    30  			expectedValue: log.LogEnvironment("development"),
    31  		},
    32  		{
    33  			path:          "Log.Level",
    34  			expectedValue: "info",
    35  		},
    36  		{
    37  			path:          "Log.Outputs",
    38  			expectedValue: []string{"stderr"},
    39  		},
    40  		{
    41  			path:          "Synchronizer.SyncChunkSize",
    42  			expectedValue: uint64(100),
    43  		},
    44  		{
    45  			path:          "PriceGetter.Type",
    46  			expectedValue: pricegetter.DefaultType,
    47  		},
    48  		{
    49  			path:          "PriceGetter.DefaultPrice",
    50  			expectedValue: pricegetter.TokenPrice{Float: new(big.Float).SetInt64(2000)},
    51  		},
    52  		{
    53  			path:          "Sequencer.WaitPeriodPoolIsEmpty",
    54  			expectedValue: types.NewDuration(1 * time.Second),
    55  		},
    56  		{
    57  			path:          "Sequencer.MaxTxsPerBatch",
    58  			expectedValue: uint64(150),
    59  		},
    60  		{
    61  			path:          "Sequencer.MaxBatchBytesSize",
    62  			expectedValue: uint64(129848),
    63  		},
    64  		{
    65  			path:          "Sequencer.BlocksAmountForTxsToBeDeleted",
    66  			expectedValue: uint64(100),
    67  		},
    68  		{
    69  			path:          "Sequencer.FrequencyToCheckTxsForDelete",
    70  			expectedValue: types.NewDuration(12 * time.Hour),
    71  		},
    72  		{
    73  			path:          "Sequencer.MaxCumulativeGasUsed",
    74  			expectedValue: uint64(30000000),
    75  		},
    76  		{
    77  			path:          "Sequencer.MaxKeccakHashes",
    78  			expectedValue: uint32(468),
    79  		},
    80  		{
    81  			path:          "Sequencer.MaxPoseidonHashes",
    82  			expectedValue: uint32(279620),
    83  		},
    84  		{
    85  			path:          "Sequencer.MaxPoseidonPaddings",
    86  			expectedValue: uint32(149796),
    87  		},
    88  		{
    89  			path:          "Sequencer.MaxMemAligns",
    90  			expectedValue: uint32(262144),
    91  		},
    92  		{
    93  			path:          "Sequencer.MaxArithmetics",
    94  			expectedValue: uint32(262144),
    95  		},
    96  		{
    97  			path:          "Sequencer.MaxBinaries",
    98  			expectedValue: uint32(262144),
    99  		},
   100  		{
   101  			path:          "Sequencer.MaxSteps",
   102  			expectedValue: uint32(8388608),
   103  		},
   104  		{
   105  			path:          "Sequencer.TxLifetimeCheckTimeout",
   106  			expectedValue: types.NewDuration(10 * time.Minute),
   107  		},
   108  		{
   109  			path:          "Sequencer.MaxTxLifetime",
   110  			expectedValue: types.NewDuration(3 * time.Hour),
   111  		},
   112  		{
   113  			path:          "Sequencer.WeightBatchBytesSize",
   114  			expectedValue: 1,
   115  		},
   116  		{
   117  			path:          "Sequencer.WeightCumulativeGasUsed",
   118  			expectedValue: 1,
   119  		},
   120  		{
   121  			path:          "Sequencer.WeightKeccakHashes",
   122  			expectedValue: 1,
   123  		},
   124  		{
   125  			path:          "Sequencer.WeightPoseidonHashes",
   126  			expectedValue: 1,
   127  		},
   128  		{
   129  			path:          "Sequencer.WeightPoseidonPaddings",
   130  			expectedValue: 1,
   131  		},
   132  		{
   133  			path:          "Sequencer.WeightMemAligns",
   134  			expectedValue: 1,
   135  		},
   136  		{
   137  			path:          "Sequencer.WeightArithmetics",
   138  			expectedValue: 1,
   139  		},
   140  		{
   141  			path:          "Sequencer.WeightBinaries",
   142  			expectedValue: 1,
   143  		},
   144  		{
   145  			path:          "Sequencer.WeightSteps",
   146  			expectedValue: 1,
   147  		},
   148  		{
   149  			path:          "Sequencer.Finalizer.GERDeadlineTimeout",
   150  			expectedValue: types.NewDuration(5 * time.Second),
   151  		},
   152  		{
   153  			path:          "Sequencer.Finalizer.ForcedBatchDeadlineTimeout",
   154  			expectedValue: types.NewDuration(60 * time.Second),
   155  		},
   156  		{
   157  			path:          "Sequencer.Finalizer.SleepDuration",
   158  			expectedValue: types.NewDuration(100 * time.Millisecond),
   159  		},
   160  		{
   161  			path:          "Sequencer.Finalizer.ResourcePercentageToCloseBatch",
   162  			expectedValue: uint32(10),
   163  		},
   164  		{
   165  			path:          "Sequencer.Finalizer.GERFinalityNumberOfBlocks",
   166  			expectedValue: uint64(64),
   167  		},
   168  		{
   169  			path:          "Sequencer.Finalizer.ClosingSignalsManagerWaitForCheckingL1Timeout",
   170  			expectedValue: types.NewDuration(10 * time.Second),
   171  		},
   172  		{
   173  			path:          "Sequencer.Finalizer.ClosingSignalsManagerWaitForCheckingGER",
   174  			expectedValue: types.NewDuration(10 * time.Second),
   175  		},
   176  		{
   177  			path:          "Sequencer.Finalizer.ClosingSignalsManagerWaitForCheckingForcedBatches",
   178  			expectedValue: types.NewDuration(10 * time.Second),
   179  		},
   180  		{
   181  			path:          "Sequencer.Finalizer.ForcedBatchesFinalityNumberOfBlocks",
   182  			expectedValue: uint64(64),
   183  		},
   184  		{
   185  			path:          "Sequencer.Finalizer.TimestampResolution",
   186  			expectedValue: types.NewDuration(10 * time.Second),
   187  		},
   188  		{
   189  			path:          "Sequencer.DBManager.PoolRetrievalInterval",
   190  			expectedValue: types.NewDuration(500 * time.Millisecond),
   191  		},
   192  		{
   193  			path:          "Sequencer.DBManager.L2ReorgRetrievalInterval",
   194  			expectedValue: types.NewDuration(5 * time.Second),
   195  		},
   196  		{
   197  			path:          "Sequencer.Worker.ResourceCostMultiplier",
   198  			expectedValue: float64(1000),
   199  		},
   200  		{
   201  			path:          "SequenceSender.WaitPeriodSendSequence",
   202  			expectedValue: types.NewDuration(5 * time.Second),
   203  		},
   204  		{
   205  			path:          "SequenceSender.LastBatchVirtualizationTimeMaxWaitPeriod",
   206  			expectedValue: types.NewDuration(5 * time.Second),
   207  		},
   208  		{
   209  			path:          "SequenceSender.MaxBatchesForL1",
   210  			expectedValue: uint64(1000),
   211  		},
   212  		{
   213  			path:          "Etherman.URL",
   214  			expectedValue: "http://localhost:8545",
   215  		},
   216  		{
   217  			path:          "NetworkConfig.L1Config.L1ChainID",
   218  			expectedValue: uint64(5),
   219  		},
   220  		{
   221  			path:          "NetworkConfig.L1Config.Supernets2Addr",
   222  			expectedValue: common.HexToAddress("0xa997cfD539E703921fD1e3Cf25b4c241a27a4c7A"),
   223  		},
   224  		{
   225  			path:          "NetworkConfig.L1Config.MaticAddr",
   226  			expectedValue: common.HexToAddress("0x1319D23c2F7034F52Eb07399702B040bA278Ca49"),
   227  		},
   228  		{
   229  			path:          "NetworkConfig.L1Config.GlobalExitRootManagerAddr",
   230  			expectedValue: common.HexToAddress("0x4d9427DCA0406358445bC0a8F88C26b704004f74"),
   231  		},
   232  		{
   233  			path:          "Etherman.MultiGasProvider",
   234  			expectedValue: false,
   235  		},
   236  		{
   237  			path:          "EthTxManager.FrequencyToMonitorTxs",
   238  			expectedValue: types.NewDuration(1 * time.Second),
   239  		},
   240  		{
   241  			path:          "EthTxManager.WaitTxToBeMined",
   242  			expectedValue: types.NewDuration(2 * time.Minute),
   243  		},
   244  		{
   245  			path:          "EthTxManager.WaitTxToBeMined",
   246  			expectedValue: types.NewDuration(2 * time.Minute),
   247  		},
   248  		{
   249  			path:          "EthTxManager.ForcedGas",
   250  			expectedValue: uint64(0),
   251  		},
   252  		{
   253  			path:          "PriceGetter.Type",
   254  			expectedValue: pricegetter.DefaultType,
   255  		},
   256  		{
   257  			path:          "PriceGetter.DefaultPrice",
   258  			expectedValue: pricegetter.TokenPrice{Float: new(big.Float).SetInt64(2000)},
   259  		},
   260  		{
   261  			path:          "L2GasPriceSuggester.DefaultGasPriceWei",
   262  			expectedValue: uint64(2000000000),
   263  		},
   264  		{
   265  			path:          "MTClient.URI",
   266  			expectedValue: "supernets2-prover:50061",
   267  		},
   268  		{
   269  			path:          "StateDB.User",
   270  			expectedValue: "state_user",
   271  		},
   272  		{
   273  			path:          "StateDB.Password",
   274  			expectedValue: "state_password",
   275  		},
   276  		{
   277  			path:          "StateDB.Name",
   278  			expectedValue: "state_db",
   279  		},
   280  		{
   281  			path:          "StateDB.Host",
   282  			expectedValue: "supernets2-state-db",
   283  		},
   284  		{
   285  			path:          "StateDB.Port",
   286  			expectedValue: "5432",
   287  		},
   288  		{
   289  			path:          "StateDB.EnableLog",
   290  			expectedValue: false,
   291  		},
   292  		{
   293  			path:          "StateDB.MaxConns",
   294  			expectedValue: 200,
   295  		},
   296  		{
   297  			path:          "Pool.MaxTxBytesSize",
   298  			expectedValue: uint64(100132),
   299  		},
   300  		{
   301  			path:          "Pool.MaxTxDataBytesSize",
   302  			expectedValue: 100000,
   303  		},
   304  
   305  		{
   306  			path:          "Pool.DefaultMinGasPriceAllowed",
   307  			expectedValue: uint64(1000000000),
   308  		},
   309  		{
   310  			path:          "Pool.MinAllowedGasPriceInterval",
   311  			expectedValue: types.NewDuration(5 * time.Minute),
   312  		},
   313  		{
   314  			path:          "Pool.PollMinAllowedGasPriceInterval",
   315  			expectedValue: types.NewDuration(15 * time.Second),
   316  		},
   317  		{
   318  			path:          "Pool.DB.User",
   319  			expectedValue: "pool_user",
   320  		},
   321  		{
   322  			path:          "Pool.DB.Password",
   323  			expectedValue: "pool_password",
   324  		},
   325  		{
   326  			path:          "Pool.DB.Name",
   327  			expectedValue: "pool_db",
   328  		},
   329  		{
   330  			path:          "Pool.DB.Host",
   331  			expectedValue: "supernets2-pool-db",
   332  		},
   333  		{
   334  			path:          "Pool.DB.Port",
   335  			expectedValue: "5432",
   336  		},
   337  		{
   338  			path:          "Pool.DB.EnableLog",
   339  			expectedValue: false,
   340  		},
   341  		{
   342  			path:          "Pool.DB.MaxConns",
   343  			expectedValue: 200,
   344  		},
   345  		{
   346  			path:          "RPC.Host",
   347  			expectedValue: "0.0.0.0",
   348  		},
   349  		{
   350  			path:          "RPC.Port",
   351  			expectedValue: int(8545),
   352  		},
   353  		{
   354  			path:          "RPC.ReadTimeout",
   355  			expectedValue: types.NewDuration(60 * time.Second),
   356  		},
   357  		{
   358  			path:          "RPC.WriteTimeout",
   359  			expectedValue: types.NewDuration(60 * time.Second),
   360  		},
   361  		{
   362  			path:          "RPC.SequencerNodeURI",
   363  			expectedValue: "",
   364  		},
   365  		{
   366  			path:          "RPC.MaxRequestsPerIPAndSecond",
   367  			expectedValue: float64(500),
   368  		},
   369  		{
   370  			path:          "RPC.EnableL2SuggestedGasPricePolling",
   371  			expectedValue: true,
   372  		},
   373  		{
   374  			path:          "RPC.WebSockets.Enabled",
   375  			expectedValue: true,
   376  		},
   377  		{
   378  			path:          "RPC.WebSockets.Host",
   379  			expectedValue: "0.0.0.0",
   380  		},
   381  		{
   382  			path:          "RPC.WebSockets.Port",
   383  			expectedValue: int(8546),
   384  		},
   385  		{
   386  			path:          "Executor.URI",
   387  			expectedValue: "supernets2-prover:50071",
   388  		},
   389  		{
   390  			path:          "Metrics.Host",
   391  			expectedValue: "0.0.0.0",
   392  		},
   393  		{
   394  			path:          "Metrics.Port",
   395  			expectedValue: 9091,
   396  		},
   397  		{
   398  			path:          "Metrics.Enabled",
   399  			expectedValue: false,
   400  		},
   401  		{
   402  			path:          "Aggregator.Host",
   403  			expectedValue: "0.0.0.0",
   404  		},
   405  		{
   406  			path:          "Aggregator.Port",
   407  			expectedValue: 50081,
   408  		},
   409  		{
   410  			path:          "Aggregator.RetryTime",
   411  			expectedValue: types.NewDuration(5 * time.Second),
   412  		},
   413  		{
   414  			path:          "Aggregator.VerifyProofInterval",
   415  			expectedValue: types.NewDuration(90 * time.Second),
   416  		},
   417  		{
   418  			path:          "Aggregator.TxProfitabilityCheckerType",
   419  			expectedValue: aggregator.TxProfitabilityCheckerType(aggregator.ProfitabilityAcceptAll),
   420  		},
   421  		{
   422  			path:          "Aggregator.TxProfitabilityMinReward",
   423  			expectedValue: aggregator.TokenAmountWithDecimals{Int: big.NewInt(1100000000000000000)},
   424  		},
   425  		{
   426  			path:          "Aggregator.ProofStatePollingInterval",
   427  			expectedValue: types.NewDuration(5 * time.Second),
   428  		},
   429  		{
   430  			path:          "Aggregator.CleanupLockedProofsInterval",
   431  			expectedValue: types.NewDuration(2 * time.Minute),
   432  		},
   433  		{
   434  			path:          "Aggregator.GeneratingProofCleanupThreshold",
   435  			expectedValue: "10m",
   436  		},
   437  	}
   438  	file, err := os.CreateTemp("", "genesisConfig")
   439  	require.NoError(t, err)
   440  	defer func() {
   441  		require.NoError(t, os.Remove(file.Name()))
   442  	}()
   443  	require.NoError(t, os.WriteFile(file.Name(), []byte("{}"), 0600))
   444  
   445  	flagSet := flag.NewFlagSet("", flag.PanicOnError)
   446  	flagSet.String(config.FlagNetwork, "testnet", "")
   447  	ctx := cli.NewContext(cli.NewApp(), flagSet, nil)
   448  	cfg, err := config.Load(ctx)
   449  	if err != nil {
   450  		t.Fatalf("Unexpected error loading default config: %v", err)
   451  	}
   452  
   453  	for _, tc := range tcs {
   454  		tc := tc
   455  		t.Run(tc.path, func(t *testing.T) {
   456  			actual := getValueFromStruct(tc.path, cfg)
   457  			require.Equal(t, tc.expectedValue, actual)
   458  		})
   459  	}
   460  }
   461  
   462  func getValueFromStruct(path string, object interface{}) interface{} {
   463  	keySlice := strings.Split(path, ".")
   464  	v := reflect.ValueOf(object)
   465  
   466  	for _, key := range keySlice {
   467  		for v.Kind() == reflect.Ptr {
   468  			v = v.Elem()
   469  		}
   470  
   471  		v = v.FieldByName(key)
   472  	}
   473  	return v.Interface()
   474  }
   475  
   476  func TestEnvVarArrayDecoding(t *testing.T) {
   477  	file, err := os.CreateTemp("", "genesisConfig")
   478  	require.NoError(t, err)
   479  	defer func() {
   480  		require.NoError(t, os.Remove(file.Name()))
   481  	}()
   482  	require.NoError(t, os.WriteFile(file.Name(), []byte("{}"), 0600))
   483  	flagSet := flag.NewFlagSet("", flag.PanicOnError)
   484  	flagSet.String(config.FlagNetwork, "testnet", "")
   485  	ctx := cli.NewContext(cli.NewApp(), flagSet, nil)
   486  
   487  	os.Setenv("SUPERNETS2_NODE_LOG_OUTPUTS", "a,b,c")
   488  	defer func() {
   489  		os.Unsetenv("SUPERNETS2_NODE_LOG_OUTPUTS")
   490  	}()
   491  
   492  	cfg, err := config.Load(ctx)
   493  	require.NoError(t, err)
   494  
   495  	assert.Equal(t, 3, len(cfg.Log.Outputs))
   496  	assert.Equal(t, "a", cfg.Log.Outputs[0])
   497  	assert.Equal(t, "b", cfg.Log.Outputs[1])
   498  	assert.Equal(t, "c", cfg.Log.Outputs[2])
   499  }