mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 08:41:07 +00:00
set aux in block import
This commit is contained in:
@@ -76,6 +76,9 @@ where
|
|||||||
fn reset_storage<I: Iterator<Item=(Vec<u8>, Vec<u8>)>>(&mut self, iter: I) -> error::Result<()>;
|
fn reset_storage<I: Iterator<Item=(Vec<u8>, Vec<u8>)>>(&mut self, iter: I) -> error::Result<()>;
|
||||||
/// Inject changes trie data into the database.
|
/// Inject changes trie data into the database.
|
||||||
fn update_changes_trie(&mut self, update: MemoryDB<H>) -> error::Result<()>;
|
fn update_changes_trie(&mut self, update: MemoryDB<H>) -> error::Result<()>;
|
||||||
|
/// Update auxiliary keys. Values are `None` if should be deleted.
|
||||||
|
fn set_aux<I>(&mut self, ops: I) -> error::Result<()>
|
||||||
|
where I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Client backend. Manages the data layer.
|
/// Client backend. Manages the data layer.
|
||||||
|
|||||||
@@ -96,6 +96,7 @@ struct BlockchainStorage<Block: BlockT> {
|
|||||||
genesis_hash: Block::Hash,
|
genesis_hash: Block::Hash,
|
||||||
cht_roots: HashMap<NumberFor<Block>, Block::Hash>,
|
cht_roots: HashMap<NumberFor<Block>, Block::Hash>,
|
||||||
leaves: LeafSet<Block::Hash, NumberFor<Block>>,
|
leaves: LeafSet<Block::Hash, NumberFor<Block>>,
|
||||||
|
aux: HashMap<Vec<u8>, Vec<u8>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// In-memory blockchain. Supports concurrent reads.
|
/// In-memory blockchain. Supports concurrent reads.
|
||||||
@@ -144,6 +145,7 @@ impl<Block: BlockT> Blockchain<Block> {
|
|||||||
genesis_hash: Default::default(),
|
genesis_hash: Default::default(),
|
||||||
cht_roots: HashMap::new(),
|
cht_roots: HashMap::new(),
|
||||||
leaves: LeafSet::new(),
|
leaves: LeafSet::new(),
|
||||||
|
aux: HashMap::new(),
|
||||||
}));
|
}));
|
||||||
Blockchain {
|
Blockchain {
|
||||||
storage: storage.clone(),
|
storage: storage.clone(),
|
||||||
@@ -247,6 +249,16 @@ impl<Block: BlockT> Blockchain<Block> {
|
|||||||
self.storage.write().finalized_hash = hash;
|
self.storage.write().finalized_hash = hash;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn write_aux(&self, ops: Vec<(Vec<u8>, Option<Vec<u8>>)>) {
|
||||||
|
let mut storage = self.storage.write();
|
||||||
|
for (k, v) in ops {
|
||||||
|
match v {
|
||||||
|
Some(v) => storage.aux.insert(k, v),
|
||||||
|
None => storage.aux.remove(&k),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Block: BlockT> HeaderBackend<Block> for Blockchain<Block> {
|
impl<Block: BlockT> HeaderBackend<Block> for Blockchain<Block> {
|
||||||
@@ -320,6 +332,7 @@ impl<Block: BlockT> light::blockchain::Storage<Block> for Blockchain<Block>
|
|||||||
header: Block::Header,
|
header: Block::Header,
|
||||||
authorities: Option<Vec<AuthorityId>>,
|
authorities: Option<Vec<AuthorityId>>,
|
||||||
state: NewBlockState,
|
state: NewBlockState,
|
||||||
|
aux_ops: Vec<(Vec<u8>, Option<Vec<u8>>)>,
|
||||||
) -> error::Result<()> {
|
) -> error::Result<()> {
|
||||||
let hash = header.hash();
|
let hash = header.hash();
|
||||||
let parent_hash = *header.parent_hash();
|
let parent_hash = *header.parent_hash();
|
||||||
@@ -328,6 +341,7 @@ impl<Block: BlockT> light::blockchain::Storage<Block> for Blockchain<Block>
|
|||||||
self.cache.insert(parent_hash, authorities);
|
self.cache.insert(parent_hash, authorities);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.write_aux(aux_ops);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -356,6 +370,7 @@ pub struct BlockImportOperation<Block: BlockT, H: Hasher> {
|
|||||||
old_state: InMemory<H>,
|
old_state: InMemory<H>,
|
||||||
new_state: Option<InMemory<H>>,
|
new_state: Option<InMemory<H>>,
|
||||||
changes_trie_update: Option<MemoryDB<H>>,
|
changes_trie_update: Option<MemoryDB<H>>,
|
||||||
|
aux: Option<Vec<(Vec<u8>, Option<Vec<u8>>)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Block, H> backend::BlockImportOperation<Block, H> for BlockImportOperation<Block, H>
|
impl<Block, H> backend::BlockImportOperation<Block, H> for BlockImportOperation<Block, H>
|
||||||
@@ -404,6 +419,13 @@ where
|
|||||||
self.new_state = Some(InMemory::from(iter.collect::<HashMap<_, _>>()));
|
self.new_state = Some(InMemory::from(iter.collect::<HashMap<_, _>>()));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn set_aux<I>(&mut self, ops: I) -> error::Result<()>
|
||||||
|
where I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>
|
||||||
|
{
|
||||||
|
self.aux = Some(ops.into_iter().collect());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// In-memory backend. Keeps all states and blocks in memory. Useful for testing.
|
/// In-memory backend. Keeps all states and blocks in memory. Useful for testing.
|
||||||
@@ -417,7 +439,6 @@ where
|
|||||||
states: RwLock<HashMap<Block::Hash, InMemory<H>>>,
|
states: RwLock<HashMap<Block::Hash, InMemory<H>>>,
|
||||||
changes_trie_storage: InMemoryChangesTrieStorage<H>,
|
changes_trie_storage: InMemoryChangesTrieStorage<H>,
|
||||||
blockchain: Blockchain<Block>,
|
blockchain: Blockchain<Block>,
|
||||||
aux: RwLock<HashMap<Vec<u8>, Vec<u8>>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Block, H> Backend<Block, H>
|
impl<Block, H> Backend<Block, H>
|
||||||
@@ -433,7 +454,6 @@ where
|
|||||||
states: RwLock::new(HashMap::new()),
|
states: RwLock::new(HashMap::new()),
|
||||||
changes_trie_storage: InMemoryChangesTrieStorage::new(),
|
changes_trie_storage: InMemoryChangesTrieStorage::new(),
|
||||||
blockchain: Blockchain::new(),
|
blockchain: Blockchain::new(),
|
||||||
aux: RwLock::new(HashMap::new()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -461,6 +481,7 @@ where
|
|||||||
old_state: state,
|
old_state: state,
|
||||||
new_state: None,
|
new_state: None,
|
||||||
changes_trie_update: None,
|
changes_trie_update: None,
|
||||||
|
aux: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -488,6 +509,10 @@ where
|
|||||||
self.blockchain.cache.insert(parent_hash, operation.pending_authorities);
|
self.blockchain.cache.insert(parent_hash, operation.pending_authorities);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(ops) = operation.aux {
|
||||||
|
self.blockchain.write_aux(ops);
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -515,18 +540,18 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn insert_aux<'a, 'b: 'a, 'c: 'a, I: IntoIterator<Item=&'a (&'c [u8], &'c [u8])>, D: IntoIterator<Item=&'a &'b [u8]>>(&self, insert: I, delete: D) -> error::Result<()> {
|
fn insert_aux<'a, 'b: 'a, 'c: 'a, I: IntoIterator<Item=&'a (&'c [u8], &'c [u8])>, D: IntoIterator<Item=&'a &'b [u8]>>(&self, insert: I, delete: D) -> error::Result<()> {
|
||||||
let mut aux = self.aux.write();
|
let mut storage = self.blockchain.storage.write();
|
||||||
for (k, v) in insert {
|
for (k, v) in insert {
|
||||||
aux.insert(k.to_vec(), v.to_vec());
|
storage.aux.insert(k.to_vec(), v.to_vec());
|
||||||
}
|
}
|
||||||
for k in delete {
|
for k in delete {
|
||||||
aux.remove(*k);
|
storage.aux.remove(*k);
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_aux(&self, key: &[u8]) -> error::Result<Option<Vec<u8>>> {
|
fn get_aux(&self, key: &[u8]) -> error::Result<Option<Vec<u8>>> {
|
||||||
Ok(self.aux.read().get(key).cloned())
|
Ok(self.blockchain.storage.read().aux.get(key).cloned())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ pub struct ImportOperation<Block: BlockT, S, F> {
|
|||||||
header: Option<Block::Header>,
|
header: Option<Block::Header>,
|
||||||
authorities: Option<Vec<AuthorityId>>,
|
authorities: Option<Vec<AuthorityId>>,
|
||||||
leaf_state: NewBlockState,
|
leaf_state: NewBlockState,
|
||||||
|
aux_ops: Vec<(Vec<u8>, Option<Vec<u8>>)>,
|
||||||
_phantom: ::std::marker::PhantomData<(S, F)>,
|
_phantom: ::std::marker::PhantomData<(S, F)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,6 +87,7 @@ impl<S, F, Block, H> ClientBackend<Block, H> for Backend<S, F> where
|
|||||||
header: None,
|
header: None,
|
||||||
authorities: None,
|
authorities: None,
|
||||||
leaf_state: NewBlockState::Normal,
|
leaf_state: NewBlockState::Normal,
|
||||||
|
aux_ops: Vec::new(),
|
||||||
_phantom: Default::default(),
|
_phantom: Default::default(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -96,6 +98,7 @@ impl<S, F, Block, H> ClientBackend<Block, H> for Backend<S, F> where
|
|||||||
header,
|
header,
|
||||||
operation.authorities,
|
operation.authorities,
|
||||||
operation.leaf_state,
|
operation.leaf_state,
|
||||||
|
operation.aux_ops,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,6 +196,13 @@ where
|
|||||||
// we're not storing anything locally => ignore changes
|
// we're not storing anything locally => ignore changes
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn set_aux<I>(&mut self, ops: I) -> ClientResult<()>
|
||||||
|
where I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>
|
||||||
|
{
|
||||||
|
self.aux_ops = ops.into_iter().collect();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Block, S, F, H> StateBackend<H> for OnDemandState<Block, S, F>
|
impl<Block, S, F, H> StateBackend<H> for OnDemandState<Block, S, F>
|
||||||
|
|||||||
@@ -35,11 +35,15 @@ use light::fetcher::{Fetcher, RemoteHeaderRequest};
|
|||||||
/// Light client blockchain storage.
|
/// Light client blockchain storage.
|
||||||
pub trait Storage<Block: BlockT>: BlockchainHeaderBackend<Block> {
|
pub trait Storage<Block: BlockT>: BlockchainHeaderBackend<Block> {
|
||||||
/// Store new header. Should refuse to revert any finalized blocks.
|
/// Store new header. Should refuse to revert any finalized blocks.
|
||||||
|
///
|
||||||
|
/// Takes new authorities, the leaf state of the new block, and
|
||||||
|
/// any auxiliary storage updates to place in the same operation.
|
||||||
fn import_header(
|
fn import_header(
|
||||||
&self,
|
&self,
|
||||||
header: Block::Header,
|
header: Block::Header,
|
||||||
authorities: Option<Vec<AuthorityId>>,
|
authorities: Option<Vec<AuthorityId>>,
|
||||||
state: NewBlockState,
|
state: NewBlockState,
|
||||||
|
aux_ops: Vec<(Vec<u8>, Option<Vec<u8>>)>,
|
||||||
) -> ClientResult<()>;
|
) -> ClientResult<()>;
|
||||||
|
|
||||||
/// Mark historic header as finalized.
|
/// Mark historic header as finalized.
|
||||||
|
|||||||
Reference in New Issue
Block a user