From 3daf485e944d9253fe190a6b96cefd508fdd98b4 Mon Sep 17 00:00:00 2001 From: Aleksei Sidorov Date: Mon, 29 Apr 2019 13:45:17 +0300 Subject: [PATCH 1/8] Port to merkledb --- Cargo.toml | 12 ++- src/api.rs | 6 +- .../data_layout/input_signatures.rs | 33 +++++---- src/blockchain/data_layout/tx_input_id.rs | 17 ++--- src/blockchain/schema.rs | 74 +++++-------------- src/blockchain/transactions.rs | 15 ++-- src/btc/macros.rs | 43 +++++------ src/btc/mod.rs | 2 +- src/btc/transaction.rs | 30 ++++---- src/service.rs | 10 +-- src/test_helpers/testkit.rs | 28 ++++--- tests/api.rs | 64 ++++++---------- tests/sync.rs | 15 ++-- 13 files changed, 159 insertions(+), 190 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7ad9c02..d3a84b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ byteorder = "1.3" clap = "2.32" derive_more = "0.14" exonum = "0.11.0" +exonum-merkledb = { git = "https://github.com/alekseysidorov/exonum.git", branch = "merkledb_fixes" } exonum_bitcoinrpc = "0.6" exonum-derive = "0.11.0" exonum-testkit = "0.11.0" @@ -32,7 +33,7 @@ hex = "0.3" log = "0.4" maplit = "1.0" matches = "0.1" -protobuf = { version = "2.4", features = ["with-serde"] } +protobuf = { version = "2.5", features = ["with-serde"] } rand = "0.4" secp256k1 = { version = "0.12", features = ["serde"] } serde = "1.0" @@ -40,7 +41,7 @@ serde_derive = "1.0" serde_json = "1.0" serde_str = "0.1" structopt = "0.2" -toml = "0.4" +toml = "0.5" [dev-dependencies] exonum-configuration = "0.11.0" @@ -49,3 +50,10 @@ proptest = "0.9" [build-dependencies] exonum-build = "0.11.0" + +[patch.crates-io] +exonum = { git = "https://github.com/alekseysidorov/exonum.git", branch = "merkledb_fixes" } +exonum-derive = { git = "https://github.com/alekseysidorov/exonum.git", branch = "merkledb_fixes" } +exonum-configuration = { git = "https://github.com/alekseysidorov/exonum.git", branch = "merkledb_fixes" } +exonum-build = { git = "https://github.com/alekseysidorov/exonum.git", branch = "merkledb_fixes" } +exonum-testkit = { git = "https://github.com/alekseysidorov/exonum.git", branch = "merkledb_fixes" } diff --git a/src/api.rs b/src/api.rs index fc4a1c2..99ccbe3 100644 --- a/src/api.rs +++ b/src/api.rs @@ -18,7 +18,7 @@ use exonum::api::{self, ServiceApiBuilder, ServiceApiState}; use exonum::blockchain::{BlockProof, Schema as CoreSchema}; use exonum::crypto::Hash; use exonum::helpers::Height; -use exonum::storage::{ListProof, MapProof}; +use exonum_merkledb::{ListProof, MapProof}; use failure::Fail; use serde_derive::{Deserialize, Serialize}; @@ -108,13 +108,13 @@ impl PublicApi for ServiceApiState { fn actual_address(&self, _query: ()) -> Result { let snapshot = self.snapshot(); - let schema = BtcAnchoringSchema::new(snapshot); + let schema = BtcAnchoringSchema::new(&snapshot); Ok(schema.actual_configuration().anchoring_address()) } fn following_address(&self, _query: ()) -> Result, Self::Error> { let snapshot = self.snapshot(); - let schema = BtcAnchoringSchema::new(snapshot); + let schema = BtcAnchoringSchema::new(&snapshot); Ok(schema .following_configuration() .map(|config| config.anchoring_address())) diff --git a/src/blockchain/data_layout/input_signatures.rs b/src/blockchain/data_layout/input_signatures.rs index 8a94073..9e12313 100644 --- a/src/blockchain/data_layout/input_signatures.rs +++ b/src/blockchain/data_layout/input_signatures.rs @@ -12,17 +12,19 @@ // See the License for the specific language governing permissions and // limitations under the License. -use exonum::crypto::{self, CryptoHash, Hash}; +use exonum::crypto::{self, Hash}; use exonum::helpers::ValidatorId; -use exonum::storage::StorageValue; +use exonum_merkledb::{BinaryValue, ObjectHash}; use byteorder::{ByteOrder, LittleEndian, WriteBytesExt}; use serde_derive::{Deserialize, Serialize}; -use std::borrow::Cow; -use std::io::{Cursor, Write}; -use std::iter::{FilterMap, IntoIterator}; -use std::vec::IntoIter; +use std::{ + borrow::Cow, + io::{Cursor, Write}, + iter::{FilterMap, IntoIterator}, + vec::IntoIter, +}; /// A set of signatures for a transaction input ordered by the validators identifiers. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] @@ -70,8 +72,8 @@ impl IntoIterator for InputSignatures { } } -impl StorageValue for InputSignatures { - fn into_bytes(self) -> Vec { +impl BinaryValue for InputSignatures { + fn to_bytes(&self) -> Vec { let mut buf = Cursor::new(Vec::new()); for signature in &self.content { let bytes = signature @@ -83,7 +85,7 @@ impl StorageValue for InputSignatures { buf.into_inner() } - fn from_bytes(value: Cow<[u8]>) -> Self { + fn from_bytes(value: Cow<[u8]>) -> Result { let mut signatures = Vec::new(); let mut reader = value.as_ref(); while !reader.is_empty() { @@ -98,15 +100,16 @@ impl StorageValue for InputSignatures { }; signatures.push(signature); } - Self { + + Ok(Self { content: signatures, - } + }) } } -impl CryptoHash for InputSignatures { - fn hash(&self) -> Hash { - crypto::hash(&self.clone().into_bytes()) +impl ObjectHash for InputSignatures { + fn object_hash(&self) -> Hash { + crypto::hash(&self.to_bytes()) } } @@ -122,7 +125,7 @@ fn test_input_signatures_storage_value() { assert_eq!(signatures.len(), 2); let bytes = signatures.clone().into_bytes(); - let signatures2 = InputSignatures::from_bytes(bytes.into()); + let signatures2 = InputSignatures::from_bytes(bytes.into()).unwrap(); assert_eq!(signatures, signatures2); assert_eq!(signatures2.into_iter().collect::>(), data); } diff --git a/src/blockchain/data_layout/tx_input_id.rs b/src/blockchain/data_layout/tx_input_id.rs index 71062d7..1700f33 100644 --- a/src/blockchain/data_layout/tx_input_id.rs +++ b/src/blockchain/data_layout/tx_input_id.rs @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -use exonum::crypto::{self, CryptoHash, Hash}; -use exonum::storage::{HashedKey, StorageKey}; +use exonum::crypto::{self, Hash}; +use exonum_merkledb::{BinaryKey, ObjectHash}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -36,7 +36,7 @@ impl TxInputId { } } -impl StorageKey for TxInputId { +impl BinaryKey for TxInputId { fn size(&self) -> usize { self.txid.size() + self.input.size() } @@ -53,23 +53,22 @@ impl StorageKey for TxInputId { Self { txid, input } } - fn write(&self, out: &mut [u8]) { + fn write(&self, out: &mut [u8]) -> usize { let mut writer = Cursor::new(out); let _ = writer.write(self.txid.as_ref()).unwrap(); writer.write_u32::(self.input).unwrap(); + self.size() } } -impl CryptoHash for TxInputId { - fn hash(&self) -> Hash { +impl ObjectHash for TxInputId { + fn object_hash(&self) -> Hash { let mut bytes = [0_u8; 36]; self.write(&mut bytes); crypto::hash(bytes.as_ref()) } } -impl HashedKey for TxInputId {} - #[test] fn test_tx_input_id_storage_key() { let txout = TxInputId { @@ -84,5 +83,5 @@ fn test_tx_input_id_storage_key() { assert_eq!(txout, txout2); let buf_hash = crypto::hash(&buf); - assert_eq!(txout2.hash(), buf_hash); + assert_eq!(txout2.object_hash(), buf_hash); } diff --git a/src/blockchain/schema.rs b/src/blockchain/schema.rs index 92d1155..7ccbe69 100644 --- a/src/blockchain/schema.rs +++ b/src/blockchain/schema.rs @@ -17,7 +17,7 @@ use exonum::blockchain::{Schema, StoredConfiguration}; use exonum::crypto::Hash; use exonum::helpers::Height; -use exonum::storage::{Fork, ProofListIndex, ProofMapIndex, Snapshot}; +use exonum_merkledb::{IndexAccess, ObjectHash, ProofListIndex, ProofMapIndex, Snapshot}; use btc_transaction_utils::multisig::RedeemScript; use log::{error, trace}; @@ -51,55 +51,55 @@ define_names!( /// Information schema for `exonum-btc-anchoring`. #[derive(Debug)] pub struct BtcAnchoringSchema { - snapshot: T, + access: T, } -impl> BtcAnchoringSchema { +impl BtcAnchoringSchema { /// Constructs schema for the given database `snapshot`. - pub fn new(snapshot: T) -> Self { - Self { snapshot } + pub fn new(access: T) -> Self { + Self { access } } /// Returns table that contains complete chain of the anchoring transactions. - pub fn anchoring_transactions_chain(&self) -> ProofListIndex<&T, Transaction> { - ProofListIndex::new(TRANSACTIONS_CHAIN, &self.snapshot) + pub fn anchoring_transactions_chain(&self) -> ProofListIndex { + ProofListIndex::new(TRANSACTIONS_CHAIN, self.access) } /// Returns the table that contains already spent funding transactions. - pub fn spent_funding_transactions(&self) -> ProofMapIndex<&T, Hash, Transaction> { - ProofMapIndex::new(SPENT_FUNDING_TRANSACTIONS, &self.snapshot) + pub fn spent_funding_transactions(&self) -> ProofMapIndex { + ProofMapIndex::new(SPENT_FUNDING_TRANSACTIONS, self.access) } /// Returns the table that contains signatures for the given transaction input. - pub fn transaction_signatures(&self) -> ProofMapIndex<&T, TxInputId, InputSignatures> { - ProofMapIndex::new(TRANSACTION_SIGNATURES, &self.snapshot) + pub fn transaction_signatures(&self) -> ProofMapIndex { + ProofMapIndex::new(TRANSACTION_SIGNATURES, self.access) } /// Returns a list of hashes of Exonum blocks headers. - pub fn anchored_blocks(&self) -> ProofListIndex<&T, Hash> { - ProofListIndex::new(ANCHORED_BLOCKS, &self.snapshot) + pub fn anchored_blocks(&self) -> ProofListIndex { + ProofListIndex::new(ANCHORED_BLOCKS, self.access) } /// Returns hashes of the stored tables. pub fn state_hash(&self) -> Vec { vec![ - self.anchoring_transactions_chain().merkle_root(), - self.spent_funding_transactions().merkle_root(), - self.transaction_signatures().merkle_root(), - self.anchored_blocks().merkle_root(), + self.anchoring_transactions_chain().object_hash(), + self.spent_funding_transactions().object_hash(), + self.transaction_signatures().object_hash(), + self.anchored_blocks().object_hash(), ] } /// Returns the actual anchoring configuration. pub fn actual_configuration(&self) -> GlobalConfig { - let actual_configuration = Schema::new(&self.snapshot).actual_configuration(); + let actual_configuration = Schema::new(self.access).actual_configuration(); Self::parse_config(&actual_configuration) .expect("Actual BTC anchoring configuration is absent") } /// Returns the nearest following configuration if it exists. pub fn following_configuration(&self) -> Option { - let following_configuration = Schema::new(&self.snapshot).following_configuration()?; + let following_configuration = Schema::new(self.access).following_configuration()?; Self::parse_config(&following_configuration) } @@ -182,7 +182,7 @@ impl> BtcAnchoringSchema { let anchoring_height = actual_state.following_anchoring_height(latest_anchored_height); let anchoring_block_hash = - Schema::new(&self.snapshot).block_hash_by_height(anchoring_height)?; + Schema::new(self.access).block_hash_by_height(anchoring_height)?; builder.payload(anchoring_height, anchoring_block_hash); builder.fee(config.transaction_fee); @@ -229,37 +229,3 @@ impl> BtcAnchoringSchema { .map(|value| serde_json::from_value(value).expect("Unable to parse configuration")) } } - -impl<'a> BtcAnchoringSchema<&'a mut Fork> { - /// Mutable variant of the [`anchoring_transactions_chain`][1] index. - /// - /// [1]: struct.BtcAnchoringSchema.html#method.anchoring_transactions_chain_mut - pub fn anchoring_transactions_chain_mut(&mut self) -> ProofListIndex<&mut Fork, Transaction> { - ProofListIndex::new(TRANSACTIONS_CHAIN, &mut self.snapshot) - } - - /// Mutable variant of the [`spent_funding_transactions`][1] index. - /// - /// [1]: struct.BtcAnchoringSchema.html#method.spent_funding_transactions - pub fn spent_funding_transactions_mut( - &mut self, - ) -> ProofMapIndex<&mut Fork, Hash, Transaction> { - ProofMapIndex::new(SPENT_FUNDING_TRANSACTIONS, &mut self.snapshot) - } - - /// Mutable variant of the [`anchored_blocks`][1] index. - /// - /// [1]: struct.BtcAnchoringSchema.html#method.anchored_blocks - pub fn transaction_signatures_mut( - &mut self, - ) -> ProofMapIndex<&mut Fork, TxInputId, InputSignatures> { - ProofMapIndex::new(TRANSACTION_SIGNATURES, &mut self.snapshot) - } - - /// Mutable variant of the [`anchored_blocks`][1] index. - /// - /// [1]: struct.BtcAnchoringSchema.html#method.anchored_blocks - pub fn anchored_blocks_mut(&mut self) -> ProofListIndex<&mut Fork, Hash> { - ProofListIndex::new(ANCHORED_BLOCKS, &mut self.snapshot) - } -} diff --git a/src/blockchain/transactions.rs b/src/blockchain/transactions.rs index d97fdc0..b3cc040 100644 --- a/src/blockchain/transactions.rs +++ b/src/blockchain/transactions.rs @@ -24,12 +24,11 @@ use btc_transaction_utils::{p2wsh::InputSigner, InputSignature, TxInRef}; use log::{info, trace}; use serde_derive::{Deserialize, Serialize}; -use crate::btc; -use crate::proto; +use std::borrow::Cow; -use super::data_layout::TxInputId; -use super::errors::SignatureError; -use super::BtcAnchoringSchema; +use crate::{btc, proto}; + +use super::{data_layout::TxInputId, errors::SignatureError, BtcAnchoringSchema}; /// Exonum message with the signature for the new anchoring transaction. #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, ProtobufConvert)] @@ -131,7 +130,7 @@ impl Transaction for TxSignature { // Adds signature to schema. input_signatures.insert(self.validator, self.input_signature.clone().into()); schema - .transaction_signatures_mut() + .transaction_signatures() .put(&input_id, input_signatures); // Tries to finalize transaction. let mut tx: btc::Transaction = tx.clone(); @@ -161,10 +160,10 @@ impl Transaction for TxSignature { trace!("Anchoring txhex: {}", tx.to_string()); // Adds finalized transaction to the tail of anchoring transactions. - schema.anchoring_transactions_chain_mut().push(tx); + schema.anchoring_transactions_chain().push(tx); if let Some(unspent_funding_tx) = schema.unspent_funding_transaction() { schema - .spent_funding_transactions_mut() + .spent_funding_transactions() .put(&unspent_funding_tx.id(), unspent_funding_tx); } } diff --git a/src/btc/macros.rs b/src/btc/macros.rs index 1661bef..0e9e441 100644 --- a/src/btc/macros.rs +++ b/src/btc/macros.rs @@ -22,26 +22,26 @@ macro_rules! impl_wrapper_for_bitcoin_type { macro_rules! impl_wrapper_for_bitcoin_consensus_encoding { ($name:ident) => { - impl ::exonum::storage::StorageValue for $name { - fn into_bytes(self) -> Vec { - ::bitcoin::consensus::serialize(&self.0) + impl exonum_merkledb::BinaryValue for $name { + fn to_bytes(&self) -> Vec { + bitcoin::consensus::serialize(&self.0) } - fn from_bytes(value: ::std::borrow::Cow<[u8]>) -> $name { - let inner = ::bitcoin::consensus::deserialize(value.as_ref()).unwrap(); - $name(inner) + fn from_bytes(value: ::std::borrow::Cow<[u8]>) -> Result<$name, failure::Error> { + let inner = bitcoin::consensus::deserialize(value.as_ref())?; + Ok($name(inner)) } } - impl ::exonum::crypto::CryptoHash for $name { - fn hash(&self) -> ::exonum::crypto::Hash { - let bytes = ::bitcoin::consensus::serialize(&self.0); - ::exonum::crypto::hash(&bytes) + impl exonum_merkledb::ObjectHash for $name { + fn object_hash(&self) -> exonum::crypto::Hash { + let bytes = bitcoin::consensus::serialize(&self.0); + exonum::crypto::hash(&bytes) } } - impl ::hex::FromHex for $name { - type Error = ::failure::Error; + impl hex::FromHex for $name { + type Error = failure::Error; fn from_hex>(hex: T) -> Result { let bytes = ::hex::decode(hex)?; @@ -50,13 +50,13 @@ macro_rules! impl_wrapper_for_bitcoin_consensus_encoding { } } - impl ::hex::ToHex for $name { - fn write_hex(&self, w: &mut W) -> ::std::fmt::Result { + impl hex::ToHex for $name { + fn write_hex(&self, w: &mut W) -> std::fmt::Result { let bytes = ::bitcoin::consensus::serialize(&self.0); bytes.write_hex(w) } - fn write_hex_upper(&self, w: &mut W) -> ::std::fmt::Result { + fn write_hex_upper(&self, w: &mut W) -> std::fmt::Result { let bytes = ::bitcoin::consensus::serialize(&self.0); bytes.write_hex_upper(w) } @@ -66,23 +66,24 @@ macro_rules! impl_wrapper_for_bitcoin_consensus_encoding { macro_rules! impl_string_conversions_for_hex { ($name:ident) => { - impl ::std::fmt::LowerHex for $name { - fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + impl std::fmt::LowerHex for $name { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { use hex::ToHex; + let mut buf = String::new(); self.write_hex(&mut buf).unwrap(); write!(f, "{}", buf) } } - impl ::std::fmt::Display for $name { - fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + impl std::fmt::Display for $name { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "{:x}", self) } } - impl ::std::str::FromStr for $name { - type Err = ::failure::Error; + impl std::str::FromStr for $name { + type Err = failure::Error; fn from_str(s: &str) -> Result { use hex::FromHex; diff --git a/src/btc/mod.rs b/src/btc/mod.rs index 14b6371..63bc49a 100644 --- a/src/btc/mod.rs +++ b/src/btc/mod.rs @@ -22,8 +22,8 @@ use bitcoin::util::address; use btc_transaction_utils; use derive_more::{From, Into}; use hex::{self, FromHex, ToHex}; - use rand::Rng; + use std::ops::Deref; #[macro_use] diff --git a/src/btc/transaction.rs b/src/btc/transaction.rs index 6876806..824d6e5 100644 --- a/src/btc/transaction.rs +++ b/src/btc/transaction.rs @@ -248,16 +248,20 @@ impl BtcAnchoringTransactionBuilder { #[cfg(test)] mod tests { - use exonum::crypto::CryptoHash; use exonum::crypto::Hash; use exonum::helpers::Height; - use exonum::storage::StorageValue; + use exonum_merkledb::{BinaryValue, ObjectHash}; + + use bitcoin::{ + blockdata::{ + opcodes::all::OP_RETURN, + script::{Builder, Script}, + transaction::{self, OutPoint, TxIn, TxOut}, + }, + network::constants::Network, + util::address::Address, + }; - use bitcoin::blockdata::opcodes::all::OP_RETURN; - use bitcoin::blockdata::script::{Builder, Script}; - use bitcoin::blockdata::transaction::{self, OutPoint, TxIn, TxOut}; - use bitcoin::network::constants::Network; - use bitcoin::util::address::Address; use bitcoin_hashes::{sha256d::Hash as Sha256dHash, Hash as BitcoinHash}; use btc_transaction_utils::multisig::RedeemScriptBuilder; use hex::FromHex; @@ -281,7 +285,7 @@ mod tests { assert_eq!(tx.to_string(), tx_hex); let bytes = tx.clone().into_bytes(); - let tx2 = Transaction::from_bytes(bytes.into()); + let tx2 = Transaction::from_bytes(bytes.into()).unwrap(); assert_eq!(tx2, tx); } @@ -373,7 +377,7 @@ mod tests { let bytes = transaction.clone().into_bytes(); assert_eq!( transaction, - ::from_bytes(Cow::Borrowed(&bytes)) + ::from_bytes(Cow::Borrowed(&bytes)).unwrap() ); } } @@ -391,7 +395,7 @@ mod tests { 062c1400"; let tx_raw = Vec::::from_hex(hex_tx).unwrap(); - let _ = ::from_bytes(Cow::Borrowed(&tx_raw)); + let _ = ::from_bytes(Cow::Borrowed(&tx_raw)).unwrap(); } #[test] @@ -406,7 +410,7 @@ mod tests { 062c1400"; let tx_raw = Vec::::from_hex(hex_tx).unwrap(); - let _ = ::from_bytes(Cow::Borrowed(&tx_raw)); + let _ = ::from_bytes(Cow::Borrowed(&tx_raw)).unwrap(); } #[test] @@ -439,7 +443,7 @@ mod tests { let mut builder = BtcAnchoringTransactionBuilder::new(&redeem_script); builder.additional_funds(funding_tx.clone()).unwrap(); builder.fee(1); - builder.payload(Height::zero(), funding_tx.hash()); + builder.payload(Height::zero(), funding_tx.object_hash()); let (tx, inputs) = builder.create().unwrap(); assert_eq!(funding_tx, inputs[0]); @@ -512,7 +516,7 @@ mod tests { builder.additional_funds(funding_tx1.clone()).unwrap(); builder.additional_funds(funding_tx2.clone()).unwrap(); builder.fee(1); - builder.payload(Height::zero(), funding_tx0.hash()); + builder.payload(Height::zero(), funding_tx0.object_hash()); let (tx, inputs) = builder.create().unwrap(); assert_eq!(inputs.len(), 3); diff --git a/src/service.rs b/src/service.rs index 985b499..671e72e 100644 --- a/src/service.rs +++ b/src/service.rs @@ -18,7 +18,7 @@ use exonum::blockchain::{ }; use exonum::crypto::Hash; use exonum::messages::RawTransaction; -use exonum::storage::{Fork, Snapshot}; +use exonum_merkledb::{Fork, Snapshot}; use serde_json::json; @@ -87,19 +87,19 @@ impl Service for BtcAnchoringService { Ok(tx.into()) } - fn initialize(&self, _fork: &mut Fork) -> serde_json::Value { + fn initialize(&self, _fork: &Fork) -> serde_json::Value { json!(self.global_config) } - fn before_commit(&self, fork: &mut Fork) { + fn before_commit(&self, fork: &Fork) { // Writes a hash of the latest block to the proof list index. - let block_header_hash = CoreSchema::new(&fork) + let block_header_hash = CoreSchema::new(fork) .block_hashes_by_height() .last() .expect("An attempt to invoke execute during the genesis block initialization."); let mut schema = BtcAnchoringSchema::new(fork); - schema.anchored_blocks_mut().push(block_header_hash); + schema.anchored_blocks().push(block_header_hash); } fn after_commit(&self, context: &ServiceContext) { diff --git a/src/test_helpers/testkit.rs b/src/test_helpers/testkit.rs index cc1bb7e..402a370 100644 --- a/src/test_helpers/testkit.rs +++ b/src/test_helpers/testkit.rs @@ -23,12 +23,14 @@ use log::trace; use maplit::hashmap; use rand::{thread_rng, Rng, SeedableRng, StdRng}; -use exonum::api; -use exonum::blockchain::{BlockProof, Blockchain, Schema as CoreSchema, StoredConfiguration}; -use exonum::crypto::{CryptoHash, Hash}; -use exonum::helpers::Height; -use exonum::messages::{Message, RawTransaction, Signed}; -use exonum::storage::MapProof; +use exonum::{ + api, + blockchain::{BlockProof, Blockchain, Schema as CoreSchema, StoredConfiguration}, + crypto::{CryptoHash, Hash}, + helpers::Height, + messages::{Message, RawTransaction, Signed}, +}; +use exonum_merkledb::{MapProof, ObjectHash}; use exonum_testkit::{ ApiKind, TestKit, TestKitApi, TestKitBuilder, TestNetworkConfiguration, TestNode, }; @@ -251,7 +253,8 @@ impl AnchoringTestKit { /// Updates the private keys pool in testkit for the transition state. pub fn renew_address(&mut self) { - let schema = BtcAnchoringSchema::new(self.snapshot()); + let snapshot = self.snapshot(); + let schema = BtcAnchoringSchema::new(&snapshot); if let BtcAnchoringState::Transition { actual_configuration, @@ -304,7 +307,7 @@ impl AnchoringTestKit { /// Returns the current anchoring global configuration. pub fn actual_anchoring_configuration(&self) -> GlobalConfig { let snapshot = self.blockchain().snapshot(); - let schema = BtcAnchoringSchema::new(snapshot); + let schema = BtcAnchoringSchema::new(&snapshot); schema.actual_configuration() } @@ -315,7 +318,8 @@ impl AnchoringTestKit { /// Returns the latest anchoring transaction. pub fn last_anchoring_tx(&self) -> Option { - let schema = BtcAnchoringSchema::new(self.snapshot()); + let snapshot = self.snapshot(); + let schema = BtcAnchoringSchema::new(&snapshot); schema.anchoring_transactions_chain().last() } @@ -325,6 +329,8 @@ impl AnchoringTestKit { &self, validators_num: u16, ) -> Result>, btc::BuilderError> { + let snapshot = self.snapshot(); + let validators = self .network() .validators() @@ -341,7 +347,7 @@ impl AnchoringTestKit { let validator_id = validator.validator_id().unwrap(); let (public_key, private_key) = validator.service_keypair(); - let schema = BtcAnchoringSchema::new(self.snapshot()); + let schema = BtcAnchoringSchema::new(&snapshot); if let Some(p) = schema.actual_proposed_anchoring_transaction() { let (proposal, proposal_inputs) = p?; @@ -473,7 +479,7 @@ fn validate_table_proof( // Checks state_hash. let checked_table_proof = to_table.check()?; ensure!( - checked_table_proof.merkle_root() == *latest_authorized_block.block.state_hash(), + checked_table_proof.root_hash() == *latest_authorized_block.block.state_hash(), "State hash doesn't match" ); let value = checked_table_proof.entries().map(|(a, b)| (*a, *b)).next(); diff --git a/tests/api.rs b/tests/api.rs index 5274ba1..0f23d0b 100644 --- a/tests/api.rs +++ b/tests/api.rs @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use exonum::{helpers::Height, storage::Snapshot}; +use exonum::helpers::Height; use exonum_btc_anchoring::{ api::{FindTransactionQuery, HeightQuery, PublicApi}, blockchain::BtcAnchoringSchema, @@ -38,11 +38,6 @@ fn find_transaction( }) } -fn btc_anchoring_schema(testkit: &AnchoringTestKit) -> BtcAnchoringSchema> { - let snapshot = testkit.snapshot(); - BtcAnchoringSchema::new(snapshot) -} - #[test] fn actual_address() { let validators_num = 4; @@ -125,7 +120,8 @@ fn find_transaction_regular() { anchoring_testkit.create_blocks_until(next_anchoring_height); } - let anchoring_schema = btc_anchoring_schema(&anchoring_testkit); + let snapshot = anchoring_testkit.snapshot(); + let anchoring_schema = BtcAnchoringSchema::new(&snapshot); let tx_chain = anchoring_schema.anchoring_transactions_chain(); assert_eq!( @@ -175,23 +171,19 @@ fn find_transaction_configuration_change() { anchoring_testkit.create_block_with_transactions(signatures); anchoring_testkit.create_block(); + let snapshot = anchoring_testkit.snapshot(); + let anchoring_schema = BtcAnchoringSchema::new(&snapshot); assert_eq!( find_transaction(&anchoring_testkit, Some(Height(0))), - btc_anchoring_schema(&anchoring_testkit) - .anchoring_transactions_chain() - .get(1) + anchoring_schema.anchoring_transactions_chain().get(1) ); assert_eq!( find_transaction(&anchoring_testkit, Some(Height(1))), - btc_anchoring_schema(&anchoring_testkit) - .anchoring_transactions_chain() - .get(1) + anchoring_schema.anchoring_transactions_chain().get(1) ); assert_eq!( find_transaction(&anchoring_testkit, None), - btc_anchoring_schema(&anchoring_testkit) - .anchoring_transactions_chain() - .get(1) + anchoring_schema.anchoring_transactions_chain().get(1) ); anchoring_testkit.create_blocks_until(Height(20)); @@ -202,29 +194,23 @@ fn find_transaction_configuration_change() { anchoring_testkit.create_block_with_transactions(signatures); anchoring_testkit.create_block(); + let snapshot = anchoring_testkit.snapshot(); + let anchoring_schema = BtcAnchoringSchema::new(&snapshot); assert_eq!( find_transaction(&anchoring_testkit, Some(Height(0))), - btc_anchoring_schema(&anchoring_testkit) - .anchoring_transactions_chain() - .get(1) + anchoring_schema.anchoring_transactions_chain().get(1) ); assert_eq!( find_transaction(&anchoring_testkit, Some(Height(1))), - btc_anchoring_schema(&anchoring_testkit) - .anchoring_transactions_chain() - .get(2) + anchoring_schema.anchoring_transactions_chain().get(2) ); assert_eq!( find_transaction(&anchoring_testkit, Some(Height(10))), - btc_anchoring_schema(&anchoring_testkit) - .anchoring_transactions_chain() - .get(2) + anchoring_schema.anchoring_transactions_chain().get(2) ); assert_eq!( find_transaction(&anchoring_testkit, None), - btc_anchoring_schema(&anchoring_testkit) - .anchoring_transactions_chain() - .get(2) + anchoring_schema.anchoring_transactions_chain().get(2) ); // Anchors block on height 20. @@ -234,35 +220,27 @@ fn find_transaction_configuration_change() { anchoring_testkit.create_block_with_transactions(signatures); anchoring_testkit.create_block(); + let snapshot = anchoring_testkit.snapshot(); + let anchoring_schema = BtcAnchoringSchema::new(&snapshot); assert_eq!( find_transaction(&anchoring_testkit, Some(Height(0))), - btc_anchoring_schema(&anchoring_testkit) - .anchoring_transactions_chain() - .get(1) + anchoring_schema.anchoring_transactions_chain().get(1) ); assert_eq!( find_transaction(&anchoring_testkit, Some(Height(1))), - btc_anchoring_schema(&anchoring_testkit) - .anchoring_transactions_chain() - .get(2) + anchoring_schema.anchoring_transactions_chain().get(2) ); assert_eq!( find_transaction(&anchoring_testkit, Some(Height(10))), - btc_anchoring_schema(&anchoring_testkit) - .anchoring_transactions_chain() - .get(2) + anchoring_schema.anchoring_transactions_chain().get(2) ); assert_eq!( find_transaction(&anchoring_testkit, Some(Height(11))), - btc_anchoring_schema(&anchoring_testkit) - .anchoring_transactions_chain() - .get(3) + anchoring_schema.anchoring_transactions_chain().get(3) ); assert_eq!( find_transaction(&anchoring_testkit, None), - btc_anchoring_schema(&anchoring_testkit) - .anchoring_transactions_chain() - .get(3) + anchoring_schema.anchoring_transactions_chain().get(3) ); } diff --git a/tests/sync.rs b/tests/sync.rs index 93414d0..7298e68 100644 --- a/tests/sync.rs +++ b/tests/sync.rs @@ -54,7 +54,8 @@ fn normal_operation() { .create_signature_tx_for_validators(2) .unwrap(); - let schema = BtcAnchoringSchema::new(anchoring_testkit.snapshot()); + let snapshot = anchoring_testkit.snapshot(); + let schema = BtcAnchoringSchema::new(&snapshot); let (proposed, _) = schema .actual_proposed_anchoring_transaction() .unwrap() @@ -78,7 +79,8 @@ fn normal_operation() { anchoring_testkit.create_blocks_until(Height(2)); - let schema = BtcAnchoringSchema::new(anchoring_testkit.snapshot()); + let snapshot = anchoring_testkit.snapshot(); + let schema = BtcAnchoringSchema::new(&snapshot); let last_tx = schema.anchoring_transactions_chain().last().unwrap(); // Should retry @@ -135,7 +137,8 @@ fn several_unsynced() { .create_signature_tx_for_validators(3) .unwrap(); - let schema = BtcAnchoringSchema::new(anchoring_testkit.snapshot()); + let snapshot = anchoring_testkit.snapshot(); + let schema = BtcAnchoringSchema::new(&snapshot); let (proposed_0, _) = schema .actual_proposed_anchoring_transaction() .unwrap() @@ -159,7 +162,8 @@ fn several_unsynced() { anchoring_testkit.create_blocks_until(Height(2)); - let schema = BtcAnchoringSchema::new(anchoring_testkit.snapshot()); + let snapshot = anchoring_testkit.snapshot(); + let schema = BtcAnchoringSchema::new(&snapshot); let last_tx = schema.anchoring_transactions_chain().last().unwrap(); // Sync failed @@ -185,7 +189,8 @@ fn several_unsynced() { .create_signature_tx_for_validators(3) .unwrap(); - let schema = BtcAnchoringSchema::new(anchoring_testkit.snapshot()); + let snapshot = anchoring_testkit.snapshot(); + let schema = BtcAnchoringSchema::new(&snapshot); let (proposed_1, _) = schema .actual_proposed_anchoring_transaction() .unwrap() From e72830e8cd60d8d852f5d669a34a1f165847d988 Mon Sep 17 00:00:00 2001 From: Aleksei Sidorov Date: Mon, 29 Apr 2019 15:58:34 +0300 Subject: [PATCH 2/8] Fix warnings --- src/blockchain/schema.rs | 2 +- src/blockchain/transactions.rs | 4 ++-- src/service.rs | 2 +- src/test_helpers/testkit.rs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/blockchain/schema.rs b/src/blockchain/schema.rs index 7ccbe69..08486f6 100644 --- a/src/blockchain/schema.rs +++ b/src/blockchain/schema.rs @@ -17,7 +17,7 @@ use exonum::blockchain::{Schema, StoredConfiguration}; use exonum::crypto::Hash; use exonum::helpers::Height; -use exonum_merkledb::{IndexAccess, ObjectHash, ProofListIndex, ProofMapIndex, Snapshot}; +use exonum_merkledb::{IndexAccess, ObjectHash, ProofListIndex, ProofMapIndex}; use btc_transaction_utils::multisig::RedeemScript; use log::{error, trace}; diff --git a/src/blockchain/transactions.rs b/src/blockchain/transactions.rs index b3cc040..82e69f0 100644 --- a/src/blockchain/transactions.rs +++ b/src/blockchain/transactions.rs @@ -62,10 +62,10 @@ impl TxSignature { } impl Transaction for TxSignature { - fn execute(&self, mut context: TransactionContext) -> ExecutionResult { + fn execute(&self, context: TransactionContext) -> ExecutionResult { // TODO Checks that transaction author is validator. let tx = &self.transaction; - let mut schema = BtcAnchoringSchema::new(context.fork()); + let schema = BtcAnchoringSchema::new(context.fork()); // Checks that the number of signatures is sufficient to spend. if schema .anchoring_transactions_chain() diff --git a/src/service.rs b/src/service.rs index 671e72e..9350b21 100644 --- a/src/service.rs +++ b/src/service.rs @@ -98,7 +98,7 @@ impl Service for BtcAnchoringService { .last() .expect("An attempt to invoke execute during the genesis block initialization."); - let mut schema = BtcAnchoringSchema::new(fork); + let schema = BtcAnchoringSchema::new(fork); schema.anchored_blocks().push(block_header_hash); } diff --git a/src/test_helpers/testkit.rs b/src/test_helpers/testkit.rs index 402a370..7d3bf46 100644 --- a/src/test_helpers/testkit.rs +++ b/src/test_helpers/testkit.rs @@ -30,7 +30,7 @@ use exonum::{ helpers::Height, messages::{Message, RawTransaction, Signed}, }; -use exonum_merkledb::{MapProof, ObjectHash}; +use exonum_merkledb::{MapProof}; use exonum_testkit::{ ApiKind, TestKit, TestKitApi, TestKitBuilder, TestNetworkConfiguration, TestNode, }; From d99d3783c79301f4fc351d7139ee839a8ca4bb8c Mon Sep 17 00:00:00 2001 From: Aleksei Sidorov Date: Mon, 29 Apr 2019 19:22:44 +0300 Subject: [PATCH 3/8] Use object access API instead of index --- src/blockchain/schema.rs | 26 +++++++++++------------ src/test_helpers/testkit.rs | 7 ++++++- tests/api.rs | 13 ++++-------- tests/sync.rs | 42 +++++++++++++++++++------------------ 4 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/blockchain/schema.rs b/src/blockchain/schema.rs index 08486f6..5dcb73b 100644 --- a/src/blockchain/schema.rs +++ b/src/blockchain/schema.rs @@ -17,7 +17,7 @@ use exonum::blockchain::{Schema, StoredConfiguration}; use exonum::crypto::Hash; use exonum::helpers::Height; -use exonum_merkledb::{IndexAccess, ObjectHash, ProofListIndex, ProofMapIndex}; +use exonum_merkledb::{ObjectAccess, ObjectHash, ProofListIndex, ProofMapIndex, RefMut}; use btc_transaction_utils::multisig::RedeemScript; use log::{error, trace}; @@ -54,30 +54,30 @@ pub struct BtcAnchoringSchema { access: T, } -impl BtcAnchoringSchema { +impl BtcAnchoringSchema { /// Constructs schema for the given database `snapshot`. pub fn new(access: T) -> Self { Self { access } } /// Returns table that contains complete chain of the anchoring transactions. - pub fn anchoring_transactions_chain(&self) -> ProofListIndex { - ProofListIndex::new(TRANSACTIONS_CHAIN, self.access) + pub fn anchoring_transactions_chain(&self) -> RefMut> { + self.access.get_object(TRANSACTIONS_CHAIN) } /// Returns the table that contains already spent funding transactions. - pub fn spent_funding_transactions(&self) -> ProofMapIndex { - ProofMapIndex::new(SPENT_FUNDING_TRANSACTIONS, self.access) + pub fn spent_funding_transactions(&self) -> RefMut> { + self.access.get_object(SPENT_FUNDING_TRANSACTIONS) } /// Returns the table that contains signatures for the given transaction input. - pub fn transaction_signatures(&self) -> ProofMapIndex { - ProofMapIndex::new(TRANSACTION_SIGNATURES, self.access) + pub fn transaction_signatures(&self) -> RefMut> { + self.access.get_object(TRANSACTION_SIGNATURES) } /// Returns a list of hashes of Exonum blocks headers. - pub fn anchored_blocks(&self) -> ProofListIndex { - ProofListIndex::new(ANCHORED_BLOCKS, self.access) + pub fn anchored_blocks(&self) -> RefMut> { + self.access.get_object(ANCHORED_BLOCKS) } /// Returns hashes of the stored tables. @@ -92,14 +92,14 @@ impl BtcAnchoringSchema { /// Returns the actual anchoring configuration. pub fn actual_configuration(&self) -> GlobalConfig { - let actual_configuration = Schema::new(self.access).actual_configuration(); + let actual_configuration = Schema::new(self.access.clone()).actual_configuration(); Self::parse_config(&actual_configuration) .expect("Actual BTC anchoring configuration is absent") } /// Returns the nearest following configuration if it exists. pub fn following_configuration(&self) -> Option { - let following_configuration = Schema::new(self.access).following_configuration()?; + let following_configuration = Schema::new(self.access.clone()).following_configuration()?; Self::parse_config(&following_configuration) } @@ -182,7 +182,7 @@ impl BtcAnchoringSchema { let anchoring_height = actual_state.following_anchoring_height(latest_anchored_height); let anchoring_block_hash = - Schema::new(self.access).block_hash_by_height(anchoring_height)?; + Schema::new(self.access.clone()).block_hash_by_height(anchoring_height)?; builder.payload(anchoring_height, anchoring_block_hash); builder.fee(config.transaction_fee); diff --git a/src/test_helpers/testkit.rs b/src/test_helpers/testkit.rs index 7d3bf46..e71e55c 100644 --- a/src/test_helpers/testkit.rs +++ b/src/test_helpers/testkit.rs @@ -30,7 +30,7 @@ use exonum::{ helpers::Height, messages::{Message, RawTransaction, Signed}, }; -use exonum_merkledb::{MapProof}; +use exonum_merkledb::{MapProof, ObjectAccess}; use exonum_testkit::{ ApiKind, TestKit, TestKitApi, TestKitBuilder, TestNetworkConfiguration, TestNode, }; @@ -418,6 +418,11 @@ impl AnchoringTestKit { .get(height.0) .unwrap() } + + /// Returns the current snapshot of the btc anchoring information schema. + pub fn schema(&self) -> BtcAnchoringSchema { + BtcAnchoringSchema::new(Arc::from(self.inner.snapshot())) + } fn get_local_cfg(&self, node: &TestNode) -> LocalConfig { self.node_configs[node.validator_id().unwrap().0 as usize].clone() diff --git a/tests/api.rs b/tests/api.rs index 0f23d0b..5e3ae17 100644 --- a/tests/api.rs +++ b/tests/api.rs @@ -14,7 +14,6 @@ use exonum::helpers::Height; use exonum_btc_anchoring::{ api::{FindTransactionQuery, HeightQuery, PublicApi}, - blockchain::BtcAnchoringSchema, btc, config::GlobalConfig, test_helpers::testkit::{AnchoringTestKit, ValidateProof}, @@ -120,8 +119,7 @@ fn find_transaction_regular() { anchoring_testkit.create_blocks_until(next_anchoring_height); } - let snapshot = anchoring_testkit.snapshot(); - let anchoring_schema = BtcAnchoringSchema::new(&snapshot); + let anchoring_schema = anchoring_testkit.schema(); let tx_chain = anchoring_schema.anchoring_transactions_chain(); assert_eq!( @@ -171,8 +169,7 @@ fn find_transaction_configuration_change() { anchoring_testkit.create_block_with_transactions(signatures); anchoring_testkit.create_block(); - let snapshot = anchoring_testkit.snapshot(); - let anchoring_schema = BtcAnchoringSchema::new(&snapshot); + let anchoring_schema = anchoring_testkit.schema(); assert_eq!( find_transaction(&anchoring_testkit, Some(Height(0))), anchoring_schema.anchoring_transactions_chain().get(1) @@ -194,8 +191,7 @@ fn find_transaction_configuration_change() { anchoring_testkit.create_block_with_transactions(signatures); anchoring_testkit.create_block(); - let snapshot = anchoring_testkit.snapshot(); - let anchoring_schema = BtcAnchoringSchema::new(&snapshot); + let anchoring_schema = anchoring_testkit.schema(); assert_eq!( find_transaction(&anchoring_testkit, Some(Height(0))), anchoring_schema.anchoring_transactions_chain().get(1) @@ -220,8 +216,7 @@ fn find_transaction_configuration_change() { anchoring_testkit.create_block_with_transactions(signatures); anchoring_testkit.create_block(); - let snapshot = anchoring_testkit.snapshot(); - let anchoring_schema = BtcAnchoringSchema::new(&snapshot); + let anchoring_schema = anchoring_testkit.schema(); assert_eq!( find_transaction(&anchoring_testkit, Some(Height(0))), anchoring_schema.anchoring_transactions_chain().get(1) diff --git a/tests/sync.rs b/tests/sync.rs index 7298e68..4b745eb 100644 --- a/tests/sync.rs +++ b/tests/sync.rs @@ -16,11 +16,12 @@ use hex::FromHex; use exonum::crypto::Hash; use exonum::helpers::Height; use exonum_bitcoinrpc as bitcoin_rpc; -use exonum_btc_anchoring::blockchain::BtcAnchoringSchema; -use exonum_btc_anchoring::btc::Transaction; -use exonum_btc_anchoring::rpc::TransactionInfo as BtcTransactionInfo; -use exonum_btc_anchoring::test_helpers::rpc::{FakeRelayRequest, FakeRelayResponse, TestRequest}; -use exonum_btc_anchoring::test_helpers::testkit::AnchoringTestKit; +use exonum_btc_anchoring::{ + btc::Transaction, + rpc::TransactionInfo as BtcTransactionInfo, + test_helpers::rpc::{FakeRelayRequest, FakeRelayResponse, TestRequest}, + test_helpers::testkit::AnchoringTestKit, +}; fn funding_tx_request() -> TestRequest { ( @@ -54,9 +55,8 @@ fn normal_operation() { .create_signature_tx_for_validators(2) .unwrap(); - let snapshot = anchoring_testkit.snapshot(); - let schema = BtcAnchoringSchema::new(&snapshot); - let (proposed, _) = schema + let (proposed, _) = anchoring_testkit + .schema() .actual_proposed_anchoring_transaction() .unwrap() .unwrap(); @@ -79,9 +79,11 @@ fn normal_operation() { anchoring_testkit.create_blocks_until(Height(2)); - let snapshot = anchoring_testkit.snapshot(); - let schema = BtcAnchoringSchema::new(&snapshot); - let last_tx = schema.anchoring_transactions_chain().last().unwrap(); + let last_tx = anchoring_testkit + .schema() + .anchoring_transactions_chain() + .last() + .unwrap(); // Should retry requests.expect(vec![ @@ -137,9 +139,8 @@ fn several_unsynced() { .create_signature_tx_for_validators(3) .unwrap(); - let snapshot = anchoring_testkit.snapshot(); - let schema = BtcAnchoringSchema::new(&snapshot); - let (proposed_0, _) = schema + let (proposed_0, _) = anchoring_testkit + .schema() .actual_proposed_anchoring_transaction() .unwrap() .unwrap(); @@ -162,9 +163,11 @@ fn several_unsynced() { anchoring_testkit.create_blocks_until(Height(2)); - let snapshot = anchoring_testkit.snapshot(); - let schema = BtcAnchoringSchema::new(&snapshot); - let last_tx = schema.anchoring_transactions_chain().last().unwrap(); + let last_tx = anchoring_testkit + .schema() + .anchoring_transactions_chain() + .last() + .unwrap(); // Sync failed requests.expect(vec![ @@ -189,9 +192,8 @@ fn several_unsynced() { .create_signature_tx_for_validators(3) .unwrap(); - let snapshot = anchoring_testkit.snapshot(); - let schema = BtcAnchoringSchema::new(&snapshot); - let (proposed_1, _) = schema + let (proposed_1, _) = anchoring_testkit + .schema() .actual_proposed_anchoring_transaction() .unwrap() .unwrap(); From bce4df47e506a5d2576c45c23e02b92b25eaeff4 Mon Sep 17 00:00:00 2001 From: Aleksei Sidorov Date: Tue, 30 Apr 2019 14:42:08 +0300 Subject: [PATCH 4/8] Polish code --- src/factory/mod.rs | 4 ++-- src/rpc.rs | 6 ------ 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/factory/mod.rs b/src/factory/mod.rs index 268edb5..5f80a2f 100644 --- a/src/factory/mod.rs +++ b/src/factory/mod.rs @@ -262,7 +262,7 @@ impl CommandExtension for Finalize { // Creates global configuration. let mut global_config = GlobalConfig::with_public_keys(network, public_keys)?; // Generates initial funding transaction. - let relay = BitcoinRpcClient::from(rpc_config.clone()); + let relay = BitcoinRpcClient::new(rpc_config.clone()); let addr = global_config.anchoring_address(); let funding_tx = if let Some(funding_txid) = funding_txid { let info = relay.transaction_info(&funding_txid)?.ok_or_else(|| { @@ -346,7 +346,7 @@ impl ServiceFactory for BtcAnchoringFactory { let btc_relay = btc_anchoring_config .local .rpc - .map(BitcoinRpcClient::from) + .map(BitcoinRpcClient::new) .map(Box::::from); let service = BtcAnchoringService::new( btc_anchoring_config.global, diff --git a/src/rpc.rs b/src/rpc.rs index 28d01ed..ed99f0b 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -77,12 +77,6 @@ impl BitcoinRpcClient { } } -impl From for BitcoinRpcClient { - fn from(cfg: BitcoinRpcConfig) -> Self { - Self::new(cfg) - } -} - impl From for Box { fn from(client: BitcoinRpcClient) -> Self { Box::new(client) as Self From 7b8e828049e54e88a52a6e655f55d3eeb484da72 Mon Sep 17 00:00:00 2001 From: Aleksei Sidorov Date: Tue, 13 Aug 2019 16:10:20 +0300 Subject: [PATCH 5/8] Modernize code style --- .travis.yml | 2 +- Cargo.toml | 12 ++-- src/api.rs | 15 +++-- .../data_layout/input_signatures.rs | 9 +-- src/blockchain/data_layout/mod.rs | 3 +- src/blockchain/data_layout/tx_input_id.rs | 3 +- src/blockchain/errors.rs | 4 +- src/blockchain/mod.rs | 12 ++-- src/blockchain/schema.rs | 22 +++---- src/blockchain/transactions.rs | 5 +- src/btc/macros.rs | 14 ++--- src/btc/mod.rs | 9 +-- src/btc/payload.rs | 9 +-- src/btc/transaction.rs | 19 ++++--- src/config.rs | 17 +++--- src/factory/args.rs | 11 ++-- src/factory/mod.rs | 32 ++++++----- src/handler.rs | 21 ++++--- src/lib.rs | 12 ++-- src/proto/mod.rs | 3 +- src/rpc.rs | 3 +- src/service.rs | 26 +++++---- src/test_helpers/rpc.rs | 9 +-- src/test_helpers/testkit.rs | 21 +++---- tests/api.rs | 21 ++----- tests/sync.rs | 57 +++++++------------ tests/testnet_tests.rs | 4 +- 27 files changed, 174 insertions(+), 201 deletions(-) diff --git a/.travis.yml b/.travis.yml index c9b5e81..2a45c5b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,7 +29,7 @@ addons: rust: # Feel free to bump this version if you need features of newer Rust. # Sync with badge in README.md - - 1.35.0 + - 1.36.0 matrix: allow_failures: diff --git a/Cargo.toml b/Cargo.toml index d3a84b0..3ba671a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ byteorder = "1.3" clap = "2.32" derive_more = "0.14" exonum = "0.11.0" -exonum-merkledb = { git = "https://github.com/alekseysidorov/exonum.git", branch = "merkledb_fixes" } +exonum-merkledb = { git = "https://github.com/exonum/exonum.git", rev = "33e45eac5c9d710a100ce80b56d2d31a5b1f6dfc" } exonum_bitcoinrpc = "0.6" exonum-derive = "0.11.0" exonum-testkit = "0.11.0" @@ -52,8 +52,8 @@ proptest = "0.9" exonum-build = "0.11.0" [patch.crates-io] -exonum = { git = "https://github.com/alekseysidorov/exonum.git", branch = "merkledb_fixes" } -exonum-derive = { git = "https://github.com/alekseysidorov/exonum.git", branch = "merkledb_fixes" } -exonum-configuration = { git = "https://github.com/alekseysidorov/exonum.git", branch = "merkledb_fixes" } -exonum-build = { git = "https://github.com/alekseysidorov/exonum.git", branch = "merkledb_fixes" } -exonum-testkit = { git = "https://github.com/alekseysidorov/exonum.git", branch = "merkledb_fixes" } +exonum = { git = "https://github.com/exonum/exonum.git", rev = "33e45eac5c9d710a100ce80b56d2d31a5b1f6dfc" } +exonum-derive = { git = "https://github.com/exonum/exonum.git", rev = "33e45eac5c9d710a100ce80b56d2d31a5b1f6dfc" } +exonum-configuration = { git = "https://github.com/exonum/exonum.git", rev = "33e45eac5c9d710a100ce80b56d2d31a5b1f6dfc" } +exonum-build = { git = "https://github.com/exonum/exonum.git", rev = "33e45eac5c9d710a100ce80b56d2d31a5b1f6dfc" } +exonum-testkit = { git = "https://github.com/exonum/exonum.git", rev = "33e45eac5c9d710a100ce80b56d2d31a5b1f6dfc" } diff --git a/src/api.rs b/src/api.rs index 99ccbe3..d57c766 100644 --- a/src/api.rs +++ b/src/api.rs @@ -14,12 +14,13 @@ //! Anchoring HTTP API implementation. -use exonum::api::{self, ServiceApiBuilder, ServiceApiState}; -use exonum::blockchain::{BlockProof, Schema as CoreSchema}; -use exonum::crypto::Hash; -use exonum::helpers::Height; +use exonum::{ + api::{self, ServiceApiBuilder, ServiceApiState}, + blockchain::{BlockProof, Schema as CoreSchema}, + crypto::Hash, + helpers::Height, +}; use exonum_merkledb::{ListProof, MapProof}; - use failure::Fail; use serde_derive::{Deserialize, Serialize}; @@ -28,9 +29,7 @@ use std::cmp::{ Ordering::{self, Equal, Greater, Less}, }; -use crate::blockchain::BtcAnchoringSchema; -use crate::btc; -use crate::BTC_ANCHORING_SERVICE_ID; +use crate::{blockchain::BtcAnchoringSchema, btc, BTC_ANCHORING_SERVICE_ID}; /// Query parameters for the find transaction request. #[derive(Debug, Clone, Copy, Serialize, Deserialize)] diff --git a/src/blockchain/data_layout/input_signatures.rs b/src/blockchain/data_layout/input_signatures.rs index 9e12313..7e41563 100644 --- a/src/blockchain/data_layout/input_signatures.rs +++ b/src/blockchain/data_layout/input_signatures.rs @@ -12,11 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -use exonum::crypto::{self, Hash}; -use exonum::helpers::ValidatorId; -use exonum_merkledb::{BinaryValue, ObjectHash}; - use byteorder::{ByteOrder, LittleEndian, WriteBytesExt}; +use exonum::{ + crypto::{self, Hash}, + helpers::ValidatorId, +}; +use exonum_merkledb::{BinaryValue, ObjectHash}; use serde_derive::{Deserialize, Serialize}; use std::{ diff --git a/src/blockchain/data_layout/mod.rs b/src/blockchain/data_layout/mod.rs index c9e03e4..0535c03 100644 --- a/src/blockchain/data_layout/mod.rs +++ b/src/blockchain/data_layout/mod.rs @@ -14,8 +14,7 @@ //! Additional data types for the BTC anchoring information schema. -pub use self::input_signatures::InputSignatures; -pub use self::tx_input_id::TxInputId; +pub use self::{input_signatures::InputSignatures, tx_input_id::TxInputId}; mod input_signatures; mod tx_input_id; diff --git a/src/blockchain/data_layout/tx_input_id.rs b/src/blockchain/data_layout/tx_input_id.rs index 1700f33..8173458 100644 --- a/src/blockchain/data_layout/tx_input_id.rs +++ b/src/blockchain/data_layout/tx_input_id.rs @@ -12,11 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use exonum::crypto::{self, Hash}; use exonum_merkledb::{BinaryKey, ObjectHash}; -use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; - use std::io::{Cursor, Read, Write}; /// Unique transaction input identifier composed of a transaction identifier diff --git a/src/blockchain/errors.rs b/src/blockchain/errors.rs index 07987dc..dd7392e 100644 --- a/src/blockchain/errors.rs +++ b/src/blockchain/errors.rs @@ -14,9 +14,7 @@ //! Error types of the BTC anchoring service. -use exonum::blockchain::ExecutionError; -use exonum::crypto::Hash; -use exonum::helpers::ValidatorId; +use exonum::{blockchain::ExecutionError, crypto::Hash, helpers::ValidatorId}; use failure_derive::Fail; diff --git a/src/blockchain/mod.rs b/src/blockchain/mod.rs index 143218f..8554360 100644 --- a/src/blockchain/mod.rs +++ b/src/blockchain/mod.rs @@ -14,17 +14,13 @@ //! Blockchain implementation details for the BTC anchoring service. -pub use self::schema::BtcAnchoringSchema; -pub use self::transactions::Transactions; - -use exonum::helpers::Height; +pub use self::{schema::BtcAnchoringSchema, transactions::Transactions}; use bitcoin::blockdata::script::Script; -use btc_transaction_utils::multisig::RedeemScript; -use btc_transaction_utils::p2wsh; +use btc_transaction_utils::{multisig::RedeemScript, p2wsh}; +use exonum::helpers::Height; -use crate::btc::Address; -use crate::config::GlobalConfig; +use crate::{btc::Address, config::GlobalConfig}; pub mod data_layout; pub mod errors; diff --git a/src/blockchain/schema.rs b/src/blockchain/schema.rs index 5dcb73b..0f247ed 100644 --- a/src/blockchain/schema.rs +++ b/src/blockchain/schema.rs @@ -14,21 +14,23 @@ //! Information schema for the btc anchoring service. -use exonum::blockchain::{Schema, StoredConfiguration}; -use exonum::crypto::Hash; -use exonum::helpers::Height; -use exonum_merkledb::{ObjectAccess, ObjectHash, ProofListIndex, ProofMapIndex, RefMut}; - use btc_transaction_utils::multisig::RedeemScript; +use exonum::{ + blockchain::{Schema, StoredConfiguration}, + crypto::Hash, + helpers::Height, +}; +use exonum_merkledb::{ObjectAccess, ObjectHash, ProofListIndex, ProofMapIndex, RefMut}; use log::{error, trace}; use serde_json; -use crate::btc::{BtcAnchoringTransactionBuilder, BuilderError, Transaction}; -use crate::config::GlobalConfig; -use crate::BTC_ANCHORING_SERVICE_NAME; +use crate::{ + btc::{BtcAnchoringTransactionBuilder, BuilderError, Transaction}, + config::GlobalConfig, + BTC_ANCHORING_SERVICE_NAME, +}; -use super::data_layout::*; -use super::BtcAnchoringState; +use super::{data_layout::*, BtcAnchoringState}; /// Defines `&str` constants with given name and value. macro_rules! define_names { diff --git a/src/blockchain/transactions.rs b/src/blockchain/transactions.rs index 82e69f0..e965d8b 100644 --- a/src/blockchain/transactions.rs +++ b/src/blockchain/transactions.rs @@ -14,18 +14,15 @@ //! BTC anchoring transactions. +use btc_transaction_utils::{p2wsh::InputSigner, InputSignature, TxInRef}; use exonum::{ blockchain::{ExecutionResult, Transaction, TransactionContext}, helpers::ValidatorId, }; use exonum_derive::{ProtobufConvert, TransactionSet}; - -use btc_transaction_utils::{p2wsh::InputSigner, InputSignature, TxInRef}; use log::{info, trace}; use serde_derive::{Deserialize, Serialize}; -use std::borrow::Cow; - use crate::{btc, proto}; use super::{data_layout::TxInputId, errors::SignatureError, BtcAnchoringSchema}; diff --git a/src/btc/macros.rs b/src/btc/macros.rs index 0e9e441..466adcf 100644 --- a/src/btc/macros.rs +++ b/src/btc/macros.rs @@ -95,21 +95,21 @@ macro_rules! impl_string_conversions_for_hex { macro_rules! impl_serde_str { ($name:ident) => { - impl ::serde::Serialize for $name { - fn serialize(&self, ser: S) -> ::std::result::Result + impl serde::Serialize for $name { + fn serialize(&self, ser: S) -> std::result::Result where - S: ::serde::Serializer, + S: serde::Serializer, { - ::serde_str::serialize(self, ser) + serde_str::serialize(self, ser) } } - impl<'de> ::serde::Deserialize<'de> for $name { + impl<'de> serde::Deserialize<'de> for $name { fn deserialize(deserializer: D) -> Result where - D: ::serde::Deserializer<'de>, + D: serde::Deserializer<'de>, { - ::serde_str::deserialize(deserializer) + serde_str::deserialize(deserializer) } } }; diff --git a/src/btc/mod.rs b/src/btc/mod.rs index 63bc49a..0d1e24e 100644 --- a/src/btc/mod.rs +++ b/src/btc/mod.rs @@ -14,11 +14,12 @@ //! Collection of wrappers for the rust-bitcoin crate. -pub use self::payload::Payload; -pub use self::transaction::{BtcAnchoringTransactionBuilder, BuilderError, Transaction}; +pub use self::{ + payload::Payload, + transaction::{BtcAnchoringTransactionBuilder, BuilderError, Transaction}, +}; -use bitcoin::network::constants::Network; -use bitcoin::util::address; +use bitcoin::{network::constants::Network, util::address}; use btc_transaction_utils; use derive_more::{From, Into}; use hex::{self, FromHex, ToHex}; diff --git a/src/btc/payload.rs b/src/btc/payload.rs index e92a8be..7ca8453 100644 --- a/src/btc/payload.rs +++ b/src/btc/payload.rs @@ -12,11 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -use exonum::crypto::Hash; -use exonum::helpers::Height; +use exonum::{crypto::Hash, helpers::Height}; -use bitcoin::blockdata::opcodes::all::OP_RETURN; -use bitcoin::blockdata::script::{Builder, Instruction, Script}; +use bitcoin::blockdata::{ + opcodes::all::OP_RETURN, + script::{Builder, Instruction, Script}, +}; use byteorder::{ByteOrder, LittleEndian}; use serde_derive::{Deserialize, Serialize}; diff --git a/src/btc/transaction.rs b/src/btc/transaction.rs index 824d6e5..fbb0b9e 100644 --- a/src/btc/transaction.rs +++ b/src/btc/transaction.rs @@ -7,11 +7,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -use exonum::crypto::Hash; -use exonum::helpers::Height; +use exonum::{crypto::Hash, helpers::Height}; -use bitcoin::blockdata::script::Script; -use bitcoin::blockdata::transaction::{self, OutPoint, TxIn, TxOut}; +use bitcoin::blockdata::{ + script::Script, + transaction::{self, OutPoint, TxIn, TxOut}, +}; use btc_transaction_utils::multisig::RedeemScript; use derive_more::{From, Into}; use failure_derive::Fail; @@ -352,7 +353,7 @@ mod tests { vout, }, script_sig: Script::default(), - sequence: 0xFFFFFFFF, + sequence: 0xFFFF_FFFF, witness: Vec::default(), } }).collect::>(); @@ -433,7 +434,7 @@ mod tests { "02536d5e1464b961562da57207e4a46edb7dade9b92aa29712ca8309c8aba5be5b", ] .iter() - .map(|h| PublicKey::from_hex(h).unwrap().0.clone()) + .map(|h| PublicKey::from_hex(h).unwrap().0) .collect::>(); let redeem_script = RedeemScriptBuilder::with_public_keys(keys) @@ -504,7 +505,7 @@ mod tests { "02536d5e1464b961562da57207e4a46edb7dade9b92aa29712ca8309c8aba5be5b", ] .iter() - .map(|h| PublicKey::from_hex(h).unwrap().0.clone()) + .map(|h| PublicKey::from_hex(h).unwrap().0) .collect::>(); let redeem_script = RedeemScriptBuilder::with_public_keys(keys) @@ -553,7 +554,7 @@ mod tests { "02536d5e1464b961562da57207e4a46edb7dade9b92aa29712ca8309c8aba5be5b", ] .iter() - .map(|h| PublicKey::from_hex(h).unwrap().0.clone()) + .map(|h| PublicKey::from_hex(h).unwrap().0) .collect::>(); let prev_tx: Transaction = Transaction::from_hex( @@ -602,7 +603,7 @@ mod tests { "02536d5e1464b961562da57207e4a46edb7dade9b92aa29712ca8309c8aba5be5b", ] .iter() - .map(|h| PublicKey::from_hex(h).unwrap().0.clone()) + .map(|h| PublicKey::from_hex(h).unwrap().0) .collect::>(); let redeem_script = RedeemScriptBuilder::with_public_keys(keys) diff --git a/src/config.rs b/src/config.rs index 3500701..cda529d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -14,21 +14,24 @@ //! BTC anchoring configuration data types. -use exonum::helpers::Height; - use bitcoin::network::constants::Network; -use btc_transaction_utils::multisig::{RedeemScript, RedeemScriptBuilder, RedeemScriptError}; -use btc_transaction_utils::p2wsh; +use btc_transaction_utils::{ + multisig::{RedeemScript, RedeemScriptBuilder, RedeemScriptError}, + p2wsh, +}; +use exonum::helpers::Height; use serde_derive::{Deserialize, Serialize}; use std::collections::HashMap; -use crate::btc::{Address, PrivateKey, PublicKey, Transaction}; -use crate::rpc::BitcoinRpcConfig; +use crate::{ + btc::{Address, PrivateKey, PublicKey, Transaction}, + rpc::BitcoinRpcConfig, +}; /// Returns sufficient number of keys for the given validators number. pub fn byzantine_quorum(total: usize) -> usize { - ::exonum::node::state::State::byzantine_majority_count(total) + exonum::node::state::State::byzantine_majority_count(total) } /// Consensus parameters in the BTC anchoring. diff --git a/src/factory/args.rs b/src/factory/args.rs index 037a192..b512343 100644 --- a/src/factory/args.rs +++ b/src/factory/args.rs @@ -12,20 +12,19 @@ // See the License for the specific language governing permissions and // limitations under the License. -use exonum::helpers::fabric::{Argument, Context}; - +use exonum::{ + crypto, + helpers::fabric::{Argument, Context}, +}; use failure::format_err; use hex::{FromHex, FromHexError}; -use serde::de::DeserializeOwned; -use serde::ser::Serialize; +use serde::{de::DeserializeOwned, ser::Serialize}; use serde_derive::{Deserialize, Serialize}; use toml; use std::collections::BTreeMap; use std::str::FromStr; -use exonum::crypto; - #[derive(Clone, Serialize, Deserialize)] pub struct Hash(pub crypto::Hash); diff --git a/src/factory/mod.rs b/src/factory/mod.rs index 5f80a2f..3d35ec4 100644 --- a/src/factory/mod.rs +++ b/src/factory/mod.rs @@ -12,26 +12,30 @@ // See the License for the specific language governing permissions and // limitations under the License. -use exonum::blockchain::Service; - -use exonum::helpers::fabric::{ - self, keys, Argument, Command, CommandExtension, CommandName, Context, ServiceFactory, -}; -use exonum::node::NodeConfig; - use bitcoin::network::constants::Network; +use exonum::{ + blockchain::Service, + helpers::fabric::{ + self, keys, Argument, Command, CommandExtension, CommandName, Context, ServiceFactory, + }, + node::NodeConfig, +}; use failure::{ensure, format_err}; use log::info; use toml; -use std::collections::{BTreeMap, HashMap}; -use std::path::PathBuf; -use std::sync::{Arc, RwLock}; +use std::{ + collections::{BTreeMap, HashMap}, + path::PathBuf, + sync::{Arc, RwLock}, +}; -use crate::btc::{gen_keypair, PrivateKey, PublicKey}; -use crate::config::{Config, GlobalConfig, LocalConfig}; -use crate::rpc::{BitcoinRpcClient, BitcoinRpcConfig, BtcRelay}; -use crate::{BtcAnchoringService, BTC_ANCHORING_SERVICE_NAME}; +use crate::{ + btc::{gen_keypair, PrivateKey, PublicKey}, + config::{Config, GlobalConfig, LocalConfig}, + rpc::{BitcoinRpcClient, BitcoinRpcConfig, BtcRelay}, + {BtcAnchoringService, BTC_ANCHORING_SERVICE_NAME}, +}; use self::args::{Hash, NamedArgumentOptional, NamedArgumentRequired, TypedArgument}; diff --git a/src/handler.rs b/src/handler.rs index 54bece0..35b0758 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -12,22 +12,21 @@ // See the License for the specific language governing permissions and // limitations under the License. -use exonum::blockchain::ServiceContext; -use exonum::helpers::ValidatorId; +use exonum::{blockchain::ServiceContext, helpers::ValidatorId}; -use btc_transaction_utils::p2wsh; -use btc_transaction_utils::TxInRef; +use btc_transaction_utils::{p2wsh, TxInRef}; use failure::format_err; use log::trace; -use std::cmp; -use std::collections::HashMap; +use std::{cmp, collections::HashMap}; -use crate::blockchain::data_layout::TxInputId; -use crate::blockchain::transactions::TxSignature; -use crate::blockchain::{BtcAnchoringSchema, BtcAnchoringState}; -use crate::btc::{Address, PrivateKey}; -use crate::rpc::BtcRelay; +use crate::{ + blockchain::data_layout::TxInputId, + blockchain::transactions::TxSignature, + blockchain::{BtcAnchoringSchema, BtcAnchoringState}, + btc::{Address, PrivateKey}, + rpc::BtcRelay, +}; /// The goal of this task is to create anchoring transactions for the corresponding heights. pub struct UpdateAnchoringChainTask<'a> { diff --git a/src/lib.rs b/src/lib.rs index 4fa7977..c3ca396 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,11 +62,6 @@ bare_trait_objects )] -use log::{error, warn}; - -mod handler; -mod proto; - pub use crate::factory::BtcAnchoringFactory as ServiceFactory; pub use crate::service::{ BtcAnchoringService, BTC_ANCHORING_SERVICE_ID, BTC_ANCHORING_SERVICE_NAME, @@ -82,12 +77,17 @@ pub mod test_helpers; pub(crate) mod factory; pub(crate) mod service; +use log::{error, warn}; + +mod handler; +mod proto; + pub(crate) trait ResultEx { fn log_error(self); fn log_warn(self); } -impl ResultEx for Result<(), T> { +impl ResultEx for Result<(), T> { fn log_error(self) { if let Err(e) = self { error!("{}", e); diff --git a/src/proto/mod.rs b/src/proto/mod.rs index 82c05c4..f8305b4 100644 --- a/src/proto/mod.rs +++ b/src/proto/mod.rs @@ -22,9 +22,8 @@ pub use self::btc_anchoring::TxSignature; use bitcoin; use btc_transaction_utils; -use failure; - use exonum::proto::ProtobufConvert; +use failure; use crate::btc; diff --git a/src/rpc.rs b/src/rpc.rs index ed99f0b..860f991 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -14,9 +14,8 @@ //! Collections of helpers for synchronization with the Bitcoin network. -use exonum::crypto::Hash; - use bitcoin::util::address::Address; +use exonum::crypto::Hash; use exonum_bitcoinrpc as bitcoin_rpc; use failure; use hex::FromHex; diff --git a/src/service.rs b/src/service.rs index 9350b21..7dce772 100644 --- a/src/service.rs +++ b/src/service.rs @@ -12,12 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -use exonum::api::ServiceApiBuilder; -use exonum::blockchain::{ - Schema as CoreSchema, Service, ServiceContext, Transaction, TransactionSet, +use exonum::{ + api::ServiceApiBuilder, + blockchain::{Schema as CoreSchema, Service, ServiceContext, Transaction, TransactionSet}, + crypto::Hash, + messages::RawTransaction, }; -use exonum::crypto::Hash; -use exonum::messages::RawTransaction; use exonum_merkledb::{Fork, Snapshot}; use serde_json::json; @@ -26,13 +26,15 @@ use std::sync::{Arc, RwLock}; use std::collections::HashMap; -use crate::api; -use crate::blockchain::{BtcAnchoringSchema, Transactions}; -use crate::btc::{Address, PrivateKey}; -use crate::config::GlobalConfig; -use crate::handler::{SyncWithBtcRelayTask, UpdateAnchoringChainTask}; -use crate::rpc::BtcRelay; -use crate::ResultEx; +use crate::{ + api, + blockchain::{BtcAnchoringSchema, Transactions}, + btc::{Address, PrivateKey}, + config::GlobalConfig, + handler::{SyncWithBtcRelayTask, UpdateAnchoringChainTask}, + rpc::BtcRelay, + ResultEx, +}; /// Anchoring service id. pub const BTC_ANCHORING_SERVICE_ID: u16 = 3; diff --git a/src/test_helpers/rpc.rs b/src/test_helpers/rpc.rs index d4c7f54..e7268a5 100644 --- a/src/test_helpers/rpc.rs +++ b/src/test_helpers/rpc.rs @@ -14,14 +14,15 @@ //! Helpers for the bitcoin rpc testing. -use exonum::crypto::Hash; - use bitcoin::util::address::Address; +use exonum::crypto::Hash; use failure; use log::trace; -use std::collections::VecDeque; -use std::sync::{Arc, Mutex}; +use std::{ + collections::VecDeque, + sync::{Arc, Mutex}, +}; use crate::{ btc, diff --git a/src/test_helpers/testkit.rs b/src/test_helpers/testkit.rs index e71e55c..bc2f0e2 100644 --- a/src/test_helpers/testkit.rs +++ b/src/test_helpers/testkit.rs @@ -17,12 +17,6 @@ use bitcoin::{self, network::constants::Network, util::address::Address}; use bitcoin_hashes::{sha256d::Hash as Sha256dHash, Hash as BitcoinHash}; use btc_transaction_utils::{multisig::RedeemScript, p2wsh, TxInRef}; -use failure::{ensure, format_err}; -use hex::FromHex; -use log::trace; -use maplit::hashmap; -use rand::{thread_rng, Rng, SeedableRng, StdRng}; - use exonum::{ api, blockchain::{BlockProof, Blockchain, Schema as CoreSchema, StoredConfiguration}, @@ -34,10 +28,17 @@ use exonum_merkledb::{MapProof, ObjectAccess}; use exonum_testkit::{ ApiKind, TestKit, TestKitApi, TestKitBuilder, TestNetworkConfiguration, TestNode, }; +use failure::{ensure, format_err}; +use hex::FromHex; +use log::trace; +use maplit::hashmap; +use rand::{thread_rng, Rng, SeedableRng, StdRng}; -use std::ops::{Deref, DerefMut}; -use std::str::FromStr; -use std::sync::{Arc, RwLock}; +use std::{ + ops::{Deref, DerefMut}, + str::FromStr, + sync::{Arc, RwLock}, +}; use crate::{ api::{BlockHeaderProof, FindTransactionQuery, HeightQuery, PublicApi, TransactionProof}, @@ -418,7 +419,7 @@ impl AnchoringTestKit { .get(height.0) .unwrap() } - + /// Returns the current snapshot of the btc anchoring information schema. pub fn schema(&self) -> BtcAnchoringSchema { BtcAnchoringSchema::new(Arc::from(self.inner.snapshot())) diff --git a/tests/api.rs b/tests/api.rs index 5e3ae17..1e960cf 100644 --- a/tests/api.rs +++ b/tests/api.rs @@ -20,8 +20,6 @@ use exonum_btc_anchoring::{ BTC_ANCHORING_SERVICE_NAME, }; -const NULL_QUERY: () = (); - fn find_transaction( anchoring_testkit: &AnchoringTestKit, height: Option, @@ -52,7 +50,7 @@ fn actual_address() { let anchoring_api = anchoring_testkit.api(); assert_eq!( - anchoring_api.actual_address(NULL_QUERY).unwrap(), + anchoring_api.actual_address(()).unwrap(), anchoring_testkit.anchoring_address() ); } @@ -60,20 +58,14 @@ fn actual_address() { #[test] fn following_address() { let validators_num = 5; - let mut anchoring_testkit = AnchoringTestKit::new_without_rpc(validators_num, 150000, 4); + let mut anchoring_testkit = AnchoringTestKit::new_without_rpc(validators_num, 150_000, 4); let signatures = anchoring_testkit .create_signature_tx_for_validators(3) .unwrap(); anchoring_testkit.create_block_with_transactions(signatures); anchoring_testkit.create_blocks_until(Height(4)); // Following address should be none for regular anchoring. - assert_eq!( - anchoring_testkit - .api() - .following_address(NULL_QUERY) - .unwrap(), - None - ); + assert_eq!(anchoring_testkit.api().following_address(()).unwrap(), None); let signatures = anchoring_testkit .create_signature_tx_for_validators(4) @@ -91,10 +83,7 @@ fn following_address() { anchoring_testkit.create_block(); assert_eq!( - anchoring_testkit - .api() - .following_address(NULL_QUERY) - .unwrap(), + anchoring_testkit.api().following_address(()).unwrap(), Some(following_address) ); } @@ -150,7 +139,7 @@ fn find_transaction_configuration_change() { let validators_num = 5; let anchoring_frequency = 10; let mut anchoring_testkit = - AnchoringTestKit::new_without_rpc(validators_num, 150000, anchoring_frequency); + AnchoringTestKit::new_without_rpc(validators_num, 150_000, anchoring_frequency); let signatures = anchoring_testkit .create_signature_tx_for_validators(4) .unwrap(); diff --git a/tests/sync.rs b/tests/sync.rs index 4b745eb..ff00e9f 100644 --- a/tests/sync.rs +++ b/tests/sync.rs @@ -13,14 +13,15 @@ use hex::FromHex; -use exonum::crypto::Hash; -use exonum::helpers::Height; +use exonum::{crypto::Hash, helpers::Height}; use exonum_bitcoinrpc as bitcoin_rpc; use exonum_btc_anchoring::{ btc::Transaction, rpc::TransactionInfo as BtcTransactionInfo, - test_helpers::rpc::{FakeRelayRequest, FakeRelayResponse, TestRequest}, - test_helpers::testkit::AnchoringTestKit, + test_helpers::{ + rpc::{FakeRelayRequest, FakeRelayResponse, TestRequest}, + testkit::AnchoringTestKit, + }, }; fn funding_tx_request() -> TestRequest { @@ -69,7 +70,7 @@ fn normal_operation() { funding_tx_request(), ( FakeRelayRequest::TransactionInfo { - id: anchoring_tx_id.clone(), + id: anchoring_tx_id, }, FakeRelayResponse::TransactionInfo(Err( bitcoin_rpc::Error::Memory(String::new()).into() @@ -90,7 +91,7 @@ fn normal_operation() { funding_tx_request(), ( FakeRelayRequest::TransactionInfo { - id: anchoring_tx_id.clone(), + id: anchoring_tx_id, }, FakeRelayResponse::TransactionInfo(Ok(None)), ), @@ -98,7 +99,7 @@ fn normal_operation() { FakeRelayRequest::SendTransaction { transaction: last_tx.clone(), }, - FakeRelayResponse::SendTransaction(Ok(anchoring_tx_id.clone())), + FakeRelayResponse::SendTransaction(Ok(anchoring_tx_id)), ), ]); @@ -109,7 +110,7 @@ fn normal_operation() { funding_tx_request(), ( FakeRelayRequest::TransactionInfo { - id: anchoring_tx_id.clone(), + id: anchoring_tx_id, }, FakeRelayResponse::TransactionInfo(Ok(Some(BtcTransactionInfo { content: last_tx.clone(), @@ -119,7 +120,7 @@ fn normal_operation() { funding_tx_request(), ( FakeRelayRequest::TransactionInfo { - id: anchoring_tx_id.clone(), + id: anchoring_tx_id, }, FakeRelayResponse::TransactionInfo(Ok(Some(BtcTransactionInfo { content: last_tx.clone(), @@ -152,9 +153,7 @@ fn several_unsynced() { requests.expect(vec![ funding_tx_request(), ( - FakeRelayRequest::TransactionInfo { - id: tx_id_0.clone(), - }, + FakeRelayRequest::TransactionInfo { id: tx_id_0 }, FakeRelayResponse::TransactionInfo(Err( bitcoin_rpc::Error::Memory(String::new()).into() )), @@ -173,16 +172,14 @@ fn several_unsynced() { requests.expect(vec![ funding_tx_request(), ( - FakeRelayRequest::TransactionInfo { - id: tx_id_0.clone(), - }, + FakeRelayRequest::TransactionInfo { id: tx_id_0 }, FakeRelayResponse::TransactionInfo(Ok(None)), ), ( FakeRelayRequest::SendTransaction { transaction: last_tx.clone(), }, - FakeRelayResponse::SendTransaction(Ok(tx_id_0.clone())), + FakeRelayResponse::SendTransaction(Ok(tx_id_0)), ), ]); @@ -202,16 +199,12 @@ fn several_unsynced() { requests.expect(vec![ ( - FakeRelayRequest::TransactionInfo { - id: tx_id_0.clone(), - }, + FakeRelayRequest::TransactionInfo { id: tx_id_0 }, FakeRelayResponse::TransactionInfo(Ok(None)), ), funding_tx_request(), ( - FakeRelayRequest::TransactionInfo { - id: tx_id_0.clone(), - }, + FakeRelayRequest::TransactionInfo { id: tx_id_0 }, FakeRelayResponse::TransactionInfo(Ok(None)), ), ( @@ -223,16 +216,12 @@ fn several_unsynced() { )), ), ( - FakeRelayRequest::TransactionInfo { - id: tx_id_0.clone(), - }, + FakeRelayRequest::TransactionInfo { id: tx_id_0 }, FakeRelayResponse::TransactionInfo(Ok(None)), ), funding_tx_request(), ( - FakeRelayRequest::TransactionInfo { - id: tx_id_0.clone(), - }, + FakeRelayRequest::TransactionInfo { id: tx_id_0 }, FakeRelayResponse::TransactionInfo(Ok(None)), ), ( @@ -255,22 +244,16 @@ fn several_unsynced() { // Should walk to first uncommitted requests.expect(vec![ ( - FakeRelayRequest::TransactionInfo { - id: tx_id_1.clone(), - }, + FakeRelayRequest::TransactionInfo { id: tx_id_1 }, FakeRelayResponse::TransactionInfo(Ok(None)), ), ( - FakeRelayRequest::TransactionInfo { - id: tx_id_0.clone(), - }, + FakeRelayRequest::TransactionInfo { id: tx_id_0 }, FakeRelayResponse::TransactionInfo(Ok(None)), ), funding_tx_request(), ( - FakeRelayRequest::TransactionInfo { - id: tx_id_0.clone(), - }, + FakeRelayRequest::TransactionInfo { id: tx_id_0 }, FakeRelayResponse::TransactionInfo(Ok(None)), ), ( diff --git a/tests/testnet_tests.rs b/tests/testnet_tests.rs index 0823350..a51f700 100644 --- a/tests/testnet_tests.rs +++ b/tests/testnet_tests.rs @@ -117,7 +117,7 @@ fn additional_funding() { #[test] fn address_changed() { let validators_num = 5; - let mut anchoring_testkit = AnchoringTestKit::new_without_rpc(validators_num, 150000, 4); + let mut anchoring_testkit = AnchoringTestKit::new_without_rpc(validators_num, 150_000, 4); let signatures = anchoring_testkit .create_signature_tx_for_validators(3) .unwrap(); @@ -183,7 +183,7 @@ fn address_changed() { #[test] fn address_changed_and_new_funding_tx() { let validators_num = 5; - let initial_sum = 150000; + let initial_sum = 150_000; let mut anchoring_testkit = AnchoringTestKit::new_without_rpc(validators_num, initial_sum, 4); let signatures = anchoring_testkit .create_signature_tx_for_validators(3) From d515b27f39581df05cba8fc72dd3ab76e58a5df7 Mon Sep 17 00:00:00 2001 From: Aleksei Sidorov Date: Tue, 13 Aug 2019 17:29:39 +0300 Subject: [PATCH 6/8] Update CI --- .travis.yml | 38 ++++++++++++++++++++++---------------- Cargo.toml | 5 ++--- exonum-dictionary.txt | 3 ++- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2a45c5b..5489f1f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,9 +3,11 @@ language: rust addons: apt: sources: - - sourceline: 'ppa:giskou/librocksdb' + - sourceline: 'ppa:exonum/rocksdb' - sourceline: 'ppa:maarten-fonville/protobuf' - sourceline: 'ppa:fsgmhoward/shadowsocks-libev' + - sourceline: 'deb [arch=amd64] https://packages.microsoft.com/ubuntu/16.04/prod xenial main' + key_url: https://packages.microsoft.com/keys/microsoft.asc packages: - binutils-dev - build-essential @@ -17,40 +19,37 @@ addons: - libelf-dev - libiberty-dev - libprotobuf-dev - - librocksdb + - librocksdb5.17 - libsnappy-dev - libsodium-dev - libssl-dev - pkg-config + - powershell - protobuf-compiler - unzip - zlib1g-dev rust: - # Feel free to bump this version if you need features of newer Rust. - # Sync with badge in README.md - 1.36.0 -matrix: - allow_failures: - - env: FEATURE=non-fatal-checks - fast_finish: true - cache: - cargo: true + npm: true directories: - - node_modules + - $HOME/.local + - $HOME/.cargo + - $HOME/.kcov + - $TRAVIS_BUILD_DIR/target + timeout: 900 -dist: trusty +dist: xenial +sudo: required env: global: - DEADLINKS_VERS=0.3.0 - - SODIUM_VERS=1.0.18 - RUSTFLAGS="-D warnings" - - ROCKSDB_LIB_DIR=/usr/lib/x86_64-linux-gnu + - ROCKSDB_LIB_DIR=/usr/lib - SNAPPY_LIB_DIR=/usr/lib/x86_64-linux-gnu - jobs: include: # Formatting & other lints that do not require compilation @@ -90,4 +89,11 @@ jobs: script: - cargo doc --no-deps - cargo deadlinks --dir target/doc - \ No newline at end of file + +before_cache: + - | + cd $TRAVIS_BUILD_DIR/target/debug + rm -rf incremental examples + find . -maxdepth 1 -type f -executable -delete + find deps -type f -size +20M -delete + du -h -d 1 . diff --git a/Cargo.toml b/Cargo.toml index 3ba671a..c65dc63 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,8 +20,7 @@ bitcoin = { version = "0.17", features = ["serde"] } bitcoin_hashes = { version = "0.3", features = ["serde"] } btc-transaction-utils = "0.5" byteorder = "1.3" -clap = "2.32" -derive_more = "0.14" +derive_more = "0.15" exonum = "0.11.0" exonum-merkledb = { git = "https://github.com/exonum/exonum.git", rev = "33e45eac5c9d710a100ce80b56d2d31a5b1f6dfc" } exonum_bitcoinrpc = "0.6" @@ -33,7 +32,7 @@ hex = "0.3" log = "0.4" maplit = "1.0" matches = "0.1" -protobuf = { version = "2.5", features = ["with-serde"] } +protobuf = { version = "2.8", features = ["with-serde"] } rand = "0.4" secp256k1 = { version = "0.12", features = ["serde"] } serde = "1.0" diff --git a/exonum-dictionary.txt b/exonum-dictionary.txt index 53cbe9f..bd33fc9 100644 --- a/exonum-dictionary.txt +++ b/exonum-dictionary.txt @@ -99,6 +99,7 @@ memorydb mempool Merkelized Merkle +merkledb millis mkdir mmoXxKhAwnhtFiAMvxJ82CKCBia751mzfY @@ -245,4 +246,4 @@ whitelisted writeln wtxid Xqsmt -Zsmmr \ No newline at end of file +Zsmmr From 26670260ee6bd2f88d14cebb23625a9822faadb6 Mon Sep 17 00:00:00 2001 From: Aleksei Sidorov Date: Tue, 13 Aug 2019 17:32:11 +0300 Subject: [PATCH 7/8] Add changelog entry --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac1b5d7..f80a928 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +- Updated to the [Exonum 0.12.0](https://github.com/exonum/exonum/releases/tag/v0.12) + release (#144). + ## 0.11.0 - 2018-03-15 ### Internal improvements From bf9b601a5db9d418730c537ad7a0b9ff37e05d82 Mon Sep 17 00:00:00 2001 From: Aleksei Sidorov Date: Wed, 14 Aug 2019 18:00:16 +0300 Subject: [PATCH 8/8] Release 0.12 --- CHANGELOG.md | 2 ++ Cargo.toml | 21 +++++++-------------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f80a928..d41abc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## 0.12.0 - 2018-08-14 + - Updated to the [Exonum 0.12.0](https://github.com/exonum/exonum/releases/tag/v0.12) release (#144). diff --git a/Cargo.toml b/Cargo.toml index c65dc63..76c86d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "exonum-btc-anchoring" edition = "2018" -version = "0.11.0" +version = "0.12.0" authors = ["The Exonum Team "] homepage = "https://exonum.com/doc/advanced/bitcoin-anchoring/" repository = "https://github.com/exonum/exonum-btc-anchoring" @@ -21,11 +21,11 @@ bitcoin_hashes = { version = "0.3", features = ["serde"] } btc-transaction-utils = "0.5" byteorder = "1.3" derive_more = "0.15" -exonum = "0.11.0" -exonum-merkledb = { git = "https://github.com/exonum/exonum.git", rev = "33e45eac5c9d710a100ce80b56d2d31a5b1f6dfc" } +exonum = "0.12.0" +exonum-merkledb = "0.12.0" exonum_bitcoinrpc = "0.6" -exonum-derive = "0.11.0" -exonum-testkit = "0.11.0" +exonum-derive = "0.12.0" +exonum-testkit = "0.12.0" failure = "0.1" failure_derive = "0.1" hex = "0.3" @@ -43,16 +43,9 @@ structopt = "0.2" toml = "0.5" [dev-dependencies] -exonum-configuration = "0.11.0" +exonum-configuration = "0.12.0" libc = "0.2" proptest = "0.9" [build-dependencies] -exonum-build = "0.11.0" - -[patch.crates-io] -exonum = { git = "https://github.com/exonum/exonum.git", rev = "33e45eac5c9d710a100ce80b56d2d31a5b1f6dfc" } -exonum-derive = { git = "https://github.com/exonum/exonum.git", rev = "33e45eac5c9d710a100ce80b56d2d31a5b1f6dfc" } -exonum-configuration = { git = "https://github.com/exonum/exonum.git", rev = "33e45eac5c9d710a100ce80b56d2d31a5b1f6dfc" } -exonum-build = { git = "https://github.com/exonum/exonum.git", rev = "33e45eac5c9d710a100ce80b56d2d31a5b1f6dfc" } -exonum-testkit = { git = "https://github.com/exonum/exonum.git", rev = "33e45eac5c9d710a100ce80b56d2d31a5b1f6dfc" } +exonum-build = "0.12.0"