Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Storage chains: indexing, renewals and reference counting #8265

Merged
19 commits merged into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion client/api/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use sp_runtime::{generic::BlockId, Justification, Storage};
use sp_runtime::traits::{Block as BlockT, NumberFor, HashFor};
use sp_state_machine::{
ChangesTrieState, ChangesTrieStorage as StateChangesTrieStorage, ChangesTrieTransaction,
StorageCollection, ChildStorageCollection, OffchainChangesCollection,
StorageCollection, ChildStorageCollection, OffchainChangesCollection, IndexOperation,
};
use sp_storage::{StorageData, StorageKey, PrefixedStorageKey, ChildInfo};
use crate::{
Expand Down Expand Up @@ -200,6 +200,9 @@ pub trait BlockImportOperation<Block: BlockT> {
/// Mark a block as new head. If both block import and set head are specified, set head
/// overrides block import's best block rule.
fn mark_head(&mut self, id: BlockId<Block>) -> sp_blockchain::Result<()>;

/// Add a transaction index operation.
fn update_transaction_index(&mut self, _index: Vec<IndexOperation>) -> sp_blockchain::Result<()>;
arkpar marked this conversation as resolved.
Show resolved Hide resolved
}

/// Interface for performing operations on the backend.
Expand Down
13 changes: 7 additions & 6 deletions client/api/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,16 @@ pub trait BlockBackend<Block: BlockT> {
/// Get block hash by number.
fn block_hash(&self, number: NumberFor<Block>) -> sp_blockchain::Result<Option<Block::Hash>>;

/// Get single extrinsic by hash.
fn extrinsic(
/// Get single indexed transaction by content hash. Note that this will only fetch transactions
/// that are indexed by the runtime with `storage_index_transaction`.
cheme marked this conversation as resolved.
Show resolved Hide resolved
arkpar marked this conversation as resolved.
Show resolved Hide resolved
fn indexed_transaction(
&self,
hash: &Block::Hash,
) -> sp_blockchain::Result<Option<<Block as BlockT>::Extrinsic>>;
) -> sp_blockchain::Result<Option<Vec<u8>>>;
bkchr marked this conversation as resolved.
Show resolved Hide resolved

/// Check if extrinsic exists.
fn have_extrinsic(&self, hash: &Block::Hash) -> sp_blockchain::Result<bool> {
Ok(self.extrinsic(hash)?.is_some())
/// Check if transaction index exists.
fn have_indexed_transaction(&self, hash: &Block::Hash) -> sp_blockchain::Result<bool> {
arkpar marked this conversation as resolved.
Show resolved Hide resolved
Ok(self.indexed_transaction(hash)?.is_some())
}
}

Expand Down
10 changes: 7 additions & 3 deletions client/api/src/in_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use sp_runtime::traits::{Block as BlockT, Header as HeaderT, Zero, NumberFor, Ha
use sp_runtime::{Justification, Storage};
use sp_state_machine::{
ChangesTrieTransaction, InMemoryBackend, Backend as StateBackend, StorageCollection,
ChildStorageCollection,
ChildStorageCollection, IndexOperation,
};
use sp_blockchain::{CachedHeaderMetadata, HeaderMetadata};

Expand Down Expand Up @@ -387,10 +387,10 @@ impl<Block: BlockT> blockchain::Backend<Block> for Blockchain<Block> {
unimplemented!()
}

fn extrinsic(
fn indexed_transaction(
&self,
_hash: &Block::Hash,
) -> sp_blockchain::Result<Option<<Block as BlockT>::Extrinsic>> {
) -> sp_blockchain::Result<Option<Vec<u8>>> {
cheme marked this conversation as resolved.
Show resolved Hide resolved
unimplemented!("Not supported by the in-mem backend.")
}
}
Expand Down Expand Up @@ -585,6 +585,10 @@ impl<Block: BlockT> backend::BlockImportOperation<Block> for BlockImportOperatio
self.set_head = Some(block);
Ok(())
}

fn update_transaction_index(&mut self, _index: Vec<IndexOperation>) -> sp_blockchain::Result<()> {
Ok(())
}
}

/// In-memory backend. Keeps all states and blocks in memory.
Expand Down
7 changes: 6 additions & 1 deletion client/api/src/leaves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use sp_runtime::traits::AtLeast32Bit;
use codec::{Encode, Decode};
use sp_blockchain::{Error, Result};

type DbHash = [u8; 32];
type DbHash = sp_core::H256;

#[derive(Debug, Clone, PartialEq, Eq)]
struct LeafSetItem<H, N> {
Expand Down Expand Up @@ -55,6 +55,11 @@ impl<H, N: Ord> FinalizationDisplaced<H, N> {
// one transaction, then there will be no overlap in the keys.
self.leaves.append(&mut other.leaves);
}

/// Iterate over all displaced leaves.
pub fn leaves(&self) -> impl IntoIterator<Item=&H> {
self.leaves.values().flatten()
}
}

/// list of leaf hashes ordered by number (descending).
Expand Down
2 changes: 1 addition & 1 deletion client/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ sp-trie = { version = "3.0.0", path = "../../primitives/trie" }
sp-consensus = { version = "0.9.0", path = "../../primitives/consensus/common" }
sp-blockchain = { version = "3.0.0", path = "../../primitives/blockchain" }
sp-database = { version = "3.0.0", path = "../../primitives/database" }
parity-db = { version = "0.2.2", optional = true }
parity-db = { version = "0.2.3", optional = true }
prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.9.0", path = "../../utils/prometheus" }

[dev-dependencies]
Expand Down
Loading