--- /dev/null +++ b/pallets/nft/src/eth/log.rs @@ -0,0 +1,38 @@ +use pallet_evm::PrecompileLog; +use sp_std::cell::RefCell; +use sp_std::vec::Vec; +use sp_core::{H160, H256}; + +#[derive(Default)] +pub struct LogRecorder(RefCell, Vec)>>); + +impl LogRecorder { + pub fn log(&self, topics: Vec, data: super::abi::AbiWriter) { + self.0.borrow_mut().push((topics, data.finish())); + } + fn retrieve_logs(self) -> Vec<(Vec, Vec)> { + self.0.replace(Vec::new()) + } + pub fn retrieve_logs_for_contract(self, contract: H160) -> Vec { + // TODO: Remove reallocation + self.retrieve_logs().into_iter() + .map(|(topics, data)| PrecompileLog(contract.clone(), topics, data)) + .collect() + } +} +impl Drop for LogRecorder { + fn drop(&mut self) { + #[cfg(feature = "std")] + { + // In debug mode, log recorder panics if dropped with logs left + let logs = self.0.borrow(); + if !logs.is_empty() { + eprintln!("Logs lost:"); + for line in logs.iter() { + eprintln!("{:?} {:?}", line.0, line.1); + } + panic!(); + } + } + } +} --- a/pallets/nft/src/eth/mod.rs +++ b/pallets/nft/src/eth/mod.rs @@ -2,4 +2,5 @@ use account::CrossAccountId; pub mod abi; use abi::{AbiReader, AbiWriter}; +pub mod log;