Skip to content

Commit

Permalink
Have the Coordinator scan the Substrate genesis block
Browse files Browse the repository at this point in the history
Also adds a workflow for running tests/coordinator.
  • Loading branch information
kayabaNerve committed Aug 2, 2023
1 parent d5c787f commit aab8a41
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 11 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/coordinator-tests.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions coordinator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub async fn scan_substrate<D: Db, Pro: Processors>(
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(
Expand All @@ -126,7 +126,7 @@ pub async fn scan_substrate<D: Db, Pro: Processors>(
},
&processors,
&serai,
&mut last_substrate_block,
&mut next_substrate_block,
)
.await
{
Expand Down
4 changes: 2 additions & 2 deletions coordinator/src/substrate/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ impl<D: Db> SubstrateDb<D> {
fn block_key() -> Vec<u8> {
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())
}

Expand Down
10 changes: 5 additions & 5 deletions coordinator/src/substrate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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}");
}

Expand Down
4 changes: 3 additions & 1 deletion tests/coordinator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion tests/coordinator/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ fn new_test() -> (Vec<(Handles, <Ristretto as Ciphersuite>::F)>, DockerTest) {
(coordinators, test)
}


#[test]
fn stack_test() {
let (_coordinators, test) = new_test();
Expand Down

0 comments on commit aab8a41

Please sign in to comment.