Skip to content
This repository has been archived by the owner on Apr 13, 2021. It is now read-only.

Add sync --upload-all to upload the entire local storage #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/database.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use bitcoin::network::serialize::BitcoinHash;
use bitcoin::network::serialize::deserialize;
use bitcoin::OutPoint;
use bitcoin::util::hash::Sha256dHash;
use regex::Regex;
use rgb::proof::Proof;
use std::collections::HashMap;
use std::fs;
use std::io::Read;
use std::io::Write;
Expand Down Expand Up @@ -30,9 +33,43 @@ impl Database {
}
}

pub fn get_proofs_for(&self, outpoint: &OutPoint) -> Vec<Proof> {
use bitcoin::network::serialize::deserialize;
pub fn list_local_proofs(&self) -> HashMap<Sha256dHash, Vec<Proof>> {
// Regex to match <txid>:<vout> folders
let outpoint_folder_regex = Regex::new(r"(?i)^[0-9a-f]{64}:[0-9]+$").unwrap();

let mut ans = HashMap::new();

for outpoint_dir in self.basedir.as_path().read_dir().expect("read_dir call failed") {
if let Ok(outpoint_dir) = outpoint_dir {
let file_name = outpoint_dir.file_name();
let folder_name = file_name.to_str();

if outpoint_folder_regex.is_match(folder_name.unwrap()) {
let txid = Sha256dHash::from_hex(folder_name.unwrap().split(":").next().unwrap()).unwrap();

let mut proofs = Vec::new();

for entry in outpoint_dir.path().read_dir().expect("read_dir call failed") {
if let Ok(entry) = entry {
let mut file = fs::File::open(entry.path()).unwrap();
let mut buffer: Vec<u8> = Vec::new();

file.read_to_end(&mut buffer);

let decoded: Proof = deserialize(&mut buffer).unwrap();
proofs.push(decoded);
}
}

ans.insert(txid, proofs);
}
}
}

ans
}

pub fn get_proofs_for(&self, outpoint: &OutPoint) -> Vec<Proof> {
let mut ans = Vec::new();

let outpoint_str = outpoint.txid.be_hex_string() + ":" + outpoint.vout.to_string().as_str();
Expand Down
26 changes: 23 additions & 3 deletions src/kaleidoscope/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,34 @@ pub struct Sync {}
impl<'a> RGBSubCommand<'a> for Sync {
fn run(matches: &'a ArgMatches<'a>, config: &Config, database: &mut Database, client: &mut Client) -> Result<(), jsonrpc::Error> {
let server = String::from(matches.value_of("server").unwrap_or(config.rgb_server.as_str()));

let unspent_utxos = rpc_list_unspent(client).unwrap();

for (outpoint, amount) in unspent_utxos {
for p in database.get_proofs_for(&outpoint) { // first upload
let proofs_to_upload = if matches.is_present("upload-all") {
database.list_local_proofs()
} else {
let mut temp_map = HashMap::new();

for (outpoint, _) in &unspent_utxos {
temp_map.insert(outpoint.txid.clone(), database.get_proofs_for(&outpoint));
}

temp_map
};


// Upload

for (txid, proofs) in proofs_to_upload {
for p in proofs {
println!(" --> Uploaded proof {}", p.bitcoin_hash());
upload_proofs(&server, &p, &outpoint.txid);
upload_proofs(&server, &p, &txid);
}
}

// Download

for (outpoint, _) in &unspent_utxos {
// ---------------------------- // TODO do not re-download proofs we already have

// then download
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate clap;
extern crate core;
extern crate hyper;
extern crate jsonrpc;
extern crate regex;
extern crate rgb;

use clap::{App, Arg, SubCommand};
Expand Down Expand Up @@ -81,7 +82,12 @@ fn main() {
.takes_value(true)
.long("server")
.value_name("SERVER[:PORT]")
.help("Overrides the default server")))
.help("Overrides the default server"))
.arg(Arg::with_name("upload-all")
.long("upload-all")
.short("a")
.help("Upload every proof instead of the ones you own"))
)
.subcommand(SubCommand::with_name("sendtoaddress")
.about("Send some RGB tokens to a specified address")
.arg(Arg::with_name("address")
Expand Down