ProofRecorder: Implement transactional support (#13769)

* TrieRecorder: Start adding support for transactions

* Adds `transactions` functions and some test

* More tests

* Docs

* Ensure that we rollback failed transactions in the storage proof

* FMT

* Update primitives/trie/src/recorder.rs

Co-authored-by: Dmitry Markin <dmitry@markin.tech>

* Review comments

* Update primitives/trie/src/recorder.rs

Co-authored-by: Sebastian Kunert <skunert49@gmail.com>

* ".git/.scripts/commands/fmt/fmt.sh"

* For the holy clippy!

* Update primitives/trie/src/recorder.rs

Co-authored-by: Anton <anton.kalyaev@gmail.com>

---------

Co-authored-by: Dmitry Markin <dmitry@markin.tech>
Co-authored-by: Sebastian Kunert <skunert49@gmail.com>
Co-authored-by: command-bot <>
Co-authored-by: Anton <anton.kalyaev@gmail.com>
This commit is contained in:
Bastian Köcher
2023-04-05 16:43:17 +02:00
committed by GitHub
parent 06f84830b6
commit fa8e323231
5 changed files with 573 additions and 40 deletions
+11 -1
View File
@@ -164,13 +164,21 @@ pub enum Extrinsic {
OffchainIndexSet(Vec<u8>, Vec<u8>),
OffchainIndexClear(Vec<u8>),
Store(Vec<u8>),
/// Read X times from the state some data and then panic!
///
/// Returns `Ok` if it didn't read anything.
ReadAndPanic(u32),
/// Read X times from the state some data.
///
/// Panics if it can not read `X` times.
Read(u32),
}
#[cfg(feature = "std")]
impl serde::Serialize for Extrinsic {
fn serialize<S>(&self, seq: S) -> Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
S: serde::Serializer,
{
self.using_encoded(|bytes| seq.serialize_bytes(bytes))
}
@@ -210,6 +218,8 @@ impl BlindCheckable for Extrinsic {
Extrinsic::OffchainIndexSet(key, value) => Ok(Extrinsic::OffchainIndexSet(key, value)),
Extrinsic::OffchainIndexClear(key) => Ok(Extrinsic::OffchainIndexClear(key)),
Extrinsic::Store(data) => Ok(Extrinsic::Store(data)),
Extrinsic::ReadAndPanic(i) => Ok(Extrinsic::ReadAndPanic(i)),
Extrinsic::Read(i) => Ok(Extrinsic::Read(i)),
}
}
}
@@ -275,6 +275,32 @@ fn execute_transaction_backend(utx: &Extrinsic, extrinsic_index: u32) -> ApplyEx
Ok(Ok(()))
},
Extrinsic::Store(data) => execute_store(data.clone()),
Extrinsic::ReadAndPanic(i) => execute_read(*i, true),
Extrinsic::Read(i) => execute_read(*i, false),
}
}
fn execute_read(read: u32, panic_at_end: bool) -> ApplyExtrinsicResult {
let mut next_key = vec![];
for _ in 0..(read as usize) {
if let Some(next) = sp_io::storage::next_key(&next_key) {
// Read the value
sp_io::storage::get(&next);
next_key = next;
} else {
if panic_at_end {
return Ok(Ok(()))
} else {
panic!("Could not read {read} times from the state");
}
}
}
if panic_at_end {
panic!("BYE")
} else {
Ok(Ok(()))
}
}