add safe and finalized tag to BlockNumber (#1792)

* add safe and finalized  tag to BlockNumber

* update test
This commit is contained in:
Salman Pathan 2022-10-17 19:29:41 +05:30 committed by GitHub
parent 84c1c3f2cd
commit 98cea53c38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 47 additions and 1 deletions

View File

@ -535,6 +535,10 @@ impl<'de> Deserialize<'de> for BlockId {
pub enum BlockNumber { pub enum BlockNumber {
/// Latest block /// Latest block
Latest, Latest,
/// Finalized block accepted as canonical
Finalized,
/// Safe head block
Safe,
/// Earliest block (genesis) /// Earliest block (genesis)
Earliest, Earliest,
/// Pending block (not yet part of the blockchain) /// Pending block (not yet part of the blockchain)
@ -562,6 +566,16 @@ impl BlockNumber {
matches!(self, BlockNumber::Latest) matches!(self, BlockNumber::Latest)
} }
/// Returns `true` if it's "finalized"
pub fn is_finalized(&self) -> bool {
matches!(self, BlockNumber::Finalized)
}
/// Returns `true` if it's "safe"
pub fn is_safe(&self) -> bool {
matches!(self, BlockNumber::Safe)
}
/// Returns `true` if it's "pending" /// Returns `true` if it's "pending"
pub fn is_pending(&self) -> bool { pub fn is_pending(&self) -> bool {
matches!(self, BlockNumber::Pending) matches!(self, BlockNumber::Pending)
@ -587,6 +601,8 @@ impl Serialize for BlockNumber {
match *self { match *self {
BlockNumber::Number(ref x) => serializer.serialize_str(&format!("0x{:x}", x)), BlockNumber::Number(ref x) => serializer.serialize_str(&format!("0x{:x}", x)),
BlockNumber::Latest => serializer.serialize_str("latest"), BlockNumber::Latest => serializer.serialize_str("latest"),
BlockNumber::Finalized => serializer.serialize_str("finalized"),
BlockNumber::Safe => serializer.serialize_str("safe"),
BlockNumber::Earliest => serializer.serialize_str("earliest"), BlockNumber::Earliest => serializer.serialize_str("earliest"),
BlockNumber::Pending => serializer.serialize_str("pending"), BlockNumber::Pending => serializer.serialize_str("pending"),
} }
@ -609,6 +625,8 @@ impl FromStr for BlockNumber {
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let block = match s { let block = match s {
"latest" => Self::Latest, "latest" => Self::Latest,
"finalized" => Self::Finalized,
"safe" => Self::Safe,
"earliest" => Self::Earliest, "earliest" => Self::Earliest,
"pending" => Self::Pending, "pending" => Self::Pending,
n => BlockNumber::Number(n.parse::<U64>().map_err(|err| err.to_string())?), n => BlockNumber::Number(n.parse::<U64>().map_err(|err| err.to_string())?),
@ -622,6 +640,8 @@ impl fmt::Display for BlockNumber {
match self { match self {
BlockNumber::Number(ref x) => format!("0x{:x}", x).fmt(f), BlockNumber::Number(ref x) => format!("0x{:x}", x).fmt(f),
BlockNumber::Latest => f.write_str("latest"), BlockNumber::Latest => f.write_str("latest"),
BlockNumber::Finalized => f.write_str("finalized"),
BlockNumber::Safe => f.write_str("safe"),
BlockNumber::Earliest => f.write_str("earliest"), BlockNumber::Earliest => f.write_str("earliest"),
BlockNumber::Pending => f.write_str("pending"), BlockNumber::Pending => f.write_str("pending"),
} }
@ -660,6 +680,18 @@ mod tests {
let id = serde_json::from_value::<BlockId>(num).unwrap(); let id = serde_json::from_value::<BlockId>(num).unwrap();
assert_eq!(id, BlockId::Number(BlockNumber::Latest)); assert_eq!(id, BlockId::Number(BlockNumber::Latest));
let num = serde_json::json!(
{ "blockNumber": "finalized" }
);
let id = serde_json::from_value::<BlockId>(num).unwrap();
assert_eq!(id, BlockId::Number(BlockNumber::Finalized));
let num = serde_json::json!(
{ "blockNumber": "safe" }
);
let id = serde_json::from_value::<BlockId>(num).unwrap();
assert_eq!(id, BlockId::Number(BlockNumber::Safe));
let num = serde_json::json!( let num = serde_json::json!(
{ "blockNumber": "earliest" } { "blockNumber": "earliest" }
); );
@ -678,6 +710,14 @@ mod tests {
let id = serde_json::from_value::<BlockId>(num).unwrap(); let id = serde_json::from_value::<BlockId>(num).unwrap();
assert_eq!(id, BlockId::Number(BlockNumber::Latest)); assert_eq!(id, BlockId::Number(BlockNumber::Latest));
let num = serde_json::json!("finalized");
let id = serde_json::from_value::<BlockId>(num).unwrap();
assert_eq!(id, BlockId::Number(BlockNumber::Finalized));
let num = serde_json::json!("safe");
let id = serde_json::from_value::<BlockId>(num).unwrap();
assert_eq!(id, BlockId::Number(BlockNumber::Safe));
let num = serde_json::json!("earliest"); let num = serde_json::json!("earliest");
let id = serde_json::from_value::<BlockId>(num).unwrap(); let id = serde_json::from_value::<BlockId>(num).unwrap();
assert_eq!(id, BlockId::Number(BlockNumber::Earliest)); assert_eq!(id, BlockId::Number(BlockNumber::Earliest));
@ -698,7 +738,13 @@ mod tests {
#[test] #[test]
fn serde_block_number() { fn serde_block_number() {
for b in &[BlockNumber::Latest, BlockNumber::Earliest, BlockNumber::Pending] { for b in &[
BlockNumber::Latest,
BlockNumber::Finalized,
BlockNumber::Safe,
BlockNumber::Earliest,
BlockNumber::Pending,
] {
let b_ser = serde_json::to_string(&b).unwrap(); let b_ser = serde_json::to_string(&b).unwrap();
let b_de: BlockNumber = serde_json::from_str(&b_ser).unwrap(); let b_de: BlockNumber = serde_json::from_str(&b_ser).unwrap();
assert_eq!(b_de, *b); assert_eq!(b_de, *b);