github.com/prysmaticlabs/prysm@v1.4.4/shared/copyutil/cloners.go (about) 1 package copyutil 2 3 import ( 4 pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" 5 ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" 6 prysmv2 "github.com/prysmaticlabs/prysm/proto/prysm/v2" 7 "github.com/prysmaticlabs/prysm/shared/bytesutil" 8 ) 9 10 // CopyETH1Data copies the provided eth1data object. 11 func CopyETH1Data(data *ethpb.Eth1Data) *ethpb.Eth1Data { 12 if data == nil { 13 return nil 14 } 15 return ðpb.Eth1Data{ 16 DepositRoot: bytesutil.SafeCopyBytes(data.DepositRoot), 17 DepositCount: data.DepositCount, 18 BlockHash: bytesutil.SafeCopyBytes(data.BlockHash), 19 } 20 } 21 22 // CopyPendingAttestation copies the provided pending attestation object. 23 func CopyPendingAttestation(att *pbp2p.PendingAttestation) *pbp2p.PendingAttestation { 24 if att == nil { 25 return nil 26 } 27 data := CopyAttestationData(att.Data) 28 return &pbp2p.PendingAttestation{ 29 AggregationBits: bytesutil.SafeCopyBytes(att.AggregationBits), 30 Data: data, 31 InclusionDelay: att.InclusionDelay, 32 ProposerIndex: att.ProposerIndex, 33 } 34 } 35 36 // CopyAttestation copies the provided attestation object. 37 func CopyAttestation(att *ethpb.Attestation) *ethpb.Attestation { 38 if att == nil { 39 return nil 40 } 41 return ðpb.Attestation{ 42 AggregationBits: bytesutil.SafeCopyBytes(att.AggregationBits), 43 Data: CopyAttestationData(att.Data), 44 Signature: bytesutil.SafeCopyBytes(att.Signature), 45 } 46 } 47 48 // CopyAttestationData copies the provided AttestationData object. 49 func CopyAttestationData(attData *ethpb.AttestationData) *ethpb.AttestationData { 50 if attData == nil { 51 return nil 52 } 53 return ðpb.AttestationData{ 54 Slot: attData.Slot, 55 CommitteeIndex: attData.CommitteeIndex, 56 BeaconBlockRoot: bytesutil.SafeCopyBytes(attData.BeaconBlockRoot), 57 Source: CopyCheckpoint(attData.Source), 58 Target: CopyCheckpoint(attData.Target), 59 } 60 } 61 62 // CopyCheckpoint copies the provided checkpoint. 63 func CopyCheckpoint(cp *ethpb.Checkpoint) *ethpb.Checkpoint { 64 if cp == nil { 65 return nil 66 } 67 return ðpb.Checkpoint{ 68 Epoch: cp.Epoch, 69 Root: bytesutil.SafeCopyBytes(cp.Root), 70 } 71 } 72 73 // CopySignedBeaconBlock copies the provided SignedBeaconBlock. 74 func CopySignedBeaconBlock(sigBlock *ethpb.SignedBeaconBlock) *ethpb.SignedBeaconBlock { 75 if sigBlock == nil { 76 return nil 77 } 78 return ðpb.SignedBeaconBlock{ 79 Block: CopyBeaconBlock(sigBlock.Block), 80 Signature: bytesutil.SafeCopyBytes(sigBlock.Signature), 81 } 82 } 83 84 // CopyBeaconBlock copies the provided BeaconBlock. 85 func CopyBeaconBlock(block *ethpb.BeaconBlock) *ethpb.BeaconBlock { 86 if block == nil { 87 return nil 88 } 89 return ðpb.BeaconBlock{ 90 Slot: block.Slot, 91 ProposerIndex: block.ProposerIndex, 92 ParentRoot: bytesutil.SafeCopyBytes(block.ParentRoot), 93 StateRoot: bytesutil.SafeCopyBytes(block.StateRoot), 94 Body: CopyBeaconBlockBody(block.Body), 95 } 96 } 97 98 // CopyBeaconBlockBody copies the provided BeaconBlockBody. 99 func CopyBeaconBlockBody(body *ethpb.BeaconBlockBody) *ethpb.BeaconBlockBody { 100 if body == nil { 101 return nil 102 } 103 return ðpb.BeaconBlockBody{ 104 RandaoReveal: bytesutil.SafeCopyBytes(body.RandaoReveal), 105 Eth1Data: CopyETH1Data(body.Eth1Data), 106 Graffiti: bytesutil.SafeCopyBytes(body.Graffiti), 107 ProposerSlashings: CopyProposerSlashings(body.ProposerSlashings), 108 AttesterSlashings: CopyAttesterSlashings(body.AttesterSlashings), 109 Attestations: CopyAttestations(body.Attestations), 110 Deposits: CopyDeposits(body.Deposits), 111 VoluntaryExits: CopySignedVoluntaryExits(body.VoluntaryExits), 112 } 113 } 114 115 // CopyProposerSlashings copies the provided ProposerSlashing array. 116 func CopyProposerSlashings(slashings []*ethpb.ProposerSlashing) []*ethpb.ProposerSlashing { 117 if slashings == nil { 118 return nil 119 } 120 newSlashings := make([]*ethpb.ProposerSlashing, len(slashings)) 121 for i, att := range slashings { 122 newSlashings[i] = CopyProposerSlashing(att) 123 } 124 return newSlashings 125 } 126 127 // CopyProposerSlashing copies the provided ProposerSlashing. 128 func CopyProposerSlashing(slashing *ethpb.ProposerSlashing) *ethpb.ProposerSlashing { 129 if slashing == nil { 130 return nil 131 } 132 return ðpb.ProposerSlashing{ 133 Header_1: CopySignedBeaconBlockHeader(slashing.Header_1), 134 Header_2: CopySignedBeaconBlockHeader(slashing.Header_2), 135 } 136 } 137 138 // CopySignedBeaconBlockHeader copies the provided SignedBeaconBlockHeader. 139 func CopySignedBeaconBlockHeader(header *ethpb.SignedBeaconBlockHeader) *ethpb.SignedBeaconBlockHeader { 140 if header == nil { 141 return nil 142 } 143 return ðpb.SignedBeaconBlockHeader{ 144 Header: CopyBeaconBlockHeader(header.Header), 145 Signature: bytesutil.SafeCopyBytes(header.Signature), 146 } 147 } 148 149 // CopyBeaconBlockHeader copies the provided BeaconBlockHeader. 150 func CopyBeaconBlockHeader(header *ethpb.BeaconBlockHeader) *ethpb.BeaconBlockHeader { 151 if header == nil { 152 return nil 153 } 154 parentRoot := bytesutil.SafeCopyBytes(header.ParentRoot) 155 stateRoot := bytesutil.SafeCopyBytes(header.StateRoot) 156 bodyRoot := bytesutil.SafeCopyBytes(header.BodyRoot) 157 return ðpb.BeaconBlockHeader{ 158 Slot: header.Slot, 159 ProposerIndex: header.ProposerIndex, 160 ParentRoot: parentRoot, 161 StateRoot: stateRoot, 162 BodyRoot: bodyRoot, 163 } 164 } 165 166 // CopyAttesterSlashings copies the provided AttesterSlashings array. 167 func CopyAttesterSlashings(slashings []*ethpb.AttesterSlashing) []*ethpb.AttesterSlashing { 168 if slashings == nil { 169 return nil 170 } 171 newSlashings := make([]*ethpb.AttesterSlashing, len(slashings)) 172 for i, slashing := range slashings { 173 newSlashings[i] = ðpb.AttesterSlashing{ 174 Attestation_1: CopyIndexedAttestation(slashing.Attestation_1), 175 Attestation_2: CopyIndexedAttestation(slashing.Attestation_2), 176 } 177 } 178 return newSlashings 179 } 180 181 // CopyIndexedAttestation copies the provided IndexedAttestation. 182 func CopyIndexedAttestation(indexedAtt *ethpb.IndexedAttestation) *ethpb.IndexedAttestation { 183 var indices []uint64 184 if indexedAtt == nil { 185 return nil 186 } else if indexedAtt.AttestingIndices != nil { 187 indices = make([]uint64, len(indexedAtt.AttestingIndices)) 188 copy(indices, indexedAtt.AttestingIndices) 189 } 190 return ðpb.IndexedAttestation{ 191 AttestingIndices: indices, 192 Data: CopyAttestationData(indexedAtt.Data), 193 Signature: bytesutil.SafeCopyBytes(indexedAtt.Signature), 194 } 195 } 196 197 // CopyAttestations copies the provided Attestation array. 198 func CopyAttestations(attestations []*ethpb.Attestation) []*ethpb.Attestation { 199 if attestations == nil { 200 return nil 201 } 202 newAttestations := make([]*ethpb.Attestation, len(attestations)) 203 for i, att := range attestations { 204 newAttestations[i] = CopyAttestation(att) 205 } 206 return newAttestations 207 } 208 209 // CopyDeposits copies the provided deposit array. 210 func CopyDeposits(deposits []*ethpb.Deposit) []*ethpb.Deposit { 211 if deposits == nil { 212 return nil 213 } 214 newDeposits := make([]*ethpb.Deposit, len(deposits)) 215 for i, dep := range deposits { 216 newDeposits[i] = CopyDeposit(dep) 217 } 218 return newDeposits 219 } 220 221 // CopyDeposit copies the provided deposit. 222 func CopyDeposit(deposit *ethpb.Deposit) *ethpb.Deposit { 223 if deposit == nil { 224 return nil 225 } 226 return ðpb.Deposit{ 227 Proof: bytesutil.Copy2dBytes(deposit.Proof), 228 Data: CopyDepositData(deposit.Data), 229 } 230 } 231 232 // CopyDepositData copies the provided deposit data. 233 func CopyDepositData(depData *ethpb.Deposit_Data) *ethpb.Deposit_Data { 234 if depData == nil { 235 return nil 236 } 237 return ðpb.Deposit_Data{ 238 PublicKey: bytesutil.SafeCopyBytes(depData.PublicKey), 239 WithdrawalCredentials: bytesutil.SafeCopyBytes(depData.WithdrawalCredentials), 240 Amount: depData.Amount, 241 Signature: bytesutil.SafeCopyBytes(depData.Signature), 242 } 243 } 244 245 // CopySignedVoluntaryExits copies the provided SignedVoluntaryExits array. 246 func CopySignedVoluntaryExits(exits []*ethpb.SignedVoluntaryExit) []*ethpb.SignedVoluntaryExit { 247 if exits == nil { 248 return nil 249 } 250 newExits := make([]*ethpb.SignedVoluntaryExit, len(exits)) 251 for i, exit := range exits { 252 newExits[i] = CopySignedVoluntaryExit(exit) 253 } 254 return newExits 255 } 256 257 // CopySignedVoluntaryExit copies the provided SignedVoluntaryExit. 258 func CopySignedVoluntaryExit(exit *ethpb.SignedVoluntaryExit) *ethpb.SignedVoluntaryExit { 259 if exit == nil { 260 return nil 261 } 262 return ðpb.SignedVoluntaryExit{ 263 Exit: ðpb.VoluntaryExit{ 264 Epoch: exit.Exit.Epoch, 265 ValidatorIndex: exit.Exit.ValidatorIndex, 266 }, 267 Signature: bytesutil.SafeCopyBytes(exit.Signature), 268 } 269 } 270 271 // CopyValidator copies the provided validator. 272 func CopyValidator(val *ethpb.Validator) *ethpb.Validator { 273 pubKey := make([]byte, len(val.PublicKey)) 274 copy(pubKey, val.PublicKey) 275 withdrawalCreds := make([]byte, len(val.WithdrawalCredentials)) 276 copy(withdrawalCreds, val.WithdrawalCredentials) 277 return ðpb.Validator{ 278 PublicKey: pubKey, 279 WithdrawalCredentials: withdrawalCreds, 280 EffectiveBalance: val.EffectiveBalance, 281 Slashed: val.Slashed, 282 ActivationEligibilityEpoch: val.ActivationEligibilityEpoch, 283 ActivationEpoch: val.ActivationEpoch, 284 ExitEpoch: val.ExitEpoch, 285 WithdrawableEpoch: val.WithdrawableEpoch, 286 } 287 } 288 289 // CopySyncCommitteeContribution copies the provided sync committee contribution object. 290 func CopySyncCommitteeContribution(c *prysmv2.SyncCommitteeContribution) *prysmv2.SyncCommitteeContribution { 291 if c == nil { 292 return nil 293 } 294 return &prysmv2.SyncCommitteeContribution{ 295 Slot: c.Slot, 296 BlockRoot: bytesutil.SafeCopyBytes(c.BlockRoot), 297 SubcommitteeIndex: c.SubcommitteeIndex, 298 AggregationBits: bytesutil.SafeCopyBytes(c.AggregationBits), 299 Signature: bytesutil.SafeCopyBytes(c.Signature), 300 } 301 }