Skip to content

Commit

Permalink
chore: review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dav1do committed Sep 12, 2024
1 parent 7106217 commit 535ada8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
25 changes: 25 additions & 0 deletions event/src/unvalidated/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@ fn get_time_event_witness_blocks(
blocks_in_path.push(serde_ipld_dagcbor::from_slice(block_bytes)?);
}

// make sure the last item truly does point to prev
match blocks_in_path
.last()
.expect("blocks_in_path is not empty")
.get(
*parts
.last()
.ok_or_else(|| anyhow!("Time Event path must have at least one item"))?,
)?
.ok_or_else(|| anyhow!("Time Event path failed to resolve last element"))?
{
Ipld::Link(cid) => {
if *cid != event.prev() {
bail!(
"the anchor commit proof {} with path {} points to invalid 'prev' commit. Expected prev='{}' but found '{}'",
event.proof,
event.path,
event.prev(),
cid,
)
}
}
_ => bail!("Time Event path does not index to a CID"),
}

Ok(blocks_in_path)
}

Expand Down
48 changes: 18 additions & 30 deletions validation/src/blockchain/eth_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ const BLOCK_CACHE_SIZE: usize = 50;
static HTTP_CLIENT: Lazy<reqwest::Client> = Lazy::new(reqwest::Client::new);

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
/// A blockchain transaction
pub struct ChainTransaction {
/// Transaction hash
pub hash: String,
/// Transaction contract input
pub input: String,
pub block: Option<BlockResponse>,
/// Information about the block in which this transaction was mined.
/// If None, the transaction exists but has not been mined yet.
pub block: Option<ChainBlock>,
}

impl ChainTransaction {
Expand All @@ -44,9 +47,12 @@ impl ChainTransaction {
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct BlockResponse {
/// A blockchain block
pub struct ChainBlock {
pub hash: String,
/// the block number
pub number: i64,
/// the unix epoch timestamp
pub timestamp: i64,
}

Expand All @@ -66,29 +72,33 @@ pub struct RpcResponse<T> {

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
/// A contract transaction. Expects fields (e.g. input) to exist that aren't required for all transactions.
pub struct EthTransaction {
/// The expected payload for eth_getTransactionByHash. It's a contract transaction
/// and expects fields (e.g. input) to exist that aren't required for all transactions.
struct EthTransaction {
/// Block hash if mined
block_hash: Option<String>,
/// Transaction hash
hash: String,
/// contract input data
/// Contract input data
input: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
/// The block information returned by eth_getBlockByHash
#[serde(rename_all = "camelCase")]
pub struct EthBlock {
struct EthBlock {
hash: String,
/// hexadecimal block number
number: String,
/// hexademical representing unix epoch time
timestamp: String,
}

impl TryFrom<EthBlock> for BlockResponse {
impl TryFrom<EthBlock> for ChainBlock {
type Error = anyhow::Error;

fn try_from(value: EthBlock) -> std::result::Result<Self, Self::Error> {
Ok(BlockResponse {
Ok(ChainBlock {
hash: value.hash,
number: i64_from_hex(&value.number).context("invalid block number")?,
timestamp: i64_from_hex(&value.timestamp).context("invalid block timestamp")?,
Expand Down Expand Up @@ -150,28 +160,6 @@ impl HttpEthRpc {
.ok_or_else(|| anyhow!("failed to retrieve chain ID"))
}

/// Get the latest block number
///
/// curl https://mainnet.infura.io/v3/{api_token} \
/// -X POST \
/// -H "Content-Type: application/json" \
/// -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params": [],"id":1}'
/// >> {"jsonrpc": "2.0", "id": 1, "result": "0x127cc18"}
async fn _eth_block_number(&self) -> Result<RpcResponse<String>> {
Ok(HTTP_CLIENT
.post(self.url.clone())
.json(&serde_json::json!({
"jsonrpc": "2.0",
"method": "eth_blockNumber",
"params": [],
"id": 1,
}))
.send()
.await?
.json()
.await?)
}

/// Get a block by its hash
///
/// curl https://mainnet.infura.io/v3/{api_token} \
Expand Down

0 comments on commit 535ada8

Please sign in to comment.