fix: txpool_inspect unable to parse contract creations (#1905)

* Fixes #1899

* replace match with if let some() else

* fix error: number too large to fit in target type

* Apply suggestions from code review

Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>

Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
This commit is contained in:
MouseLess.eth 2022-11-29 13:18:18 -08:00 committed by GitHub
parent d48187ac0d
commit 82f19e0789
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 53 additions and 33 deletions

View File

@ -60,13 +60,14 @@ impl<'de> Visitor<'de> for TxpoolInspectSummaryVisitor {
let addr = match addr_split[0] {
"" => None,
"0x" => None,
"contract creation" => None,
addr => {
Some(Address::from_str(&addr.to_string().split_off(2)).map_err(de::Error::custom)?)
Some(Address::from_str(addr.trim_start_matches("0x")).map_err(de::Error::custom)?)
}
};
let value = U256::from(u64::from_str(value_split[0]).map_err(de::Error::custom)?);
let gas = U256::from(u64::from_str(gas_split[0]).map_err(de::Error::custom)?);
let gas_price = U256::from(u64::from_str(gas_price_split[0]).map_err(de::Error::custom)?);
let value = U256::from_dec_str(value_split[0]).map_err(de::Error::custom)?;
let gas = U256::from_dec_str(gas_split[0]).map_err(de::Error::custom)?;
let gas_price = U256::from_dec_str(gas_price_split[0]).map_err(de::Error::custom)?;
Ok(TxpoolInspectSummary { to: addr, value, gas, gas_price })
}
@ -89,12 +90,14 @@ impl Serialize for TxpoolInspectSummary {
where
S: serde::Serializer,
{
let formatted_to = if let Some(to) = self.to {
format!("{:?}", to)
} else {
"contract creation".to_string()
};
let formatted = format!(
"{:?}: {} wei + {} gas × {} wei",
self.to.unwrap_or_default(),
self.value,
self.gas,
self.gas_price
"{}: {} wei + {} gas × {} wei",
formatted_to, self.value, self.gas, self.gas_price
);
serializer.serialize_str(&formatted)
}
@ -341,6 +344,9 @@ mod tests {
"252351": "0xd10e3Be2bc8f959Bc8C41CF65F60dE721cF89ADF: 0 wei + 65792 gas × 2000000000 wei",
"252352": "0xd10e3Be2bc8f959Bc8C41CF65F60dE721cF89ADF: 0 wei + 65780 gas × 2000000000 wei",
"252353": "0xd10e3Be2bc8f959Bc8C41CF65F60dE721cF89ADF: 0 wei + 65780 gas × 2000000000 wei"
},
"0x00000000863B56a3C1f0F1be8BC4F8b7BD78F57a": {
"40": "contract creation: 0 wei + 612412 gas × 6000000000 wei"
}
},
"queued": {
@ -380,9 +386,9 @@ mod tests {
"124930".to_string(),
TxpoolInspectSummary {
to: Some(Address::from_str("000000000000000000000000000000000000007E").unwrap()),
value: U256::from(0u64),
gas: U256::from(100187u64),
gas_price: U256::from(20000000000u64),
value: U256::from(0u128),
gas: U256::from(100187u128),
gas_price: U256::from(20000000000u128),
},
);
pending_map.insert(
@ -394,40 +400,54 @@ mod tests {
"252350".to_string(),
TxpoolInspectSummary {
to: Some(Address::from_str("d10e3Be2bc8f959Bc8C41CF65F60dE721cF89ADF").unwrap()),
value: U256::from(0u64),
gas: U256::from(65792u64),
gas_price: U256::from(2000000000u64),
value: U256::from(0u128),
gas: U256::from(65792u128),
gas_price: U256::from(2000000000u128),
},
);
pending_map_inner.insert(
"252351".to_string(),
TxpoolInspectSummary {
to: Some(Address::from_str("d10e3Be2bc8f959Bc8C41CF65F60dE721cF89ADF").unwrap()),
value: U256::from(0u64),
gas: U256::from(65792u64),
gas_price: U256::from(2000000000u64),
value: U256::from(0u128),
gas: U256::from(65792u128),
gas_price: U256::from(2000000000u128),
},
);
pending_map_inner.insert(
"252352".to_string(),
TxpoolInspectSummary {
to: Some(Address::from_str("d10e3Be2bc8f959Bc8C41CF65F60dE721cF89ADF").unwrap()),
value: U256::from(0u64),
gas: U256::from(65780u64),
gas_price: U256::from(2000000000u64),
value: U256::from(0u128),
gas: U256::from(65780u128),
gas_price: U256::from(2000000000u128),
},
);
pending_map_inner.insert(
"252353".to_string(),
TxpoolInspectSummary {
to: Some(Address::from_str("d10e3Be2bc8f959Bc8C41CF65F60dE721cF89ADF").unwrap()),
value: U256::from(0u64),
gas: U256::from(65780u64),
gas_price: U256::from(2000000000u64),
value: U256::from(0u128),
gas: U256::from(65780u128),
gas_price: U256::from(2000000000u128),
},
);
pending_map.insert(
Address::from_str("201354729f8d0f8b64e9a0c353c672c6a66b3857").unwrap(),
pending_map_inner.clone(),
);
pending_map_inner.clear();
pending_map_inner.insert(
"40".to_string(),
TxpoolInspectSummary {
to: None,
value: U256::from(0u128),
gas: U256::from(612412u128),
gas_price: U256::from(6000000000u128),
},
);
pending_map.insert(
Address::from_str("00000000863B56a3C1f0F1be8BC4F8b7BD78F57a").unwrap(),
pending_map_inner,
);
let mut queued_map = BTreeMap::new();
@ -436,18 +456,18 @@ mod tests {
"7".to_string(),
TxpoolInspectSummary {
to: Some(Address::from_str("3479BE69e07E838D9738a301Bb0c89e8EA2Bef4a").unwrap()),
value: U256::from(1000000000000000u64),
gas: U256::from(21000u64),
gas_price: U256::from(10000000000u64),
value: U256::from(1000000000000000u128),
gas: U256::from(21000u128),
gas_price: U256::from(10000000000u128),
},
);
queued_map_inner.insert(
"8".to_string(),
TxpoolInspectSummary {
to: Some(Address::from_str("73Aaf691bc33fe38f86260338EF88f9897eCaa4F").unwrap()),
value: U256::from(1000000000000000u64),
gas: U256::from(21000u64),
gas_price: U256::from(10000000000u64),
value: U256::from(1000000000000000u128),
gas: U256::from(21000u128),
gas_price: U256::from(10000000000u128),
},
);
queued_map.insert(
@ -459,9 +479,9 @@ mod tests {
"3".to_string(),
TxpoolInspectSummary {
to: Some(Address::from_str("73Aaf691bc33fe38f86260338EF88f9897eCaa4F").unwrap()),
value: U256::from(10000000000000000u64),
gas: U256::from(21000u64),
gas_price: U256::from(10000000000u64),
value: U256::from(10000000000000000u128),
gas: U256::from(21000u128),
gas_price: U256::from(10000000000u128),
},
);
queued_map.insert(