Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition between WAL and ledger state synchronization #379

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/serve/grpc/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ fn point_to_reset_tip_response(point: ChainPoint) -> u5c::sync::FollowTipRespons

pub struct SyncServiceImpl {
wal: wal::redb::WalStore,
ledger: LedgerStore,
mapper: interop::Mapper<LedgerStore>,
}

impl SyncServiceImpl {
pub fn new(wal: wal::redb::WalStore, ledger: LedgerStore) -> Self {
Self {
wal,
ledger: ledger.clone(),
mapper: Mapper::new(ledger),
}
}
Expand Down Expand Up @@ -179,8 +181,6 @@ impl u5c::sync::sync_service_server::SyncService for SyncServiceImpl {
.ok_or(Status::internal("can't find WAL sequence"))?
};

let mapper = self.mapper.clone();

// Find the intersect, skip 1 block, then convert each to a tip response
// We skip 1 block to mimic the ouroboros chainsync miniprotocol convention
// We both agree that the intersection point is in our past, so it doesn't
Expand All @@ -190,9 +190,33 @@ impl u5c::sync::sync_service_server::SyncService for SyncServiceImpl {

let reset = once(async { Ok(point_to_reset_tip_response(point)) });

let forward = wal::WalStream::start(self.wal.clone(), from_seq)
.skip(1)
.map(move |(_, log)| Ok(wal_log_to_tip_response(&mapper, &log)));
let mapper = self.mapper.clone();
let ledger = self.ledger.clone();
let wal = self.wal.clone();

let forward =
wal::WalStream::start(wal.clone(), from_seq)
.skip(1)
.then(move |(wal_seq, log)| {
let mapper = mapper.clone();
let ledger = ledger.clone();
let wal = wal.clone();

async move {
// Wait for ledger to catch up before processing the block
loop {
if let Ok(Some(ledger_cursor)) = ledger.cursor() {
let ledger_seq = wal.assert_point(&ledger_cursor.into()).unwrap();
// Check if ledger has caught up to this block
if ledger_seq >= wal_seq {
break;
}
}
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
Ok(wal_log_to_tip_response(&mapper, &log))
}
});

let stream = reset.chain(forward);

Expand Down
7 changes: 7 additions & 0 deletions src/wal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ledger;
use itertools::Itertools;
use pallas::network::miniprotocols::Point as PallasPoint;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -37,6 +38,12 @@ impl PartialEq for ChainPoint {
}
}

impl From<ledger::ChainPoint> for ChainPoint {
fn from(value: ledger::ChainPoint) -> Self {
crate::wal::ChainPoint::Specific(value.0, value.1)
}
}

impl From<PallasPoint> for ChainPoint {
fn from(value: PallasPoint) -> Self {
match value {
Expand Down