github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/cmd/bootstrap/run/qc_test.go (about) 1 package run 2 3 import ( 4 "crypto/rand" 5 "testing" 6 7 "github.com/onflow/crypto" 8 "github.com/stretchr/testify/require" 9 10 "github.com/onflow/flow-go/model/bootstrap" 11 "github.com/onflow/flow-go/model/flow" 12 "github.com/onflow/flow-go/module/signature" 13 "github.com/onflow/flow-go/utils/unittest" 14 ) 15 16 func TestGenerateRootQC(t *testing.T) { 17 participantData := createSignerData(t, 3) 18 19 block := unittest.GenesisFixture() 20 21 votes, err := GenerateRootBlockVotes(block, participantData) 22 require.NoError(t, err) 23 24 _, invalid, err := GenerateRootQC(block, votes, participantData, participantData.Identities()) 25 require.NoError(t, err) 26 require.Len(t, invalid, 0) // no invalid votes 27 } 28 29 func TestGenerateRootQCWithSomeInvalidVotes(t *testing.T) { 30 participantData := createSignerData(t, 10) 31 32 block := unittest.GenesisFixture() 33 34 votes, err := GenerateRootBlockVotes(block, participantData) 35 require.NoError(t, err) 36 37 // make 2 votes invalid 38 votes[0].SigData = unittest.SignatureFixture() // make invalid signature 39 votes[1].SignerID = unittest.IdentifierFixture() // make invalid signer 40 41 _, invalid, err := GenerateRootQC(block, votes, participantData, participantData.Identities()) 42 require.NoError(t, err) 43 require.Len(t, invalid, 2) // 2 invalid votes 44 } 45 46 func createSignerData(t *testing.T, n int) *ParticipantData { 47 identities := unittest.IdentityListFixture(n).Sort(flow.Canonical[flow.Identity]) 48 49 networkingKeys := unittest.NetworkingKeys(n) 50 stakingKeys := unittest.StakingKeys(n) 51 52 seed := make([]byte, crypto.KeyGenSeedMinLen) 53 _, err := rand.Read(seed) 54 require.NoError(t, err) 55 randomBSKs, randomBPKs, groupKey, err := crypto.BLSThresholdKeyGen(n, 56 signature.RandomBeaconThreshold(n), seed) 57 require.NoError(t, err) 58 59 participantLookup := make(map[flow.Identifier]flow.DKGParticipant) 60 participants := make([]Participant, n) 61 62 for i, identity := range identities { 63 64 // add to lookup 65 lookupParticipant := flow.DKGParticipant{ 66 Index: uint(i), 67 KeyShare: randomBPKs[i], 68 } 69 participantLookup[identity.NodeID] = lookupParticipant 70 71 // add to participant list 72 nodeInfo := bootstrap.NewPrivateNodeInfo( 73 identity.NodeID, 74 identity.Role, 75 identity.Address, 76 identity.InitialWeight, 77 networkingKeys[i], 78 stakingKeys[i], 79 ) 80 participants[i] = Participant{ 81 NodeInfo: nodeInfo, 82 RandomBeaconPrivKey: randomBSKs[i], 83 } 84 } 85 86 participantData := &ParticipantData{ 87 Participants: participants, 88 Lookup: participantLookup, 89 GroupKey: groupKey, 90 } 91 92 return participantData 93 }