1
+ use std:: collections:: HashMap ;
2
+
3
+ use async_trait:: async_trait;
4
+ use eth2:: { BeaconNodeHttpClient , Error } ;
5
+ use eth2:: types:: { BeaconBlockHeader , BlobSidecarList , BlockHeaderAndSignature , BlockHeaderData , BlockId , EthSpec , ExecutionOptimisticFinalizedResponse , GenericResponse , Hash256 , MainnetEthSpec , SignatureBytes , Slot } ;
6
+
7
+ use crate :: blob_test_helper:: { FIVE , FOUR , new_blob_sidecars, ONE , ORIGIN_BLOCK , START_SLOT , THREE , TWO } ;
8
+
9
+ #[ async_trait]
10
+ pub trait BeaconClient {
11
+ async fn get_beacon_headers_block_id (
12
+ & self ,
13
+ block_id : BlockId ,
14
+ ) -> Result < Option < ExecutionOptimisticFinalizedResponse < BlockHeaderData > > , Error > ;
15
+
16
+ async fn get_blobs (
17
+ & self ,
18
+ block_id : BlockId ,
19
+ indices : Option < & [ u64 ] > ,
20
+ ) -> Result < Option < GenericResponse < BlobSidecarList < MainnetEthSpec > > > , Error > ;
21
+ }
22
+
23
+ pub struct BeaconClientStub < E : EthSpec > {
24
+ pub headers : HashMap < String , BlockHeaderData > ,
25
+ pub blobs : HashMap < String , BlobSidecarList < E > > ,
26
+ }
27
+
28
+ #[ async_trait]
29
+ impl BeaconClient for BeaconClientStub < MainnetEthSpec > {
30
+ async fn get_beacon_headers_block_id ( & self , block_id : BlockId ) -> Result < Option < ExecutionOptimisticFinalizedResponse < BlockHeaderData > > , Error > {
31
+ let header = self . headers . get ( block_id. to_string ( ) . as_str ( ) ) ;
32
+
33
+ Ok ( header. map ( |h| ExecutionOptimisticFinalizedResponse {
34
+ execution_optimistic : Some ( true ) ,
35
+ finalized : Some ( true ) ,
36
+ data : h. clone ( ) ,
37
+ } ) )
38
+ }
39
+
40
+ async fn get_blobs ( & self , block_id : BlockId , _indices : Option < & [ u64 ] > ) -> Result < Option < GenericResponse < BlobSidecarList < MainnetEthSpec > > > , Error > {
41
+ let blobs = self . blobs . get ( block_id. to_string ( ) . as_str ( ) ) ;
42
+
43
+ Ok ( blobs. map ( |b| GenericResponse {
44
+ data : b. clone ( ) ,
45
+ } ) )
46
+ }
47
+ }
48
+
49
+ impl Default for BeaconClientStub < MainnetEthSpec > {
50
+ fn default ( ) -> Self {
51
+ let make_header = |slot : Slot , hash : Hash256 , parent : Hash256 | -> BlockHeaderData {
52
+ BlockHeaderData {
53
+ root : hash,
54
+ canonical : false ,
55
+ header : BlockHeaderAndSignature {
56
+ message : BeaconBlockHeader {
57
+ slot,
58
+ proposer_index : 0 ,
59
+ parent_root : parent,
60
+ state_root : Hash256 :: default ( ) ,
61
+ body_root : Hash256 :: default ( ) ,
62
+ } ,
63
+ signature : SignatureBytes :: empty ( ) ,
64
+ } ,
65
+ }
66
+ } ;
67
+
68
+ let start_slot: Slot = START_SLOT ;
69
+ let origin_blobs = new_blob_sidecars ( 1 ) ;
70
+ let one_blobs = new_blob_sidecars ( 2 ) ;
71
+ let two_blobs = new_blob_sidecars ( 0 ) ;
72
+ let three_blobs = new_blob_sidecars ( 4 ) ;
73
+ let four_blobs = new_blob_sidecars ( 5 ) ;
74
+ let five_blobs = new_blob_sidecars ( 6 ) ;
75
+
76
+ BeaconClientStub {
77
+ headers : HashMap :: from ( [
78
+ (
79
+ ORIGIN_BLOCK . to_string ( ) ,
80
+ make_header ( start_slot, * ORIGIN_BLOCK , Hash256 :: from_slice ( & [ 9 , 9 , 9 ] ) ) ,
81
+ ) ,
82
+ (
83
+ ONE . to_string ( ) ,
84
+ make_header ( start_slot + 1 , * ONE , * ORIGIN_BLOCK ) ,
85
+ ) ,
86
+ (
87
+ TWO . to_string ( ) ,
88
+ make_header ( start_slot + 2 , * TWO , * ONE ) ,
89
+ ) ,
90
+ (
91
+ THREE . to_string ( ) ,
92
+ make_header ( start_slot + 3 , * THREE , * TWO ) ,
93
+ ) ,
94
+ (
95
+ FOUR . to_string ( ) ,
96
+ make_header ( start_slot + 4 , * FOUR , * THREE ) ,
97
+ ) ,
98
+ (
99
+ FIVE . to_string ( ) ,
100
+ make_header ( start_slot + 5 , * FIVE , * FOUR ) ,
101
+ ) ,
102
+ (
103
+ "head" . to_string ( ) ,
104
+ make_header ( start_slot + 5 , * FIVE , * FOUR ) ,
105
+ ) ,
106
+ (
107
+ "finalized" . to_string ( ) ,
108
+ make_header ( start_slot + 3 , * THREE , * TWO ) ,
109
+ ) ,
110
+ (
111
+ start_slot. as_u64 ( ) . to_string ( ) ,
112
+ make_header ( start_slot, * ORIGIN_BLOCK , Hash256 :: from_slice ( & [ 9 , 9 , 9 ] ) ) ,
113
+ ) ,
114
+ (
115
+ ( start_slot + 1 ) . as_u64 ( ) . to_string ( ) ,
116
+ make_header ( start_slot + 1 , * ONE , * ORIGIN_BLOCK ) ,
117
+ ) ,
118
+ (
119
+ ( start_slot + 2 ) . as_u64 ( ) . to_string ( ) ,
120
+ make_header ( start_slot + 2 , * TWO , * ONE ) ,
121
+ ) ,
122
+ (
123
+ ( start_slot + 3 ) . as_u64 ( ) . to_string ( ) ,
124
+ make_header ( start_slot + 3 , * THREE , * TWO ) ,
125
+ ) ,
126
+ (
127
+ ( start_slot + 4 ) . as_u64 ( ) . to_string ( ) ,
128
+ make_header ( start_slot + 4 , * FOUR , * THREE ) ,
129
+ ) ,
130
+ (
131
+ ( start_slot + 5 ) . as_u64 ( ) . to_string ( ) ,
132
+ make_header ( start_slot + 5 , * FIVE , * FOUR ) ,
133
+ ) ,
134
+ ] ) ,
135
+
136
+ blobs : HashMap :: from ( [
137
+ ( ORIGIN_BLOCK . to_string ( ) ,
138
+ origin_blobs. clone ( ) ) ,
139
+ ( ONE . to_string ( ) ,
140
+ one_blobs. clone ( ) ) ,
141
+ ( TWO . to_string ( ) ,
142
+ two_blobs. clone ( ) ) ,
143
+ ( THREE . to_string ( ) ,
144
+ three_blobs. clone ( ) ) ,
145
+ ( FOUR . to_string ( ) ,
146
+ four_blobs. clone ( ) ) ,
147
+ ( FIVE . to_string ( ) ,
148
+ five_blobs. clone ( ) ) ,
149
+ ( "head" . to_string ( ) ,
150
+ five_blobs. clone ( ) ) ,
151
+ ( "finalized" . to_string ( ) ,
152
+ three_blobs. clone ( ) ) ,
153
+ ( start_slot. as_u64 ( ) . to_string ( ) ,
154
+ origin_blobs. clone ( ) ) ,
155
+ ( ( start_slot + 1 ) . as_u64 ( ) . to_string ( ) ,
156
+ one_blobs. clone ( ) ) ,
157
+ ( ( start_slot + 2 ) . as_u64 ( ) . to_string ( ) ,
158
+ two_blobs. clone ( ) ) ,
159
+ ( ( start_slot + 3 ) . as_u64 ( ) . to_string ( ) ,
160
+ three_blobs. clone ( ) ) ,
161
+ ( ( start_slot + 4 ) . as_u64 ( ) . to_string ( ) ,
162
+ four_blobs. clone ( ) ) ,
163
+ ( ( start_slot + 5 ) . as_u64 ( ) . to_string ( ) ,
164
+ five_blobs. clone ( ) ) ,
165
+ ] ) ,
166
+ }
167
+ }
168
+ }
169
+
170
+ impl BeaconClientStub < MainnetEthSpec > {
171
+ pub fn new ( ) -> Self {
172
+ Self {
173
+ headers : HashMap :: new ( ) ,
174
+ blobs : HashMap :: new ( ) ,
175
+ }
176
+ }
177
+ }
178
+
179
+ pub struct BeaconClientEth2 {
180
+ pub beacon_client : BeaconNodeHttpClient ,
181
+ }
182
+
183
+ #[ async_trait]
184
+ impl BeaconClient for BeaconClientEth2 {
185
+ async fn get_beacon_headers_block_id ( & self , block_id : BlockId ) -> Result < Option < ExecutionOptimisticFinalizedResponse < BlockHeaderData > > , Error > {
186
+ return self . beacon_client . get_beacon_headers_block_id ( block_id) . await ;
187
+ }
188
+
189
+ async fn get_blobs ( & self , block_id : BlockId , indices : Option < & [ u64 ] > ) -> Result < Option < GenericResponse < BlobSidecarList < MainnetEthSpec > > > , Error > {
190
+ return self . beacon_client . get_blobs ( block_id, indices) . await ;
191
+ }
192
+ }
0 commit comments