Minor cleanup, range proof pmmr validation and tests

This commit is contained in:
Ignotus Peverell 2018-02-24 23:39:26 +00:00
parent 3ab616b8be
commit 341a3c9f4d
No known key found for this signature in database
GPG key ID: 99CD25F39F8F8211
4 changed files with 20 additions and 57 deletions

View file

@ -379,7 +379,6 @@ impl<'a> Extension<'a> {
// Now prune the utxo_pmmr, rproof_pmmr and their storage. // Now prune the utxo_pmmr, rproof_pmmr and their storage.
// Input is not valid if we cannot prune successfully (to spend an unspent output). // Input is not valid if we cannot prune successfully (to spend an unspent output).
// TODO: rm log, skip list for utxo and range proofs
match self.utxo_pmmr.prune(pos, height as u32) { match self.utxo_pmmr.prune(pos, height as u32) {
Ok(true) => { Ok(true) => {
self.rproof_pmmr self.rproof_pmmr
@ -628,7 +627,7 @@ impl<'a> Extension<'a> {
Ok((sum_kernel, fees)) Ok((sum_kernel, fees))
} }
/// Sums all our UTXO commitments /// Sums all our UTXO commitments, checking range proofs at the same time
fn sum_utxos(&self) -> Result<Commitment, Error> { fn sum_utxos(&self) -> Result<Commitment, Error> {
let mut sum_utxo = None; let mut sum_utxo = None;
let mut utxo_count = 0; let mut utxo_count = 0;
@ -636,11 +635,19 @@ impl<'a> Extension<'a> {
for n in 1..self.utxo_pmmr.unpruned_size()+1 { for n in 1..self.utxo_pmmr.unpruned_size()+1 {
if pmmr::bintree_postorder_height(n) == 0 { if pmmr::bintree_postorder_height(n) == 0 {
if let Some((_,output)) = self.utxo_pmmr.get(n, true) { if let Some((_,output)) = self.utxo_pmmr.get(n, true) {
if n == 1 { let out = output.expect("not a leaf node");
sum_utxo = Some(output.expect("not a leaf node").commit); let commit = out.commit.clone();
match self.rproof_pmmr.get(n, true) {
Some((_, Some(rp))) => out.to_output(rp).verify_proof()?,
res => {
return Err(Error::OutputNotFound);
}
}
if let None = sum_utxo {
sum_utxo = Some(commit);
} else { } else {
let secp = secp.lock().unwrap(); let secp = secp.lock().unwrap();
sum_utxo = Some(secp.commit_sum(vec![sum_utxo.unwrap(), output.expect("not a leaf node").commit], vec![])?); sum_utxo = Some(secp.commit_sum(vec![sum_utxo.unwrap(), commit], vec![])?);
} }
utxo_count += 1; utxo_count += 1;
} }
@ -651,42 +658,6 @@ impl<'a> Extension<'a> {
} }
} }
/*fn store_element<T>(file_store: &mut FlatFileStore<T>, data: T)
-> Result<(), String>
where
T: ser::Readable + ser::Writeable + Clone
{
file_store.append(vec![data])
}
fn read_element_at_pmmr_index<T>(file_store: &FlatFileStore<T>, pos: u64) -> Option<T>
where
T: ser::Readable + ser::Writeable + Clone
{
let leaf_index = pmmr::leaf_index(pos);
// flat files are zero indexed
file_store.get(leaf_index - 1)
}
fn _remove_element_at_pmmr_index<T>(file_store: &mut FlatFileStore<T>, pos: u64)
-> Result<(), String>
where
T: ser::Readable + ser::Writeable + Clone
{
let leaf_index = pmmr::leaf_index(pos);
// flat files are zero indexed
file_store.remove(vec![leaf_index - 1])
}
fn rewind_to_pmmr_index<T>(file_store: &mut FlatFileStore<T>, pos: u64) -> Result<(), String>
where
T: ser::Readable + ser::Writeable + Clone
{
let leaf_index = pmmr::leaf_index(pos);
// flat files are zero indexed
file_store.rewind(leaf_index - 1)
}*/
/// Output and kernel MMR indexes at the end of the provided block. /// Output and kernel MMR indexes at the end of the provided block.
/// This requires us to know the "last" output processed in the block /// This requires us to know the "last" output processed in the block
/// and needs to be consistent with how we originally processed /// and needs to be consistent with how we originally processed

View file

@ -117,9 +117,10 @@ fn mine_empty_chain() {
// now check the block height index // now check the block height index
let header_by_height = chain.get_header_by_height(n).unwrap(); let header_by_height = chain.get_header_by_height(n).unwrap();
assert_eq!(header_by_height.hash(), bhash); assert_eq!(header_by_height.hash(), bhash);
}
chain.validate().unwrap(); chain.validate().unwrap();
} }
}
#[test] #[test]
fn mine_forks() { fn mine_forks() {
@ -281,6 +282,7 @@ fn spend_in_fork() {
let next = prepare_block_tx(&kc, &fork_head, &chain, 7, vec![&tx1]); let next = prepare_block_tx(&kc, &fork_head, &chain, 7, vec![&tx1]);
let prev_main = next.header.clone(); let prev_main = next.header.clone();
chain.process_block(next.clone(), chain::Options::SKIP_POW).unwrap(); chain.process_block(next.clone(), chain::Options::SKIP_POW).unwrap();
chain.validate().unwrap();
println!("tx 1 processed, should have 6 outputs or 396 bytes in file, first skipped"); println!("tx 1 processed, should have 6 outputs or 396 bytes in file, first skipped");
@ -296,6 +298,7 @@ fn spend_in_fork() {
let next = prepare_block_tx(&kc, &prev_main, &chain, 9, vec![&tx2]); let next = prepare_block_tx(&kc, &prev_main, &chain, 9, vec![&tx2]);
let prev_main = next.header.clone(); let prev_main = next.header.clone();
chain.process_block(next, chain::Options::SKIP_POW).unwrap(); chain.process_block(next, chain::Options::SKIP_POW).unwrap();
chain.validate().unwrap();
println!("tx 2 processed"); println!("tx 2 processed");
/*panic!("Stop");*/ /*panic!("Stop");*/
@ -308,6 +311,7 @@ fn spend_in_fork() {
let fork_next = prepare_fork_block_tx(&kc, &prev_fork, &chain, 8, vec![&tx2]); let fork_next = prepare_fork_block_tx(&kc, &prev_fork, &chain, 8, vec![&tx2]);
let prev_fork = fork_next.header.clone(); let prev_fork = fork_next.header.clone();
chain.process_block(fork_next, chain::Options::SKIP_POW).unwrap(); chain.process_block(fork_next, chain::Options::SKIP_POW).unwrap();
chain.validate().unwrap();
// check state // check state
let head = chain.head_header().unwrap(); let head = chain.head_header().unwrap();
@ -320,6 +324,7 @@ fn spend_in_fork() {
let fork_next = prepare_fork_block(&kc, &prev_fork, &chain, 10); let fork_next = prepare_fork_block(&kc, &prev_fork, &chain, 10);
let prev_fork = fork_next.header.clone(); let prev_fork = fork_next.header.clone();
chain.process_block(fork_next, chain::Options::SKIP_POW).unwrap(); chain.process_block(fork_next, chain::Options::SKIP_POW).unwrap();
chain.validate().unwrap();
// check state // check state
let head = chain.head_header().unwrap(); let head = chain.head_header().unwrap();

View file

@ -863,15 +863,12 @@ impl OutputStoreable {
} }
/// Return a regular output /// Return a regular output
pub fn to_output(self) -> Output { pub fn to_output(self, rproof: RangeProof) -> Output {
Output{ Output{
features: self.features, features: self.features,
commit: self.commit, commit: self.commit,
switch_commit_hash: self.switch_commit_hash, switch_commit_hash: self.switch_commit_hash,
proof: RangeProof{ proof: rproof,
proof:[0; secp::constants::MAX_PROOF_SIZE],
plen: 0,
},
} }
} }
} }

View file

@ -43,7 +43,6 @@ pub struct AppendOnlyFile {
buffer_start: usize, buffer_start: usize,
buffer: Vec<u8>, buffer: Vec<u8>,
buffer_start_bak: usize, buffer_start_bak: usize,
unflushed_data_size: usize,
} }
impl AppendOnlyFile { impl AppendOnlyFile {
@ -61,7 +60,6 @@ impl AppendOnlyFile {
buffer_start: 0, buffer_start: 0,
buffer: vec![], buffer: vec![],
buffer_start_bak: 0, buffer_start_bak: 0,
unflushed_data_size: 0,
}; };
if let Ok(sz) = aof.size() { if let Ok(sz) = aof.size() {
if sz > 0 { if sz > 0 {
@ -75,7 +73,6 @@ impl AppendOnlyFile {
/// Append data to the file. Until the append-only file is synced, data is /// Append data to the file. Until the append-only file is synced, data is
/// only written to memory. /// only written to memory.
pub fn append(&mut self, buf: &mut Vec<u8>) { pub fn append(&mut self, buf: &mut Vec<u8>) {
self.unflushed_data_size += buf.len();
self.buffer.append(buf); self.buffer.append(buf);
} }
@ -102,7 +99,6 @@ impl AppendOnlyFile {
self.file.sync_data()?; self.file.sync_data()?;
self.buffer = vec![]; self.buffer = vec![];
self.mmap = Some(unsafe { memmap::Mmap::map(&self.file)? }); self.mmap = Some(unsafe { memmap::Mmap::map(&self.file)? });
self.unflushed_data_size = 0;
Ok(()) Ok(())
} }
@ -114,7 +110,6 @@ impl AppendOnlyFile {
self.buffer_start_bak = 0; self.buffer_start_bak = 0;
} }
self.buffer = vec![]; self.buffer = vec![];
self.unflushed_data_size = 0;
} }
/// Read length bytes of data at offset from the file. Leverages the memory /// Read length bytes of data at offset from the file. Leverages the memory
@ -186,11 +181,6 @@ impl AppendOnlyFile {
fs::metadata(&self.path).map(|md| md.len()) fs::metadata(&self.path).map(|md| md.len())
} }
/// Current size of file in bytes + size of unsaved data
pub fn size_with_unsaved(&self) -> u64 {
self.size().unwrap() + self.unflushed_data_size as u64
}
/// Path of the underlying file /// Path of the underlying file
pub fn path(&self) -> String { pub fn path(&self) -> String {
self.path.clone() self.path.clone()