/
cloners.go
336 lines (307 loc) · 9.44 KB
/
cloners.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package pbc
import (
"google.golang.org/protobuf/proto"
)
// CopyAttestation copies the provided attestation object.
func CopyAttestation(att *Attestation) *Attestation {
if att == nil {
return nil
}
return &Attestation{
AggregationBits: safeCopyBytes(att.AggregationBits),
Data: CopyAttestationData(att.Data),
Signature: safeCopyBytes(att.Signature),
}
}
// CopyAttestationData copies the provided AttestationData object.
func CopyAttestationData(attData *AttestationData) *AttestationData {
if attData == nil {
return nil
}
return &AttestationData{
Slot: attData.Slot,
CommitteeIndex: attData.CommitteeIndex,
BeaconBlockRoot: safeCopyBytes(attData.BeaconBlockRoot),
Source: CopyCheckpoint(attData.Source),
Target: CopyCheckpoint(attData.Target),
}
}
// CopyCheckpoint copies the provided checkpoint.
func CopyCheckpoint(cp *Checkpoint) *Checkpoint {
if cp == nil {
return nil
}
return &Checkpoint{
Epoch: cp.Epoch,
Root: safeCopyBytes(cp.Root),
}
}
// CopySignedBlock copies the provided SignedBlock.
func CopySignedBlock(sigBlock *SignedBlock) *SignedBlock {
if sigBlock == nil {
return nil
}
return &SignedBlock{
Block: CopyBlock(sigBlock.Block),
Signature: safeCopyBytes(sigBlock.Signature),
}
}
// CopyBlock copies the provided Block.
func CopyBlock(block *Block) *Block {
if block == nil {
return nil
}
return &Block{
Slot: block.Slot,
ProposerIndex: block.ProposerIndex,
ParentRoot: safeCopyBytes(block.ParentRoot),
StateRoot: safeCopyBytes(block.StateRoot),
Body: CopyBlockBody(block.Body),
}
}
// CopyBlockBody copies the provided BlockBody.
func CopyBlockBody(body *BlockBody) *BlockBody {
if body == nil {
return nil
}
return &BlockBody{
RandaoReveal: safeCopyBytes(body.RandaoReveal),
Graffiti: safeCopyBytes(body.Graffiti),
Txs: CopySignedTxs(body.Txs),
ProposerSlashings: CopyProposerSlashings(body.ProposerSlashings),
AttesterSlashings: CopyAttesterSlashings(body.AttesterSlashings),
Attestations: CopyAttestations(body.Attestations),
}
}
// CopySignedTxs copies the provided SignedTransaction array.
func CopySignedTxs(txs []*SignedTransaction) []*SignedTransaction {
if txs == nil {
return nil
}
newTxs := make([]*SignedTransaction, len(txs))
for i, tx := range txs {
newTxs[i] = CopySignedTx(tx)
}
return newTxs
}
// CopySignedTx copies the provided SignedTransaction.
func CopySignedTx(tx *SignedTransaction) *SignedTransaction {
if tx == nil {
return nil
}
return &SignedTransaction{
Tx: CopyTx(tx.Tx),
Signature: safeCopyBytes(tx.Signature),
}
}
// CopyTxs copies the provided Transaction array.
func CopyTxs(txs []*Transaction) []*Transaction {
if txs == nil {
return nil
}
newTxs := make([]*Transaction, len(txs))
for i, tx := range txs {
newTxs[i] = CopyTx(tx)
}
return newTxs
}
// CopyTx copies the provided Transaction.
func CopyTx(tx *Transaction) *Transaction {
if tx == nil {
return nil
}
var bf *TxDataBalanceTransfer
if tx.TxDataBalanceTransfer != nil {
bf = &TxDataBalanceTransfer{
To: safeCopyBytes(tx.TxDataBalanceTransfer.To),
Amount: tx.TxDataBalanceTransfer.Amount,
}
}
var vd *TxDataValidatorDeposit
if tx.TxDataValidatorDeposit != nil {
vd = &TxDataValidatorDeposit{
Amount: tx.TxDataValidatorDeposit.Amount,
}
}
var ad *TxDataAuditorDeposit
if tx.TxDataAuditorDeposit != nil {
ad = &TxDataAuditorDeposit{
Amount: tx.TxDataAuditorDeposit.Amount,
Decoder: tx.TxDataAuditorDeposit.Decoder,
}
}
var oc *TxDataObjectCommit
if tx.TxDataObjectCommit != nil {
oc = &TxDataObjectCommit{
Owner: safeCopyBytes(tx.TxDataObjectCommit.Owner),
Depot: safeCopyBytes(tx.TxDataObjectCommit.Depot),
DepotDiscoveryId: safeCopyBytes(tx.TxDataObjectCommit.DepotDiscoveryId),
Hash: safeCopyBytes(tx.TxDataObjectCommit.Hash),
Size: tx.TxDataObjectCommit.Size,
EncodedHash: safeCopyBytes(tx.TxDataObjectCommit.EncodedHash),
EncodedSize: tx.TxDataObjectCommit.EncodedSize,
NumBlocks: tx.TxDataObjectCommit.NumBlocks,
Duration: tx.TxDataObjectCommit.Duration,
Fee: tx.TxDataObjectCommit.Fee,
Pledge: tx.TxDataObjectCommit.Pledge,
Deadline: tx.TxDataObjectCommit.Deadline,
}
}
var oa *TxDataObjectAudit
if tx.TxDataObjectAudit != nil {
var rands [][]byte
for _, v := range tx.TxDataObjectAudit.Rands {
rands = append(rands, safeCopyBytes(v))
}
oa = &TxDataObjectAudit{
CommitTxHash: safeCopyBytes(tx.TxDataObjectAudit.CommitTxHash),
AuditHash: safeCopyBytes(tx.TxDataObjectAudit.AuditHash),
Auditor: safeCopyBytes(tx.TxDataObjectAudit.Auditor),
Depot: safeCopyBytes(tx.TxDataObjectAudit.Depot),
Hash: safeCopyBytes(tx.TxDataObjectAudit.Hash),
Size: tx.TxDataObjectAudit.Size,
EncodedHash: safeCopyBytes(tx.TxDataObjectAudit.EncodedHash),
EncodedSize: tx.TxDataObjectAudit.EncodedSize,
NumBlocks: tx.TxDataObjectAudit.NumBlocks,
Rands: rands,
}
}
var och *TxDataObjectChallenge
if tx.TxDataObjectChallenge != nil {
och = &TxDataObjectChallenge{
Depot: safeCopyBytes(tx.TxDataObjectChallenge.Depot),
CommitTxHash: safeCopyBytes(tx.TxDataObjectChallenge.CommitTxHash),
}
}
var op *TxDataObjectPoR
if tx.TxDataObjectPoR != nil {
var aggs [][]byte
for _, v := range tx.TxDataObjectPoR.BlockAggs {
aggs = append(aggs, safeCopyBytes(v))
}
op = &TxDataObjectPoR{
CommitTxHash: safeCopyBytes(tx.TxDataObjectPoR.CommitTxHash),
BlockAggs: aggs,
Sigma: safeCopyBytes(tx.TxDataObjectPoR.Sigma),
}
}
return &Transaction{
Type: tx.Type,
From: safeCopyBytes(tx.From),
ChainId: tx.ChainId,
Nonce: tx.Nonce,
GasPrice: tx.GasPrice,
GasLimit: tx.GasLimit,
TxDataBalanceTransfer: bf,
TxDataValidatorDeposit: vd,
TxDataAuditorDeposit: ad,
TxDataObjectCommit: oc,
TxDataObjectAudit: oa,
TxDataObjectChallenge: och,
TxDataObjectPoR: op,
}
}
// CopyProposerSlashings copies the provided ProposerSlashing array.
func CopyProposerSlashings(slashings []*ProposerSlashing) []*ProposerSlashing {
if slashings == nil {
return nil
}
newSlashings := make([]*ProposerSlashing, len(slashings))
for i, att := range slashings {
newSlashings[i] = CopyProposerSlashing(att)
}
return newSlashings
}
// CopyProposerSlashing copies the provided ProposerSlashing.
func CopyProposerSlashing(slashing *ProposerSlashing) *ProposerSlashing {
if slashing == nil {
return nil
}
return &ProposerSlashing{
Header_1: CopySignedBlockHeader(slashing.Header_1),
Header_2: CopySignedBlockHeader(slashing.Header_2),
}
}
// CopySignedBlockHeader copies the provided SignedBlockHeader.
func CopySignedBlockHeader(header *SignedBlockHeader) *SignedBlockHeader {
if header == nil {
return nil
}
return &SignedBlockHeader{
Header: CopyBlockHeader(header.Header),
Signature: safeCopyBytes(header.Signature),
}
}
// CopyBlockHeader copies the provided BlockHeader.
func CopyBlockHeader(header *BlockHeader) *BlockHeader {
if header == nil {
return nil
}
parentRoot := safeCopyBytes(header.ParentRoot)
stateRoot := safeCopyBytes(header.StateRoot)
bodyRoot := safeCopyBytes(header.BodyRoot)
return &BlockHeader{
Slot: header.Slot,
ProposerIndex: header.ProposerIndex,
ParentRoot: parentRoot,
StateRoot: stateRoot,
BodyRoot: bodyRoot,
}
}
// CopyAttesterSlashings copies the provided AttesterSlashings array.
func CopyAttesterSlashings(slashings []*AttesterSlashing) []*AttesterSlashing {
if slashings == nil {
return nil
}
newSlashings := make([]*AttesterSlashing, len(slashings))
for i, slashing := range slashings {
newSlashings[i] = &AttesterSlashing{
Attestation_1: CopyIndexedAttestation(slashing.Attestation_1),
Attestation_2: CopyIndexedAttestation(slashing.Attestation_2),
}
}
return newSlashings
}
// CopyIndexedAttestation copies the provided IndexedAttestation.
func CopyIndexedAttestation(indexedAtt *IndexedAttestation) *IndexedAttestation {
var indices []uint64
if indexedAtt == nil {
return nil
} else if indexedAtt.AttestingIndices != nil {
indices = make([]uint64, len(indexedAtt.AttestingIndices))
copy(indices, indexedAtt.AttestingIndices)
}
return &IndexedAttestation{
AttestingIndices: indices,
Data: CopyAttestationData(indexedAtt.Data),
Signature: safeCopyBytes(indexedAtt.Signature),
}
}
// CopyAttestations copies the provided Attestation array.
func CopyAttestations(attestations []*Attestation) []*Attestation {
if attestations == nil {
return nil
}
newAttestations := make([]*Attestation, len(attestations))
for i, att := range attestations {
newAttestations[i] = CopyAttestation(att)
}
return newAttestations
}
// CopyValidator copies the provided validator.
func CopyValidator(val *Validator) *Validator {
pubKey := make([]byte, len(val.PublicKey))
copy(pubKey, val.PublicKey)
return &Validator{
PublicKey: pubKey,
Balance: val.Balance,
Slashed: val.Slashed,
ActivationEligibilityEpoch: val.ActivationEligibilityEpoch,
ActivationEpoch: val.ActivationEpoch,
ExitEpoch: val.ExitEpoch,
WithdrawableEpoch: val.WithdrawableEpoch,
}
}
func (a *Account) Copy() *Account {
return proto.Clone(a).(*Account)
}