Skip to content

Instantly share code, notes, and snippets.

@PlasmaPower
Last active December 19, 2018 06:17
Show Gist options
  • Save PlasmaPower/d9d529d5d6abfee18ae1290ececed232 to your computer and use it in GitHub Desktop.
Save PlasmaPower/d9d529d5d6abfee18ae1290ececed232 to your computer and use it in GitHub Desktop.
extern crate lmdb_zero as lmdb;
extern crate nanocurrency_types;
use lmdb::LmdbResultExt;
use nanocurrency_types::Account;
use std::collections::HashMap;
use std::env;
fn get_pattern(addr: &str) -> [u8; 64] {
let mut res = [0u8; 64];
let mut indicies = HashMap::new();
for (c, r) in addr.chars().zip(res.iter_mut()) {
let len = indicies.len();
*r = *indicies.entry(c).or_insert(len as u8);
}
res
}
fn main() {
let mut args = env::args();
args.next();
let env = unsafe {
let mut builder = lmdb::EnvBuilder::new().unwrap();
builder.set_maxdbs(64).unwrap();
builder
.open(
&args.next().expect("Expected DB path as first argument"),
lmdb::open::NOSUBDIR | lmdb::open::NOTLS,
0o600,
)
.unwrap()
};
let target_pattern = get_pattern(&args.next().expect("Expected target as second argument"));
let accounts_v0_db =
lmdb::Database::open(&env, Some("accounts"), &lmdb::DatabaseOptions::defaults()).unwrap();
let accounts_v1_db = lmdb::Database::open(
&env,
Some("accounts_v1"),
&lmdb::DatabaseOptions::defaults(),
)
.unwrap();
let pending_db =
lmdb::Database::open(&env, Some("pending"), &lmdb::DatabaseOptions::defaults()).unwrap();
let pending_v1_db =
lmdb::Database::open(&env, Some("pending_v1"), &lmdb::DatabaseOptions::defaults()).unwrap();
let handle_account = |account_slice| {
let mut account_arr = [0u8; 32];
account_arr.copy_from_slice(account_slice);
let account = Account(account_arr);
let account_s = account.to_string();
let pattern = get_pattern(&account_s);
if &pattern as &[u8] == &target_pattern as &[u8] {
println!("{}", account_s);
}
};
let txn = lmdb::ReadTransaction::new(&env).unwrap();
let access = txn.access();
let mut accounts_it = txn.cursor(&accounts_v0_db).unwrap();
let mut current_kv = accounts_it.first::<[u8], [u8]>(&access).to_opt().unwrap();
while let Some((account_slice, _)) = current_kv {
handle_account(account_slice);
current_kv = accounts_it.next::<[u8], [u8]>(&access).to_opt().unwrap();
}
accounts_it = txn.cursor(&accounts_v1_db).unwrap();
let mut current_kv = accounts_it.first::<[u8], [u8]>(&access).to_opt().unwrap();
while let Some((account_slice, _)) = current_kv {
handle_account(account_slice);
current_kv = accounts_it.next::<[u8], [u8]>(&access).to_opt().unwrap();
}
let mut pending_it = txn.cursor(&pending_db).unwrap();
current_kv = pending_it.first::<[u8], [u8]>(&access).to_opt().unwrap();
while let Some((pending_key, _)) = current_kv {
handle_account(&pending_key[..32]);
current_kv = pending_it.next::<[u8], [u8]>(&access).to_opt().unwrap();
}
pending_it = txn.cursor(&pending_v1_db).unwrap();
current_kv = pending_it.first::<[u8], [u8]>(&access).to_opt().unwrap();
while let Some((pending_key, _)) = current_kv {
handle_account(&pending_key[..32]);
current_kv = pending_it.next::<[u8], [u8]>(&access).to_opt().unwrap();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment