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

     1  package flow_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/onflow/crypto"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/onflow/flow-go/model/flow"
    10  	"github.com/onflow/flow-go/utils/unittest"
    11  )
    12  
    13  func TestClusterQCVoteData_Equality(t *testing.T) {
    14  
    15  	pks := unittest.PublicKeysFixture(2, crypto.BLSBLS12381)
    16  
    17  	_ = len(pks)
    18  
    19  	t.Run("empty structures are equal", func(t *testing.T) {
    20  		a := &flow.ClusterQCVoteData{}
    21  		b := &flow.ClusterQCVoteData{}
    22  		require.True(t, a.EqualTo(b))
    23  		require.True(t, b.EqualTo(a))
    24  	})
    25  
    26  	t.Run("sig data triggers", func(t *testing.T) {
    27  		a := &flow.ClusterQCVoteData{
    28  			SigData:  []byte{1, 2},
    29  			VoterIDs: nil,
    30  		}
    31  		b := &flow.ClusterQCVoteData{
    32  			SigData:  []byte{1, 3},
    33  			VoterIDs: nil,
    34  		}
    35  		require.False(t, a.EqualTo(b))
    36  		require.False(t, b.EqualTo(a))
    37  	})
    38  
    39  	t.Run("VoterID len difference triggers", func(t *testing.T) {
    40  		a := &flow.ClusterQCVoteData{
    41  			SigData:  nil,
    42  			VoterIDs: []flow.Identifier{flow.HashToID([]byte{1, 2, 3})},
    43  		}
    44  		b := &flow.ClusterQCVoteData{
    45  			SigData:  nil,
    46  			VoterIDs: []flow.Identifier{},
    47  		}
    48  		require.False(t, a.EqualTo(b))
    49  		require.False(t, b.EqualTo(a))
    50  	})
    51  
    52  	t.Run("VoterID len values triggers", func(t *testing.T) {
    53  		a := &flow.ClusterQCVoteData{
    54  			SigData:  nil,
    55  			VoterIDs: []flow.Identifier{flow.HashToID([]byte{1, 2, 3})},
    56  		}
    57  		b := &flow.ClusterQCVoteData{
    58  			SigData:  nil,
    59  			VoterIDs: []flow.Identifier{flow.HashToID([]byte{3, 2, 1})},
    60  		}
    61  		require.False(t, a.EqualTo(b))
    62  		require.False(t, b.EqualTo(a))
    63  	})
    64  
    65  	t.Run("filled structures match with same data", func(t *testing.T) {
    66  		a := &flow.ClusterQCVoteData{
    67  			SigData:  []byte{3, 3, 3},
    68  			VoterIDs: []flow.Identifier{flow.HashToID([]byte{1, 2, 3}), flow.HashToID([]byte{3, 2, 1})},
    69  		}
    70  		b := &flow.ClusterQCVoteData{
    71  			SigData:  []byte{3, 3, 3},
    72  			VoterIDs: []flow.Identifier{flow.HashToID([]byte{1, 2, 3}), flow.HashToID([]byte{3, 2, 1})},
    73  		}
    74  		require.True(t, a.EqualTo(b))
    75  		require.True(t, b.EqualTo(a))
    76  	})
    77  }
    78  
    79  func TestEpochCommit_EqualTo(t *testing.T) {
    80  
    81  	qcA := flow.ClusterQCVoteData{
    82  		SigData:  []byte{3, 3, 3},
    83  		VoterIDs: []flow.Identifier{flow.HashToID([]byte{1, 2, 3}), flow.HashToID([]byte{3, 2, 1})},
    84  	}
    85  
    86  	qcB := flow.ClusterQCVoteData{
    87  		SigData:  []byte{1, 1, 1},
    88  		VoterIDs: []flow.Identifier{flow.HashToID([]byte{1, 2, 3}), flow.HashToID([]byte{3, 2, 1})},
    89  	}
    90  
    91  	pks := unittest.PublicKeysFixture(2, crypto.BLSBLS12381)
    92  
    93  	t.Run("empty are equal", func(t *testing.T) {
    94  		a := &flow.EpochCommit{}
    95  		b := &flow.EpochCommit{}
    96  
    97  		require.True(t, a.EqualTo(b))
    98  		require.True(t, b.EqualTo(a))
    99  	})
   100  
   101  	t.Run("counter diff", func(t *testing.T) {
   102  		a := &flow.EpochCommit{Counter: 1}
   103  		b := &flow.EpochCommit{Counter: 2}
   104  
   105  		require.False(t, a.EqualTo(b))
   106  		require.False(t, b.EqualTo(a))
   107  	})
   108  
   109  	t.Run("ClusterQCs diffs", func(t *testing.T) {
   110  		a := &flow.EpochCommit{ClusterQCs: []flow.ClusterQCVoteData{qcA}}
   111  		b := &flow.EpochCommit{ClusterQCs: []flow.ClusterQCVoteData{qcB}}
   112  
   113  		require.False(t, a.EqualTo(b))
   114  		require.False(t, b.EqualTo(a))
   115  	})
   116  
   117  	t.Run("ClusterQCs different lengths", func(t *testing.T) {
   118  		a := &flow.EpochCommit{ClusterQCs: []flow.ClusterQCVoteData{qcA}}
   119  		b := &flow.EpochCommit{ClusterQCs: []flow.ClusterQCVoteData{}}
   120  
   121  		require.False(t, a.EqualTo(b))
   122  		require.False(t, b.EqualTo(a))
   123  	})
   124  
   125  	t.Run("ClusterQCs same", func(t *testing.T) {
   126  
   127  		a := &flow.EpochCommit{ClusterQCs: []flow.ClusterQCVoteData{qcA}}
   128  		b := &flow.EpochCommit{ClusterQCs: []flow.ClusterQCVoteData{qcA}}
   129  
   130  		require.True(t, a.EqualTo(b))
   131  		require.True(t, b.EqualTo(a))
   132  	})
   133  
   134  	t.Run("DKGGroupKey same", func(t *testing.T) {
   135  
   136  		a := &flow.EpochCommit{DKGGroupKey: pks[1]}
   137  		b := &flow.EpochCommit{DKGGroupKey: pks[1]}
   138  
   139  		require.True(t, a.EqualTo(b))
   140  		require.True(t, b.EqualTo(a))
   141  	})
   142  
   143  	t.Run("DKGGroupKey diff", func(t *testing.T) {
   144  
   145  		a := &flow.EpochCommit{DKGGroupKey: pks[1]}
   146  		b := &flow.EpochCommit{DKGGroupKey: pks[0]}
   147  
   148  		require.False(t, a.EqualTo(b))
   149  		require.False(t, b.EqualTo(a))
   150  	})
   151  
   152  	t.Run("DKGParticipantKeys different length", func(t *testing.T) {
   153  
   154  		a := &flow.EpochCommit{DKGParticipantKeys: []crypto.PublicKey{}}
   155  		b := &flow.EpochCommit{DKGParticipantKeys: []crypto.PublicKey{pks[0]}}
   156  
   157  		require.False(t, a.EqualTo(b))
   158  		require.False(t, b.EqualTo(a))
   159  	})
   160  
   161  	t.Run("DKGParticipantKeys different data", func(t *testing.T) {
   162  
   163  		a := &flow.EpochCommit{DKGParticipantKeys: []crypto.PublicKey{pks[1]}}
   164  		b := &flow.EpochCommit{DKGParticipantKeys: []crypto.PublicKey{pks[0]}}
   165  
   166  		require.False(t, a.EqualTo(b))
   167  		require.False(t, b.EqualTo(a))
   168  	})
   169  
   170  	t.Run("DKGParticipantKeys same data", func(t *testing.T) {
   171  
   172  		a := &flow.EpochCommit{DKGParticipantKeys: []crypto.PublicKey{pks[1]}}
   173  		b := &flow.EpochCommit{DKGParticipantKeys: []crypto.PublicKey{pks[1]}}
   174  
   175  		require.True(t, a.EqualTo(b))
   176  		require.True(t, b.EqualTo(a))
   177  	})
   178  }
   179  
   180  func TestEpochSetup_EqualTo(t *testing.T) {
   181  
   182  	identityA := &unittest.IdentityFixture().IdentitySkeleton
   183  	identityB := &unittest.IdentityFixture().IdentitySkeleton
   184  
   185  	assignmentA := flow.AssignmentList{[]flow.Identifier{[32]byte{1, 2, 3}, [32]byte{2, 2, 2}}}
   186  	assignmentB := flow.AssignmentList{[]flow.Identifier{[32]byte{1, 2, 3}, [32]byte{}}}
   187  
   188  	t.Run("empty are the same", func(t *testing.T) {
   189  		a := &flow.EpochSetup{}
   190  		b := &flow.EpochSetup{}
   191  
   192  		require.True(t, a.EqualTo(b))
   193  		require.True(t, b.EqualTo(a))
   194  	})
   195  
   196  	t.Run("Counter", func(t *testing.T) {
   197  		a := &flow.EpochSetup{Counter: 1}
   198  		b := &flow.EpochSetup{Counter: 2}
   199  
   200  		require.False(t, a.EqualTo(b))
   201  		require.False(t, b.EqualTo(a))
   202  	})
   203  
   204  	t.Run("FirstView", func(t *testing.T) {
   205  		a := &flow.EpochSetup{FirstView: 1}
   206  		b := &flow.EpochSetup{FirstView: 2}
   207  
   208  		require.False(t, a.EqualTo(b))
   209  		require.False(t, b.EqualTo(a))
   210  	})
   211  
   212  	t.Run("DKGPhase1FinalView", func(t *testing.T) {
   213  		a := &flow.EpochSetup{DKGPhase1FinalView: 1}
   214  		b := &flow.EpochSetup{DKGPhase1FinalView: 2}
   215  
   216  		require.False(t, a.EqualTo(b))
   217  		require.False(t, b.EqualTo(a))
   218  	})
   219  
   220  	t.Run("DKGPhase2FinalView", func(t *testing.T) {
   221  		a := &flow.EpochSetup{DKGPhase2FinalView: 1}
   222  		b := &flow.EpochSetup{DKGPhase2FinalView: 2}
   223  
   224  		require.False(t, a.EqualTo(b))
   225  		require.False(t, b.EqualTo(a))
   226  	})
   227  
   228  	t.Run("DKGPhase3FinalView", func(t *testing.T) {
   229  		a := &flow.EpochSetup{DKGPhase3FinalView: 1}
   230  		b := &flow.EpochSetup{DKGPhase3FinalView: 2}
   231  
   232  		require.False(t, a.EqualTo(b))
   233  		require.False(t, b.EqualTo(a))
   234  	})
   235  
   236  	t.Run("FinalView", func(t *testing.T) {
   237  		a := &flow.EpochSetup{FinalView: 1}
   238  		b := &flow.EpochSetup{FinalView: 2}
   239  
   240  		require.False(t, a.EqualTo(b))
   241  		require.False(t, b.EqualTo(a))
   242  	})
   243  
   244  	t.Run("Participants length differ", func(t *testing.T) {
   245  
   246  		a := &flow.EpochSetup{Participants: flow.IdentitySkeletonList{identityA}}
   247  		b := &flow.EpochSetup{Participants: flow.IdentitySkeletonList{}}
   248  
   249  		require.False(t, a.EqualTo(b))
   250  		require.False(t, b.EqualTo(a))
   251  	})
   252  
   253  	t.Run("Participants length same but different data", func(t *testing.T) {
   254  
   255  		a := &flow.EpochSetup{Participants: flow.IdentitySkeletonList{identityA}}
   256  		b := &flow.EpochSetup{Participants: flow.IdentitySkeletonList{identityB}}
   257  
   258  		require.False(t, a.EqualTo(b))
   259  		require.False(t, b.EqualTo(a))
   260  	})
   261  
   262  	t.Run("Participants length same with same data", func(t *testing.T) {
   263  
   264  		a := &flow.EpochSetup{Participants: flow.IdentitySkeletonList{identityA}}
   265  		b := &flow.EpochSetup{Participants: flow.IdentitySkeletonList{identityA}}
   266  
   267  		require.True(t, a.EqualTo(b))
   268  		require.True(t, b.EqualTo(a))
   269  	})
   270  
   271  	t.Run("Assignments different", func(t *testing.T) {
   272  
   273  		a := &flow.EpochSetup{Assignments: assignmentA}
   274  		b := &flow.EpochSetup{Assignments: assignmentB}
   275  
   276  		require.False(t, a.EqualTo(b))
   277  		require.False(t, b.EqualTo(a))
   278  	})
   279  
   280  	t.Run("Assignments same", func(t *testing.T) {
   281  
   282  		a := &flow.EpochSetup{Assignments: assignmentB}
   283  		b := &flow.EpochSetup{Assignments: assignmentB}
   284  
   285  		require.True(t, a.EqualTo(b))
   286  		require.True(t, b.EqualTo(a))
   287  	})
   288  
   289  	t.Run("RandomSource same", func(t *testing.T) {
   290  
   291  		a := &flow.EpochSetup{RandomSource: []byte{1, 2, 3}}
   292  		b := &flow.EpochSetup{RandomSource: []byte{1, 2, 3}}
   293  
   294  		require.True(t, a.EqualTo(b))
   295  		require.True(t, b.EqualTo(a))
   296  	})
   297  
   298  	t.Run("RandomSource diff", func(t *testing.T) {
   299  
   300  		a := &flow.EpochSetup{RandomSource: []byte{1, 2, 3}}
   301  		b := &flow.EpochSetup{RandomSource: []byte{1}}
   302  
   303  		require.False(t, a.EqualTo(b))
   304  		require.False(t, b.EqualTo(a))
   305  	})
   306  }