Skip to content

Commit

Permalink
docs: Add block download example
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega committed Jan 17, 2022
1 parent d15b397 commit f4f7a60
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/block-download/.gitignore
@@ -0,0 +1,4 @@
/target

scratchpad
.DS_Store
14 changes: 14 additions & 0 deletions examples/block-download/Cargo.toml
@@ -0,0 +1,14 @@
[package]
name = "block-download"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
pallas = { path = "../../pallas" }
net2 = "0.2.37"
env_logger = "0.9.0"
hex = "0.4.3"

[workspace]
56 changes: 56 additions & 0 deletions examples/block-download/src/main.rs
@@ -0,0 +1,56 @@
use net2::TcpStreamExt;

use pallas::ledger::alonzo::*;
use pallas::ouroboros::network::blockfetch::{BatchClient, Observer};
use pallas::ouroboros::network::handshake::{
n2n::{Client, VersionTable},
MAINNET_MAGIC,
};
use pallas::ouroboros::network::machines::primitives::Point;
use pallas::ouroboros::network::machines::run_agent;
use pallas::ouroboros::network::multiplexer::Multiplexer;
use std::net::TcpStream;

#[derive(Debug)]
struct BlockPrinter;

impl Observer for BlockPrinter {
fn on_block_received(&self, body: Vec<u8>) -> Result<(), Box<dyn std::error::Error>> {
println!("{}", hex::encode(&body));
println!("----------");
BlockWrapper::decode_fragment(&body[..])?;
Ok(())
}
}

fn main() {
env_logger::init();

let bearer = TcpStream::connect("relays-new.cardano-mainnet.iohk.io:3001").unwrap();
bearer.set_nodelay(true).unwrap();
bearer.set_keepalive_ms(Some(30_000u32)).unwrap();

let mut muxer = Multiplexer::setup(bearer, &vec![0, 3]).unwrap();

let mut hs_channel = muxer.use_channel(0);
let versions = VersionTable::v4_and_above(MAINNET_MAGIC);
let last = run_agent(Client::initial(versions), &mut hs_channel).unwrap();

let range = (
Point(
26249860,
hex::decode("915386f44ad3a7fccee949c9d3fe43f5a20459c7401f990e1cc7d52c10be1fd6")
.unwrap(),
),
Point(
26250057,
hex::decode("5fec758c8aaff4a7683c27b075dc3984d8d982839cc56470a682d1411c9f8198")
.unwrap(),
),
);

let mut bf_channel = muxer.use_channel(3);
let bf = BatchClient::initial(range, BlockPrinter {});
let bf_last = run_agent(bf, &mut bf_channel);
println!("{:?}", bf_last);
}

0 comments on commit f4f7a60

Please sign in to comment.