-
Notifications
You must be signed in to change notification settings - Fork 257
Cache transaction output #4772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jackzhhuang
wants to merge
4
commits into
dual-verse-dag
Choose a base branch
from
cache-transaction-output
base: dual-verse-dag
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Cache transaction output #4772
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| // Copyright (c) The Starcoin Core Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| use dashmap::DashMap; | ||
| use once_cell::sync::Lazy; | ||
| use starcoin_crypto::HashValue; | ||
| use starcoin_vm2_vm_types::transaction::TransactionOutput as TransactionOutput2; | ||
| use starcoin_vm_types::transaction::TransactionOutput as TransactionOutput1; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Cached block outputs containing VM1 and VM2 transaction outputs | ||
| #[derive(Debug, Clone)] | ||
| pub struct CachedBlockOutputs { | ||
| pub vm1_outputs: Option<Arc<Vec<TransactionOutput1>>>, | ||
| pub vm2_outputs: Option<Arc<Vec<TransactionOutput2>>>, | ||
| } | ||
|
|
||
| impl CachedBlockOutputs { | ||
| pub fn new( | ||
| vm1_outputs: Option<Vec<TransactionOutput1>>, | ||
| vm2_outputs: Option<Vec<TransactionOutput2>>, | ||
| ) -> Self { | ||
| Self { | ||
| vm1_outputs: vm1_outputs.map(Arc::new), | ||
| vm2_outputs: vm2_outputs.map(Arc::new), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Cache for transaction outputs, keyed by txn_accumulator_root | ||
| /// | ||
| /// The txn_accumulator_root uniquely identifies a transaction list, | ||
| /// so it can be used as a cache key even before the block_id is known. | ||
| pub struct TransactionOutputCache { | ||
| cache: DashMap<HashValue, CachedBlockOutputs>, | ||
| } | ||
|
|
||
| impl TransactionOutputCache { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| cache: DashMap::new(), | ||
| } | ||
| } | ||
|
|
||
| /// Insert outputs using txn_accumulator_root as key | ||
| pub fn insert_outputs( | ||
| &self, | ||
| txn_accumulator_root: HashValue, | ||
| vm1_outputs: Option<Vec<TransactionOutput1>>, | ||
| vm2_outputs: Option<Vec<TransactionOutput2>>, | ||
| ) { | ||
| let cached = CachedBlockOutputs::new(vm1_outputs, vm2_outputs); | ||
| self.cache.insert(txn_accumulator_root, cached); | ||
| } | ||
|
|
||
| /// Get outputs using txn_accumulator_root as key | ||
| pub fn get(&self, txn_accumulator_root: HashValue) -> Option<CachedBlockOutputs> { | ||
| self.cache.get(&txn_accumulator_root).map(|v| v.clone()) | ||
| } | ||
|
|
||
| /// Remove outputs using txn_accumulator_root as key | ||
| pub fn remove(&self, txn_accumulator_root: HashValue) { | ||
| self.cache.remove(&txn_accumulator_root); | ||
| } | ||
|
|
||
| pub fn len(&self) -> usize { | ||
| self.cache.len() | ||
| } | ||
|
|
||
| pub fn is_empty(&self) -> bool { | ||
| self.cache.is_empty() | ||
| } | ||
|
|
||
| pub fn clear(&self) { | ||
| self.cache.clear(); | ||
| } | ||
| } | ||
|
|
||
| impl Default for TransactionOutputCache { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| static GLOBAL_TXN_OUTPUT_CACHE: Lazy<TransactionOutputCache> = | ||
| Lazy::new(TransactionOutputCache::new); | ||
|
|
||
| pub fn global_txn_output_cache() -> &'static TransactionOutputCache { | ||
| &GLOBAL_TXN_OUTPUT_CACHE | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_cache_insert_and_get() { | ||
| let cache = TransactionOutputCache::new(); | ||
| let txn_root = HashValue::random(); | ||
|
|
||
| assert!(cache.is_empty()); | ||
| assert!(cache.get(txn_root).is_none()); | ||
|
|
||
| cache.insert_outputs(txn_root, None, None); | ||
| assert_eq!(cache.len(), 1); | ||
|
|
||
| let result = cache.get(txn_root); | ||
| assert!(result.is_some()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_cache_remove() { | ||
| let cache = TransactionOutputCache::new(); | ||
| let txn_root = HashValue::random(); | ||
|
|
||
| cache.insert_outputs(txn_root, None, None); | ||
| assert_eq!(cache.len(), 1); | ||
|
|
||
| cache.remove(txn_root); | ||
| assert!(cache.is_empty()); | ||
| assert!(cache.get(txn_root).is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_cache_clear() { | ||
| let cache = TransactionOutputCache::new(); | ||
|
|
||
| for _ in 0..5 { | ||
| let txn_root = HashValue::random(); | ||
| cache.insert_outputs(txn_root, None, None); | ||
| } | ||
|
|
||
| assert_eq!(cache.len(), 5); | ||
| cache.clear(); | ||
| assert!(cache.is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_global_cache() { | ||
| let cache = global_txn_output_cache(); | ||
| let initial_len = cache.len(); | ||
|
|
||
| let txn_root = HashValue::random(); | ||
| cache.insert_outputs(txn_root, None, None); | ||
|
|
||
| assert_eq!(cache.len(), initial_len + 1); | ||
|
|
||
| cache.remove(txn_root); | ||
| assert_eq!(cache.len(), initial_len); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.