github.com/MetalBlockchain/subnet-evm@v0.4.9/plugin/evm/config_test.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package evm
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/ethereum/go-ethereum/common"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestUnmarshalConfig(t *testing.T) {
    17  	tests := []struct {
    18  		name        string
    19  		givenJSON   []byte
    20  		expected    Config
    21  		expectedErr bool
    22  	}{
    23  		{
    24  			"string durations parsed",
    25  			[]byte(`{"api-max-duration": "1m", "continuous-profiler-frequency": "2m", "tx-pool-rejournal": "3m30s"}`),
    26  			Config{APIMaxDuration: Duration{1 * time.Minute}, ContinuousProfilerFrequency: Duration{2 * time.Minute}, TxPoolRejournal: Duration{3*time.Minute + 30*time.Second}},
    27  			false,
    28  		},
    29  		{
    30  			"integer durations parsed",
    31  			[]byte(fmt.Sprintf(`{"api-max-duration": "%v", "continuous-profiler-frequency": "%v"}`, 1*time.Minute, 2*time.Minute)),
    32  			Config{APIMaxDuration: Duration{1 * time.Minute}, ContinuousProfilerFrequency: Duration{2 * time.Minute}},
    33  			false,
    34  		},
    35  		{
    36  			"nanosecond durations parsed",
    37  			[]byte(`{"api-max-duration": 5000000000, "continuous-profiler-frequency": 5000000000, "tx-pool-rejournal": 9000000000}`),
    38  			Config{APIMaxDuration: Duration{5 * time.Second}, ContinuousProfilerFrequency: Duration{5 * time.Second}, TxPoolRejournal: Duration{9 * time.Second}},
    39  			false,
    40  		},
    41  		{
    42  			"bad durations",
    43  			[]byte(`{"api-max-duration": "bad-duration"}`),
    44  			Config{},
    45  			true,
    46  		},
    47  
    48  		{
    49  			"tx pool configurations",
    50  			[]byte(`{"tx-pool-journal": "hello", "tx-pool-price-limit": 1, "tx-pool-price-bump": 2, "tx-pool-account-slots": 3, "tx-pool-global-slots": 4, "tx-pool-account-queue": 5, "tx-pool-global-queue": 6}`),
    51  			Config{
    52  				TxPoolJournal:      "hello",
    53  				TxPoolPriceLimit:   1,
    54  				TxPoolPriceBump:    2,
    55  				TxPoolAccountSlots: 3,
    56  				TxPoolGlobalSlots:  4,
    57  				TxPoolAccountQueue: 5,
    58  				TxPoolGlobalQueue:  6,
    59  			},
    60  			false,
    61  		},
    62  
    63  		{
    64  			"state sync enabled",
    65  			[]byte(`{"state-sync-enabled":true}`),
    66  			Config{StateSyncEnabled: true},
    67  			false,
    68  		},
    69  		{
    70  			"state sync sources",
    71  			[]byte(`{"state-sync-ids": "NodeID-CaBYJ9kzHvrQFiYWowMkJGAQKGMJqZoat"}`),
    72  			Config{StateSyncIDs: "NodeID-CaBYJ9kzHvrQFiYWowMkJGAQKGMJqZoat"},
    73  			false,
    74  		},
    75  		{
    76  			"empty tx lookup limit",
    77  			[]byte(`{}`),
    78  			Config{TxLookupLimit: 0},
    79  			false,
    80  		},
    81  		{
    82  			"zero tx lookup limit",
    83  			[]byte(`{"tx-lookup-limit": 0}`),
    84  			func() Config {
    85  				return Config{TxLookupLimit: 0}
    86  			}(),
    87  			false,
    88  		},
    89  		{
    90  			"1 tx lookup limit",
    91  			[]byte(`{"tx-lookup-limit": 1}`),
    92  			func() Config {
    93  				return Config{TxLookupLimit: 1}
    94  			}(),
    95  			false,
    96  		},
    97  		{
    98  			"-1 tx lookup limit",
    99  			[]byte(`{"tx-lookup-limit": -1}`),
   100  			Config{},
   101  			true,
   102  		},
   103  		{
   104  			"allow unprotected tx hashes",
   105  			[]byte(`{"allow-unprotected-tx-hashes": ["0x803351deb6d745e91545a6a3e1c0ea3e9a6a02a1a4193b70edfcd2f40f71a01c"]}`),
   106  			Config{AllowUnprotectedTxHashes: []common.Hash{common.HexToHash("0x803351deb6d745e91545a6a3e1c0ea3e9a6a02a1a4193b70edfcd2f40f71a01c")}},
   107  			false,
   108  		},
   109  	}
   110  
   111  	for _, tt := range tests {
   112  		t.Run(tt.name, func(t *testing.T) {
   113  			var tmp Config
   114  			err := json.Unmarshal(tt.givenJSON, &tmp)
   115  			if tt.expectedErr {
   116  				assert.Error(t, err)
   117  			} else {
   118  				assert.NoError(t, err)
   119  				assert.Equal(t, tt.expected, tmp)
   120  			}
   121  		})
   122  	}
   123  }