Skip to content

Commit 35151c7

Browse files
authored
deduplicating rest propose block (#15147)
* deduplicating rest propose block * gaz * linting * gaz and linting * remove unneeded import" " * gofmt
1 parent c07479b commit 35151c7

12 files changed

+559
-969
lines changed

api/server/structs/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ go_test(
5757
deps = [
5858
"//proto/engine/v1:go_default_library",
5959
"//proto/prysm/v1alpha1:go_default_library",
60+
"//testing/assert:go_default_library",
6061
"//testing/require:go_default_library",
6162
"@com_github_ethereum_go_ethereum//common:go_default_library",
6263
"@com_github_ethereum_go_ethereum//common/hexutil:go_default_library",

api/server/structs/conversions_test.go

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"testing"
55

66
eth "github.com/OffchainLabs/prysm/v6/proto/prysm/v1alpha1"
7+
"github.com/OffchainLabs/prysm/v6/testing/assert"
78
"github.com/OffchainLabs/prysm/v6/testing/require"
9+
"github.com/ethereum/go-ethereum/common/hexutil"
810
)
911

1012
func TestDepositSnapshotFromConsensus(t *testing.T) {
@@ -102,12 +104,266 @@ func TestProposerSlashing_ToConsensus(t *testing.T) {
102104
require.ErrorContains(t, errNilValue.Error(), err)
103105
}
104106

107+
func TestProposerSlashing_FromConsensus(t *testing.T) {
108+
input := []*eth.ProposerSlashing{
109+
{
110+
Header_1: &eth.SignedBeaconBlockHeader{
111+
Header: &eth.BeaconBlockHeader{
112+
Slot: 1,
113+
ProposerIndex: 2,
114+
ParentRoot: []byte{3},
115+
StateRoot: []byte{4},
116+
BodyRoot: []byte{5},
117+
},
118+
Signature: []byte{6},
119+
},
120+
Header_2: &eth.SignedBeaconBlockHeader{
121+
Header: &eth.BeaconBlockHeader{
122+
Slot: 7,
123+
ProposerIndex: 8,
124+
ParentRoot: []byte{9},
125+
StateRoot: []byte{10},
126+
BodyRoot: []byte{11},
127+
},
128+
Signature: []byte{12},
129+
},
130+
},
131+
{
132+
Header_1: &eth.SignedBeaconBlockHeader{
133+
Header: &eth.BeaconBlockHeader{
134+
Slot: 13,
135+
ProposerIndex: 14,
136+
ParentRoot: []byte{15},
137+
StateRoot: []byte{16},
138+
BodyRoot: []byte{17},
139+
},
140+
Signature: []byte{18},
141+
},
142+
Header_2: &eth.SignedBeaconBlockHeader{
143+
Header: &eth.BeaconBlockHeader{
144+
Slot: 19,
145+
ProposerIndex: 20,
146+
ParentRoot: []byte{21},
147+
StateRoot: []byte{22},
148+
BodyRoot: []byte{23},
149+
},
150+
Signature: []byte{24},
151+
},
152+
},
153+
}
154+
155+
expectedResult := []*ProposerSlashing{
156+
{
157+
SignedHeader1: &SignedBeaconBlockHeader{
158+
Message: &BeaconBlockHeader{
159+
Slot: "1",
160+
ProposerIndex: "2",
161+
ParentRoot: hexutil.Encode([]byte{3}),
162+
StateRoot: hexutil.Encode([]byte{4}),
163+
BodyRoot: hexutil.Encode([]byte{5}),
164+
},
165+
Signature: hexutil.Encode([]byte{6}),
166+
},
167+
SignedHeader2: &SignedBeaconBlockHeader{
168+
Message: &BeaconBlockHeader{
169+
Slot: "7",
170+
ProposerIndex: "8",
171+
ParentRoot: hexutil.Encode([]byte{9}),
172+
StateRoot: hexutil.Encode([]byte{10}),
173+
BodyRoot: hexutil.Encode([]byte{11}),
174+
},
175+
Signature: hexutil.Encode([]byte{12}),
176+
},
177+
},
178+
{
179+
SignedHeader1: &SignedBeaconBlockHeader{
180+
Message: &BeaconBlockHeader{
181+
Slot: "13",
182+
ProposerIndex: "14",
183+
ParentRoot: hexutil.Encode([]byte{15}),
184+
StateRoot: hexutil.Encode([]byte{16}),
185+
BodyRoot: hexutil.Encode([]byte{17}),
186+
},
187+
Signature: hexutil.Encode([]byte{18}),
188+
},
189+
SignedHeader2: &SignedBeaconBlockHeader{
190+
Message: &BeaconBlockHeader{
191+
Slot: "19",
192+
ProposerIndex: "20",
193+
ParentRoot: hexutil.Encode([]byte{21}),
194+
StateRoot: hexutil.Encode([]byte{22}),
195+
BodyRoot: hexutil.Encode([]byte{23}),
196+
},
197+
Signature: hexutil.Encode([]byte{24}),
198+
},
199+
},
200+
}
201+
202+
result := ProposerSlashingsFromConsensus(input)
203+
assert.DeepEqual(t, expectedResult, result)
204+
}
205+
105206
func TestAttesterSlashing_ToConsensus(t *testing.T) {
106207
a := &AttesterSlashing{Attestation1: nil, Attestation2: nil}
107208
_, err := a.ToConsensus()
108209
require.ErrorContains(t, errNilValue.Error(), err)
109210
}
110211

212+
func TestAttesterSlashing_FromConsensus(t *testing.T) {
213+
input := []*eth.AttesterSlashing{
214+
{
215+
Attestation_1: &eth.IndexedAttestation{
216+
AttestingIndices: []uint64{1, 2},
217+
Data: &eth.AttestationData{
218+
Slot: 3,
219+
CommitteeIndex: 4,
220+
BeaconBlockRoot: []byte{5},
221+
Source: &eth.Checkpoint{
222+
Epoch: 6,
223+
Root: []byte{7},
224+
},
225+
Target: &eth.Checkpoint{
226+
Epoch: 8,
227+
Root: []byte{9},
228+
},
229+
},
230+
Signature: []byte{10},
231+
},
232+
Attestation_2: &eth.IndexedAttestation{
233+
AttestingIndices: []uint64{11, 12},
234+
Data: &eth.AttestationData{
235+
Slot: 13,
236+
CommitteeIndex: 14,
237+
BeaconBlockRoot: []byte{15},
238+
Source: &eth.Checkpoint{
239+
Epoch: 16,
240+
Root: []byte{17},
241+
},
242+
Target: &eth.Checkpoint{
243+
Epoch: 18,
244+
Root: []byte{19},
245+
},
246+
},
247+
Signature: []byte{20},
248+
},
249+
},
250+
{
251+
Attestation_1: &eth.IndexedAttestation{
252+
AttestingIndices: []uint64{21, 22},
253+
Data: &eth.AttestationData{
254+
Slot: 23,
255+
CommitteeIndex: 24,
256+
BeaconBlockRoot: []byte{25},
257+
Source: &eth.Checkpoint{
258+
Epoch: 26,
259+
Root: []byte{27},
260+
},
261+
Target: &eth.Checkpoint{
262+
Epoch: 28,
263+
Root: []byte{29},
264+
},
265+
},
266+
Signature: []byte{30},
267+
},
268+
Attestation_2: &eth.IndexedAttestation{
269+
AttestingIndices: []uint64{31, 32},
270+
Data: &eth.AttestationData{
271+
Slot: 33,
272+
CommitteeIndex: 34,
273+
BeaconBlockRoot: []byte{35},
274+
Source: &eth.Checkpoint{
275+
Epoch: 36,
276+
Root: []byte{37},
277+
},
278+
Target: &eth.Checkpoint{
279+
Epoch: 38,
280+
Root: []byte{39},
281+
},
282+
},
283+
Signature: []byte{40},
284+
},
285+
},
286+
}
287+
288+
expectedResult := []*AttesterSlashing{
289+
{
290+
Attestation1: &IndexedAttestation{
291+
AttestingIndices: []string{"1", "2"},
292+
Data: &AttestationData{
293+
Slot: "3",
294+
CommitteeIndex: "4",
295+
BeaconBlockRoot: hexutil.Encode([]byte{5}),
296+
Source: &Checkpoint{
297+
Epoch: "6",
298+
Root: hexutil.Encode([]byte{7}),
299+
},
300+
Target: &Checkpoint{
301+
Epoch: "8",
302+
Root: hexutil.Encode([]byte{9}),
303+
},
304+
},
305+
Signature: hexutil.Encode([]byte{10}),
306+
},
307+
Attestation2: &IndexedAttestation{
308+
AttestingIndices: []string{"11", "12"},
309+
Data: &AttestationData{
310+
Slot: "13",
311+
CommitteeIndex: "14",
312+
BeaconBlockRoot: hexutil.Encode([]byte{15}),
313+
Source: &Checkpoint{
314+
Epoch: "16",
315+
Root: hexutil.Encode([]byte{17}),
316+
},
317+
Target: &Checkpoint{
318+
Epoch: "18",
319+
Root: hexutil.Encode([]byte{19}),
320+
},
321+
},
322+
Signature: hexutil.Encode([]byte{20}),
323+
},
324+
},
325+
{
326+
Attestation1: &IndexedAttestation{
327+
AttestingIndices: []string{"21", "22"},
328+
Data: &AttestationData{
329+
Slot: "23",
330+
CommitteeIndex: "24",
331+
BeaconBlockRoot: hexutil.Encode([]byte{25}),
332+
Source: &Checkpoint{
333+
Epoch: "26",
334+
Root: hexutil.Encode([]byte{27}),
335+
},
336+
Target: &Checkpoint{
337+
Epoch: "28",
338+
Root: hexutil.Encode([]byte{29}),
339+
},
340+
},
341+
Signature: hexutil.Encode([]byte{30}),
342+
},
343+
Attestation2: &IndexedAttestation{
344+
AttestingIndices: []string{"31", "32"},
345+
Data: &AttestationData{
346+
Slot: "33",
347+
CommitteeIndex: "34",
348+
BeaconBlockRoot: hexutil.Encode([]byte{35}),
349+
Source: &Checkpoint{
350+
Epoch: "36",
351+
Root: hexutil.Encode([]byte{37}),
352+
},
353+
Target: &Checkpoint{
354+
Epoch: "38",
355+
Root: hexutil.Encode([]byte{39}),
356+
},
357+
},
358+
Signature: hexutil.Encode([]byte{40}),
359+
},
360+
},
361+
}
362+
363+
result := AttesterSlashingsFromConsensus(input)
364+
assert.DeepEqual(t, expectedResult, result)
365+
}
366+
111367
func TestIndexedAttestation_ToConsensus(t *testing.T) {
112368
a := &IndexedAttestation{
113369
AttestingIndices: []string{"1"},
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### Fixed
2+
3+
- Fixed gocognit on propose block rest path.
4+
5+
### Ignored
6+
7+
- Removed jsonify functions that duplicate FromConsensus functions in the structs package for rest calls on propose block.

validator/client/beacon-api/beacon_block_json_helpers.go

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,6 @@ func jsonifyTransactions(transactions [][]byte) []string {
1919
return jsonTransactions
2020
}
2121

22-
func jsonifyBlsToExecutionChanges(blsToExecutionChanges []*ethpb.SignedBLSToExecutionChange) []*structs.SignedBLSToExecutionChange {
23-
jsonBlsToExecutionChanges := make([]*structs.SignedBLSToExecutionChange, len(blsToExecutionChanges))
24-
for index, signedBlsToExecutionChange := range blsToExecutionChanges {
25-
blsToExecutionChangeJson := &structs.BLSToExecutionChange{
26-
ValidatorIndex: apiutil.Uint64ToString(signedBlsToExecutionChange.Message.ValidatorIndex),
27-
FromBLSPubkey: hexutil.Encode(signedBlsToExecutionChange.Message.FromBlsPubkey),
28-
ToExecutionAddress: hexutil.Encode(signedBlsToExecutionChange.Message.ToExecutionAddress),
29-
}
30-
signedJson := &structs.SignedBLSToExecutionChange{
31-
Message: blsToExecutionChangeJson,
32-
Signature: hexutil.Encode(signedBlsToExecutionChange.Signature),
33-
}
34-
jsonBlsToExecutionChanges[index] = signedJson
35-
}
36-
return jsonBlsToExecutionChanges
37-
}
38-
3922
func jsonifyEth1Data(eth1Data *ethpb.Eth1Data) *structs.Eth1Data {
4023
return &structs.Eth1Data{
4124
BlockHash: hexutil.Encode(eth1Data.BlockHash),
@@ -60,52 +43,6 @@ func jsonifySingleAttestations(attestations []*ethpb.SingleAttestation) []*struc
6043
return jsonAttestations
6144
}
6245

63-
func jsonifyAttesterSlashings(attesterSlashings []*ethpb.AttesterSlashing) []*structs.AttesterSlashing {
64-
jsonAttesterSlashings := make([]*structs.AttesterSlashing, len(attesterSlashings))
65-
for index, attesterSlashing := range attesterSlashings {
66-
jsonAttesterSlashing := &structs.AttesterSlashing{
67-
Attestation1: jsonifyIndexedAttestation(attesterSlashing.Attestation_1),
68-
Attestation2: jsonifyIndexedAttestation(attesterSlashing.Attestation_2),
69-
}
70-
jsonAttesterSlashings[index] = jsonAttesterSlashing
71-
}
72-
return jsonAttesterSlashings
73-
}
74-
75-
func jsonifyDeposits(deposits []*ethpb.Deposit) []*structs.Deposit {
76-
jsonDeposits := make([]*structs.Deposit, len(deposits))
77-
for depositIndex, deposit := range deposits {
78-
proofs := make([]string, len(deposit.Proof))
79-
for proofIndex, proof := range deposit.Proof {
80-
proofs[proofIndex] = hexutil.Encode(proof)
81-
}
82-
83-
jsonDeposit := &structs.Deposit{
84-
Data: &structs.DepositData{
85-
Amount: apiutil.Uint64ToString(deposit.Data.Amount),
86-
Pubkey: hexutil.Encode(deposit.Data.PublicKey),
87-
Signature: hexutil.Encode(deposit.Data.Signature),
88-
WithdrawalCredentials: hexutil.Encode(deposit.Data.WithdrawalCredentials),
89-
},
90-
Proof: proofs,
91-
}
92-
jsonDeposits[depositIndex] = jsonDeposit
93-
}
94-
return jsonDeposits
95-
}
96-
97-
func jsonifyProposerSlashings(proposerSlashings []*ethpb.ProposerSlashing) []*structs.ProposerSlashing {
98-
jsonProposerSlashings := make([]*structs.ProposerSlashing, len(proposerSlashings))
99-
for index, proposerSlashing := range proposerSlashings {
100-
jsonProposerSlashing := &structs.ProposerSlashing{
101-
SignedHeader1: jsonifySignedBeaconBlockHeader(proposerSlashing.Header_1),
102-
SignedHeader2: jsonifySignedBeaconBlockHeader(proposerSlashing.Header_2),
103-
}
104-
jsonProposerSlashings[index] = jsonProposerSlashing
105-
}
106-
return jsonProposerSlashings
107-
}
108-
10946
// JsonifySignedVoluntaryExits converts an array of voluntary exit structs to a JSON hex string compatible format.
11047
func JsonifySignedVoluntaryExits(voluntaryExits []*ethpb.SignedVoluntaryExit) []*structs.SignedVoluntaryExit {
11148
jsonSignedVoluntaryExits := make([]*structs.SignedVoluntaryExit, len(voluntaryExits))

0 commit comments

Comments
 (0)