From 71664457de4575b40a7bea98823a2dc8ab7316cc Mon Sep 17 00:00:00 2001 From: Ignotus Peverell Date: Fri, 7 Dec 2018 22:43:37 +0000 Subject: [PATCH 01/16] Clean servers crate dev dependency on config --- Cargo.lock | 3 --- servers/Cargo.toml | 1 - servers/tests/api.rs | 1 - servers/tests/dandelion.rs | 1 - servers/tests/wallet.rs | 1 - 5 files changed, 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5c13bdd0a..3af765eaa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -751,7 +751,6 @@ dependencies = [ "grin_keychain 0.4.2", "grin_store 0.4.2", "grin_util 0.4.2", - "grin_wallet 0.4.2", "lmdb-zero 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "lru-cache 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -854,7 +853,6 @@ dependencies = [ "grin_keychain 0.4.2", "grin_store 0.4.2", "grin_util 0.4.2", - "grin_wallet 0.4.2", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)", @@ -885,7 +883,6 @@ dependencies = [ "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", "grin_api 0.4.2", "grin_chain 0.4.2", - "grin_config 0.4.2", "grin_core 0.4.2", "grin_keychain 0.4.2", "grin_p2p 0.4.2", diff --git a/servers/Cargo.toml b/servers/Cargo.toml index 74fc79c17..24ab2ff3c 100644 --- a/servers/Cargo.toml +++ b/servers/Cargo.toml @@ -36,4 +36,3 @@ grin_wallet = { path = "../wallet", version = "0.4.2" } [dev-dependencies] blake2-rfc = "0.2" -grin_config = { path = "../config", version = "0.4.2" } diff --git a/servers/tests/api.rs b/servers/tests/api.rs index 538f50c8f..7482fbbc8 100644 --- a/servers/tests/api.rs +++ b/servers/tests/api.rs @@ -17,7 +17,6 @@ extern crate log; extern crate grin_api as api; extern crate grin_chain as chain; -extern crate grin_config as config; extern crate grin_core as core; extern crate grin_p2p as p2p; extern crate grin_servers as servers; diff --git a/servers/tests/dandelion.rs b/servers/tests/dandelion.rs index d7f6a52ee..99d7d38ea 100644 --- a/servers/tests/dandelion.rs +++ b/servers/tests/dandelion.rs @@ -17,7 +17,6 @@ extern crate log; extern crate grin_api as api; extern crate grin_chain as chain; -extern crate grin_config as config; extern crate grin_core as core; extern crate grin_p2p as p2p; extern crate grin_servers as servers; diff --git a/servers/tests/wallet.rs b/servers/tests/wallet.rs index 62d531f73..08c609aa6 100644 --- a/servers/tests/wallet.rs +++ b/servers/tests/wallet.rs @@ -17,7 +17,6 @@ extern crate log; extern crate grin_api as api; extern crate grin_chain as chain; -extern crate grin_config as config; extern crate grin_core as core; extern crate grin_p2p as p2p; extern crate grin_servers as servers; From aedac483f5a116b91a8baf6acffd70e5f980b8cc Mon Sep 17 00:00:00 2001 From: hashmap Date: Sat, 8 Dec 2018 00:59:40 +0100 Subject: [PATCH 02/16] Convert to Rust 2018 edition (#2084) * Convert to Rust 2018 edition * Update gitignore --- .gitignore | 1 + Cargo.lock | 2 - Cargo.toml | 1 + api/Cargo.toml | 1 + api/src/auth.rs | 17 ++- api/src/client.rs | 23 +-- api/src/{handlers/mod.rs => handlers.rs} | 16 +- api/src/handlers/blocks_api.rs | 16 +- api/src/handlers/chain_api.rs | 16 +- api/src/handlers/peers_api.rs | 8 +- api/src/handlers/pool_api.rs | 22 +-- api/src/handlers/server_api.rs | 12 +- api/src/handlers/transactions_api.rs | 14 +- api/src/handlers/utils.rs | 12 +- api/src/lib.rs | 49 +++--- api/src/rest.rs | 17 ++- api/src/router.rs | 11 +- api/src/types.rs | 21 +-- api/src/web.rs | 6 +- api/tests/rest.rs | 9 +- chain/Cargo.toml | 1 + chain/src/chain.rs | 55 ++++--- chain/src/error.rs | 17 +-- chain/src/lib.rs | 29 ++-- chain/src/pipe.rs | 91 ++++++----- chain/src/store.rs | 25 ++- chain/src/{txhashset/mod.rs => txhashset.rs} | 0 chain/src/txhashset/rewindable_kernel_view.rs | 16 +- chain/src/txhashset/txhashset.rs | 83 +++++----- chain/src/txhashset/utxo_view.rs | 12 +- chain/src/types.rs | 10 +- chain/tests/data_file_integrity.rs | 44 +++--- chain/tests/mine_simple_chain.rs | 91 ++++++----- chain/tests/store_indices.rs | 27 ++-- chain/tests/test_coinbase_maturity.rs | 62 ++++---- chain/tests/test_txhashset.rs | 30 ++-- config/Cargo.toml | 1 + config/src/comments.rs | 144 ++++++++++++------ config/src/config.rs | 10 +- config/src/lib.rs | 17 +-- config/src/types.rs | 8 +- core/Cargo.toml | 1 + core/fuzz/src/main.rs | 3 +- core/src/consensus.rs | 4 +- core/src/{core/mod.rs => core.rs} | 4 +- core/src/core/block.rs | 35 ++--- core/src/core/block_sums.rs | 12 +- core/src/core/committed.rs | 10 +- core/src/core/compact_block.rs | 14 +- core/src/core/hash.rs | 12 +- core/src/core/id.rs | 12 +- core/src/core/merkle_proof.rs | 16 +- core/src/core/{pmmr/mod.rs => pmmr.rs} | 0 core/src/core/pmmr/backend.rs | 6 +- core/src/core/pmmr/pmmr.rs | 21 +-- core/src/core/pmmr/readonly_pmmr.rs | 17 ++- core/src/core/pmmr/rewindable_pmmr.rs | 15 +- core/src/core/transaction.rs | 64 ++++---- core/src/core/verifier_cache.rs | 10 +- core/src/genesis.rs | 8 +- core/src/global.rs | 10 +- core/src/lib.rs | 18 +-- core/src/libtx/aggsig.rs | 10 +- core/src/libtx/build.rs | 34 +++-- core/src/libtx/error.rs | 10 +- core/src/libtx/mod.rs | 6 +- core/src/libtx/proof.rs | 12 +- core/src/libtx/reward.rs | 16 +- core/src/libtx/slate.rs | 27 ++-- core/src/macros.rs | 4 +- core/src/{pow/mod.rs => pow.rs} | 30 ++-- core/src/pow/common.rs | 12 +- core/src/pow/cuckaroo.rs | 13 +- core/src/pow/cuckatoo.rs | 10 +- core/src/pow/error.rs | 4 +- core/src/pow/lean.rs | 10 +- core/src/pow/types.rs | 24 +-- core/src/ser.rs | 75 +++++---- core/tests/block.rs | 82 +++++----- core/tests/{common/mod.rs => common.rs} | 37 +++-- core/tests/consensus.rs | 21 +-- core/tests/core.rs | 60 ++++---- core/tests/merkle_proof.rs | 15 +- core/tests/pmmr.rs | 29 ++-- core/tests/transaction.rs | 13 +- .../{vec_backend/mod.rs => vec_backend.rs} | 23 ++- core/tests/verifier_cache.rs | 23 ++- keychain/Cargo.toml | 1 + keychain/src/base58.rs | 10 +- keychain/src/extkey_bip32.rs | 29 ++-- keychain/src/keychain.rs | 35 +++-- keychain/src/lib.rs | 22 +-- keychain/src/mnemonic.rs | 8 +- keychain/src/types.rs | 38 ++--- p2p/Cargo.toml | 1 + p2p/src/conn.rs | 28 ++-- p2p/src/handshake.rs | 14 +- p2p/src/lib.rs | 25 ++- p2p/src/msg.rs | 52 ++++--- p2p/src/peer.rs | 30 ++-- p2p/src/peers.rs | 20 +-- p2p/src/protocol.rs | 16 +- p2p/src/serv.rs | 20 +-- p2p/src/store.rs | 10 +- p2p/src/types.rs | 12 +- p2p/tests/peer_handshake.rs | 22 +-- p2p/tests/ser_deser.rs | 5 +- pool/Cargo.toml | 1 + pool/src/lib.rs | 15 +- pool/src/pool.rs | 32 ++-- pool/src/transaction_pool.rs | 34 +++-- pool/src/types.rs | 15 +- pool/tests/block_building.rs | 33 ++-- pool/tests/block_reconciliation.rs | 33 ++-- pool/tests/coinbase_maturity.rs | 29 ++-- pool/tests/{common/mod.rs => common.rs} | 51 +++---- pool/tests/transaction_pool.rs | 96 +++++------- servers/Cargo.toml | 1 + servers/src/{common/mod.rs => common.rs} | 0 servers/src/common/adapters.rs | 28 ++-- servers/src/common/stats.rs | 10 +- servers/src/common/types.rs | 18 +-- servers/src/{grin/mod.rs => grin.rs} | 0 servers/src/grin/dandelion_monitor.rs | 16 +- servers/src/grin/seed.rs | 19 +-- servers/src/grin/server.rs | 35 ++--- servers/src/grin/{sync/mod.rs => sync.rs} | 0 servers/src/grin/sync/body_sync.rs | 11 +- servers/src/grin/sync/header_sync.rs | 10 +- servers/src/grin/sync/state_sync.rs | 10 +- servers/src/grin/sync/syncer.rs | 14 +- servers/src/lib.rs | 40 ++--- servers/src/{mining/mod.rs => mining.rs} | 0 servers/src/mining/mine_block.rs | 25 +-- servers/src/mining/stratumserver.rs | 28 ++-- servers/src/mining/test_miner.rs | 25 +-- .../src/{webwallet/mod.rs => webwallet.rs} | 0 servers/tests/api.rs | 28 ++-- servers/tests/dandelion.rs | 15 +- .../tests/{framework/mod.rs => framework.rs} | 37 ++--- servers/tests/simulnet.rs | 47 +++--- servers/tests/stratum.rs | 20 +-- servers/tests/wallet.rs | 15 +- src/bin/cmd/client.rs | 15 +- src/bin/cmd/config.rs | 2 +- src/bin/cmd/server.rs | 24 +-- src/bin/cmd/wallet.rs | 11 +- src/bin/cmd/wallet_args.rs | 19 +-- src/bin/grin.rs | 51 +++---- src/bin/tui/menu.rs | 10 +- src/bin/tui/mining.rs | 78 ++++++---- src/bin/tui/mod.rs | 6 +- src/bin/tui/peers.rs | 36 +++-- src/bin/tui/status.rs | 49 +++--- src/bin/tui/table.rs | 20 +-- src/bin/tui/types.rs | 4 +- src/bin/tui/ui.rs | 16 +- src/bin/tui/version.rs | 10 +- src/build/build.rs | 7 +- store/Cargo.toml | 1 + store/src/leaf_set.rs | 10 +- store/src/lib.rs | 19 +-- store/src/lmdb.rs | 11 +- store/src/pmmr.rs | 18 +-- store/src/prune_list.rs | 4 +- store/src/types.rs | 3 +- store/tests/pmmr.rs | 78 +++++----- store/tests/prune_list.rs | 4 +- store/tests/test_bitmap.rs | 4 +- store/tests/utxo_set_perf.rs | 10 +- util/Cargo.toml | 1 + util/src/lib.rs | 22 +-- util/src/logger.rs | 8 +- util/src/read_write.rs | 4 +- util/src/secp_static.rs | 4 +- util/src/zip.rs | 6 +- util/tests/file.rs | 4 +- util/tests/zip.rs | 4 +- wallet/Cargo.toml | 1 + wallet/src/{adapters/mod.rs => adapters.rs} | 7 +- wallet/src/adapters/file.rs | 9 +- wallet/src/adapters/http.rs | 13 +- wallet/src/adapters/keybase.rs | 51 ++++--- wallet/src/adapters/null.rs | 6 +- wallet/src/command.rs | 14 +- wallet/src/controller.rs | 91 ++++++----- wallet/src/display.rs | 12 +- wallet/src/error.rs | 31 ++-- wallet/src/lib.rs | 54 +++---- wallet/src/libtx.rs | 51 +++++++ wallet/src/{libwallet/mod.rs => libwallet.rs} | 2 +- wallet/src/libwallet/api.rs | 24 +-- wallet/src/libwallet/error.rs | 44 +++--- .../{internal/mod.rs => internal.rs} | 0 wallet/src/libwallet/internal/keys.rs | 6 +- wallet/src/libwallet/internal/restore.rs | 14 +- wallet/src/libwallet/internal/selection.rs | 16 +- wallet/src/libwallet/internal/tx.rs | 18 +-- wallet/src/libwallet/internal/updater.rs | 31 ++-- wallet/src/libwallet/types.rs | 63 ++++---- wallet/src/lmdb_wallet.rs | 34 +++-- .../{node_clients/mod.rs => node_clients.rs} | 0 wallet/src/node_clients/http.rs | 12 +- .../mod.rs => test_framework.rs} | 40 ++--- wallet/src/test_framework/testclient.rs | 50 +++--- wallet/src/types.rs | 10 +- wallet/tests/accounts.rs | 26 ++-- wallet/tests/file.rs | 27 ++-- wallet/tests/libwallet.rs | 84 +++++----- wallet/tests/repost.rs | 28 ++-- wallet/tests/restore.rs | 31 ++-- wallet/tests/self_send.rs | 26 ++-- wallet/tests/transaction.rs | 31 ++-- 213 files changed, 2317 insertions(+), 2341 deletions(-) rename api/src/{handlers/mod.rs => handlers.rs} (96%) rename chain/src/{txhashset/mod.rs => txhashset.rs} (100%) rename core/src/{core/mod.rs => core.rs} (98%) rename core/src/core/{pmmr/mod.rs => pmmr.rs} (100%) rename core/src/{pow/mod.rs => pow.rs} (91%) rename core/tests/{common/mod.rs => common.rs} (87%) rename core/tests/{vec_backend/mod.rs => vec_backend.rs} (87%) rename pool/tests/{common/mod.rs => common.rs} (88%) rename servers/src/{common/mod.rs => common.rs} (100%) rename servers/src/{grin/mod.rs => grin.rs} (100%) rename servers/src/grin/{sync/mod.rs => sync.rs} (100%) rename servers/src/{mining/mod.rs => mining.rs} (100%) rename servers/src/{webwallet/mod.rs => webwallet.rs} (100%) rename servers/tests/{framework/mod.rs => framework.rs} (96%) rename wallet/src/{adapters/mod.rs => adapters.rs} (95%) create mode 100644 wallet/src/libtx.rs rename wallet/src/{libwallet/mod.rs => libwallet.rs} (94%) rename wallet/src/libwallet/{internal/mod.rs => internal.rs} (100%) rename wallet/src/{node_clients/mod.rs => node_clients.rs} (100%) rename wallet/src/{test_framework/mod.rs => test_framework.rs} (87%) diff --git a/.gitignore b/.gitignore index 01259d892..44cdbf52d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ .grin* node* !node_clients +!node_clients.rs target Cargo.lock *.iml diff --git a/Cargo.lock b/Cargo.lock index 5c13bdd0a..c85e4cffc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -751,7 +751,6 @@ dependencies = [ "grin_keychain 0.4.2", "grin_store 0.4.2", "grin_util 0.4.2", - "grin_wallet 0.4.2", "lmdb-zero 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "lru-cache 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -854,7 +853,6 @@ dependencies = [ "grin_keychain 0.4.2", "grin_store 0.4.2", "grin_util 0.4.2", - "grin_wallet 0.4.2", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.80 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index ad5625825..0b4fe7d9a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ keywords = [ "crypto", "grin", "mimblewimble" ] readme = "README.md" exclude = ["**/*.grin", "**/*.grin2"] build = "src/build/build.rs" +edition = "2018" [workspace] members = ["api", "chain", "config", "core", "keychain", "p2p", "servers", "store", "util", "pool", "wallet"] diff --git a/api/Cargo.toml b/api/Cargo.toml index 16a12ae99..61a691024 100644 --- a/api/Cargo.toml +++ b/api/Cargo.toml @@ -7,6 +7,7 @@ license = "Apache-2.0" repository = "https://github.com/mimblewimble/grin" keywords = [ "crypto", "grin", "mimblewimble" ] workspace = ".." +edition = "2018" [dependencies] failure = "0.1.1" diff --git a/api/src/auth.rs b/api/src/auth.rs index 84a48248e..5b2b2f8da 100644 --- a/api/src/auth.rs +++ b/api/src/auth.rs @@ -12,11 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. +use crate::router::{Handler, HandlerObj, ResponseFuture}; use futures::future::ok; use hyper::header::{HeaderValue, AUTHORIZATION, WWW_AUTHENTICATE}; use hyper::{Body, Request, Response, StatusCode}; use ring::constant_time::verify_slices_are_equal; -use router::{Handler, HandlerObj, ResponseFuture}; // Basic Authentication Middleware pub struct BasicAuthMiddleware { @@ -37,12 +37,14 @@ impl Handler for BasicAuthMiddleware { fn call( &self, req: Request, - mut handlers: Box>, + mut handlers: Box>, ) -> ResponseFuture { - if req.headers().contains_key(AUTHORIZATION) && verify_slices_are_equal( - req.headers()[AUTHORIZATION].as_bytes(), - &self.api_basic_auth.as_bytes(), - ).is_ok() + if req.headers().contains_key(AUTHORIZATION) + && verify_slices_are_equal( + req.headers()[AUTHORIZATION].as_bytes(), + &self.api_basic_auth.as_bytes(), + ) + .is_ok() { handlers.next().unwrap().call(req, handlers) } else { @@ -58,7 +60,8 @@ fn unauthorized_response(basic_realm: &str) -> ResponseFuture { .header( WWW_AUTHENTICATE, HeaderValue::from_str(basic_realm).unwrap(), - ).body(Body::empty()) + ) + .body(Body::empty()) .unwrap(); Box::new(ok(response)) } diff --git a/api/src/client.rs b/api/src/client.rs index b6bac3b33..bc176924c 100644 --- a/api/src/client.rs +++ b/api/src/client.rs @@ -14,22 +14,20 @@ //! High level JSON/HTTP client API +use crate::rest::{Error, ErrorKind}; +use crate::util::to_base64; use failure::{Fail, ResultExt}; +use futures::future::{err, ok, Either}; use http::uri::{InvalidUri, Uri}; use hyper::header::{ACCEPT, AUTHORIZATION, USER_AGENT}; use hyper::rt::{Future, Stream}; use hyper::{Body, Client, Request}; +use hyper_rustls; use serde::{Deserialize, Serialize}; use serde_json; - -use futures::future::{err, ok, Either}; -use hyper_rustls; use tokio::runtime::Runtime; -use rest::{Error, ErrorKind}; -use util::to_base64; - -pub type ClientResponseFuture = Box + Send>; +pub type ClientResponseFuture = Box + Send>; /// Helper function to easily issue a HTTP GET request against a given URL that /// returns a JSON object. Handles request building, JSON deserialization and @@ -143,7 +141,8 @@ fn build_request<'a>( .body(match body { None => Body::empty(), Some(json) => json.into(), - }).map_err(|e| { + }) + .map_err(|e| { ErrorKind::RequestError(format!("Bad request {} {}: {}", method, url, e)).into() }) } @@ -185,7 +184,7 @@ where })) } -fn send_request_async(req: Request) -> Box + Send> { +fn send_request_async(req: Request) -> Box + Send> { let https = hyper_rustls::HttpsConnector::new(1); let client = Client::builder().build::<_, Body>(https); Box::new( @@ -196,14 +195,16 @@ fn send_request_async(req: Request) -> Box diff --git a/api/src/handlers/chain_api.rs b/api/src/handlers/chain_api.rs index 17560cc47..150de6821 100644 --- a/api/src/handlers/chain_api.rs +++ b/api/src/handlers/chain_api.rs @@ -13,18 +13,18 @@ // limitations under the License. use super::utils::{get_output, w}; -use chain; -use core::core::hash::Hashed; +use crate::chain; +use crate::core::core::hash::Hashed; +use crate::rest::*; +use crate::router::{Handler, ResponseFuture}; +use crate::types::*; +use crate::util; +use crate::util::secp::pedersen::Commitment; +use crate::web::*; use hyper::{Body, Request, StatusCode}; -use rest::*; -use router::{Handler, ResponseFuture}; use std::collections::HashMap; use std::sync::Weak; -use types::*; use url::form_urlencoded; -use util; -use util::secp::pedersen::Commitment; -use web::*; /// Chain handler. Get the head details. /// GET /v1/chain diff --git a/api/src/handlers/peers_api.rs b/api/src/handlers/peers_api.rs index 5ca27fb8c..c700b54b1 100644 --- a/api/src/handlers/peers_api.rs +++ b/api/src/handlers/peers_api.rs @@ -13,12 +13,12 @@ // limitations under the License. use super::utils::w; +use crate::p2p; +use crate::p2p::types::{PeerInfoDisplay, ReasonForBan}; +use crate::router::{Handler, ResponseFuture}; +use crate::web::*; use hyper::{Body, Request, StatusCode}; -use p2p; -use p2p::types::{PeerInfoDisplay, ReasonForBan}; -use router::{Handler, ResponseFuture}; use std::sync::Weak; -use web::*; pub struct PeersAllHandler { pub peers: Weak, diff --git a/api/src/handlers/pool_api.rs b/api/src/handlers/pool_api.rs index 2628323ee..f96d3685e 100644 --- a/api/src/handlers/pool_api.rs +++ b/api/src/handlers/pool_api.rs @@ -13,22 +13,22 @@ // limitations under the License. use super::utils::w; -use core::core::hash::Hashed; -use core::core::Transaction; -use core::ser; +use crate::core::core::hash::Hashed; +use crate::core::core::Transaction; +use crate::core::ser; +use crate::pool; +use crate::rest::*; +use crate::router::{Handler, ResponseFuture}; +use crate::types::*; +use crate::util; +use crate::util::RwLock; +use crate::web::*; use futures::future::ok; use futures::Future; use hyper::{Body, Request, StatusCode}; -use pool; -use rest::*; -use router::{Handler, ResponseFuture}; use std::collections::HashMap; use std::sync::Weak; -use types::*; use url::form_urlencoded; -use util; -use util::RwLock; -use web::*; /// Get basic information about the transaction pool. /// GET /v1/pool @@ -60,7 +60,7 @@ pub struct PoolPushHandler { } impl PoolPushHandler { - fn update_pool(&self, req: Request) -> Box + Send> { + fn update_pool(&self, req: Request) -> Box + Send> { let params = match req.uri().query() { Some(query_string) => form_urlencoded::parse(query_string.as_bytes()) .into_owned() diff --git a/api/src/handlers/server_api.rs b/api/src/handlers/server_api.rs index e7fc5cd6f..42a829a65 100644 --- a/api/src/handlers/server_api.rs +++ b/api/src/handlers/server_api.rs @@ -13,14 +13,14 @@ // limitations under the License. use super::utils::w; -use chain; +use crate::chain; +use crate::p2p; +use crate::rest::*; +use crate::router::{Handler, ResponseFuture}; +use crate::types::*; +use crate::web::*; use hyper::{Body, Request}; -use p2p; -use rest::*; -use router::{Handler, ResponseFuture}; use std::sync::Weak; -use types::*; -use web::*; // RESTful index of available api endpoints // GET /v1/ diff --git a/api/src/handlers/transactions_api.rs b/api/src/handlers/transactions_api.rs index bbd01a483..6dc879855 100644 --- a/api/src/handlers/transactions_api.rs +++ b/api/src/handlers/transactions_api.rs @@ -13,18 +13,18 @@ // limitations under the License. use super::utils::w; -use chain; +use crate::chain; +use crate::rest::*; +use crate::router::{Handler, ResponseFuture}; +use crate::types::*; +use crate::util; +use crate::util::secp::pedersen::Commitment; +use crate::web::*; use failure::ResultExt; use hyper::{Body, Request, StatusCode}; -use rest::*; -use router::{Handler, ResponseFuture}; use std::collections::HashMap; use std::sync::Weak; -use types::*; use url::form_urlencoded; -use util; -use util::secp::pedersen::Commitment; -use web::*; // Sum tree handler. Retrieve the roots: // GET /v1/txhashset/roots diff --git a/api/src/handlers/utils.rs b/api/src/handlers/utils.rs index 41e864bbf..2e2bc01e3 100644 --- a/api/src/handlers/utils.rs +++ b/api/src/handlers/utils.rs @@ -12,14 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -use chain; -use core::core::{OutputFeatures, OutputIdentifier}; +use crate::chain; +use crate::core::core::{OutputFeatures, OutputIdentifier}; +use crate::rest::*; +use crate::types::*; +use crate::util; +use crate::util::secp::pedersen::Commitment; use failure::ResultExt; -use rest::*; use std::sync::{Arc, Weak}; -use types::*; -use util; -use util::secp::pedersen::Commitment; // All handlers use `Weak` references instead of `Arc` to avoid cycles that // can never be destroyed. These 2 functions are simple helpers to reduce the diff --git a/api/src/lib.rs b/api/src/lib.rs index cde0399ef..72816796b 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -12,36 +12,31 @@ // See the License for the specific language governing permissions and // limitations under the License. -extern crate grin_chain as chain; -extern crate grin_core as core; -extern crate grin_p2p as p2p; -extern crate grin_pool as pool; -extern crate grin_store as store; -extern crate grin_util as util; -extern crate url; +use grin_chain as chain; +use grin_core as core; +use grin_p2p as p2p; +use grin_pool as pool; -extern crate failure; +use grin_util as util; + +use failure; #[macro_use] extern crate failure_derive; -extern crate hyper; +use hyper; #[macro_use] extern crate lazy_static; -extern crate regex; -extern crate ring; -extern crate serde; + +use serde; #[macro_use] extern crate serde_derive; -extern crate serde_json; +use serde_json; #[macro_use] extern crate log; -extern crate futures; -extern crate http; -extern crate hyper_rustls; -extern crate rustls; -extern crate tokio; -extern crate tokio_core; -extern crate tokio_rustls; -extern crate tokio_tcp; + +use hyper_rustls; +use rustls; + +use tokio_tcp; pub mod auth; pub mod client; @@ -51,9 +46,9 @@ mod router; mod types; mod web; -pub use auth::BasicAuthMiddleware; -pub use handlers::start_rest_apis; -pub use rest::*; -pub use router::*; -pub use types::*; -pub use web::*; +pub use crate::auth::BasicAuthMiddleware; +pub use crate::handlers::start_rest_apis; +pub use crate::rest::*; +pub use crate::router::*; +pub use crate::types::*; +pub use crate::web::*; diff --git a/api/src/rest.rs b/api/src/rest.rs index 0badb975d..fa26ef64a 100644 --- a/api/src/rest.rs +++ b/api/src/rest.rs @@ -18,12 +18,12 @@ //! To use it, just have your service(s) implement the ApiEndpoint trait and //! register them on a ApiServer. +use crate::router::{Handler, HandlerObj, ResponseFuture, Router}; use failure::{Backtrace, Context, Fail, ResultExt}; use futures::sync::oneshot; use futures::Stream; use hyper::rt::Future; use hyper::{rt, Body, Request, Server}; -use router::{Handler, HandlerObj, ResponseFuture, Router}; use rustls; use rustls::internal::pemfile; use std::fmt::{self, Display}; @@ -55,7 +55,7 @@ pub enum ErrorKind { } impl Fail for Error { - fn cause(&self) -> Option<&Fail> { + fn cause(&self) -> Option<&dyn Fail> { self.inner.cause() } @@ -65,7 +65,7 @@ impl Fail for Error { } impl Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { Display::fmt(&self.inner, f) } } @@ -196,7 +196,8 @@ impl ApiServer { .map_err(|e| eprintln!("HTTP API server error: {}", e)); rt::run(server); - }).map_err(|_| ErrorKind::Internal("failed to spawn API thread".to_string()).into()) + }) + .map_err(|_| ErrorKind::Internal("failed to spawn API thread".to_string()).into()) } /// Starts the TLS ApiServer at the provided address. @@ -228,13 +229,15 @@ impl ApiServer { error!("accept_async failed: {}", e); Ok(None) } - }).filter_map(|x| x); + }) + .filter_map(|x| x); let server = Server::builder(tls) .serve(router) .map_err(|e| eprintln!("HTTP API server error: {}", e)); rt::run(server); - }).map_err(|_| ErrorKind::Internal("failed to spawn API thread".to_string()).into()) + }) + .map_err(|_| ErrorKind::Internal("failed to spawn API thread".to_string()).into()) } /// Stops the API server, it panics in case of error @@ -258,7 +261,7 @@ impl Handler for LoggingMiddleware { fn call( &self, req: Request, - mut handlers: Box>, + mut handlers: Box>, ) -> ResponseFuture { debug!("REST call: {} {}", req.method(), req.uri().path()); handlers.next().unwrap().call(req, handlers) diff --git a/api/src/router.rs b/api/src/router.rs index 5f24b033b..ad94a726b 100644 --- a/api/src/router.rs +++ b/api/src/router.rs @@ -26,7 +26,7 @@ lazy_static! { static ref WILDCARD_STOP_HASH: u64 = calculate_hash(&"**"); } -pub type ResponseFuture = Box, Error = hyper::Error> + Send>; +pub type ResponseFuture = Box, Error = hyper::Error> + Send>; pub trait Handler { fn get(&self, _req: Request) -> ResponseFuture { @@ -68,7 +68,7 @@ pub trait Handler { fn call( &self, req: Request, - mut _handlers: Box>, + mut _handlers: Box>, ) -> ResponseFuture { match req.method() { &Method::GET => self.get(req), @@ -105,7 +105,7 @@ struct NodeId(usize); const MAX_CHILDREN: usize = 16; -pub type HandlerObj = Arc; +pub type HandlerObj = Arc; #[derive(Clone)] pub struct Node { @@ -147,7 +147,8 @@ impl Router { .find(|&id| { let node_key = self.node(*id).key; node_key == key || node_key == *WILDCARD_HASH || node_key == *WILDCARD_STOP_HASH - }).cloned() + }) + .cloned() } fn add_empty_node(&mut self, parent: NodeId, key: u64) -> NodeId { @@ -225,7 +226,7 @@ impl NewService for Router { type Error = hyper::Error; type InitError = hyper::Error; type Service = Router; - type Future = Box + Send>; + type Future = Box + Send>; fn new_service(&self) -> Self::Future { Box::new(future::ok(self.clone())) } diff --git a/api/src/types.rs b/api/src/types.rs index 379cfc923..2024d07fb 100644 --- a/api/src/types.rs +++ b/api/src/types.rs @@ -14,17 +14,17 @@ use std::sync::Arc; -use chain; -use core::core::hash::Hashed; -use core::core::merkle_proof::MerkleProof; -use core::{core, ser}; -use p2p; +use crate::chain; +use crate::core::core::hash::Hashed; +use crate::core::core::merkle_proof::MerkleProof; +use crate::core::{core, ser}; +use crate::p2p; +use crate::util; +use crate::util::secp::pedersen; use serde; use serde::de::MapAccess; use serde::ser::SerializeStruct; use std::fmt; -use util; -use util::secp::pedersen; macro_rules! no_dup { ($field:ident) => { @@ -210,7 +210,7 @@ struct PrintableCommitmentVisitor; impl<'de> serde::de::Visitor<'de> for PrintableCommitmentVisitor { type Value = PrintableCommitment; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter.write_str("a Pedersen commitment") } @@ -361,7 +361,7 @@ impl<'de> serde::de::Deserialize<'de> for OutputPrintable { impl<'de> serde::de::Visitor<'de> for OutputPrintableVisitor { type Value = OutputPrintable; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter.write_str("a print able Output") } @@ -571,7 +571,8 @@ impl BlockPrintable { Some(&block.header), include_proof, ) - }).collect(); + }) + .collect(); let kernels = block .kernels() .iter() diff --git a/api/src/web.rs b/api/src/web.rs index c7f487979..10eebcbb2 100644 --- a/api/src/web.rs +++ b/api/src/web.rs @@ -1,14 +1,14 @@ +use crate::rest::*; +use crate::router::ResponseFuture; use futures::future::{err, ok}; use futures::{Future, Stream}; use hyper::{Body, Request, Response, StatusCode}; -use rest::*; -use router::ResponseFuture; use serde::{Deserialize, Serialize}; use serde_json; use std::fmt::Debug; /// Parse request body -pub fn parse_body(req: Request) -> Box + Send> +pub fn parse_body(req: Request) -> Box + Send> where for<'de> T: Deserialize<'de> + Send + 'static, { diff --git a/api/tests/rest.rs b/api/tests/rest.rs index e6e564819..641ce3d2e 100644 --- a/api/tests/rest.rs +++ b/api/tests/rest.rs @@ -1,8 +1,7 @@ -extern crate grin_api as api; -extern crate grin_util as util; -extern crate hyper; +use grin_api as api; +use grin_util as util; -use api::*; +use crate::api::*; use hyper::{Body, Request}; use std::net::SocketAddr; use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT}; @@ -41,7 +40,7 @@ impl Handler for CounterMiddleware { fn call( &self, req: Request, - mut handlers: Box>, + mut handlers: Box>, ) -> ResponseFuture { self.counter.fetch_add(1, Ordering::SeqCst); handlers.next().unwrap().call(req, handlers) diff --git a/chain/Cargo.toml b/chain/Cargo.toml index b1a305961..ebb7b4d9a 100644 --- a/chain/Cargo.toml +++ b/chain/Cargo.toml @@ -7,6 +7,7 @@ license = "Apache-2.0" repository = "https://github.com/mimblewimble/grin" keywords = [ "crypto", "grin", "mimblewimble" ] workspace = ".." +edition = "2018" [dependencies] bitflags = "1" diff --git a/chain/src/chain.rs b/chain/src/chain.rs index f2563ec07..a9e115924 100644 --- a/chain/src/chain.rs +++ b/chain/src/chain.rs @@ -15,32 +15,30 @@ //! Facade and handler for the rest of the blockchain implementation //! and mostly the chain pipeline. +use crate::core::core::hash::{Hash, Hashed, ZERO_HASH}; +use crate::core::core::merkle_proof::MerkleProof; +use crate::core::core::verifier_cache::VerifierCache; +use crate::core::core::{ + Block, BlockHeader, BlockSums, Committed, Output, OutputIdentifier, Transaction, TxKernelEntry, +}; +use crate::core::global; +use crate::core::pow; +use crate::error::{Error, ErrorKind}; +use crate::lmdb; +use crate::pipe; +use crate::store; +use crate::txhashset; +use crate::types::{ + BlockStatus, ChainAdapter, NoStatus, Options, Tip, TxHashSetRoots, TxHashsetWriteStatus, +}; +use crate::util::secp::pedersen::{Commitment, RangeProof}; +use crate::util::RwLock; +use grin_store::Error::NotFoundErr; use std::collections::HashMap; use std::fs::File; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; use std::time::{Duration, Instant}; -use util::RwLock; - -use lmdb; - -use core::core::hash::{Hash, Hashed, ZERO_HASH}; -use core::core::merkle_proof::MerkleProof; -use core::core::verifier_cache::VerifierCache; -use core::core::{ - Block, BlockHeader, BlockSums, Committed, Output, OutputIdentifier, Transaction, TxKernelEntry, -}; -use core::global; -use core::pow; -use error::{Error, ErrorKind}; -use grin_store::Error::NotFoundErr; -use pipe; -use store; -use txhashset; -use types::{ - BlockStatus, ChainAdapter, NoStatus, Options, Tip, TxHashSetRoots, TxHashsetWriteStatus, -}; -use util::secp::pedersen::{Commitment, RangeProof}; /// Orphan pool size is limited by MAX_ORPHAN_SIZE pub const MAX_ORPHAN_SIZE: usize = 200; @@ -144,10 +142,10 @@ impl OrphanBlockPool { pub struct Chain { db_root: String, store: Arc, - adapter: Arc, + adapter: Arc, orphans: Arc, txhashset: Arc>, - verifier_cache: Arc>, + verifier_cache: Arc>, // POW verification function pow_verifier: fn(&BlockHeader) -> Result<(), pow::Error>, archive_mode: bool, @@ -161,10 +159,10 @@ impl Chain { pub fn init( db_root: String, db_env: Arc, - adapter: Arc, + adapter: Arc, genesis: Block, pow_verifier: fn(&BlockHeader) -> Result<(), pow::Error>, - verifier_cache: Arc>, + verifier_cache: Arc>, archive_mode: bool, ) -> Result { let chain_store = store::ChainStore::new(db_env)?; @@ -782,7 +780,7 @@ impl Chain { &self, h: Hash, txhashset_data: File, - status: &TxHashsetWriteStatus, + status: &dyn TxHashsetWriteStatus, ) -> Result<(), Error> { status.on_setup(); @@ -988,7 +986,8 @@ impl Chain { if outputs.0 != rangeproofs.0 || outputs.1.len() != rangeproofs.1.len() { return Err(ErrorKind::TxHashSetErr(String::from( "Output and rangeproof sets don't match", - )).into()); + )) + .into()); } let mut output_vec: Vec = vec![]; for (ref x, &y) in outputs.1.iter().zip(rangeproofs.1.iter()) { @@ -1131,7 +1130,7 @@ impl Chain { /// Builds an iterator on blocks starting from the current chain head and /// running backward. Specialized to return information pertaining to block /// difficulty calculation (timestamp and previous difficulties). - pub fn difficulty_iter(&self) -> store::DifficultyIter { + pub fn difficulty_iter(&self) -> store::DifficultyIter<'_> { let head = self.head().unwrap(); let store = self.store.clone(); store::DifficultyIter::from(head.last_block_h, store) diff --git a/chain/src/error.rs b/chain/src/error.rs index 133a2bdd1..1ef741280 100644 --- a/chain/src/error.rs +++ b/chain/src/error.rs @@ -13,17 +13,16 @@ // limitations under the License. //! Error types for chain +use crate::core::core::{block, committed, transaction}; +use crate::core::ser; +use crate::keychain; +use crate::util::secp; +use crate::util::secp::pedersen::Commitment; use failure::{Backtrace, Context, Fail}; +use grin_store as store; use std::fmt::{self, Display}; use std::io; -use core::core::{block, committed, transaction}; -use core::ser; -use grin_store as store; -use keychain; -use util::secp; -use util::secp::pedersen::Commitment; - /// Error definition #[derive(Debug, Fail)] pub struct Error { @@ -132,7 +131,7 @@ pub enum ErrorKind { } impl Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let cause = match self.cause() { Some(c) => format!("{}", c), None => String::from("Unknown"), @@ -155,7 +154,7 @@ impl Error { self.inner.get_context().clone() } /// get cause - pub fn cause(&self) -> Option<&Fail> { + pub fn cause(&self) -> Option<&dyn Fail> { self.inner.cause() } /// get backtrace diff --git a/chain/src/lib.rs b/chain/src/lib.rs index 9b6e65ff6..29ff83bd7 100644 --- a/chain/src/lib.rs +++ b/chain/src/lib.rs @@ -22,24 +22,19 @@ #[macro_use] extern crate bitflags; -extern crate byteorder; -extern crate croaring; -extern crate lmdb_zero as lmdb; -extern crate lru_cache; -extern crate serde; + +use lmdb_zero as lmdb; + #[macro_use] extern crate serde_derive; #[macro_use] extern crate log; -extern crate chrono; -extern crate failure; -#[macro_use] -extern crate failure_derive; -extern crate grin_core as core; -extern crate grin_keychain as keychain; -extern crate grin_store; -extern crate grin_util as util; +use failure; +use grin_core as core; +use grin_keychain as keychain; +use grin_store; +use grin_util as util; mod chain; mod error; @@ -50,7 +45,7 @@ pub mod types; // Re-export the base interface -pub use chain::{Chain, MAX_ORPHAN_SIZE}; -pub use error::{Error, ErrorKind}; -pub use store::ChainStore; -pub use types::{BlockStatus, ChainAdapter, Options, Tip, TxHashsetWriteStatus}; +pub use crate::chain::{Chain, MAX_ORPHAN_SIZE}; +pub use crate::error::{Error, ErrorKind}; +pub use crate::store::ChainStore; +pub use crate::types::{BlockStatus, ChainAdapter, Options, Tip, TxHashsetWriteStatus}; diff --git a/chain/src/pipe.rs b/chain/src/pipe.rs index 8f74a357b..1bc599bdf 100644 --- a/chain/src/pipe.rs +++ b/chain/src/pipe.rs @@ -14,25 +14,23 @@ //! Implementation of the chain block acceptance (or refusal) pipeline. -use std::sync::Arc; -use util::RwLock; - +use crate::chain::OrphanBlockPool; +use crate::core::consensus; +use crate::core::core::hash::Hashed; +use crate::core::core::verifier_cache::VerifierCache; +use crate::core::core::Committed; +use crate::core::core::{Block, BlockHeader, BlockSums}; +use crate::core::global; +use crate::core::pow; +use crate::error::{Error, ErrorKind}; +use crate::store; +use crate::txhashset; +use crate::types::{Options, Tip}; +use crate::util::RwLock; use chrono::prelude::Utc; use chrono::Duration; - -use chain::OrphanBlockPool; -use core::consensus; -use core::core::hash::Hashed; -use core::core::verifier_cache::VerifierCache; -use core::core::Committed; -use core::core::{Block, BlockHeader, BlockSums}; -use core::global; -use core::pow; -use error::{Error, ErrorKind}; use grin_store; -use store; -use txhashset; -use types::{Options, Tip}; +use std::sync::Arc; /// Contextual information required to process a new block and either reject or /// accept it. @@ -46,7 +44,7 @@ pub struct BlockContext<'a> { /// The active batch to use for block processing. pub batch: store::Batch<'a>, /// The verifier cache (caching verifier for rangeproofs and kernel signatures) - pub verifier_cache: Arc>, + pub verifier_cache: Arc>, /// Recent orphan blocks to avoid double-processing pub orphans: Arc, } @@ -56,7 +54,7 @@ pub struct BlockContext<'a> { fn process_header_for_block( header: &BlockHeader, is_fork: bool, - ctx: &mut BlockContext, + ctx: &mut BlockContext<'_>, ) -> Result<(), Error> { txhashset::header_extending(&mut ctx.txhashset, &mut ctx.batch, |extension| { extension.force_rollback(); @@ -77,7 +75,7 @@ fn process_header_for_block( // Check if we already know about this block for various reasons // from cheapest to most expensive (delay hitting the db until last). -fn check_known(block: &Block, ctx: &mut BlockContext) -> Result<(), Error> { +fn check_known(block: &Block, ctx: &mut BlockContext<'_>) -> Result<(), Error> { check_known_head(&block.header, ctx)?; check_known_orphans(&block.header, ctx)?; check_known_store(&block.header, ctx)?; @@ -87,7 +85,7 @@ fn check_known(block: &Block, ctx: &mut BlockContext) -> Result<(), Error> { /// Runs the block processing pipeline, including validation and finding a /// place for the new block in the chain. /// Returns new head if chain head updated. -pub fn process_block(b: &Block, ctx: &mut BlockContext) -> Result, Error> { +pub fn process_block(b: &Block, ctx: &mut BlockContext<'_>) -> Result, Error> { // TODO should just take a promise for a block with a full header so we don't // spend resources reading the full block when its header is invalid @@ -183,7 +181,7 @@ pub fn process_block(b: &Block, ctx: &mut BlockContext) -> Result, E /// This is only ever used during sync and uses a context based on sync_head. pub fn sync_block_headers( headers: &[BlockHeader], - ctx: &mut BlockContext, + ctx: &mut BlockContext<'_>, ) -> Result, Error> { if let Some(header) = headers.first() { debug!( @@ -251,7 +249,7 @@ pub fn sync_block_headers( /// We validate the header but we do not store it or update header head based /// on this. We will update these once we get the block back after requesting /// it. -pub fn process_block_header(header: &BlockHeader, ctx: &mut BlockContext) -> Result<(), Error> { +pub fn process_block_header(header: &BlockHeader, ctx: &mut BlockContext<'_>) -> Result<(), Error> { debug!( "pipe: process_block_header: {} at {}", header.hash(), @@ -266,7 +264,7 @@ pub fn process_block_header(header: &BlockHeader, ctx: &mut BlockContext) -> Res /// Quick in-memory check to fast-reject any block header we've already handled /// recently. Keeps duplicates from the network in check. /// ctx here is specific to the header_head (tip of the header chain) -fn check_header_known(header: &BlockHeader, ctx: &mut BlockContext) -> Result<(), Error> { +fn check_header_known(header: &BlockHeader, ctx: &mut BlockContext<'_>) -> Result<(), Error> { let header_head = ctx.batch.header_head()?; if header.hash() == header_head.last_block_h || header.hash() == header_head.prev_block_h { return Err(ErrorKind::Unfit("header already known".to_string()).into()); @@ -277,7 +275,7 @@ fn check_header_known(header: &BlockHeader, ctx: &mut BlockContext) -> Result<() /// Quick in-memory check to fast-reject any block handled recently. /// Keeps duplicates from the network in check. /// Checks against the last_block_h and prev_block_h of the chain head. -fn check_known_head(header: &BlockHeader, ctx: &mut BlockContext) -> Result<(), Error> { +fn check_known_head(header: &BlockHeader, ctx: &mut BlockContext<'_>) -> Result<(), Error> { let head = ctx.batch.head()?; let bh = header.hash(); if bh == head.last_block_h || bh == head.prev_block_h { @@ -287,7 +285,7 @@ fn check_known_head(header: &BlockHeader, ctx: &mut BlockContext) -> Result<(), } /// Check if this block is in the set of known orphans. -fn check_known_orphans(header: &BlockHeader, ctx: &mut BlockContext) -> Result<(), Error> { +fn check_known_orphans(header: &BlockHeader, ctx: &mut BlockContext<'_>) -> Result<(), Error> { if ctx.orphans.contains(&header.hash()) { Err(ErrorKind::Unfit("already known in orphans".to_string()).into()) } else { @@ -296,7 +294,7 @@ fn check_known_orphans(header: &BlockHeader, ctx: &mut BlockContext) -> Result<( } // Check if this block is in the store already. -fn check_known_store(header: &BlockHeader, ctx: &mut BlockContext) -> Result<(), Error> { +fn check_known_store(header: &BlockHeader, ctx: &mut BlockContext<'_>) -> Result<(), Error> { match ctx.batch.block_exists(&header.hash()) { Ok(true) => { let head = ctx.batch.head()?; @@ -321,7 +319,10 @@ fn check_known_store(header: &BlockHeader, ctx: &mut BlockContext) -> Result<(), // Find the previous header from the store. // Return an Orphan error if we cannot find the previous header. -fn prev_header_store(header: &BlockHeader, batch: &mut store::Batch) -> Result { +fn prev_header_store( + header: &BlockHeader, + batch: &mut store::Batch<'_>, +) -> Result { let prev = batch.get_previous_header(&header).map_err(|e| match e { grin_store::Error::NotFoundErr(_) => ErrorKind::Orphan, _ => ErrorKind::StoreErr(e, "check prev header".into()), @@ -332,7 +333,7 @@ fn prev_header_store(header: &BlockHeader, batch: &mut store::Batch) -> Result Result<(), Error> { +fn validate_header(header: &BlockHeader, ctx: &mut BlockContext<'_>) -> Result<(), Error> { // check version, enforces scheduled hard fork if !consensus::valid_header_version(header.height, header.version) { error!( @@ -425,7 +426,7 @@ fn validate_header(header: &BlockHeader, ctx: &mut BlockContext) -> Result<(), E Ok(()) } -fn validate_block(block: &Block, ctx: &mut BlockContext) -> Result<(), Error> { +fn validate_block(block: &Block, ctx: &mut BlockContext<'_>) -> Result<(), Error> { let prev = ctx.batch.get_previous_header(&block.header)?; block .validate(&prev.total_kernel_offset, ctx.verifier_cache.clone()) @@ -437,7 +438,10 @@ fn validate_block(block: &Block, ctx: &mut BlockContext) -> Result<(), Error> { /// Verify the block is not attempting to spend coinbase outputs /// before they have sufficiently matured. /// Note: requires a txhashset extension. -fn verify_coinbase_maturity(block: &Block, ext: &mut txhashset::Extension) -> Result<(), Error> { +fn verify_coinbase_maturity( + block: &Block, + ext: &mut txhashset::Extension<'_>, +) -> Result<(), Error> { ext.verify_coinbase_maturity(&block.inputs(), block.header.height)?; Ok(()) } @@ -447,7 +451,7 @@ fn verify_coinbase_maturity(block: &Block, ext: &mut txhashset::Extension) -> Re /// This allows us to verify kernel sums across the full utxo and kernel sets /// based on block_sums of previous block, accounting for the inputs|outputs|kernels /// of the new block. -fn verify_block_sums(b: &Block, ext: &mut txhashset::Extension) -> Result<(), Error> { +fn verify_block_sums(b: &Block, ext: &mut txhashset::Extension<'_>) -> Result<(), Error> { // TODO - this is 2 db calls, can we optimize this? // Retrieve the block_sums for the previous block. let prev = ext.batch.get_previous_header(&b.header)?; @@ -462,7 +466,7 @@ fn verify_block_sums(b: &Block, ext: &mut txhashset::Extension) -> Result<(), Er // Verify the kernel sums for the block_sums with the new block applied. let (utxo_sum, kernel_sum) = - (block_sums, b as &Committed).verify_kernel_sums(overage, offset)?; + (block_sums, b as &dyn Committed).verify_kernel_sums(overage, offset)?; // Save the new block_sums for the new block to the db via the batch. ext.batch.save_block_sums( @@ -478,7 +482,10 @@ fn verify_block_sums(b: &Block, ext: &mut txhashset::Extension) -> Result<(), Er /// Fully validate the block by applying it to the txhashset extension. /// Check both the txhashset roots and sizes are correct after applying the block. -fn apply_block_to_txhashset(block: &Block, ext: &mut txhashset::Extension) -> Result<(), Error> { +fn apply_block_to_txhashset( + block: &Block, + ext: &mut txhashset::Extension<'_>, +) -> Result<(), Error> { ext.validate_header_root(&block.header)?; ext.apply_block(block)?; ext.validate_roots()?; @@ -488,7 +495,7 @@ fn apply_block_to_txhashset(block: &Block, ext: &mut txhashset::Extension) -> Re /// Officially adds the block to our chain. /// Header must be added separately (assume this has been done previously). -fn add_block(b: &Block, batch: &store::Batch) -> Result<(), Error> { +fn add_block(b: &Block, batch: &store::Batch<'_>) -> Result<(), Error> { batch .save_block(b) .map_err(|e| ErrorKind::StoreErr(e, "pipe save block".to_owned()))?; @@ -496,7 +503,7 @@ fn add_block(b: &Block, batch: &store::Batch) -> Result<(), Error> { } /// Update the block chain tail so we can know the exact tail of full blocks in this node -fn update_body_tail(bh: &BlockHeader, batch: &store::Batch) -> Result<(), Error> { +fn update_body_tail(bh: &BlockHeader, batch: &store::Batch<'_>) -> Result<(), Error> { let tip = Tip::from_header(bh); batch .save_body_tail(&tip) @@ -506,7 +513,7 @@ fn update_body_tail(bh: &BlockHeader, batch: &store::Batch) -> Result<(), Error> } /// Officially adds the block header to our header chain. -fn add_block_header(bh: &BlockHeader, batch: &store::Batch) -> Result<(), Error> { +fn add_block_header(bh: &BlockHeader, batch: &store::Batch<'_>) -> Result<(), Error> { batch .save_block_header(bh) .map_err(|e| ErrorKind::StoreErr(e, "pipe save header".to_owned()))?; @@ -516,7 +523,7 @@ fn add_block_header(bh: &BlockHeader, batch: &store::Batch) -> Result<(), Error> /// Directly updates the head if we've just appended a new block to it or handle /// the situation where we've just added enough work to have a fork with more /// work than the head. -fn update_head(b: &Block, ctx: &BlockContext) -> Result, Error> { +fn update_head(b: &Block, ctx: &BlockContext<'_>) -> Result, Error> { // if we made a fork with more work than the head (which should also be true // when extending the head), update it let head = ctx.batch.head()?; @@ -544,7 +551,7 @@ fn has_more_work(header: &BlockHeader, head: &Tip) -> bool { } /// Update the sync head so we can keep syncing from where we left off. -fn update_sync_head(bh: &BlockHeader, batch: &mut store::Batch) -> Result<(), Error> { +fn update_sync_head(bh: &BlockHeader, batch: &mut store::Batch<'_>) -> Result<(), Error> { let tip = Tip::from_header(bh); batch .save_sync_head(&tip) @@ -554,7 +561,7 @@ fn update_sync_head(bh: &BlockHeader, batch: &mut store::Batch) -> Result<(), Er } /// Update the header head if this header has most work. -fn update_header_head(bh: &BlockHeader, ctx: &mut BlockContext) -> Result, Error> { +fn update_header_head(bh: &BlockHeader, ctx: &mut BlockContext<'_>) -> Result, Error> { let header_head = ctx.batch.header_head()?; if has_more_work(&bh, &header_head) { let tip = Tip::from_header(bh); @@ -576,7 +583,7 @@ fn update_header_head(bh: &BlockHeader, ctx: &mut BlockContext) -> Result