docs: Add block download example (#24)
This commit is contained in:
parent
d15b3974e3
commit
26d74796b4
3 changed files with 74 additions and 0 deletions
4
examples/block-download/.gitignore
vendored
Normal file
4
examples/block-download/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
/target
|
||||
|
||||
scratchpad
|
||||
.DS_Store
|
||||
14
examples/block-download/Cargo.toml
Normal file
14
examples/block-download/Cargo.toml
Normal file
|
|
@ -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
examples/block-download/src/main.rs
Normal file
56
examples/block-download/src/main.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue