github.com/prysmaticlabs/prysm@v1.4.4/validator/testing/protection_history.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 6 types "github.com/prysmaticlabs/eth2-types" 7 "github.com/prysmaticlabs/prysm/shared/bls" 8 "github.com/prysmaticlabs/prysm/shared/bytesutil" 9 "github.com/prysmaticlabs/prysm/shared/params" 10 "github.com/prysmaticlabs/prysm/shared/rand" 11 "github.com/prysmaticlabs/prysm/validator/db/kv" 12 "github.com/prysmaticlabs/prysm/validator/slashing-protection/local/standard-protection-format/format" 13 ) 14 15 // MockSlashingProtectionJSON creates a mock, full slashing protection JSON struct 16 // using attesting and proposing histories provided. 17 func MockSlashingProtectionJSON( 18 publicKeys [][48]byte, 19 attestingHistories [][]*kv.AttestationRecord, 20 proposalHistories []kv.ProposalHistoryForPubkey, 21 ) (*format.EIPSlashingProtectionFormat, error) { 22 standardProtectionFormat := &format.EIPSlashingProtectionFormat{} 23 standardProtectionFormat.Metadata.GenesisValidatorsRoot = fmt.Sprintf("%#x", bytesutil.PadTo([]byte{32}, 32)) 24 standardProtectionFormat.Metadata.InterchangeFormatVersion = format.InterchangeFormatVersion 25 for i := 0; i < len(publicKeys); i++ { 26 data := &format.ProtectionData{ 27 Pubkey: fmt.Sprintf("%#x", publicKeys[i]), 28 } 29 if len(attestingHistories) > 0 { 30 for _, att := range attestingHistories[i] { 31 data.SignedAttestations = append(data.SignedAttestations, &format.SignedAttestation{ 32 TargetEpoch: fmt.Sprintf("%d", att.Target), 33 SourceEpoch: fmt.Sprintf("%d", att.Source), 34 SigningRoot: fmt.Sprintf("%#x", att.SigningRoot), 35 }) 36 } 37 } 38 if len(proposalHistories) > 0 { 39 for _, proposal := range proposalHistories[i].Proposals { 40 block := &format.SignedBlock{ 41 Slot: fmt.Sprintf("%d", proposal.Slot), 42 SigningRoot: fmt.Sprintf("%#x", proposal.SigningRoot), 43 } 44 data.SignedBlocks = append(data.SignedBlocks, block) 45 } 46 } 47 standardProtectionFormat.Data = append(standardProtectionFormat.Data, data) 48 } 49 return standardProtectionFormat, nil 50 } 51 52 // MockAttestingAndProposalHistories given a number of validators, creates mock attesting 53 // and proposing histories within WEAK_SUBJECTIVITY_PERIOD bounds. 54 func MockAttestingAndProposalHistories(pubkeys [][48]byte) ([][]*kv.AttestationRecord, []kv.ProposalHistoryForPubkey) { 55 // deduplicate and transform them into our internal format. 56 numValidators := len(pubkeys) 57 attData := make([][]*kv.AttestationRecord, numValidators) 58 proposalData := make([]kv.ProposalHistoryForPubkey, numValidators) 59 gen := rand.NewGenerator() 60 for v := 0; v < numValidators; v++ { 61 latestTarget := types.Epoch(gen.Intn(int(params.BeaconConfig().WeakSubjectivityPeriod) / 1000)) 62 // If 0, we change the value to 1 as the we compute source by doing (target-1) 63 // to prevent any underflows in this setup helper. 64 if latestTarget == 0 { 65 latestTarget = 1 66 } 67 historicalAtts := make([]*kv.AttestationRecord, 0) 68 proposals := make([]kv.Proposal, 0) 69 for i := types.Epoch(1); i < latestTarget; i++ { 70 signingRoot := [32]byte{} 71 signingRootStr := fmt.Sprintf("%d", i) 72 copy(signingRoot[:], signingRootStr) 73 historicalAtts = append(historicalAtts, &kv.AttestationRecord{ 74 Source: i - 1, 75 Target: i, 76 SigningRoot: signingRoot, 77 PubKey: pubkeys[v], 78 }) 79 } 80 for i := types.Epoch(1); i <= latestTarget; i++ { 81 signingRoot := [32]byte{} 82 signingRootStr := fmt.Sprintf("%d", i) 83 copy(signingRoot[:], signingRootStr) 84 proposals = append(proposals, kv.Proposal{ 85 Slot: types.Slot(i), 86 SigningRoot: signingRoot[:], 87 }) 88 } 89 proposalData[v] = kv.ProposalHistoryForPubkey{Proposals: proposals} 90 attData[v] = historicalAtts 91 } 92 return attData, proposalData 93 } 94 95 // CreateRandomPubKeys -- 96 func CreateRandomPubKeys(numValidators int) ([][48]byte, error) { 97 pubKeys := make([][48]byte, numValidators) 98 for i := 0; i < numValidators; i++ { 99 randKey, err := bls.RandKey() 100 if err != nil { 101 return nil, err 102 } 103 copy(pubKeys[i][:], randKey.PublicKey().Marshal()) 104 } 105 return pubKeys, nil 106 } 107 108 // CreateMockRoots -- 109 func CreateMockRoots(numRoots int) [][32]byte { 110 roots := make([][32]byte, numRoots) 111 for i := 0; i < numRoots; i++ { 112 var rt [32]byte 113 copy(rt[:], fmt.Sprintf("%d", i)) 114 } 115 return roots 116 }