Skip to content

Commit

Permalink
LRU process proposal cache
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed Jul 4, 2024
1 parent 64f133e commit c5b9147
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl Shell {
self.state
.in_mem_mut()
.process_proposal_cache
.insert(block_hash, result);
.put(block_hash, result);
}
Ok(Response::ProcessProposal(response))
}
Expand Down Expand Up @@ -218,7 +218,7 @@ impl Shell {
if recheck_process_proposal {
let process_proposal_result = match self
.state
.in_mem()
.in_mem_mut()
.process_proposal_cache
.get(&finalize_req.block_hash)
{
Expand Down
4 changes: 2 additions & 2 deletions crates/node/src/shims/abcipp_shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl AbcippShim {
match self
.service
.state
.in_mem()
.in_mem_mut()
.process_proposal_cache
.get(&block_hash)
{
Expand Down Expand Up @@ -216,7 +216,7 @@ impl AbcippShim {
.state
.in_mem_mut()
.process_proposal_cache
.insert(block_hash.to_owned(), result.clone());
.put(block_hash.to_owned(), result.clone());

result
}
Expand Down
1 change: 1 addition & 0 deletions crates/state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namada_storage = { path = "../storage" }
namada_tx = { path = "../tx" }

borsh.workspace = true
clru.workspace = true
itertools.workspace = true
linkme = {workspace = true, optional = true}
smooth-operator.workspace = true
Expand Down
15 changes: 10 additions & 5 deletions crates/state/src/in_memory.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::num::NonZeroUsize;

use clru::CLruCache;
use namada_core::address::{Address, EstablishedAddressGen, InternalAddress};
use namada_core::borsh::{BorshDeserialize, BorshSerialize};
use namada_core::chain::{ChainId, CHAIN_ID_LENGTH};
Expand Down Expand Up @@ -74,13 +77,13 @@ where
/// Data that needs to be committed to the merkle tree
pub commit_only_data: CommitOnlyData,
/// Cache of the results of process proposal for the next height to decide.
/// The different proposed blocks are indexed by their hash. This is used
/// A LRU cache is used to prevent consuming too much memory at times where
/// a node cannot make progress and keeps evaluating new proposals. The
/// different proposed blocks are indexed by their hash. This is used
/// to avoid running process proposal more than once internally because of
/// the shim or the recheck option (comet only calls it at most once
/// for a given height/round)
// FIXME: would be better to limit the size of this cache. CLruCache?
pub process_proposal_cache:
namada_core::collections::HashMap<Hash, ProcessProposalCachedResult>,
pub process_proposal_cache: CLruCache<Hash, ProcessProposalCachedResult>,
}

/// Last committed block
Expand Down Expand Up @@ -162,7 +165,9 @@ where
eth_events_queue: EthEventsQueue::default(),
storage_read_past_height_limit,
commit_only_data: CommitOnlyData::default(),
process_proposal_cache: Default::default(),
process_proposal_cache: CLruCache::new(
NonZeroUsize::new(10).unwrap(),
),
}
}

Expand Down
7 changes: 6 additions & 1 deletion crates/state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,9 @@ where
#[cfg(any(test, feature = "testing"))]
pub mod testing {

use std::num::NonZeroUsize;

use clru::CLruCache;
use namada_core::address;
use namada_core::address::EstablishedAddressGen;
use namada_core::chain::ChainId;
Expand Down Expand Up @@ -658,7 +661,9 @@ pub mod testing {
eth_events_queue: EthEventsQueue::default(),
storage_read_past_height_limit: Some(1000),
commit_only_data: CommitOnlyData::default(),
process_proposal_cache: Default::default(),
process_proposal_cache: CLruCache::new(
NonZeroUsize::new(10).unwrap(),
),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions wasm_for_tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c5b9147

Please sign in to comment.