From 1143d842389858cd710a4fe714645ab5d8ae4fb2 Mon Sep 17 00:00:00 2001 From: Yeastplume Date: Mon, 5 Mar 2018 19:33:44 +0000 Subject: [PATCH] Remove Sumtree References and disambiguate some naming (#747) * start of renamathon * api renaming * Rename UTXO-Output to lessen ambiguity * compile warning * compile error * readme fix * remove file commit in error --- README.md | 2 +- api/src/client.rs | 2 +- api/src/handlers.rs | 90 +++++------ api/src/lib.rs | 2 +- api/src/rest.rs | 2 +- api/src/types.rs | 62 +++---- chain/src/chain.rs | 108 ++++++------- chain/src/lib.rs | 4 +- chain/src/pipe.rs | 34 ++-- chain/src/store.rs | 2 +- chain/src/{sumtree.rs => txhashset.rs} | 216 ++++++++++++------------- chain/src/types.rs | 44 ++--- chain/tests/data_file_integrity.rs | 12 +- chain/tests/mine_simple_chain.rs | 14 +- chain/tests/store_indices.rs | 2 +- chain/tests/test_coinbase_maturity.rs | 10 +- config/src/config.rs | 2 +- config/src/lib.rs | 2 +- config/src/types.rs | 2 +- core/benches/sumtree.rs | 6 +- core/src/core/block.rs | 18 +-- core/src/core/hash.rs | 2 +- core/src/core/pmmr.rs | 2 +- core/src/core/target.rs | 2 +- core/src/core/transaction.rs | 12 +- core/src/genesis.rs | 2 +- core/src/lib.rs | 2 +- core/src/macros.rs | 2 +- core/src/ser.rs | 2 +- doc/code_structure.md | 2 +- doc/coinbase_maturity.md | 12 +- grin/src/adapters.rs | 26 +-- grin/src/lib.rs | 2 +- grin/src/miner.rs | 6 +- grin/src/seed.rs | 2 +- grin/src/server.rs | 2 +- grin/src/sync.rs | 24 +-- grin/src/types.rs | 2 +- grin/tests/api.rs | 100 ++++++------ grin/tests/framework/mod.rs | 2 +- grin/tests/simulnet.rs | 2 +- grin/tests/wallet.rs | 2 +- keychain/src/blind.rs | 2 +- keychain/src/extkey.rs | 2 +- keychain/src/keychain.rs | 2 +- keychain/src/lib.rs | 2 +- p2p/src/conn.rs | 2 +- p2p/src/handshake.rs | 2 +- p2p/src/lib.rs | 4 +- p2p/src/msg.rs | 34 ++-- p2p/src/peer.rs | 22 +-- p2p/src/peers.rs | 16 +- p2p/src/protocol.rs | 32 ++-- p2p/src/serv.rs | 8 +- p2p/src/store.rs | 2 +- p2p/src/types.rs | 24 +-- p2p/tests/peer_handshake.rs | 2 +- pool/src/blockchain.rs | 44 ++--- pool/src/graph.rs | 2 +- pool/src/lib.rs | 2 +- pool/src/pool.rs | 40 ++--- pool/src/types.rs | 6 +- pow/src/cuckoo.rs | 2 +- pow/src/lib.rs | 2 +- pow/src/plugin.rs | 2 +- pow/src/siphash.rs | 2 +- pow/src/types.rs | 2 +- src/bin/client.rs | 2 +- src/bin/grin.rs | 2 +- store/src/lib.rs | 2 +- store/src/pmmr.rs | 10 +- store/tests/pmmr.rs | 2 +- util/src/hex.rs | 2 +- util/src/lib.rs | 2 +- util/src/logger.rs | 2 +- util/src/types.rs | 2 +- util/src/zip.rs | 2 +- util/tests/zip.rs | 2 +- wallet/src/checker.rs | 14 +- wallet/src/client.rs | 2 +- wallet/src/handlers.rs | 2 +- wallet/src/info.rs | 2 +- wallet/src/lib.rs | 2 +- wallet/src/outputs.rs | 2 +- wallet/src/receiver.rs | 2 +- wallet/src/restore.rs | 14 +- wallet/src/sender.rs | 2 +- wallet/src/server.rs | 2 +- wallet/src/types.rs | 2 +- 89 files changed, 591 insertions(+), 591 deletions(-) rename chain/src/{sumtree.rs => txhashset.rs} (76%) diff --git a/README.md b/README.md index 0434fbd45..d6ed839e8 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Grin is an in-progress implementation of the MimbleWimble protocol. Many charact * Cuckoo Cycle proof of work (at least to start with). * Relatively fast block time (a minute or less, possibly decreasing as networks improve). * Fixed block reward, both over time and in blocks (fees are not additive). - * Transaction fees are based on the number of UTXO created/destroyed and total transaction size. + * Transaction fees are based on the number of Outputs created/destroyed and total transaction size. * Smooth curve for difficulty adjustments. To learn more, read our [introduction to MimbleWimble and Grin](doc/intro.md). diff --git a/api/src/client.rs b/api/src/client.rs index e7a17d8f3..9c73cd32d 100644 --- a/api/src/client.rs +++ b/api/src/client.rs @@ -1,4 +1,4 @@ -// Copyright 2016 The Grin Developers +// Copyright 2018 The Grin Developers // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/api/src/handlers.rs b/api/src/handlers.rs index 8f0cecbfb..09bab3676 100644 --- a/api/src/handlers.rs +++ b/api/src/handlers.rs @@ -1,4 +1,4 @@ -// Copyright 2017 The Grin Developers +// Copyright 2018 The Grin Developers // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -58,15 +58,15 @@ impl Handler for IndexHandler { } // Supports retrieval of multiple outputs in a single request - -// GET /v1/chain/utxos/byids?id=xxx,yyy,zzz -// GET /v1/chain/utxos/byids?id=xxx&id=yyy&id=zzz -// GET /v1/chain/utxos/byheight?start_height=101&end_height=200 -struct UtxoHandler { +// GET /v1/chain/outputs/byids?id=xxx,yyy,zzz +// GET /v1/chain/outputs/byids?id=xxx&id=yyy&id=zzz +// GET /v1/chain/outputs/byheight?start_height=101&end_height=200 +struct OutputHandler { chain: Weak, } -impl UtxoHandler { - fn get_utxo(&self, id: &str) -> Result { +impl OutputHandler { + fn get_output(&self, id: &str) -> Result { let c = util::from_hex(String::from(id)) .map_err(|_| Error::Argument(format!("Not a valid commitment: {}", id)))?; let commit = Commitment::from_vec(c); @@ -82,13 +82,13 @@ impl UtxoHandler { for x in outputs.iter() { if let Ok(_) = w(&self.chain).is_unspent(&x) { - return Ok(Utxo::new(&commit)); + return Ok(Output::new(&commit)); } } Err(Error::NotFound) } - fn utxos_by_ids(&self, req: &mut Request) -> Vec { + fn outputs_by_ids(&self, req: &mut Request) -> Vec { let mut commitments: Vec<&str> = vec![]; if let Ok(params) = req.get_ref::() { if let Some(ids) = params.get("id") { @@ -100,15 +100,15 @@ impl UtxoHandler { } } - debug!(LOGGER, "utxos_by_ids: {:?}", commitments); + debug!(LOGGER, "outputs_by_ids: {:?}", commitments); - let mut utxos: Vec = vec![]; + let mut outputs: Vec = vec![]; for x in commitments { - if let Ok(utxo) = self.get_utxo(x) { - utxos.push(utxo); + if let Ok(output) = self.get_output(x) { + outputs.push(output); } } - utxos + outputs } fn outputs_at_height( @@ -186,7 +186,7 @@ impl UtxoHandler { } } -impl Handler for UtxoHandler { +impl Handler for OutputHandler { fn handle(&self, req: &mut Request) -> IronResult { let url = req.url.clone(); let mut path_elems = url.path(); @@ -194,7 +194,7 @@ impl Handler for UtxoHandler { path_elems.pop(); } match *path_elems.last().unwrap() { - "byids" => json_response(&self.utxos_by_ids(req)), + "byids" => json_response(&self.outputs_by_ids(req)), "byheight" => json_response(&self.outputs_block_batch(req)), _ => Ok(Response::with((status::BadRequest, ""))), } @@ -202,40 +202,40 @@ impl Handler for UtxoHandler { } // Sum tree handler. Retrieve the roots: -// GET /v1/pmmrtrees/roots +// GET /v1/txhashset/roots // // Last inserted nodes:: -// GET /v1/pmmrtrees/lastutxos (gets last 10) -// GET /v1/pmmrtrees/lastutxos?n=5 -// GET /v1/pmmrtrees/lastrangeproofs -// GET /v1/pmmrtrees/lastkernels -struct SumTreeHandler { +// GET /v1/txhashset/lastoutputs (gets last 10) +// GET /v1/txhashset/lastoutputs?n=5 +// GET /v1/txhashset/lastrangeproofs +// GET /v1/txhashset/lastkernels +struct TxHashSetHandler { chain: Weak, } -impl SumTreeHandler { +impl TxHashSetHandler { // gets roots - fn get_roots(&self) -> SumTrees { - SumTrees::from_head(w(&self.chain)) + fn get_roots(&self) -> TxHashSet { + TxHashSet::from_head(w(&self.chain)) } - // gets last n utxos inserted in to the tree - fn get_last_n_utxo(&self, distance: u64) -> Vec { - PmmrTreeNode::get_last_n_utxo(w(&self.chain), distance) + // gets last n outputs inserted in to the tree + fn get_last_n_output(&self, distance: u64) -> Vec { + TxHashSetNode::get_last_n_output(w(&self.chain), distance) } - // gets last n utxos inserted in to the tree - fn get_last_n_rangeproof(&self, distance: u64) -> Vec { - PmmrTreeNode::get_last_n_rangeproof(w(&self.chain), distance) + // gets last n outputs inserted in to the tree + fn get_last_n_rangeproof(&self, distance: u64) -> Vec { + TxHashSetNode::get_last_n_rangeproof(w(&self.chain), distance) } - // gets last n utxos inserted in to the tree - fn get_last_n_kernel(&self, distance: u64) -> Vec { - PmmrTreeNode::get_last_n_kernel(w(&self.chain), distance) + // gets last n outputs inserted in to the tree + fn get_last_n_kernel(&self, distance: u64) -> Vec { + TxHashSetNode::get_last_n_kernel(w(&self.chain), distance) } } -impl Handler for SumTreeHandler { +impl Handler for TxHashSetHandler { fn handle(&self, req: &mut Request) -> IronResult { let url = req.url.clone(); let mut path_elems = url.path(); @@ -255,7 +255,7 @@ impl Handler for SumTreeHandler { } match *path_elems.last().unwrap() { "roots" => json_response_pretty(&self.get_roots()), - "lastutxos" => json_response_pretty(&self.get_last_n_utxo(last_n)), + "lastoutputs" => json_response_pretty(&self.get_last_n_output(last_n)), "lastrangeproofs" => json_response_pretty(&self.get_last_n_rangeproof(last_n)), "lastkernels" => json_response_pretty(&self.get_last_n_kernel(last_n)), _ => Ok(Response::with((status::BadRequest, ""))), @@ -572,7 +572,7 @@ pub fn start_rest_apis( .name("apis".to_string()) .spawn(move || { // build handlers and register them under the appropriate endpoint - let utxo_handler = UtxoHandler { + let output_handler = OutputHandler { chain: chain.clone(), }; let block_handler = BlockHandler { @@ -585,7 +585,7 @@ pub fn start_rest_apis( chain: chain.clone(), peers: peers.clone(), }; - let sumtree_handler = SumTreeHandler { + let txhashset_handler = TxHashSetHandler { chain: chain.clone(), }; let pool_info_handler = PoolInfoHandler { @@ -610,12 +610,12 @@ pub fn start_rest_apis( let route_list = vec![ "get blocks".to_string(), "get chain".to_string(), - "get chain/utxos".to_string(), + "get chain/outputs".to_string(), "get status".to_string(), - "get pmmrtrees/roots".to_string(), - "get pmmrtrees/lastutxos?n=10".to_string(), - "get pmmrtrees/lastrangeproofs".to_string(), - "get pmmrtrees/lastkernels".to_string(), + "get txhashset/roots".to_string(), + "get txhashset/lastoutputs?n=10".to_string(), + "get txhashset/lastrangeproofs".to_string(), + "get txhashset/lastkernels".to_string(), "get pool".to_string(), "post pool/push".to_string(), "post peers/a.b.c.d:p/ban".to_string(), @@ -631,9 +631,9 @@ pub fn start_rest_apis( index: get "/" => index_handler, blocks: get "/blocks/*" => block_handler, chain_tip: get "/chain" => chain_tip_handler, - chain_utxos: get "/chain/utxos/*" => utxo_handler, + chain_outputs: get "/chain/outputs/*" => output_handler, status: get "/status" => status_handler, - sumtree_roots: get "/pmmrtrees/*" => sumtree_handler, + txhashset_roots: get "/txhashset/*" => txhashset_handler, pool_info: get "/pool" => pool_info_handler, pool_push: post "/pool/push" => pool_push_handler, peers_all: get "/peers/all" => peers_all_handler, diff --git a/api/src/lib.rs b/api/src/lib.rs index e18eac7a1..29c37739f 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2016 The Grin Developers +// Copyright 2018 The Grin Developers // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/api/src/rest.rs b/api/src/rest.rs index 0db3c5ce3..6d99f37ab 100644 --- a/api/src/rest.rs +++ b/api/src/rest.rs @@ -1,4 +1,4 @@ -// Copyright 2016 The Grin Developers +// Copyright 2018 The Grin Developers // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/api/src/types.rs b/api/src/types.rs index cb8ebad19..014bcf9dd 100644 --- a/api/src/types.rs +++ b/api/src/types.rs @@ -84,64 +84,64 @@ impl Status { } } -/// Sumtrees +/// TxHashSet #[derive(Serialize, Deserialize, Debug, Clone)] -pub struct SumTrees { - /// UTXO Root Hash - pub utxo_root_hash: String, +pub struct TxHashSet { + /// Output Root Hash + pub output_root_hash: String, // Rangeproof root hash pub range_proof_root_hash: String, // Kernel set root hash pub kernel_root_hash: String, } -impl SumTrees { - pub fn from_head(head: Arc) -> SumTrees { - let roots = head.get_sumtree_roots(); - SumTrees { - utxo_root_hash: roots.0.to_hex(), +impl TxHashSet { + pub fn from_head(head: Arc) -> TxHashSet { + let roots = head.get_txhashset_roots(); + TxHashSet { + output_root_hash: roots.0.to_hex(), range_proof_root_hash: roots.1.to_hex(), kernel_root_hash: roots.2.to_hex(), } } } -/// Wrapper around a list of sumtree nodes, so it can be +/// Wrapper around a list of txhashset nodes, so it can be /// presented properly via json #[derive(Serialize, Deserialize, Debug, Clone)] -pub struct PmmrTreeNode { +pub struct TxHashSetNode { // The hash pub hash: String, } -impl PmmrTreeNode { - pub fn get_last_n_utxo(chain: Arc, distance: u64) -> Vec { +impl TxHashSetNode { + pub fn get_last_n_output(chain: Arc, distance: u64) -> Vec { let mut return_vec = Vec::new(); - let last_n = chain.get_last_n_utxo(distance); + let last_n = chain.get_last_n_output(distance); for x in last_n { - return_vec.push(PmmrTreeNode { + return_vec.push(TxHashSetNode { hash: util::to_hex(x.0.to_vec()), }); } return_vec } - pub fn get_last_n_rangeproof(head: Arc, distance: u64) -> Vec { + pub fn get_last_n_rangeproof(head: Arc, distance: u64) -> Vec { let mut return_vec = Vec::new(); let last_n = head.get_last_n_rangeproof(distance); for elem in last_n { - return_vec.push(PmmrTreeNode { + return_vec.push(TxHashSetNode { hash: util::to_hex(elem.0.to_vec()), }); } return_vec } - pub fn get_last_n_kernel(head: Arc, distance: u64) -> Vec { + pub fn get_last_n_kernel(head: Arc, distance: u64) -> Vec { let mut return_vec = Vec::new(); let last_n = head.get_last_n_kernel(distance); for elem in last_n { - return_vec.push(PmmrTreeNode { + return_vec.push(TxHashSetNode { hash: util::to_hex(elem.0.to_vec()), }); } @@ -156,14 +156,14 @@ pub enum OutputType { } #[derive(Debug, Serialize, Deserialize, Clone)] -pub struct Utxo { +pub struct Output { /// The output commitment representing the amount pub commit: PrintableCommitment, } -impl Utxo { - pub fn new(commit: &pedersen::Commitment) -> Utxo { - Utxo { +impl Output { + pub fn new(commit: &pedersen::Commitment) -> Output { + Output { commit: PrintableCommitment(commit.clone()), } } @@ -503,11 +503,11 @@ pub struct BlockHeaderPrintable { pub previous: String, /// rfc3339 timestamp at which the block was built. pub timestamp: String, - /// Merklish root of all the commitments in the UTXO set - pub utxo_root: String, - /// Merklish root of all range proofs in the UTXO set + /// Merklish root of all the commitments in the TxHashSet + pub output_root: String, + /// Merklish root of all range proofs in the TxHashSet pub range_proof_root: String, - /// Merklish root of all transaction kernels in the UTXO set + /// Merklish root of all transaction kernels in the TxHashSet pub kernel_root: String, /// Nonce increment used to mine this block. pub nonce: u64, @@ -525,7 +525,7 @@ impl BlockHeaderPrintable { height: h.height, previous: util::to_hex(h.previous.to_vec()), timestamp: h.timestamp.rfc3339().to_string(), - utxo_root: util::to_hex(h.utxo_root.to_vec()), + output_root: util::to_hex(h.output_root.to_vec()), range_proof_root: util::to_hex(h.range_proof_root.to_vec()), kernel_root: util::to_hex(h.kernel_root.to_vec()), nonce: h.nonce, @@ -643,7 +643,7 @@ mod test { use serde_json; #[test] - fn serialize_output() { + fn serialize_output_printable() { let hex_output = "{\ \"output_type\":\"Coinbase\",\ @@ -660,10 +660,10 @@ mod test { } #[test] - fn serialize_utxo() { + fn serialize_output() { let hex_commit = "{\"commit\":\"083eafae5d61a85ab07b12e1a51b3918d8e6de11fc6cde641d54af53608aa77b9f\"}"; - let deserialized: Utxo = serde_json::from_str(&hex_commit).unwrap(); + let deserialized: Output = serde_json::from_str(&hex_commit).unwrap(); let serialized = serde_json::to_string(&deserialized).unwrap(); assert_eq!(serialized, hex_commit); } diff --git a/chain/src/chain.rs b/chain/src/chain.rs index fa012fdb0..5f653821c 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -1,4 +1,4 @@ -// Copyright 2016 The Grin Developers +// Copyright 2018 The Grin Developers // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -29,7 +29,7 @@ use core::global; use grin_store::Error::NotFoundErr; use pipe; use store; -use sumtree; +use txhashset; use types::*; use util::secp::pedersen::RangeProof; use util::LOGGER; @@ -110,7 +110,7 @@ impl OrphanBlockPool { } /// Facade to the blockchain block processing pipeline and storage. Provides -/// the current view of the UTXO set according to the chain state. Also +/// the current view of the TxHashSet according to the chain state. Also /// maintains locking for the pipeline to avoid conflicting processing. pub struct Chain { db_root: String, @@ -119,7 +119,7 @@ pub struct Chain { head: Arc>, orphans: Arc, - sumtrees: Arc>, + txhashset: Arc>, // POW verification function pow_verifier: fn(&BlockHeader, u32) -> bool, @@ -156,13 +156,13 @@ impl Chain { // check if we have a head in store, otherwise the genesis block is it let head = store.head(); - let sumtree_md = match head { + let txhashset_md = match head { Ok(h) => Some(store.get_block_pmmr_file_metadata(&h.last_block_h)?), Err(NotFoundErr) => None, Err(e) => return Err(Error::StoreErr(e, "chain init load head".to_owned())), }; - let mut sumtrees = sumtree::SumTrees::open(db_root.clone(), store.clone(), sumtree_md)?; + let mut txhashset = txhashset::TxHashSet::open(db_root.clone(), store.clone(), txhashset_md)?; let head = store.head(); let head = match head { @@ -172,7 +172,7 @@ impl Chain { store.save_block(&genesis)?; store.setup_height(&genesis.header, &tip)?; if genesis.kernels.len() > 0 { - sumtree::extending(&mut sumtrees, |extension| extension.apply_block(&genesis))?; + txhashset::extending(&mut txhashset, |extension| extension.apply_block(&genesis))?; } // saving a new tip based on genesis @@ -184,7 +184,7 @@ impl Chain { genesis.header.nonce, genesis.header.pow, ); - pipe::save_pmmr_metadata(&tip, &sumtrees, store.clone())?; + pipe::save_pmmr_metadata(&tip, &txhashset, store.clone())?; tip } Err(e) => return Err(Error::StoreErr(e, "chain init load head".to_owned())), @@ -202,7 +202,7 @@ impl Chain { adapter: adapter, head: Arc::new(Mutex::new(head)), orphans: Arc::new(OrphanBlockPool::new()), - sumtrees: Arc::new(RwLock::new(sumtrees)), + txhashset: Arc::new(RwLock::new(txhashset)), pow_verifier: pow_verifier, }) } @@ -349,7 +349,7 @@ impl Chain { store: self.store.clone(), head: head, pow_verifier: self.pow_verifier, - sumtrees: self.sumtrees.clone(), + txhashset: self.txhashset.clone(), } } @@ -396,15 +396,15 @@ impl Chain { /// This querying is done in a way that is consistent with the current chain state, /// specifically the current winning (valid, most work) fork. pub fn is_unspent(&self, output_ref: &OutputIdentifier) -> Result { - let mut sumtrees = self.sumtrees.write().unwrap(); - sumtrees.is_unspent(output_ref) + let mut txhashset = self.txhashset.write().unwrap(); + txhashset.is_unspent(output_ref) } /// Validate the current chain state. pub fn validate(&self) -> Result<(), Error> { let header = self.store.head_header()?; - let mut sumtrees = self.sumtrees.write().unwrap(); - sumtree::extending(&mut sumtrees, |extension| extension.validate(&header)) + let mut txhashset = self.txhashset.write().unwrap(); + txhashset::extending(&mut txhashset, |extension| extension.validate(&header)) } /// Check if the input has matured sufficiently for the given block height. @@ -412,23 +412,23 @@ impl Chain { /// An input spending a non-coinbase output will always pass this check. pub fn is_matured(&self, input: &Input, height: u64) -> Result<(), Error> { if input.features.contains(OutputFeatures::COINBASE_OUTPUT) { - let mut sumtrees = self.sumtrees.write().unwrap(); + let mut txhashset = self.txhashset.write().unwrap(); let output = OutputIdentifier::from_input(&input); - let hash = sumtrees.is_unspent(&output)?; + let hash = txhashset.is_unspent(&output)?; let header = self.get_block_header(&input.block_hash())?; input.verify_maturity(hash, &header, height)?; } Ok(()) } - /// Sets the sumtree roots on a brand new block by applying the block on the - /// current sumtree state. - pub fn set_sumtree_roots(&self, b: &mut Block, is_fork: bool) -> Result<(), Error> { - let mut sumtrees = self.sumtrees.write().unwrap(); + /// Sets the txhashset roots on a brand new block by applying the block on the + /// current txhashset state. + pub fn set_txhashset_roots(&self, b: &mut Block, is_fork: bool) -> Result<(), Error> { + let mut txhashset = self.txhashset.write().unwrap(); let store = self.store.clone(); - let roots = sumtree::extending(&mut sumtrees, |extension| { - // apply the block on the sumtrees and check the resulting root + let roots = txhashset::extending(&mut txhashset, |extension| { + // apply the block on the txhashset and check the resulting root if is_fork { pipe::rewind_and_apply_fork(b, store, extension)?; } @@ -437,7 +437,7 @@ impl Chain { Ok(extension.roots()) })?; - b.header.utxo_root = roots.utxo_root; + b.header.output_root = roots.output_root; b.header.range_proof_root = roots.rproof_root; b.header.kernel_root = roots.kernel_root; Ok(()) @@ -449,9 +449,9 @@ impl Chain { output: &OutputIdentifier, block: &Block, ) -> Result { - let mut sumtrees = self.sumtrees.write().unwrap(); + let mut txhashset = self.txhashset.write().unwrap(); - let merkle_proof = sumtree::extending(&mut sumtrees, |extension| { + let merkle_proof = txhashset::extending(&mut txhashset, |extension| { extension.force_rollback(); extension.merkle_proof_via_rewind(output, block) })?; @@ -459,66 +459,66 @@ impl Chain { Ok(merkle_proof) } - /// Returns current sumtree roots - pub fn get_sumtree_roots(&self) -> (Hash, Hash, Hash) { - let mut sumtrees = self.sumtrees.write().unwrap(); - sumtrees.roots() + /// Returns current txhashset roots + pub fn get_txhashset_roots(&self) -> (Hash, Hash, Hash) { + let mut txhashset = self.txhashset.write().unwrap(); + txhashset.roots() } - /// Provides a reading view into the current sumtree state as well as + /// Provides a reading view into the current txhashset state as well as /// the required indexes for a consumer to rewind to a consistent state /// at the provided block hash. - pub fn sumtrees_read(&self, h: Hash) -> Result<(u64, u64, File), Error> { + pub fn txhashset_read(&self, h: Hash) -> Result<(u64, u64, File), Error> { let b = self.get_block(&h)?; // get the indexes for the block let out_index: u64; let kernel_index: u64; { - let sumtrees = self.sumtrees.read().unwrap(); - let (oi, ki) = sumtrees.indexes_at(&b)?; + let txhashset = self.txhashset.read().unwrap(); + let (oi, ki) = txhashset.indexes_at(&b)?; out_index = oi; kernel_index = ki; } // prepares the zip and return the corresponding Read - let sumtree_reader = sumtree::zip_read(self.db_root.clone())?; - Ok((out_index, kernel_index, sumtree_reader)) + let txhashset_reader = txhashset::zip_read(self.db_root.clone())?; + Ok((out_index, kernel_index, txhashset_reader)) } - /// Writes a reading view on a sumtree state that's been provided to us. + /// Writes a reading view on a txhashset state that's been provided to us. /// If we're willing to accept that new state, the data stream will be /// read as a zip file, unzipped and the resulting state files should be /// rewound to the provided indexes. - pub fn sumtrees_write( + pub fn txhashset_write( &self, h: Hash, rewind_to_output: u64, rewind_to_kernel: u64, - sumtree_data: File, + txhashset_data: File, ) -> Result<(), Error> { let head = self.head().unwrap(); let header_head = self.get_header_head().unwrap(); if header_head.height - head.height < global::cut_through_horizon() as u64 { - return Err(Error::InvalidSumtree("not needed".to_owned())); + return Err(Error::InvalidTxHashSet("not needed".to_owned())); } let header = self.store.get_block_header(&h)?; - sumtree::zip_write(self.db_root.clone(), sumtree_data)?; + txhashset::zip_write(self.db_root.clone(), txhashset_data)?; - let mut sumtrees = sumtree::SumTrees::open(self.db_root.clone(), self.store.clone(), None)?; - sumtree::extending(&mut sumtrees, |extension| { + let mut txhashset = txhashset::TxHashSet::open(self.db_root.clone(), self.store.clone(), None)?; + txhashset::extending(&mut txhashset, |extension| { extension.rewind_pos(header.height, rewind_to_output, rewind_to_kernel)?; extension.validate(&header)?; - // TODO validate kernels and their sums with UTXOs + // TODO validate kernels and their sums with Outputs extension.rebuild_index()?; Ok(()) })?; - // replace the chain sumtrees with the newly built one + // replace the chain txhashset with the newly built one { - let mut sumtrees_ref = self.sumtrees.write().unwrap(); - *sumtrees_ref = sumtrees; + let mut txhashset_ref = self.txhashset.write().unwrap(); + *txhashset_ref = txhashset; } // setup new head @@ -534,22 +534,22 @@ impl Chain { Ok(()) } - /// returns the last n nodes inserted into the utxo sum tree - pub fn get_last_n_utxo(&self, distance: u64) -> Vec<(Hash, Option)> { - let mut sumtrees = self.sumtrees.write().unwrap(); - sumtrees.last_n_utxo(distance) + /// returns the last n nodes inserted into the output sum tree + pub fn get_last_n_output(&self, distance: u64) -> Vec<(Hash, Option)> { + let mut txhashset = self.txhashset.write().unwrap(); + txhashset.last_n_output(distance) } /// as above, for rangeproofs pub fn get_last_n_rangeproof(&self, distance: u64) -> Vec<(Hash, Option)> { - let mut sumtrees = self.sumtrees.write().unwrap(); - sumtrees.last_n_rangeproof(distance) + let mut txhashset = self.txhashset.write().unwrap(); + txhashset.last_n_rangeproof(distance) } /// as above, for kernels pub fn get_last_n_kernel(&self, distance: u64) -> Vec<(Hash, Option)> { - let mut sumtrees = self.sumtrees.write().unwrap(); - sumtrees.last_n_kernel(distance) + let mut txhashset = self.txhashset.write().unwrap(); + txhashset.last_n_kernel(distance) } /// Total difficulty at the head of the chain diff --git a/chain/src/lib.rs b/chain/src/lib.rs index 72b6645d6..10a56ff9b 100644 --- a/chain/src/lib.rs +++ b/chain/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2016 The Grin Developers +// Copyright 2018 The Grin Developers // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -37,7 +37,7 @@ extern crate grin_util as util; mod chain; pub mod pipe; pub mod store; -pub mod sumtree; +pub mod txhashset; pub mod types; // Re-export the base interface diff --git a/chain/src/pipe.rs b/chain/src/pipe.rs index 1843ec38e..90912c2cc 100644 --- a/chain/src/pipe.rs +++ b/chain/src/pipe.rs @@ -1,4 +1,4 @@ -// Copyright 2016 The Grin Developers +// Copyright 2018 The Grin Developers // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -25,7 +25,7 @@ use core::core::target::Difficulty; use grin_store; use types::*; use store; -use sumtree; +use txhashset; use core::global; use util::LOGGER; @@ -41,7 +41,7 @@ pub struct BlockContext { /// The POW verification function pub pow_verifier: fn(&BlockHeader, u32) -> bool, /// MMR sum tree states - pub sumtrees: Arc>, + pub txhashset: Arc>, } /// Runs the block processing pipeline, including validation and finding a @@ -85,8 +85,8 @@ pub fn process_block(b: &Block, mut ctx: BlockContext) -> Result, Er // valid header and we have a previous block, time to take the lock on the sum // trees - let local_sumtrees = ctx.sumtrees.clone(); - let mut sumtrees = local_sumtrees.write().unwrap(); + let local_txhashset = ctx.txhashset.clone(); + let mut txhashset = local_txhashset.write().unwrap(); // update head now that we're in the lock ctx.head = ctx.store @@ -95,7 +95,7 @@ pub fn process_block(b: &Block, mut ctx: BlockContext) -> Result, Er // start a chain extension unit of work dependent on the success of the // internal validation and saving operations - let result = sumtree::extending(&mut sumtrees, |mut extension| { + let result = txhashset::extending(&mut txhashset, |mut extension| { validate_block(b, &mut ctx, &mut extension)?; debug!( LOGGER, @@ -114,7 +114,7 @@ pub fn process_block(b: &Block, mut ctx: BlockContext) -> Result, Er match result { Ok(t) => { - save_pmmr_metadata(&Tip::from_block(&b.header), &sumtrees, ctx.store.clone())?; + save_pmmr_metadata(&Tip::from_block(&b.header), &txhashset, ctx.store.clone())?; Ok(t) } Err(e) => Err(e), @@ -124,11 +124,11 @@ pub fn process_block(b: &Block, mut ctx: BlockContext) -> Result, Er /// Save pmmr index location for a given block pub fn save_pmmr_metadata( t: &Tip, - sumtrees: &sumtree::SumTrees, + txhashset: &txhashset::TxHashSet, store: Arc, ) -> Result<(), Error> { // Save pmmr file metadata for this block - let block_file_md = sumtrees.last_file_metadata(); + let block_file_md = txhashset.last_file_metadata(); store .save_block_pmmr_file_metadata(&t.last_block_h, &block_file_md) .map_err(|e| Error::StoreErr(e, "saving pmmr file metadata".to_owned()))?; @@ -153,9 +153,9 @@ pub fn sync_block_header( add_block_header(bh, &mut sync_ctx)?; // TODO - confirm this is needed during sync process (I don't see how it is) - // we do not touch the sumtrees when syncing headers + // we do not touch the txhashset when syncing headers // just taking the shared lock - let _ = header_ctx.sumtrees.write().unwrap(); + let _ = header_ctx.txhashset.write().unwrap(); // now update the header_head (if new header with most work) and the sync_head // (always) @@ -329,7 +329,7 @@ fn validate_header(header: &BlockHeader, ctx: &mut BlockContext) -> Result<(), E fn validate_block( b: &Block, ctx: &mut BlockContext, - ext: &mut sumtree::Extension, + ext: &mut txhashset::Extension, ) -> Result<(), Error> { // main isolated block validation, checks all commitment sums and sigs b.validate().map_err(&Error::InvalidBlockProof)?; @@ -342,14 +342,14 @@ fn validate_block( ext.apply_block(&b)?; let roots = ext.roots(); - if roots.utxo_root != b.header.utxo_root || roots.rproof_root != b.header.range_proof_root + if roots.output_root != b.header.output_root || roots.rproof_root != b.header.range_proof_root || roots.kernel_root != b.header.kernel_root { ext.dump(false); debug!( LOGGER, - "validate_block: utxo roots - {:?}, {:?}", roots.utxo_root, b.header.utxo_root, + "validate_block: output roots - {:?}, {:?}", roots.output_root, b.header.output_root, ); debug!( LOGGER, @@ -489,13 +489,13 @@ fn update_header_head(bh: &BlockHeader, ctx: &mut BlockContext) -> Result