Skip to content

Commit

Permalink
test(550): Block hash verification test
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-l committed Feb 12, 2024
1 parent db233cd commit bd5eb40
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
14 changes: 11 additions & 3 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ jobs:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- run: rustup target add wasm32-unknown-unknown
- run: cargo test --all
- run: cargo check -p beerus-core --target wasm32-unknown-unknown
- name: Run the tests
env:
BEERUS_VERSION: "ci-test"
NETWORK: "mainnet"
ETH_EXECUTION_RPC: ${{ secrets.ETH_EXECUTION_RPC_MAINNET }}
STARKNET_RPC: ${{ secrets.STARKNET_RPC_MAINNET }}
run: cargo test --all
- name: Check against wasm32
run: |
rustup target add wasm32-unknown-unknown
cargo check -p beerus-core --target wasm32-unknown-unknown
clippy:
runs-on: ubuntu-latest
Expand Down
65 changes: 65 additions & 0 deletions crates/core/tests/block_hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use beerus_core::block_hash::compute_block_hash;
use starknet::{
core::types::{
BlockId, BlockTag, Event, EventFilter, MaybePendingBlockWithTxs,
},
providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider},
};
use url::Url;

#[tokio::test]
async fn verify_latest_block_hash() {
let rpc_client = {
let rpc_url = std::env::var("STARKNET_RPC")
.expect("Missing STARKNET_RPC env var");
JsonRpcClient::new(HttpTransport::new(Url::parse(&rpc_url).unwrap()))
};

let block_id = BlockId::Tag(BlockTag::Latest);
let block = rpc_client.get_block_with_txs(block_id).await.unwrap();

let block = match block {
MaybePendingBlockWithTxs::Block(block) => block,
_ => panic!("unexpected block response type"),
};

let page_size = 1024;
let mut events = vec![];
let mut last_token = None;
let mut continue_ = true;

while continue_ {
let events_page = rpc_client
.get_events(
EventFilter {
from_block: Some(block_id),
to_block: Some(block_id),
address: None,
keys: None,
},
last_token,
page_size,
)
.await
.unwrap();
last_token = events_page.continuation_token;

if last_token.is_none() {
continue_ = false;
}

let mut new_events = events_page
.events
.into_iter()
.map(|e| Event {
from_address: e.from_address,
keys: e.keys,
data: e.data,
})
.collect::<Vec<Event>>();
events.append(&mut new_events);
}

let expected = block.block_hash;
assert_eq!(compute_block_hash(&block, &events), expected);
}

0 comments on commit bd5eb40

Please sign in to comment.