1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
//! Cardano chain follow module.
use std::{future::Future, path::PathBuf};
use pallas::network::{facades::PeerClient, miniprotocols::Point};
use tokio::{
sync::{mpsc, oneshot},
task::JoinHandle,
};
use crate::{
mithril_snapshot::MithrilSnapshot, Error, MultiEraBlockData, Network, PointOrTip, Result,
};
/// Default [`Follower`] block buffer size.
const DEFAULT_CHAIN_UPDATE_BUFFER_SIZE: usize = 32;
/// Enum of chain updates received by the follower.
pub enum ChainUpdate {
/// New block inserted on chain.
Block(MultiEraBlockData),
/// Chain rollback to the given block.
Rollback(MultiEraBlockData),
}
impl ChainUpdate {
/// Gets the chain update's block data.
#[must_use]
pub fn block_data(&self) -> &MultiEraBlockData {
match self {
ChainUpdate::Block(block_data) | ChainUpdate::Rollback(block_data) => block_data,
}
}
}
/// Builder used to create [`FollowerConfig`]s.
pub struct FollowerConfigBuilder {
/// Block buffer size option.
chain_update_buffer_size: usize,
/// Where to start following from.
follow_from: PointOrTip,
/// Path to the Mithril snapshot the follower should use.
mithril_snapshot_path: Option<PathBuf>,
}
impl Default for FollowerConfigBuilder {
fn default() -> Self {
Self {
chain_update_buffer_size: DEFAULT_CHAIN_UPDATE_BUFFER_SIZE,
follow_from: PointOrTip::Tip,
mithril_snapshot_path: None,
}
}
}
impl FollowerConfigBuilder {
/// Sets the size of the chain updates buffer used by the [`Follower`].
///
/// # Arguments
///
/// * `chain_update_buffer_size`: Size of the chain updates buffer.
#[must_use]
pub fn chain_update_buffer_size(mut self, block_buffer_size: usize) -> Self {
self.chain_update_buffer_size = block_buffer_size;
self
}
/// Sets the point at which the follower will start following from.
///
/// # Arguments
///
/// * `from`: Sync starting point.
#[must_use]
pub fn follow_from<P>(mut self, from: P) -> Self
where P: Into<PointOrTip> {
self.follow_from = from.into();
self
}
/// Sets the path of the Mithril snapshot the [`Follower`] will use.
///
/// # Arguments
///
/// * `path`: Mithril snapshot path.
#[must_use]
pub fn mithril_snapshot_path(mut self, path: PathBuf) -> Self {
self.mithril_snapshot_path = Some(path);
self
}
/// Builds a [`FollowerConfig`].
#[must_use]
pub fn build(self) -> FollowerConfig {
FollowerConfig {
chain_update_buffer_size: self.chain_update_buffer_size,
follow_from: self.follow_from,
mithril_snapshot_path: self.mithril_snapshot_path,
}
}
}
/// Configuration for the Cardano chain follower.
#[derive(Clone)]
pub struct FollowerConfig {
/// Configured chain update buffer size.
pub chain_update_buffer_size: usize,
/// Where to start following from.
pub follow_from: PointOrTip,
/// Path to the Mithril snapshot the follower should use.
pub mithril_snapshot_path: Option<PathBuf>,
}
/// Information used to connect to a client.
#[derive(Clone)]
struct ClientConnectInfo {
/// Node's address
address: String,
/// Network magic
network: Network,
}
/// Handler for receiving the read block response from the client.
pub struct ReadBlock(tokio::task::JoinHandle<Result<MultiEraBlockData>>);
impl Future for ReadBlock {
type Output = Result<MultiEraBlockData>;
fn poll(
mut self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
let p = &mut self.0;
// Using tokio pin instead of, e.g., pin-project because we use tokio as the async runtime
// lib for this crate.
tokio::pin!(p);
match p.poll(cx) {
std::task::Poll::Ready(res) => {
match res {
Ok(res) => std::task::Poll::Ready(res),
Err(_) => std::task::Poll::Ready(Err(Error::InternalError)),
}
},
std::task::Poll::Pending => std::task::Poll::Pending,
}
}
}
/// Handler for receiving the read block range response from the client.
pub struct ReadBlockRange(tokio::task::JoinHandle<Result<Vec<MultiEraBlockData>>>);
impl Future for ReadBlockRange {
type Output = Result<Vec<MultiEraBlockData>>;
fn poll(
mut self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
let p = &mut self.0;
// Using tokio pin instead of, e.g., pin-project because we use tokio as the async runtime
// lib for this crate.
tokio::pin!(p);
match p.poll(cx) {
std::task::Poll::Ready(res) => {
match res {
Ok(res) => std::task::Poll::Ready(res),
Err(_) => std::task::Poll::Ready(Err(Error::InternalError)),
}
},
std::task::Poll::Pending => std::task::Poll::Pending,
}
}
}
/// Cardano chain follower.
pub struct Follower {
/// Client connection information.
///
/// This is used to open more connections when needed.
client_connect_info: ClientConnectInfo,
/// Chain update receiver.
chain_update_rx: mpsc::Receiver<Result<ChainUpdate>>,
/// Follow task request sender.
follow_task_request_tx: mpsc::Sender<task::SetReadPointerRequest>,
/// Follow task thread join handle.
follow_task_join_handle: JoinHandle<()>,
/// Optional Mithril snapshot information.
mithril_snapshot: Option<MithrilSnapshot>,
}
impl Follower {
/// Connects the follower to a producer using the node-to-node protocol.
///
/// # Arguments
///
/// * `address`: Address of the node to connect to.
/// * `network`: The [Network] the client is assuming it's connecting to.
/// * `config`: Follower's configuration (see [`FollowerConfigBuilder`]).
///
/// # Errors
///
/// Returns Err if the connection could not be established.
pub async fn connect(address: &str, network: Network, config: FollowerConfig) -> Result<Self> {
let mut client = PeerClient::connect(address, network.into())
.await
.map_err(Error::Client)?;
let Some(follow_from) = set_client_read_pointer(&mut client, config.follow_from).await?
else {
return Err(Error::SetReadPointer);
};
let mithril_snapshot = if let Some(path) = config.mithril_snapshot_path {
Some(MithrilSnapshot::from_path(path)?)
} else {
None
};
let connect_info = ClientConnectInfo {
address: address.to_string(),
network,
};
let (task_request_tx, chain_update_rx, task_join_handle) = task::FollowTask::spawn(
client,
connect_info,
mithril_snapshot.clone(),
config.chain_update_buffer_size,
follow_from,
);
let client_connect_info = ClientConnectInfo {
address: address.to_string(),
network,
};
Ok(Self {
client_connect_info,
chain_update_rx,
follow_task_request_tx: task_request_tx,
follow_task_join_handle: task_join_handle,
mithril_snapshot,
})
}
/// Set the follower's chain read-pointer. Returns None if the point was
/// not found on the chain.
///
/// # Arguments
///
/// * `at`: Point at which to set the read-pointer.
///
/// # Errors
///
/// Returns Err if something went wrong while communicating with the producer.
pub async fn set_read_pointer<P>(&self, at: P) -> Result<Option<Point>>
where P: Into<PointOrTip> {
let (response_tx, response_rx) = oneshot::channel();
let req = task::SetReadPointerRequest {
at: at.into(),
response_tx,
};
self.follow_task_request_tx
.send(req)
.await
.map_err(|_| Error::FollowTaskNotRunning)?;
response_rx.await.map_err(|_| Error::FollowTaskNotRunning)?
}
/// Requests the client to read a block.
///
/// # Arguments
///
/// * `at`: Point at which to read the block.
#[must_use]
pub fn read_block<P>(&self, at: P) -> ReadBlock
where P: Into<PointOrTip> {
let connect_info = self.client_connect_info.clone();
let mithril_snapshot = self.mithril_snapshot.clone();
let at = at.into();
let join_handle = tokio::spawn(async move {
let mut client = PeerClient::connect(connect_info.address, connect_info.network.into())
.await
.map_err(Error::Client)?;
match at {
PointOrTip::Tip => {
let point = resolve_tip(&mut client).await?;
read_block_from_network(&mut client, point).await
},
PointOrTip::Point(point) => {
let snapshot_res = mithril_snapshot
.as_ref()
.and_then(|snapshot| snapshot.try_read_block(point.clone()).ok())
.flatten();
match snapshot_res {
Some(block_data) => {
tracing::trace!("Read block from Mithril snapshot");
Ok(block_data)
},
None => read_block_from_network(&mut client, point).await,
}
},
}
});
ReadBlock(join_handle)
}
/// Request the client to read a block range.
///
/// # Arguments
///
/// * `from`: Block range start.
/// * `to`: Block range end.
#[must_use]
pub fn read_block_range<P>(&self, from: Point, to: P) -> ReadBlockRange
where P: Into<PointOrTip> {
let connect_info = self.client_connect_info.clone();
let mithril_snapshot = self.mithril_snapshot.clone();
let to = to.into();
let join_handle = tokio::spawn(async move {
let mut client = PeerClient::connect(connect_info.address, connect_info.network.into())
.await
.map_err(Error::Client)?;
match to {
PointOrTip::Tip => {
let to_point = resolve_tip(&mut client).await?;
read_block_range_from_network(&mut client, from, to_point).await
},
PointOrTip::Point(to) => {
let snapshot_res = mithril_snapshot
.as_ref()
.and_then(|snapshot| {
snapshot.try_read_block_range(from.clone(), to.clone()).ok()
})
.flatten();
match snapshot_res {
Some((last_point_read, mut block_data_vec)) => {
// If we couldn't get all the blocks from the snapshot,
// try fetching the remaining ones from the network.
if last_point_read.slot_or_default() < to.slot_or_default() {
let network_blocks =
read_block_range_from_network(&mut client, last_point_read, to)
.await?;
// Discard 1st point as it's already been read from
// the snapshot
let mut network_blocks_iter = network_blocks.into_iter();
drop(network_blocks_iter.next());
block_data_vec.extend(network_blocks_iter);
}
Ok(block_data_vec)
},
None => read_block_range_from_network(&mut client, from, to).await,
}
},
}
});
ReadBlockRange(join_handle)
}
/// Receive the next chain update from the producer.
///
/// # Errors
///
/// Returns Err if any producer communication errors occurred.
pub async fn next(&mut self) -> Result<ChainUpdate> {
self.chain_update_rx
.recv()
.await
.ok_or(Error::FollowTaskNotRunning)?
}
/// Closes the follower connection and stops its background task.
///
/// # Errors
///
/// Returns Err if some error occurred in the background task.
pub async fn close(self) -> std::result::Result<(), tokio::task::JoinError> {
// NOTE(FelipeRosa): For now just abort the task since it needs no cancellation
self.follow_task_join_handle.abort();
self.follow_task_join_handle.await
}
}
/// Contains functions related to the Follower's background task.
mod task {
use pallas::{
ledger::traverse::MultiEraHeader,
network::{
facades::PeerClient,
miniprotocols::{chainsync, Point},
},
};
use tokio::sync::{mpsc, oneshot};
use super::{set_client_read_pointer, ChainUpdate, ClientConnectInfo};
use crate::{mithril_snapshot::MithrilSnapshot, Error, MultiEraBlockData, PointOrTip, Result};
/// Request the task to set the read pointer to the given point or to the
/// tip.
pub(super) struct SetReadPointerRequest {
/// Point at which to set the read pointer.
pub(super) at: PointOrTip,
/// The channel that will be used to send the request's response.
pub(super) response_tx: oneshot::Sender<Result<Option<Point>>>,
}
/// Holds state for a follow task.
pub(super) struct FollowTask {
/// Client connection info.
connect_info: ClientConnectInfo,
/// Optional Mithril Snapshot that will be used by the follow task when fetching
/// chain updates.
mithril_snapshot: Option<MithrilSnapshot>,
/// Request receiver.
request_rx: mpsc::Receiver<SetReadPointerRequest>,
/// Chain update sender.
chain_update_tx: mpsc::Sender<crate::Result<ChainUpdate>>,
}
impl FollowTask {
/// Spawn a follow task.
pub(super) fn spawn(
client: PeerClient, connect_info: ClientConnectInfo,
mithril_snapshot: Option<MithrilSnapshot>, buffer_size: usize, follow_from: Point,
) -> (
mpsc::Sender<SetReadPointerRequest>,
mpsc::Receiver<crate::Result<ChainUpdate>>,
tokio::task::JoinHandle<()>,
) {
let (request_tx, request_rx) = mpsc::channel(1);
let (chain_update_tx, chain_update_rx) = mpsc::channel(buffer_size);
let this = Self {
connect_info,
mithril_snapshot,
request_rx,
chain_update_tx,
};
(
request_tx,
chain_update_rx,
tokio::spawn(this.run(client, follow_from)),
)
}
/// Runs the follow task.
///
/// It keeps asking the connected node for new chain updates. Every update and
/// communication errors are sent through the channel to the follower.
///
/// Backpressure is achieved with the chain update channel's limited size.
async fn run(mut self, client: PeerClient, from: Point) {
let fetch_chain_updates_fut = Self::fetch_chain_updates(
client,
self.mithril_snapshot.as_ref(),
self.chain_update_tx.clone(),
from,
);
tokio::pin!(fetch_chain_updates_fut);
loop {
tokio::select! {
Some(SetReadPointerRequest { at, response_tx }) = self.request_rx.recv() => {
let res = PeerClient::connect(&self.connect_info.address, self.connect_info.network.into())
.await;
let Ok(mut client) = res else {
drop(response_tx.send(Err(crate::Error::SetReadPointer)));
continue;
};
match set_client_read_pointer(&mut client, at).await {
Ok(Some(from)) => {
fetch_chain_updates_fut.set(Self::fetch_chain_updates(
client,
self.mithril_snapshot.as_ref(),
self.chain_update_tx.clone(),
from.clone(),
));
drop(response_tx.send(Ok(Some(from))));
}
Ok(None) => {
drop(response_tx.send(Ok(None)));
}
Err(_) => {
drop(response_tx.send(Err(crate::Error::SetReadPointer)));
continue;
}
}
}
() = &mut fetch_chain_updates_fut => {}
}
}
}
/// Sends the next chain update to the follower.
/// This can be either read from the Mithril snapshot (if configured) or
/// from the N2N remote client.
async fn fetch_chain_updates(
mut client: PeerClient, mithril_snapshot: Option<&MithrilSnapshot>,
chain_update_tx: mpsc::Sender<crate::Result<ChainUpdate>>, from: Point,
) {
let mut current_point = from;
let set_to_snapshot = mithril_snapshot
.and_then(|snapshot| snapshot.try_read_blocks_from_point(current_point.clone()));
if let Some(iter) = set_to_snapshot {
let mut last_recv_from_snapshot = false;
for result in iter {
let mut fallback = false;
if let Ok(raw_block_data) = result {
let block_data = MultiEraBlockData(raw_block_data);
match block_data.decode() {
Ok(block) => {
current_point =
Point::Specific(block.slot(), block.hash().to_vec());
if chain_update_tx
.send(Ok(ChainUpdate::Block(block_data)))
.await
.is_err()
{
return;
}
last_recv_from_snapshot = true;
},
Err(_) => {
fallback = true;
},
}
} else {
fallback = true;
}
// If we, for any reason, we failed to get the block from the
// Mithril snapshot, fallback to the getting it from the client.
if fallback {
let res = set_client_read_pointer(
&mut client,
PointOrTip::Point(current_point.clone()),
)
.await;
match res {
Ok(Some(p)) => {
current_point = p;
if !Self::send_next_chain_update(
&mut client,
chain_update_tx.clone(),
)
.await
{
return;
}
},
Ok(None) | Err(_) => {
drop(
chain_update_tx
.send(Err(crate::Error::SetReadPointer))
.await,
);
return;
},
}
}
}
if last_recv_from_snapshot {
let res = set_client_read_pointer(
&mut client,
PointOrTip::Point(current_point.clone()),
)
.await;
if let Err(e) = res {
drop(chain_update_tx.send(Err(e)).await);
return;
}
// Skip the next update from the client since we've already
// read it the Mithril snapshot.
drop(Self::next_from_client(&mut client).await);
}
}
while Self::send_next_chain_update(&mut client, chain_update_tx.clone()).await {}
}
/// Waits for the next update from the node the client is connected to.
///
/// Is cancelled by closing the `chain_update_tx` receiver end (explicitly or by
/// dropping it).
async fn next_from_client(client: &mut PeerClient) -> crate::Result<Option<ChainUpdate>> {
tracing::trace!("Requesting next chain update");
let res = {
match client.chainsync().state() {
chainsync::State::CanAwait => client.chainsync().recv_while_can_await().await,
chainsync::State::MustReply => client.chainsync().recv_while_must_reply().await,
_ => client.chainsync().request_next().await,
}
.map_err(Error::Chainsync)?
};
tracing::trace!("Received block data from client");
match res {
chainsync::NextResponse::RollForward(header, _tip) => {
let decoded_header = MultiEraHeader::decode(
header.variant,
header.byron_prefix.map(|p| p.0),
&header.cbor,
)
.map_err(Error::Codec)?;
let point =
Point::Specific(decoded_header.slot(), decoded_header.hash().to_vec());
tracing::trace!(point = ?point, "Fetching roll forward block data");
let block_data = client
.blockfetch()
.fetch_single(point)
.await
.map_err(Error::Blockfetch)?;
Ok(Some(ChainUpdate::Block(MultiEraBlockData(block_data))))
},
chainsync::NextResponse::RollBackward(point, _tip) => {
tracing::trace!(point = ?point, "Fetching roll backward block data");
let block_data = client
.blockfetch()
.fetch_single(point)
.await
.map_err(Error::Blockfetch)?;
Ok(Some(ChainUpdate::Rollback(MultiEraBlockData(block_data))))
},
chainsync::NextResponse::Await => Ok(None),
}
}
/// Sends the next chain update through the follower's chain update channel.
async fn send_next_chain_update(
client: &mut PeerClient, chain_update_tx: mpsc::Sender<crate::Result<ChainUpdate>>,
) -> bool {
loop {
let res = Self::next_from_client(client).await;
match res {
Err(err) => {
if chain_update_tx.send(Err(err)).await.is_err() {
return false;
}
},
Ok(next_response) => {
if let Some(chain_update) = next_response {
if chain_update_tx.send(Ok(chain_update)).await.is_err() {
return false;
}
return true;
}
},
}
}
}
}
}
/// Sets the N2N remote client's read pointer.
async fn set_client_read_pointer(client: &mut PeerClient, at: PointOrTip) -> Result<Option<Point>> {
match at {
PointOrTip::Point(Point::Origin) => {
client
.chainsync()
.intersect_origin()
.await
.map(Some)
.map_err(Error::Chainsync)
},
PointOrTip::Point(p @ Point::Specific(..)) => {
client
.chainsync()
.find_intersect(vec![p])
.await
.map(|(point, _)| point)
.map_err(Error::Chainsync)
},
PointOrTip::Tip => {
client
.chainsync()
.intersect_tip()
.await
.map(Some)
.map_err(Error::Chainsync)
},
}
}
/// Finds the tip point.
///
/// NOTE: This changes the client's read pointer position.
#[inline]
async fn resolve_tip(client: &mut PeerClient) -> Result<Point> {
client
.chainsync()
.intersect_tip()
.await
.map_err(Error::Chainsync)
}
/// Reads a block from the network using the N2N client.
async fn read_block_from_network(
blockfetch_client: &mut PeerClient, point: Point,
) -> Result<MultiEraBlockData> {
// Used in tracing
let slot = point.slot_or_default();
let block_data = blockfetch_client
.blockfetch()
.fetch_single(point)
.await
.map_err(Error::Blockfetch)?;
tracing::trace!(slot, "Block read from n2n");
Ok(MultiEraBlockData(block_data))
}
/// Reads a range of blocks from the network using the N2N client.
async fn read_block_range_from_network(
blockfetch_client: &mut PeerClient, from: Point, to: Point,
) -> Result<Vec<MultiEraBlockData>> {
// Used in tracing
let from_slot = from.slot_or_default();
let to_slot = to.slot_or_default();
let data_vec = blockfetch_client
.blockfetch()
.fetch_range((from, to))
.await
.map_err(Error::Blockfetch)?
.into_iter()
.map(MultiEraBlockData)
.collect();
tracing::trace!(from_slot, to_slot, "Block range read from n2n");
Ok(data_vec)
}