@@ -19,7 +19,7 @@ use std::fmt;
1919use std:: ops:: Range ;
2020use std:: str:: FromStr ;
2121use std:: time:: Duration ;
22- use types:: { Hash256 , PowBlock , Uint256 } ;
22+ use types:: Hash256 ;
2323
2424/// `keccak("DepositEvent(bytes,bytes,bytes,bytes,bytes)")`
2525pub const DEPOSIT_EVENT_TOPIC : & str =
@@ -49,7 +49,6 @@ pub enum Eth1Id {
4949#[ derive( Clone , Copy ) ]
5050pub enum BlockQuery {
5151 Number ( u64 ) ,
52- Hash ( Hash256 ) ,
5352 Latest ,
5453}
5554
@@ -136,6 +135,13 @@ pub async fn get_chain_id(endpoint: &SensitiveUrl, timeout: Duration) -> Result<
136135 }
137136}
138137
138+ #[ derive( Debug , PartialEq , Clone ) ]
139+ pub struct Block {
140+ pub hash : Hash256 ,
141+ pub timestamp : u64 ,
142+ pub number : u64 ,
143+ }
144+
139145/// Returns the current block number.
140146///
141147/// Uses HTTP JSON RPC at `endpoint`. E.g., `http://localhost:8545`.
@@ -150,74 +156,40 @@ pub async fn get_block_number(endpoint: &SensitiveUrl, timeout: Duration) -> Res
150156 . map_err ( |e| format ! ( "Failed to get block number: {}" , e) )
151157}
152158
153- /// Gets a block by hash or block number.
159+ /// Gets a block hash by block number.
154160///
155161/// Uses HTTP JSON RPC at `endpoint`. E.g., `http://localhost:8545`.
156162pub async fn get_block (
157163 endpoint : & SensitiveUrl ,
158164 query : BlockQuery ,
159165 timeout : Duration ,
160- ) -> Result < PowBlock , String > {
166+ ) -> Result < Block , String > {
161167 let query_param = match query {
162168 BlockQuery :: Number ( block_number) => format ! ( "0x{:x}" , block_number) ,
163- BlockQuery :: Hash ( hash) => format ! ( "{:?}" , hash) , // debug formatting ensures output not truncated
164169 BlockQuery :: Latest => "latest" . to_string ( ) ,
165170 } ;
166- let rpc_method = match query {
167- BlockQuery :: Number ( _) | BlockQuery :: Latest => "eth_getBlockByNumber" ,
168- BlockQuery :: Hash ( _) => "eth_getBlockByHash" ,
169- } ;
170171 let params = json ! ( [
171172 query_param,
172173 false // do not return full tx objects.
173174 ] ) ;
174175
175- let response_body = send_rpc_request ( endpoint, rpc_method , params, timeout) . await ?;
176+ let response_body = send_rpc_request ( endpoint, "eth_getBlockByNumber" , params, timeout) . await ?;
176177 let response = response_result_or_error ( & response_body)
177- . map_err ( |e| format ! ( "{} failed: {}" , rpc_method , e) ) ?;
178+ . map_err ( |e| format ! ( "eth_getBlockByNumber failed: {}" , e) ) ?;
178179
179- let block_hash : Vec < u8 > = hex_to_bytes (
180+ let hash : Vec < u8 > = hex_to_bytes (
180181 response
181182 . get ( "hash" )
182183 . ok_or ( "No hash for block" ) ?
183184 . as_str ( )
184185 . ok_or ( "Block hash was not string" ) ?,
185186 ) ?;
186- let block_hash : Hash256 = if block_hash . len ( ) == 32 {
187- Hash256 :: from_slice ( & block_hash )
187+ let hash : Hash256 = if hash . len ( ) == 32 {
188+ Hash256 :: from_slice ( & hash )
188189 } else {
189- return Err ( format ! ( "Block hash was not 32 bytes: {:?}" , block_hash ) ) ;
190+ return Err ( format ! ( "Block has was not 32 bytes: {:?}" , hash ) ) ;
190191 } ;
191192
192- let parent_hash: Vec < u8 > = hex_to_bytes (
193- response
194- . get ( "parentHash" )
195- . ok_or ( "No parent hash for block" ) ?
196- . as_str ( )
197- . ok_or ( "Parent hash was not string" ) ?,
198- ) ?;
199- let parent_hash: Hash256 = if parent_hash. len ( ) == 32 {
200- Hash256 :: from_slice ( & parent_hash)
201- } else {
202- return Err ( format ! ( "parent hash was not 32 bytes: {:?}" , parent_hash) ) ;
203- } ;
204-
205- let total_difficulty_str = response
206- . get ( "totalDifficulty" )
207- . ok_or ( "No total difficulty for block" ) ?
208- . as_str ( )
209- . ok_or ( "Total difficulty was not a string" ) ?;
210- let total_difficulty = Uint256 :: from_str ( total_difficulty_str)
211- . map_err ( |e| format ! ( "total_difficulty from_str {:?}" , e) ) ?;
212-
213- let difficulty_str = response
214- . get ( "difficulty" )
215- . ok_or ( "No difficulty for block" ) ?
216- . as_str ( )
217- . ok_or ( "Difficulty was not a string" ) ?;
218- let difficulty =
219- Uint256 :: from_str ( difficulty_str) . map_err ( |e| format ! ( "difficulty from_str {:?}" , e) ) ?;
220-
221193 let timestamp = hex_to_u64_be (
222194 response
223195 . get ( "timestamp" )
@@ -226,28 +198,22 @@ pub async fn get_block(
226198 . ok_or ( "Block timestamp was not string" ) ?,
227199 ) ?;
228200
229- let block_number = hex_to_u64_be (
201+ let number = hex_to_u64_be (
230202 response
231203 . get ( "number" )
232204 . ok_or ( "No number for block" ) ?
233205 . as_str ( )
234206 . ok_or ( "Block number was not string" ) ?,
235207 ) ?;
236208
237- if block_number <= usize:: max_value ( ) as u64 {
238- Ok ( PowBlock {
239- block_hash,
240- parent_hash,
241- total_difficulty,
242- difficulty,
209+ if number <= usize:: max_value ( ) as u64 {
210+ Ok ( Block {
211+ hash,
243212 timestamp,
244- block_number ,
213+ number ,
245214 } )
246215 } else {
247- Err ( format ! (
248- "Block number {} is larger than a usize" ,
249- block_number
250- ) )
216+ Err ( format ! ( "Block number {} is larger than a usize" , number) )
251217 }
252218 . map_err ( |e| format ! ( "Failed to get block number: {}" , e) )
253219}
@@ -479,7 +445,7 @@ pub async fn send_rpc_request(
479445}
480446
481447/// Accepts an entire HTTP body (as a string) and returns either the `result` field or the `error['message']` field, as a serde `Value`.
482- pub fn response_result_or_error ( response : & str ) -> Result < Value , RpcError > {
448+ fn response_result_or_error ( response : & str ) -> Result < Value , RpcError > {
483449 let json = serde_json:: from_str :: < Value > ( response)
484450 . map_err ( |e| RpcError :: InvalidJson ( e. to_string ( ) ) ) ?;
485451
@@ -501,7 +467,7 @@ pub fn response_result_or_error(response: &str) -> Result<Value, RpcError> {
501467/// Therefore, this function is only useful for numbers encoded by the JSON RPC.
502468///
503469/// E.g., `0x01 == 1`
504- pub fn hex_to_u64_be ( hex : & str ) -> Result < u64 , String > {
470+ fn hex_to_u64_be ( hex : & str ) -> Result < u64 , String > {
505471 u64:: from_str_radix ( strip_prefix ( hex) ?, 16 )
506472 . map_err ( |e| format ! ( "Failed to parse hex as u64: {:?}" , e) )
507473}
0 commit comments