github.com/MetalBlockchain/subnet-evm@v0.4.9/utils/fork_test.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package utils
     5  
     6  import (
     7  	"math/big"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestIsForked(t *testing.T) {
    14  	type test struct {
    15  		fork, block *big.Int
    16  		isForked    bool
    17  	}
    18  
    19  	for name, test := range map[string]test{
    20  		"nil fork at 0": {
    21  			fork:     nil,
    22  			block:    big.NewInt(0),
    23  			isForked: false,
    24  		},
    25  		"nil fork at non-zero": {
    26  			fork:     nil,
    27  			block:    big.NewInt(100),
    28  			isForked: false,
    29  		},
    30  		"zero fork at genesis": {
    31  			fork:     big.NewInt(0),
    32  			block:    big.NewInt(0),
    33  			isForked: true,
    34  		},
    35  		"pre fork timestamp": {
    36  			fork:     big.NewInt(100),
    37  			block:    big.NewInt(50),
    38  			isForked: false,
    39  		},
    40  		"at fork timestamp": {
    41  			fork:     big.NewInt(100),
    42  			block:    big.NewInt(100),
    43  			isForked: true,
    44  		},
    45  		"post fork timestamp": {
    46  			fork:     big.NewInt(100),
    47  			block:    big.NewInt(150),
    48  			isForked: true,
    49  		},
    50  	} {
    51  		t.Run(name, func(t *testing.T) {
    52  			res := IsForked(test.fork, test.block)
    53  			assert.Equal(t, test.isForked, res)
    54  		})
    55  	}
    56  }
    57  
    58  func TestIsForkTransition(t *testing.T) {
    59  	type test struct {
    60  		fork, parent, current *big.Int
    61  		transitioned          bool
    62  	}
    63  
    64  	for name, test := range map[string]test{
    65  		"not active at genesis": {
    66  			fork:         nil,
    67  			parent:       nil,
    68  			current:      big.NewInt(0),
    69  			transitioned: false,
    70  		},
    71  		"activate at genesis": {
    72  			fork:         big.NewInt(0),
    73  			parent:       nil,
    74  			current:      big.NewInt(0),
    75  			transitioned: true,
    76  		},
    77  		"nil fork arbitrary transition": {
    78  			fork:         nil,
    79  			parent:       big.NewInt(100),
    80  			current:      big.NewInt(101),
    81  			transitioned: false,
    82  		},
    83  		"nil fork transition same timestamp": {
    84  			fork:         nil,
    85  			parent:       big.NewInt(100),
    86  			current:      big.NewInt(100),
    87  			transitioned: false,
    88  		},
    89  		"exact match on current timestamp": {
    90  			fork:         big.NewInt(100),
    91  			parent:       big.NewInt(99),
    92  			current:      big.NewInt(100),
    93  			transitioned: true,
    94  		},
    95  		"current same as parent does not transition twice": {
    96  			fork:         big.NewInt(100),
    97  			parent:       big.NewInt(101),
    98  			current:      big.NewInt(101),
    99  			transitioned: false,
   100  		},
   101  		"current, parent, and fork same should not transition twice": {
   102  			fork:         big.NewInt(100),
   103  			parent:       big.NewInt(100),
   104  			current:      big.NewInt(100),
   105  			transitioned: false,
   106  		},
   107  		"current transitions after fork": {
   108  			fork:         big.NewInt(100),
   109  			parent:       big.NewInt(99),
   110  			current:      big.NewInt(101),
   111  			transitioned: true,
   112  		},
   113  		"current and parent come after fork": {
   114  			fork:         big.NewInt(100),
   115  			parent:       big.NewInt(101),
   116  			current:      big.NewInt(102),
   117  			transitioned: false,
   118  		},
   119  	} {
   120  		t.Run(name, func(t *testing.T) {
   121  			res := IsForkTransition(test.fork, test.parent, test.current)
   122  			assert.Equal(t, test.transitioned, res)
   123  		})
   124  	}
   125  }