diff --git a/substrate/core/client/db/src/lib.rs b/substrate/core/client/db/src/lib.rs index 36ab5f4b2a58c0748ab57a30c2340ca437b3a9b9..2d1fe3fd7cac9de629be0d1a689353ff6952976f 100644 --- a/substrate/core/client/db/src/lib.rs +++ b/substrate/core/client/db/src/lib.rs @@ -277,6 +277,18 @@ pub struct BlockImportOperation<Block: BlockT, H: Hasher> { updates: MemoryDB<H>, changes_trie_updates: MemoryDB<H>, pending_block: Option<PendingBlock<Block>>, + aux_ops: Vec<(Vec<u8>, Option<Vec<u8>>)>, +} + +impl<Block: BlockT, H: Hasher> BlockImportOperation<Block, H> { + fn apply_aux(&mut self, transaction: &mut DBTransaction) { + for (key, maybe_val) in self.aux_ops.drain(..) { + match maybe_val { + Some(val) => transaction.put_vec(columns::AUX, &key, val), + None => transaction.delete(columns::AUX, &key), + } + } + } } impl<Block> client::backend::BlockImportOperation<Block, Blake2Hasher> @@ -326,6 +338,13 @@ where Block: BlockT, self.changes_trie_updates = update; Ok(()) } + + fn set_aux<I>(&mut self, ops: I) -> Result<(), client::error::Error> + where I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)> + { + self.aux_ops = ops.into_iter().collect(); + Ok(()) + } } struct StorageDb<Block: BlockT> { @@ -581,6 +600,7 @@ impl<Block> client::backend::Backend<Block, Blake2Hasher> for Backend<Block> whe old_state: state, updates: MemoryDB::default(), changes_trie_updates: MemoryDB::default(), + aux_ops: Vec::new(), }) } @@ -588,6 +608,7 @@ impl<Block> client::backend::Backend<Block, Blake2Hasher> for Backend<Block> whe -> Result<(), client::error::Error> { let mut transaction = DBTransaction::new(); + operation.apply_aux(&mut transaction); if let Some(pending_block) = operation.pending_block { let hash = pending_block.header.hash(); diff --git a/substrate/core/client/db/src/light.rs b/substrate/core/client/db/src/light.rs index f336df4d28effc231b3cf37504c71a13f8b7a250..eed75f25f8f0d1de42eb974e0366426f3aa72854 100644 --- a/substrate/core/client/db/src/light.rs +++ b/substrate/core/client/db/src/light.rs @@ -43,6 +43,7 @@ pub(crate) mod columns { pub const HEADER: Option<u32> = Some(2); pub const CACHE: Option<u32> = Some(3); pub const CHT: Option<u32> = Some(4); + pub const AUX: Option<u32> = Some(5); } /// Light blockchain storage. Stores most recent headers + CHTs for older headers. @@ -238,6 +239,7 @@ impl<Block> LightBlockchainStorage<Block> for LightStorage<Block> header: Block::Header, authorities: Option<Vec<AuthorityId>>, leaf_state: NewBlockState, + aux_ops: Vec<(Vec<u8>, Option<Vec<u8>>)>, ) -> ClientResult<()> { let mut transaction = DBTransaction::new(); @@ -253,6 +255,13 @@ impl<Block> LightBlockchainStorage<Block> for LightStorage<Block> ::utils::number_and_hash_to_lookup_key(number, hash) }; + for (key, maybe_val) in aux_ops { + match maybe_val { + Some(val) => transaction.put_vec(columns::AUX, &key, val), + None => transaction.delete(columns::AUX, &key), + } + } + if leaf_state.is_best() { // handle reorg. { @@ -427,7 +436,7 @@ pub(crate) mod tests { ) -> Hash { let header = prepare_header(parent, number, extrinsics_root); let hash = header.hash(); - db.import_header(header, authorities, NewBlockState::Best).unwrap(); + db.import_header(header, authorities, NewBlockState::Best, Vec::new()).unwrap(); hash } @@ -439,7 +448,7 @@ pub(crate) mod tests { ) -> Hash { let header = prepare_header(parent, number, Default::default()); let hash = header.hash(); - db.import_header(header, authorities, NewBlockState::Best).unwrap(); + db.import_header(header, authorities, NewBlockState::Best, Vec::new()).unwrap(); hash } @@ -451,7 +460,7 @@ pub(crate) mod tests { ) -> Hash { let header = prepare_header(parent, number, Default::default()); let hash = header.hash(); - db.import_header(header, authorities, NewBlockState::Final).unwrap(); + db.import_header(header, authorities, NewBlockState::Final, Vec::new()).unwrap(); hash } @@ -463,7 +472,7 @@ pub(crate) mod tests { ) -> Hash { let header = prepare_header(parent, number, Default::default()); let hash = header.hash(); - db.import_header(header, authorities, NewBlockState::Normal).unwrap(); + db.import_header(header, authorities, NewBlockState::Normal, Vec::new()).unwrap(); hash }