Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jsdw committed Nov 9, 2023
1 parent 76926cd commit d12b195
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions bin/archive-demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
use desub::{Chain, Decoder};
use subxt::{
config::PolkadotConfig,
backend::{ legacy::{ LegacyRpcMethods, rpc_methods::{ NumberOrHex, Bytes } }, rpc::{ RpcClient, rpc_params } }, ext::codec::Decode
backend::{
legacy::{
rpc_methods::{Bytes, NumberOrHex},
LegacyRpcMethods,
},
rpc::{rpc_params, RpcClient},
},
config::PolkadotConfig,
};
use desub::{ Decoder, Chain };

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
pretty_env_logger::init();

// Connect to a node with an RPC client:
let rpc_client = RpcClient::from_url("wss://rpc.polkadot.io").await?;
let methods = LegacyRpcMethods::<PolkadotConfig>::new(rpc_client.clone());
// Connect to a node with an RPC client:
let rpc_client = RpcClient::from_url("wss://rpc.polkadot.io").await?;
let methods = LegacyRpcMethods::<PolkadotConfig>::new(rpc_client.clone());

let mut bn = 1;
let mut decoder = Decoder::new(Chain::Polkadot);
let mut bn = 1;
let mut decoder = Decoder::new(Chain::Polkadot);

loop {
let hash = methods.chain_get_block_hash(Some(NumberOrHex::Number(bn))).await?.unwrap();
let runtime_version = methods.state_get_runtime_version(Some(hash)).await?;
let spec_version = runtime_version.spec_version;
let block = methods.chain_get_block(Some(hash)).await?.unwrap();
loop {
let hash = methods.chain_get_block_hash(Some(NumberOrHex::Number(bn))).await?.unwrap();
let runtime_version = methods.state_get_runtime_version(Some(hash)).await?;
let spec_version = runtime_version.spec_version;
let block = methods.chain_get_block(Some(hash)).await?.unwrap();

let ext_bytes = make_extrinsic_bytes(block.block.extrinsics);
let ext_bytes = make_extrinsic_bytes(block.block.extrinsics);

if !decoder.has_version(spec_version) {
// download the relevant metadata bytes, since the decoder doesn't have it yet.
let md: Bytes = rpc_client
.request("state_getMetadata", rpc_params![hash])
.await?;
decoder.register_version(spec_version, &md.0)?;
}
if !decoder.has_version(spec_version) {
// download the relevant metadata bytes, since the decoder doesn't have it yet.
let md: Bytes = rpc_client.request("state_getMetadata", rpc_params![hash]).await?;
decoder.register_version(spec_version, &md.0)?;
}

println!("# Decoding exts for block {bn}");
let decoded_exts = decoder.decode_extrinsics(spec_version, &ext_bytes)?;
println!("# Decoding exts for block {bn}");
let decoded_exts = decoder.decode_extrinsics(spec_version, &ext_bytes)?;

println!("{decoded_exts}");
bn += 10_000;
}
println!("{decoded_exts}");
bn += 10_000;
}
}

// A hack because we get the exts back as a vec of bytes and
// desub wants the whole block body as bytes.
fn make_extrinsic_bytes(exts: Vec<Bytes>) -> Vec<u8> {
use subxt::utils::Encoded;
use subxt::ext::codec::Encode;
// The inner `Bytes` are already encoded and contain the compact length etc,
// so don't encode them again by wrapping them in `Encoded`.
let e: Vec<Encoded> = exts.into_iter().map(|ext| Encoded(ext.0)).collect();
e.encode()
}
use subxt::ext::codec::Encode;
use subxt::utils::Encoded;
// The inner `Bytes` are already encoded and contain the compact length etc,
// so don't encode them again by wrapping them in `Encoded`.
let e: Vec<Encoded> = exts.into_iter().map(|ext| Encoded(ext.0)).collect();
e.encode()
}

0 comments on commit d12b195

Please sign in to comment.