github.com/koko1123/flow-go-1@v0.29.6/state/protocol/badger/validity_test.go (about) 1 package badger 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/onflow/flow-go/crypto" 10 "github.com/koko1123/flow-go-1/model/flow" 11 "github.com/koko1123/flow-go-1/model/flow/filter" 12 "github.com/koko1123/flow-go-1/utils/unittest" 13 ) 14 15 var participants = unittest.IdentityListFixture(20, unittest.WithAllRoles()) 16 17 func TestEpochSetupValidity(t *testing.T) { 18 t.Run("invalid first/final view", func(t *testing.T) { 19 _, result, _ := unittest.BootstrapFixture(participants) 20 setup := result.ServiceEvents[0].Event.(*flow.EpochSetup) 21 // set an invalid final view for the first epoch 22 setup.FinalView = setup.FirstView 23 24 err := isValidEpochSetup(setup) 25 require.Error(t, err) 26 }) 27 28 t.Run("non-canonically ordered identities", func(t *testing.T) { 29 _, result, _ := unittest.BootstrapFixture(participants) 30 setup := result.ServiceEvents[0].Event.(*flow.EpochSetup) 31 // randomly shuffle the identities so they are not canonically ordered 32 setup.Participants = setup.Participants.DeterministicShuffle(time.Now().UnixNano()) 33 34 err := isValidEpochSetup(setup) 35 require.Error(t, err) 36 }) 37 38 t.Run("invalid cluster assignments", func(t *testing.T) { 39 _, result, _ := unittest.BootstrapFixture(participants) 40 setup := result.ServiceEvents[0].Event.(*flow.EpochSetup) 41 // create an invalid cluster assignment (node appears in multiple clusters) 42 collector := participants.Filter(filter.HasRole(flow.RoleCollection))[0] 43 setup.Assignments = append(setup.Assignments, []flow.Identifier{collector.NodeID}) 44 45 err := isValidEpochSetup(setup) 46 require.Error(t, err) 47 }) 48 49 t.Run("short seed", func(t *testing.T) { 50 _, result, _ := unittest.BootstrapFixture(participants) 51 setup := result.ServiceEvents[0].Event.(*flow.EpochSetup) 52 setup.RandomSource = unittest.SeedFixture(crypto.SeedMinLenDKG - 1) 53 54 err := isValidEpochSetup(setup) 55 require.Error(t, err) 56 }) 57 } 58 59 func TestBootstrapInvalidEpochCommit(t *testing.T) { 60 t.Run("inconsistent counter", func(t *testing.T) { 61 _, result, _ := unittest.BootstrapFixture(participants) 62 setup := result.ServiceEvents[0].Event.(*flow.EpochSetup) 63 commit := result.ServiceEvents[1].Event.(*flow.EpochCommit) 64 // use a different counter for the commit 65 commit.Counter = setup.Counter + 1 66 67 err := isValidEpochCommit(commit, setup) 68 require.Error(t, err) 69 }) 70 71 t.Run("inconsistent cluster QCs", func(t *testing.T) { 72 _, result, _ := unittest.BootstrapFixture(participants) 73 setup := result.ServiceEvents[0].Event.(*flow.EpochSetup) 74 commit := result.ServiceEvents[1].Event.(*flow.EpochCommit) 75 // add an extra QC to commit 76 extraQC := unittest.QuorumCertificateWithSignerIDsFixture() 77 commit.ClusterQCs = append(commit.ClusterQCs, flow.ClusterQCVoteDataFromQC(extraQC)) 78 79 err := isValidEpochCommit(commit, setup) 80 require.Error(t, err) 81 }) 82 83 t.Run("missing dkg group key", func(t *testing.T) { 84 _, result, _ := unittest.BootstrapFixture(participants) 85 setup := result.ServiceEvents[0].Event.(*flow.EpochSetup) 86 commit := result.ServiceEvents[1].Event.(*flow.EpochCommit) 87 commit.DKGGroupKey = nil 88 89 err := isValidEpochCommit(commit, setup) 90 require.Error(t, err) 91 }) 92 93 t.Run("inconsistent DKG participants", func(t *testing.T) { 94 _, result, _ := unittest.BootstrapFixture(participants) 95 setup := result.ServiceEvents[0].Event.(*flow.EpochSetup) 96 commit := result.ServiceEvents[1].Event.(*flow.EpochCommit) 97 // add an extra DKG participant key 98 commit.DKGParticipantKeys = append(commit.DKGParticipantKeys, unittest.KeyFixture(crypto.BLSBLS12381).PublicKey()) 99 100 err := isValidEpochCommit(commit, setup) 101 require.Error(t, err) 102 }) 103 }