github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/bootstrap/node_info_test.go (about)

     1  package bootstrap_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/onflow/flow-go/model/bootstrap"
    12  	"github.com/onflow/flow-go/model/flow"
    13  	"github.com/onflow/flow-go/utils/unittest"
    14  )
    15  
    16  func TestIdentityListCanonical(t *testing.T) {
    17  	nodes := unittest.NodeInfosFixture(20)
    18  	// make sure the list is not sorted
    19  	nodes[0].NodeID[0], nodes[1].NodeID[0] = 2, 1
    20  	require.False(t, flow.IsIdentifierCanonical(nodes[0].NodeID, nodes[1].NodeID))
    21  	ids := bootstrap.ToIdentityList(nodes)
    22  	assert.False(t, flow.IsIdentityListCanonical(ids))
    23  
    24  	// make a copy of the original list of nodes
    25  	nodesCopy := make([]bootstrap.NodeInfo, len(nodes))
    26  	copy(nodesCopy, nodes)
    27  
    28  	sortedNodes := bootstrap.Sort(nodes, flow.Canonical[flow.Identity])
    29  	sortedIds := bootstrap.ToIdentityList(sortedNodes)
    30  	require.True(t, flow.IsIdentityListCanonical(sortedIds))
    31  	// make sure original list didn't change
    32  	assert.Equal(t, nodesCopy, nodes)
    33  
    34  	// check `IsIdentityListCanonical` detects order equality in a sorted list
    35  	nodes[1] = nodes[10] // add a duplication
    36  	copy(nodesCopy, nodes)
    37  	sortedNodes = bootstrap.Sort(nodes, flow.Canonical[flow.Identity])
    38  	sortedIds = bootstrap.ToIdentityList(sortedNodes)
    39  	assert.False(t, flow.IsIdentityListCanonical(sortedIds))
    40  	// make sure original list didn't change
    41  	assert.Equal(t, nodesCopy, nodes)
    42  }
    43  
    44  func TestNodeConfigEncodingJSON(t *testing.T) {
    45  	t.Run("normal node config", func(t *testing.T) {
    46  		conf := unittest.NodeConfigFixture()
    47  		enc, err := json.Marshal(conf)
    48  		require.NoError(t, err)
    49  		var dec bootstrap.NodeConfig
    50  		err = json.Unmarshal(enc, &dec)
    51  		require.NoError(t, err)
    52  		assert.Equal(t, conf, dec)
    53  	})
    54  	t.Run("compat: should accept old files using Stake field", func(t *testing.T) {
    55  		conf := unittest.NodeConfigFixture()
    56  		enc, err := json.Marshal(conf)
    57  		require.NoError(t, err)
    58  		// emulate the old encoding by replacing the new field with old field name
    59  		enc = []byte(strings.Replace(string(enc), "Weight", "Stake", 1))
    60  		var dec bootstrap.NodeConfig
    61  		err = json.Unmarshal(enc, &dec)
    62  		require.NoError(t, err)
    63  		assert.Equal(t, conf, dec)
    64  	})
    65  }
    66  
    67  func TestNodeInfoPubEncodingJSON(t *testing.T) {
    68  	t.Run("normal node info", func(t *testing.T) {
    69  		conf := unittest.NodeInfoFixture().Public()
    70  		enc, err := json.Marshal(conf)
    71  		require.NoError(t, err)
    72  		var dec bootstrap.NodeInfoPub
    73  		err = json.Unmarshal(enc, &dec)
    74  		require.NoError(t, err)
    75  		assert.True(t, dec.Equals(&conf))
    76  	})
    77  	t.Run("compat: should accept old files using Stake field", func(t *testing.T) {
    78  		conf := unittest.NodeInfoFixture().Public()
    79  		enc, err := json.Marshal(conf)
    80  		require.NoError(t, err)
    81  		// emulate the old encoding by replacing the new field with old field name
    82  		enc = []byte(strings.Replace(string(enc), "Weight", "Stake", 1))
    83  		var dec bootstrap.NodeInfoPub
    84  		err = json.Unmarshal(enc, &dec)
    85  		require.NoError(t, err)
    86  		assert.True(t, dec.Equals(&conf))
    87  	})
    88  }