From aab8a417db3399c98249c0b9eda20149106fe53f Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Wed, 2 Aug 2023 12:18:50 -0400 Subject: [PATCH] Have the Coordinator scan the Substrate genesis block Also adds a workflow for running tests/coordinator. --- .github/workflows/coordinator-tests.yml | 44 +++++++++++++++++++++++++ coordinator/src/main.rs | 4 +-- coordinator/src/substrate/db.rs | 4 +-- coordinator/src/substrate/mod.rs | 10 +++--- tests/coordinator/src/lib.rs | 4 ++- tests/coordinator/src/tests/mod.rs | 1 - 6 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/coordinator-tests.yml diff --git a/.github/workflows/coordinator-tests.yml b/.github/workflows/coordinator-tests.yml new file mode 100644 index 00000000..1374a552 --- /dev/null +++ b/.github/workflows/coordinator-tests.yml @@ -0,0 +1,44 @@ +name: Coordinator Tests + +on: + push: + branches: + - develop + paths: + - "common/**" + - "crypto/**" + - "coins/**" + - "message-queue/**" + - "orchestration/message-queue/**" + - "coordinator/**" + - "orchestration/coordinator/**" + - "tests/docker/**" + - "tests/coordinator/**" + + pull_request: + paths: + - "common/**" + - "crypto/**" + - "coins/**" + - "message-queue/**" + - "orchestration/message-queue/**" + - "coordinator/**" + - "orchestration/coordinator/**" + - "tests/docker/**" + - "tests/coordinator/**" + + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Install Build Dependencies + uses: ./.github/actions/build-dependencies + with: + github-token: ${{ inputs.github-token }} + + - name: Run coordinator Docker tests + run: cd tests/coordinator && GITHUB_CI=true cargo test diff --git a/coordinator/src/main.rs b/coordinator/src/main.rs index 5bc5440d..d04bd059 100644 --- a/coordinator/src/main.rs +++ b/coordinator/src/main.rs @@ -107,7 +107,7 @@ pub async fn scan_substrate( log::info!("scanning substrate"); let mut db = substrate::SubstrateDb::new(db); - let mut last_substrate_block = db.last_block(); + let mut next_substrate_block = db.next_block(); loop { match substrate::handle_new_blocks( @@ -126,7 +126,7 @@ pub async fn scan_substrate( }, &processors, &serai, - &mut last_substrate_block, + &mut next_substrate_block, ) .await { diff --git a/coordinator/src/substrate/db.rs b/coordinator/src/substrate/db.rs index c5fefdca..f301256c 100644 --- a/coordinator/src/substrate/db.rs +++ b/coordinator/src/substrate/db.rs @@ -14,12 +14,12 @@ impl SubstrateDb { fn block_key() -> Vec { Self::substrate_key(b"block", []) } - pub fn set_last_block(&mut self, block: u64) { + pub fn set_next_block(&mut self, block: u64) { let mut txn = self.0.txn(); txn.put(Self::block_key(), block.to_le_bytes()); txn.commit(); } - pub fn last_block(&self) -> u64 { + pub fn next_block(&self) -> u64 { u64::from_le_bytes(self.0.get(Self::block_key()).unwrap_or(vec![0; 8]).try_into().unwrap()) } diff --git a/coordinator/src/substrate/mod.rs b/coordinator/src/substrate/mod.rs index 3e90bcc8..819d9616 100644 --- a/coordinator/src/substrate/mod.rs +++ b/coordinator/src/substrate/mod.rs @@ -302,17 +302,17 @@ pub async fn handle_new_blocks< create_new_tributary: CNT, processors: &Pro, serai: &Serai, - last_block: &mut u64, + next_block: &mut u64, ) -> Result<(), SeraiError> { // Check if there's been a new Substrate block let latest = serai.get_latest_block().await?; let latest_number = latest.number(); - if latest_number == *last_block { + if latest_number < *next_block { return Ok(()); } let mut latest = Some(latest); - for b in (*last_block + 1) ..= latest_number { + for b in *next_block ..= latest_number { log::info!("found substrate block {b}"); handle_block( db, @@ -330,8 +330,8 @@ pub async fn handle_new_blocks< }, ) .await?; - *last_block += 1; - db.set_last_block(*last_block); + *next_block += 1; + db.set_next_block(*next_block); log::info!("handled substrate block {b}"); } diff --git a/tests/coordinator/src/lib.rs b/tests/coordinator/src/lib.rs index 26033941..52b6b815 100644 --- a/tests/coordinator/src/lib.rs +++ b/tests/coordinator/src/lib.rs @@ -6,7 +6,9 @@ use ciphersuite::{group::ff::PrimeField, Ciphersuite, Ristretto}; use serai_client::primitives::NetworkId; -use dockertest::{PullPolicy, Image, LogAction, LogPolicy, LogSource, LogOptions, StartPolicy, Composition}; +use dockertest::{ + PullPolicy, Image, LogAction, LogPolicy, LogSource, LogOptions, StartPolicy, Composition, +}; #[cfg(test)] mod tests; diff --git a/tests/coordinator/src/tests/mod.rs b/tests/coordinator/src/tests/mod.rs index 32acc596..ce57fe73 100644 --- a/tests/coordinator/src/tests/mod.rs +++ b/tests/coordinator/src/tests/mod.rs @@ -30,7 +30,6 @@ fn new_test() -> (Vec<(Handles, ::F)>, DockerTest) { (coordinators, test) } - #[test] fn stack_test() { let (_coordinators, test) = new_test();