Created
February 5, 2019 21:55
-
-
Save rust-play/76bda0fd58f4033bc7d5c68b75ff112a to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate itertools; | |
extern crate tokio_core; | |
extern crate web3; | |
use itertools::Itertools; | |
use tokio::runtime::Runtime; | |
use web3::{ | |
futures::{self, Future, Stream}, | |
types::{Block, BlockId, BlockNumber, Transaction}, | |
}; | |
// only restricts requests that will be sent paralllely in a batch request. | |
const MAX_PARALLEL_REQUESTS: usize = 128; | |
fn main() { | |
let mut event_loop = tokio_core::reactor::Core::new().unwrap(); | |
let transport = web3::transports::Http::with_event_loop( | |
"http://localhost:50000", | |
&event_loop.handle(), | |
MAX_PARALLEL_REQUESTS, | |
) | |
.unwrap(); | |
let remote = event_loop.remote(); | |
let web3 = web3::Web3::new(web3::transports::Batch::new(transport)); | |
let (low, high) = (0, 7_000_000); | |
for j in (low..high).chunks(50).into_iter() { | |
for i in j { | |
let block = web3 | |
.eth() | |
.block_with_txs(BlockId::Number(BlockNumber::Number(i))) | |
.then(move |block| { | |
// do stuff with block | |
Ok(()) | |
}); | |
println!("Added {:?}", i); | |
remote.spawn(move |_| block) | |
} | |
// submit_batch returns a BatchFuture | |
// https://docs.rs/web3/0.5.1/web3/transports/batch/struct.BatchFuture.html | |
// which has to be spawned in event loop to be actually executed. | |
// web3.transport().submit_batch() is a non-blocking operation | |
// and even when spawned does nothing until all the tasks have been spawned in event loop | |
let result = web3.transport().submit_batch().then(|_| Ok(())); | |
remote.spawn(move |_| result); | |
event_loop.turn(None); | |
} | |
loop { | |
event_loop.turn(None); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment