Skip to content

Commit c006bc8

Browse files
committed
RPC type BlockNumber support only block hash case
1 parent 01bd77f commit c006bc8

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

crates/rpc/rpc-eth-types/src/block_number.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,25 @@ impl<'a> Visitor<'a> for BlockNumberVisitor {
185185
"safe" => Ok(BlockNumber::Safe),
186186
"finalized" => Ok(BlockNumber::Finalized),
187187
_ if value.starts_with("0x") => {
188-
u64::from_str_radix(&value[2..], 16)
189-
.map(BlockNumber::Num)
190-
.map_err(|e| {
191-
SerdeError::custom(format!(
192-
"Invalid block number: {}",
193-
e
194-
))
188+
// Since there is no way to clearly distinguish between a DATA parameter and a QUANTITY parameter. A str is therefore deserialized into a Block Number: <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1898.md>
189+
// However, since the hex string should be a QUANTITY, we can safely assume that if the len is 66 bytes, it is in fact a hash, ref <https://github.com/ethereum/go-ethereum/blob/ee530c0d5aa70d2c00ab5691a89ab431b73f8165/rpc/types.go#L184-L184>
190+
if value.len() == 66 {
191+
let hash =
192+
value[2..].parse().map_err(SerdeError::custom)?;
193+
Ok(BlockNumber::Hash {
194+
hash,
195+
require_canonical: None,
195196
})
197+
} else {
198+
u64::from_str_radix(&value[2..], 16)
199+
.map(BlockNumber::Num)
200+
.map_err(|e| {
201+
SerdeError::custom(format!(
202+
"Invalid block number: {}",
203+
e
204+
))
205+
})
206+
}
196207
}
197208
_ => Err(SerdeError::custom(
198209
"Invalid block number: missing 0x prefix".to_string(),

0 commit comments

Comments
 (0)