diff --git a/substrate/core/client/src/light/backend.rs b/substrate/core/client/src/light/backend.rs index fcc58e0ad794223b36ac03b25c7b66802b94a0b5..ca5c84cae2bc62a435e9ad9dd90d829a3a525db3 100644 --- a/substrate/core/client/src/light/backend.rs +++ b/substrate/core/client/src/light/backend.rs @@ -130,6 +130,16 @@ impl<S, F, Block, H> ClientBackend<Block, H> for Backend<S, F> where operation.leaf_state, operation.aux_ops, )?; + } else { + for (key, maybe_val) in operation.aux_ops { + match maybe_val { + Some(val) => self.blockchain.storage().insert_aux( + &[(&key[..], &val[..])], + ::std::iter::empty(), + )?, + None => self.blockchain.storage().insert_aux(::std::iter::empty(), &[&key[..]])?, + } + } } Ok(()) @@ -311,3 +321,21 @@ where None } } + +#[cfg(test)] +mod tests { + use primitives::Blake2Hasher; + use test_client::runtime::Block; + use crate::light::blockchain::tests::{DummyBlockchain, DummyStorage}; + use super::*; + + #[test] + fn light_aux_store_is_updated_via_non_importing_op() { + let backend = Backend::new(Arc::new(DummyBlockchain::new(DummyStorage::new()))); + let mut op = ClientBackend::<Block, Blake2Hasher>::begin_operation(&backend).unwrap(); + BlockImportOperation::<Block, Blake2Hasher>::insert_aux(&mut op, vec![(vec![1], Some(vec![2]))]).unwrap(); + ClientBackend::<Block, Blake2Hasher>::commit_operation(&backend, op).unwrap(); + + assert_eq!(AuxStore::get_aux(&backend, &[1]).unwrap(), Some(vec![2])); + } +} diff --git a/substrate/core/client/src/light/blockchain.rs b/substrate/core/client/src/light/blockchain.rs index 82fff6be6f77f295d13db71baad68c0581028652..6cad6c684176d043149821be1397317c3965b3b0 100644 --- a/substrate/core/client/src/light/blockchain.rs +++ b/substrate/core/client/src/light/blockchain.rs @@ -174,12 +174,14 @@ pub mod tests { pub struct DummyStorage { pub changes_tries_cht_roots: HashMap<u64, Hash>, + pub aux_store: Mutex<HashMap<Vec<u8>, Vec<u8>>>, } impl DummyStorage { pub fn new() -> Self { DummyStorage { changes_tries_cht_roots: HashMap::new(), + aux_store: Mutex::new(HashMap::new()), } } } @@ -213,12 +215,15 @@ pub mod tests { 'c: 'a, I: IntoIterator<Item=&'a(&'c [u8], &'c [u8])>, D: IntoIterator<Item=&'a &'b [u8]>, - >(&self, _insert: I, _delete: D) -> ClientResult<()> { - Err(ClientErrorKind::Backend("Test error".into()).into()) + >(&self, insert: I, _delete: D) -> ClientResult<()> { + for (k, v) in insert.into_iter() { + self.aux_store.lock().insert(k.to_vec(), v.to_vec()); + } + Ok(()) } - fn get_aux(&self, _key: &[u8]) -> ClientResult<Option<Vec<u8>>> { - Err(ClientErrorKind::Backend("Test error".into()).into()) + fn get_aux(&self, key: &[u8]) -> ClientResult<Option<Vec<u8>>> { + Ok(self.aux_store.lock().get(key).cloned()) } }