@@ -3,125 +3,153 @@ package structs
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
- "strconv"
7
6
8
7
"github.com/ethereum/go-ethereum/common/hexutil"
9
8
"github.com/pkg/errors"
10
- v1 "github.com/prysmaticlabs/prysm/v5/proto/eth/v1 "
11
- v2 "github.com/prysmaticlabs/prysm/v5/proto/eth/v2 "
12
- "github.com/prysmaticlabs/prysm/v5/proto/migration "
9
+ "github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces "
10
+ enginev1 "github.com/prysmaticlabs/prysm/v5/proto/engine/v1 "
11
+ "github.com/prysmaticlabs/prysm/v5/runtime/version "
13
12
)
14
13
15
- func LightClientUpdateFromConsensus (update * v2 .LightClientUpdate ) (* LightClientUpdate , error ) {
16
- attestedHeader , err := lightClientHeaderContainerToJSON (update .AttestedHeader )
14
+ func LightClientUpdateFromConsensus (update interfaces .LightClientUpdate ) (* LightClientUpdate , error ) {
15
+ attestedHeader , err := lightClientHeaderToJSON (update .AttestedHeader () )
17
16
if err != nil {
18
17
return nil , errors .Wrap (err , "could not marshal attested light client header" )
19
18
}
20
- finalizedHeader , err := lightClientHeaderContainerToJSON (update .FinalizedHeader )
19
+ finalizedHeader , err := lightClientHeaderToJSON (update .FinalizedHeader () )
21
20
if err != nil {
22
21
return nil , errors .Wrap (err , "could not marshal finalized light client header" )
23
22
}
23
+ finalityBranch := update .FinalityBranch ()
24
+
25
+ var scBranch [][32 ]byte
26
+ if update .Version () >= version .Electra {
27
+ b , err := update .NextSyncCommitteeBranchElectra ()
28
+ if err != nil {
29
+ return nil , err
30
+ }
31
+ scBranch = b [:]
32
+ } else {
33
+ b , err := update .NextSyncCommitteeBranch ()
34
+ if err != nil {
35
+ return nil , err
36
+ }
37
+ scBranch = b [:]
38
+ }
24
39
25
40
return & LightClientUpdate {
26
41
AttestedHeader : attestedHeader ,
27
- NextSyncCommittee : SyncCommitteeFromConsensus (migration . V2SyncCommitteeToV1Alpha1 ( update .NextSyncCommittee )),
28
- NextSyncCommitteeBranch : branchToJSON (update . NextSyncCommitteeBranch ),
42
+ NextSyncCommittee : SyncCommitteeFromConsensus (update .NextSyncCommittee ( )),
43
+ NextSyncCommitteeBranch : branchToJSON (scBranch ),
29
44
FinalizedHeader : finalizedHeader ,
30
- FinalityBranch : branchToJSON (update . FinalityBranch ),
31
- SyncAggregate : syncAggregateToJSON (update .SyncAggregate ),
32
- SignatureSlot : strconv . FormatUint ( uint64 ( update .SignatureSlot ), 10 ),
45
+ FinalityBranch : branchToJSON (finalityBranch [:] ),
46
+ SyncAggregate : SyncAggregateFromConsensus (update .SyncAggregate () ),
47
+ SignatureSlot : fmt . Sprintf ( "%d" , update .SignatureSlot () ),
33
48
}, nil
34
49
}
35
50
36
- func LightClientFinalityUpdateFromConsensus (update * v2 .LightClientFinalityUpdate ) (* LightClientFinalityUpdate , error ) {
37
- attestedHeader , err := lightClientHeaderContainerToJSON (update .AttestedHeader )
51
+ func LightClientFinalityUpdateFromConsensus (update interfaces .LightClientFinalityUpdate ) (* LightClientFinalityUpdate , error ) {
52
+ attestedHeader , err := lightClientHeaderToJSON (update .AttestedHeader () )
38
53
if err != nil {
39
54
return nil , errors .Wrap (err , "could not marshal attested light client header" )
40
55
}
41
- finalizedHeader , err := lightClientHeaderContainerToJSON (update .FinalizedHeader )
56
+ finalizedHeader , err := lightClientHeaderToJSON (update .FinalizedHeader () )
42
57
if err != nil {
43
58
return nil , errors .Wrap (err , "could not marshal finalized light client header" )
44
59
}
60
+ finalityBranch := update .FinalityBranch ()
45
61
46
62
return & LightClientFinalityUpdate {
47
63
AttestedHeader : attestedHeader ,
48
64
FinalizedHeader : finalizedHeader ,
49
- FinalityBranch : branchToJSON (update . FinalityBranch ),
50
- SyncAggregate : syncAggregateToJSON (update .SyncAggregate ),
51
- SignatureSlot : strconv . FormatUint ( uint64 ( update .SignatureSlot ), 10 ),
65
+ FinalityBranch : branchToJSON (finalityBranch [:] ),
66
+ SyncAggregate : SyncAggregateFromConsensus (update .SyncAggregate () ),
67
+ SignatureSlot : fmt . Sprintf ( "%d" , update .SignatureSlot () ),
52
68
}, nil
53
69
}
54
70
55
- func LightClientOptimisticUpdateFromConsensus (update * v2 .LightClientOptimisticUpdate ) (* LightClientOptimisticUpdate , error ) {
56
- attestedHeader , err := lightClientHeaderContainerToJSON (update .AttestedHeader )
71
+ func LightClientOptimisticUpdateFromConsensus (update interfaces .LightClientOptimisticUpdate ) (* LightClientOptimisticUpdate , error ) {
72
+ attestedHeader , err := lightClientHeaderToJSON (update .AttestedHeader () )
57
73
if err != nil {
58
74
return nil , errors .Wrap (err , "could not marshal attested light client header" )
59
75
}
60
76
61
77
return & LightClientOptimisticUpdate {
62
78
AttestedHeader : attestedHeader ,
63
- SyncAggregate : syncAggregateToJSON (update .SyncAggregate ),
64
- SignatureSlot : strconv . FormatUint ( uint64 ( update .SignatureSlot ), 10 ),
79
+ SyncAggregate : SyncAggregateFromConsensus (update .SyncAggregate () ),
80
+ SignatureSlot : fmt . Sprintf ( "%d" , update .SignatureSlot () ),
65
81
}, nil
66
82
}
67
83
68
- func branchToJSON ( branchBytes [][]byte ) []string {
84
+ func branchToJSON [ S [][32 ]byte ]( branchBytes S ) []string {
69
85
if branchBytes == nil {
70
86
return nil
71
87
}
72
88
branch := make ([]string , len (branchBytes ))
73
89
for i , root := range branchBytes {
74
- branch [i ] = hexutil .Encode (root )
90
+ branch [i ] = hexutil .Encode (root [:] )
75
91
}
76
92
return branch
77
93
}
78
94
79
- func syncAggregateToJSON (input * v1.SyncAggregate ) * SyncAggregate {
80
- return & SyncAggregate {
81
- SyncCommitteeBits : hexutil .Encode (input .SyncCommitteeBits ),
82
- SyncCommitteeSignature : hexutil .Encode (input .SyncCommitteeSignature ),
83
- }
84
- }
85
-
86
- func lightClientHeaderContainerToJSON (container * v2.LightClientHeaderContainer ) (json.RawMessage , error ) {
95
+ func lightClientHeaderToJSON (header interfaces.LightClientHeader ) (json.RawMessage , error ) {
87
96
// In the case that a finalizedHeader is nil.
88
- if container == nil {
97
+ if header == nil {
89
98
return nil , nil
90
99
}
91
100
92
- beacon , err := container .GetBeacon ()
93
- if err != nil {
94
- return nil , errors .Wrap (err , "could not get beacon block header" )
95
- }
96
-
97
- var header any
101
+ var result any
98
102
99
- switch t := ( container . Header ).( type ) {
100
- case * v2. LightClientHeaderContainer_HeaderAltair :
101
- header = & LightClientHeader {Beacon : BeaconBlockHeaderFromConsensus (migration . V1HeaderToV1Alpha1 ( beacon ))}
102
- case * v2. LightClientHeaderContainer_HeaderCapella :
103
- execution , err := ExecutionPayloadHeaderCapellaFromConsensus ( t . HeaderCapella . Execution )
103
+ switch v := header . Version (); v {
104
+ case version . Altair :
105
+ result = & LightClientHeader {Beacon : BeaconBlockHeaderFromConsensus (header . Beacon ( ))}
106
+ case version . Capella :
107
+ exInterface , err := header . Execution ( )
104
108
if err != nil {
105
109
return nil , err
106
110
}
107
- header = & LightClientHeaderCapella {
108
- Beacon : BeaconBlockHeaderFromConsensus (migration .V1HeaderToV1Alpha1 (beacon )),
111
+ ex , ok := exInterface .Proto ().(* enginev1.ExecutionPayloadHeaderCapella )
112
+ if ! ok {
113
+ return nil , fmt .Errorf ("execution data is not %T" , & enginev1.ExecutionPayloadHeaderCapella {})
114
+ }
115
+ execution , err := ExecutionPayloadHeaderCapellaFromConsensus (ex )
116
+ if err != nil {
117
+ return nil , err
118
+ }
119
+ executionBranch , err := header .ExecutionBranch ()
120
+ if err != nil {
121
+ return nil , err
122
+ }
123
+ result = & LightClientHeaderCapella {
124
+ Beacon : BeaconBlockHeaderFromConsensus (header .Beacon ()),
109
125
Execution : execution ,
110
- ExecutionBranch : branchToJSON (t .HeaderCapella .ExecutionBranch ),
126
+ ExecutionBranch : branchToJSON (executionBranch [:]),
127
+ }
128
+ case version .Deneb :
129
+ exInterface , err := header .Execution ()
130
+ if err != nil {
131
+ return nil , err
132
+ }
133
+ ex , ok := exInterface .Proto ().(* enginev1.ExecutionPayloadHeaderDeneb )
134
+ if ! ok {
135
+ return nil , fmt .Errorf ("execution data is not %T" , & enginev1.ExecutionPayloadHeaderDeneb {})
136
+ }
137
+ execution , err := ExecutionPayloadHeaderDenebFromConsensus (ex )
138
+ if err != nil {
139
+ return nil , err
111
140
}
112
- case * v2.LightClientHeaderContainer_HeaderDeneb :
113
- execution , err := ExecutionPayloadHeaderDenebFromConsensus (t .HeaderDeneb .Execution )
141
+ executionBranch , err := header .ExecutionBranch ()
114
142
if err != nil {
115
143
return nil , err
116
144
}
117
- header = & LightClientHeaderDeneb {
118
- Beacon : BeaconBlockHeaderFromConsensus (migration . V1HeaderToV1Alpha1 ( beacon )),
145
+ result = & LightClientHeaderDeneb {
146
+ Beacon : BeaconBlockHeaderFromConsensus (header . Beacon ( )),
119
147
Execution : execution ,
120
- ExecutionBranch : branchToJSON (t . HeaderDeneb . ExecutionBranch ),
148
+ ExecutionBranch : branchToJSON (executionBranch [:] ),
121
149
}
122
150
default :
123
- return nil , fmt .Errorf ("unsupported header type %T " , t )
151
+ return nil , fmt .Errorf ("unsupported header version %s " , version . String ( v ) )
124
152
}
125
153
126
- return json .Marshal (header )
154
+ return json .Marshal (result )
127
155
}
0 commit comments