Skip to content

Commit

Permalink
never speak of the verifier cache again (#3628)
Browse files Browse the repository at this point in the history
  • Loading branch information
antiochp committed Apr 1, 2021
1 parent cccaf98 commit f6ec77a
Show file tree
Hide file tree
Showing 37 changed files with 189 additions and 735 deletions.
11 changes: 4 additions & 7 deletions api/src/foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use crate::chain::{Chain, SyncState};
use crate::core::core::hash::Hash;
use crate::core::core::transaction::Transaction;
use crate::core::core::verifier_cache::VerifierCache;
use crate::handlers::blocks_api::{BlockHandler, HeaderHandler};
use crate::handlers::chain_api::{ChainHandler, KernelHandler, OutputHandler};
use crate::handlers::pool_api::PoolHandler;
Expand All @@ -39,22 +38,20 @@ use std::sync::Weak;
/// Methods in this API are intended to be 'single use'.
///

pub struct Foreign<B, P, V>
pub struct Foreign<B, P>
where
B: BlockChain,
P: PoolAdapter,
V: VerifierCache + 'static,
{
pub chain: Weak<Chain>,
pub tx_pool: Weak<RwLock<pool::TransactionPool<B, P, V>>>,
pub tx_pool: Weak<RwLock<pool::TransactionPool<B, P>>>,
pub sync_state: Weak<SyncState>,
}

impl<B, P, V> Foreign<B, P, V>
impl<B, P> Foreign<B, P>
where
B: BlockChain,
P: PoolAdapter,
V: VerifierCache + 'static,
{
/// Create a new API instance with the chain, transaction pool, peers and `sync_state`. All subsequent
/// API calls will operate on this instance of node API.
Expand All @@ -71,7 +68,7 @@ where

pub fn new(
chain: Weak<Chain>,
tx_pool: Weak<RwLock<pool::TransactionPool<B, P, V>>>,
tx_pool: Weak<RwLock<pool::TransactionPool<B, P>>>,
sync_state: Weak<SyncState>,
) -> Self {
Foreign {
Expand Down
8 changes: 3 additions & 5 deletions api/src/foreign_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

use crate::core::core::hash::Hash;
use crate::core::core::transaction::Transaction;
use crate::core::core::verifier_cache::VerifierCache;
use crate::foreign::Foreign;
use crate::pool::PoolEntry;
use crate::pool::{BlockChain, PoolAdapter};
Expand Down Expand Up @@ -742,11 +741,10 @@ pub trait ForeignRpc: Sync + Send {
fn push_transaction(&self, tx: Transaction, fluff: Option<bool>) -> Result<(), ErrorKind>;
}

impl<B, P, V> ForeignRpc for Foreign<B, P, V>
impl<B, P> ForeignRpc for Foreign<B, P>
where
B: BlockChain,
P: PoolAdapter,
V: VerifierCache + 'static,
{
fn get_header(
&self,
Expand Down Expand Up @@ -856,7 +854,7 @@ macro_rules! doctest_helper_json_rpc_foreign_assert_response {
// create temporary grin server, run jsonrpc request on node api, delete server, return
// json response.

{
{
/*use grin_servers::test_framework::framework::run_doctest;
use grin_util as util;
use serde_json;
Expand Down Expand Up @@ -890,6 +888,6 @@ macro_rules! doctest_helper_json_rpc_foreign_assert_response {
serde_json::to_string_pretty(&expected_response).unwrap()
);
}*/
}
}
};
}
19 changes: 7 additions & 12 deletions api/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use crate::auth::{
};
use crate::chain;
use crate::chain::{Chain, SyncState};
use crate::core::core::verifier_cache::VerifierCache;
use crate::foreign::Foreign;
use crate::foreign_rpc::ForeignRpc;
use crate::owner::Owner;
Expand All @@ -48,10 +47,10 @@ use std::sync::{Arc, Weak};

/// Listener version, providing same API but listening for requests on a
/// port and wrapping the calls
pub fn node_apis<B, P, V>(
pub fn node_apis<B, P>(
addr: &str,
chain: Arc<chain::Chain>,
tx_pool: Arc<RwLock<pool::TransactionPool<B, P, V>>>,
tx_pool: Arc<RwLock<pool::TransactionPool<B, P>>>,
peers: Arc<p2p::Peers>,
sync_state: Arc<chain::SyncState>,
api_secret: Option<String>,
Expand All @@ -61,7 +60,6 @@ pub fn node_apis<B, P, V>(
where
B: BlockChain + 'static,
P: PoolAdapter + 'static,
V: VerifierCache + 'static,
{
let mut router = Router::new();

Expand Down Expand Up @@ -173,27 +171,25 @@ impl crate::router::Handler for OwnerAPIHandlerV2 {
}

/// V2 API Handler/Wrapper for foreign functions
pub struct ForeignAPIHandlerV2<B, P, V>
pub struct ForeignAPIHandlerV2<B, P>
where
B: BlockChain,
P: PoolAdapter,
V: VerifierCache + 'static,
{
pub chain: Weak<Chain>,
pub tx_pool: Weak<RwLock<pool::TransactionPool<B, P, V>>>,
pub tx_pool: Weak<RwLock<pool::TransactionPool<B, P>>>,
pub sync_state: Weak<SyncState>,
}

impl<B, P, V> ForeignAPIHandlerV2<B, P, V>
impl<B, P> ForeignAPIHandlerV2<B, P>
where
B: BlockChain,
P: PoolAdapter,
V: VerifierCache + 'static,
{
/// Create a new foreign API handler for GET methods
pub fn new(
chain: Weak<Chain>,
tx_pool: Weak<RwLock<pool::TransactionPool<B, P, V>>>,
tx_pool: Weak<RwLock<pool::TransactionPool<B, P>>>,
sync_state: Weak<SyncState>,
) -> Self {
ForeignAPIHandlerV2 {
Expand All @@ -204,11 +200,10 @@ where
}
}

impl<B, P, V> crate::router::Handler for ForeignAPIHandlerV2<B, P, V>
impl<B, P> crate::router::Handler for ForeignAPIHandlerV2<B, P>
where
B: BlockChain + 'static,
P: PoolAdapter + 'static,
V: VerifierCache + 'static,
{
fn post(&self, req: Request<Body>) -> ResponseFuture {
let api = Foreign::new(
Expand Down
30 changes: 11 additions & 19 deletions api/src/handlers/pool_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use super::utils::w;
use crate::core::core::hash::Hashed;
use crate::core::core::verifier_cache::VerifierCache;
use crate::core::core::Transaction;
use crate::core::ser::{self, ProtocolVersion};
use crate::pool::{self, BlockChain, PoolAdapter, PoolEntry};
Expand All @@ -30,20 +29,18 @@ use std::sync::Weak;

/// Get basic information about the transaction pool.
/// GET /v1/pool
pub struct PoolInfoHandler<B, P, V>
pub struct PoolInfoHandler<B, P>
where
B: BlockChain,
P: PoolAdapter,
V: VerifierCache + 'static,
{
pub tx_pool: Weak<RwLock<pool::TransactionPool<B, P, V>>>,
pub tx_pool: Weak<RwLock<pool::TransactionPool<B, P>>>,
}

impl<B, P, V> Handler for PoolInfoHandler<B, P, V>
impl<B, P> Handler for PoolInfoHandler<B, P>
where
B: BlockChain,
P: PoolAdapter,
V: VerifierCache + 'static,
{
fn get(&self, _req: Request<Body>) -> ResponseFuture {
let pool_arc = w_fut!(&self.tx_pool);
Expand All @@ -55,20 +52,18 @@ where
}
}

pub struct PoolHandler<B, P, V>
pub struct PoolHandler<B, P>
where
B: BlockChain,
P: PoolAdapter,
V: VerifierCache + 'static,
{
pub tx_pool: Weak<RwLock<pool::TransactionPool<B, P, V>>>,
pub tx_pool: Weak<RwLock<pool::TransactionPool<B, P>>>,
}

impl<B, P, V> PoolHandler<B, P, V>
impl<B, P> PoolHandler<B, P>
where
B: BlockChain,
P: PoolAdapter,
V: VerifierCache + 'static,
{
pub fn get_pool_size(&self) -> Result<usize, Error> {
let pool_arc = w(&self.tx_pool)?;
Expand Down Expand Up @@ -117,23 +112,21 @@ struct TxWrapper {

/// Push new transaction to our local transaction pool.
/// POST /v1/pool/push_tx
pub struct PoolPushHandler<B, P, V>
pub struct PoolPushHandler<B, P>
where
B: BlockChain,
P: PoolAdapter,
V: VerifierCache + 'static,
{
pub tx_pool: Weak<RwLock<pool::TransactionPool<B, P, V>>>,
pub tx_pool: Weak<RwLock<pool::TransactionPool<B, P>>>,
}

async fn update_pool<B, P, V>(
pool: Weak<RwLock<pool::TransactionPool<B, P, V>>>,
async fn update_pool<B, P>(
pool: Weak<RwLock<pool::TransactionPool<B, P>>>,
req: Request<Body>,
) -> Result<(), Error>
where
B: BlockChain,
P: PoolAdapter,
V: VerifierCache + 'static,
{
let pool = w(&pool)?;
let params = QueryParams::from(req.uri().query());
Expand Down Expand Up @@ -169,11 +162,10 @@ where
Ok(())
}

impl<B, P, V> Handler for PoolPushHandler<B, P, V>
impl<B, P> Handler for PoolPushHandler<B, P>
where
B: BlockChain + 'static,
P: PoolAdapter + 'static,
V: VerifierCache + 'static,
{
fn post(&self, req: Request<Body>) -> ResponseFuture {
let pool = self.tx_pool.clone();
Expand Down
5 changes: 0 additions & 5 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

use crate::core::core::hash::{Hash, Hashed};
use crate::core::core::merkle_proof::MerkleProof;
use crate::core::core::verifier_cache::VerifierCache;
use crate::core::core::{
Block, BlockHeader, BlockSums, Committed, Inputs, KernelFeatures, Output, OutputIdentifier,
SegmentIdentifier, Transaction, TxKernel,
Expand Down Expand Up @@ -149,7 +148,6 @@ pub struct Chain {
orphans: Arc<OrphanBlockPool>,
txhashset: Arc<RwLock<txhashset::TxHashSet>>,
header_pmmr: Arc<RwLock<txhashset::PMMRHandle<BlockHeader>>>,
verifier_cache: Arc<RwLock<dyn VerifierCache>>,
pibd_segmenter: Arc<RwLock<Option<Segmenter>>>,
// POW verification function
pow_verifier: fn(&BlockHeader) -> Result<(), pow::Error>,
Expand All @@ -166,7 +164,6 @@ impl Chain {
adapter: Arc<dyn ChainAdapter + Send + Sync>,
genesis: Block,
pow_verifier: fn(&BlockHeader) -> Result<(), pow::Error>,
verifier_cache: Arc<RwLock<dyn VerifierCache>>,
archive_mode: bool,
) -> Result<Chain, Error> {
let store = Arc::new(store::ChainStore::new(&db_root)?);
Expand Down Expand Up @@ -201,7 +198,6 @@ impl Chain {
header_pmmr: Arc::new(RwLock::new(header_pmmr)),
pibd_segmenter: Arc::new(RwLock::new(None)),
pow_verifier,
verifier_cache,
archive_mode,
genesis: genesis.header,
};
Expand Down Expand Up @@ -434,7 +430,6 @@ impl Chain {
Ok(pipe::BlockContext {
opts,
pow_verifier: self.pow_verifier,
verifier_cache: self.verifier_cache.clone(),
header_pmmr,
txhashset,
batch,
Expand Down
7 changes: 1 addition & 6 deletions chain/src/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

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, Block, BlockHeader, BlockSums, HeaderVersion, OutputIdentifier, TransactionBody,
Expand All @@ -27,8 +26,6 @@ use crate::error::{Error, ErrorKind};
use crate::store;
use crate::txhashset;
use crate::types::{CommitPos, Options, Tip};
use crate::util::RwLock;
use std::sync::Arc;

/// Contextual information required to process a new block and either reject or
/// accept it.
Expand All @@ -43,8 +40,6 @@ pub struct BlockContext<'a> {
pub header_pmmr: &'a mut txhashset::PMMRHandle<BlockHeader>,
/// 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<RwLock<dyn VerifierCache>>,
}

// If this block has greater total difficulty than treat as unknown in current context.
Expand Down Expand Up @@ -419,7 +414,7 @@ fn validate_header(header: &BlockHeader, ctx: &mut BlockContext<'_>) -> Result<(
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())
.validate(&prev.total_kernel_offset)
.map_err(ErrorKind::InvalidBlockProof)?;
Ok(())
}
Expand Down
5 changes: 0 additions & 5 deletions chain/tests/chain_test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@ use self::chain::types::NoopAdapter;
use self::chain::types::Options;
use self::chain::Chain;
use self::core::core::hash::Hashed;
use self::core::core::verifier_cache::LruVerifierCache;
use self::core::core::Block;
use self::core::genesis;
use self::core::global::ChainTypes;
use self::core::libtx::{self, reward};
use self::core::{consensus, global, pow};
use self::keychain::{ExtKeychainPath, Keychain};
use self::util::RwLock;
use chrono::Duration;
use grin_chain as chain;
use grin_core as core;
use grin_keychain as keychain;
use grin_util as util;
use std::fs;
use std::sync::Arc;

Expand All @@ -37,13 +34,11 @@ pub fn clean_output_dir(dir_name: &str) {
}

pub fn init_chain(dir_name: &str, genesis: Block) -> Chain {
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
Chain::init(
dir_name.to_string(),
Arc::new(NoopAdapter {}),
genesis,
pow::verify_size,
verifier_cache,
false,
)
.unwrap()
Expand Down
5 changes: 0 additions & 5 deletions chain/tests/mine_simple_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use self::chain::types::{NoopAdapter, Tip};
use self::chain::Chain;
use self::core::core::hash::Hashed;
use self::core::core::verifier_cache::LruVerifierCache;
use self::core::core::{Block, BlockHeader, KernelFeatures, Transaction};
use self::core::global::ChainTypes;
use self::core::libtx::{self, build, ProofBuilder};
Expand Down Expand Up @@ -56,13 +55,11 @@ impl ChainAdapter for StatusAdapter {
fn setup_with_status_adapter(dir_name: &str, genesis: Block, adapter: Arc<StatusAdapter>) -> Chain {
util::init_test_logger();
clean_output_dir(dir_name);
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
let chain = chain::Chain::init(
dir_name.to_string(),
adapter,
genesis,
pow::verify_size,
verifier_cache,
false,
)
.unwrap();
Expand Down Expand Up @@ -904,13 +901,11 @@ where
fn actual_diff_iter_output() {
global::set_local_chain_type(ChainTypes::AutomatedTesting);
let genesis_block = pow::mine_genesis_block().unwrap();
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
let chain = chain::Chain::init(
"../.grin".to_string(),
Arc::new(NoopAdapter {}),
genesis_block,
pow::verify_size,
verifier_cache,
false,
)
.unwrap();
Expand Down
Loading

0 comments on commit f6ec77a

Please sign in to comment.