From ec2ef8d27314426227d570c689941dcd9aff3f3b Mon Sep 17 00:00:00 2001 From: Colin Roberts Date: Wed, 13 Dec 2023 12:55:31 -0700 Subject: [PATCH 1/3] add: serde derives under flag --- crates/revm/src/db/in_memory_db.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/revm/src/db/in_memory_db.rs b/crates/revm/src/db/in_memory_db.rs index 196f6d5fbf..434c417b75 100644 --- a/crates/revm/src/db/in_memory_db.rs +++ b/crates/revm/src/db/in_memory_db.rs @@ -18,6 +18,7 @@ pub type InMemoryDB = CacheDB; /// whereas contracts are identified by their code hash, and are stored in the `contracts` map. /// The [DbAccount] holds the code hash of the contract, which is used to look up the contract in the `contracts` map. #[derive(Debug, Clone)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct CacheDB { /// Account info where None means it is not existing. Not existing state is needed for Pre TANGERINE forks. /// `code` is always `None`, and bytecode can be found in `contracts`. @@ -288,6 +289,7 @@ impl DatabaseRef for CacheDB { } #[derive(Debug, Clone, Default)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct DbAccount { pub info: AccountInfo, /// If account is selfdestructed or newly created, storage will be cleared. @@ -330,6 +332,7 @@ impl From for DbAccount { } #[derive(Debug, Clone, Default, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum AccountState { /// Before Spurious Dragon hardfork there was a difference between empty and not existing. /// And we are flagging it here. From 8b69a597e39af16730cca35ce175cdd38b163c39 Mon Sep 17 00:00:00 2001 From: Colin Roberts Date: Wed, 13 Dec 2023 13:08:12 -0700 Subject: [PATCH 2/3] add: serde traits to `EmptyDBTyped` --- crates/revm/src/db/emptydb.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/revm/src/db/emptydb.rs b/crates/revm/src/db/emptydb.rs index dbdb630614..2a8b482623 100644 --- a/crates/revm/src/db/emptydb.rs +++ b/crates/revm/src/db/emptydb.rs @@ -10,6 +10,7 @@ pub type EmptyDB = EmptyDBTyped; /// An empty database that always returns default values when queried. /// /// This is generic over a type which is used as the database error type. +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct EmptyDBTyped { _phantom: PhantomData, } From d05de0cd651df987f7dafaae0d60a2dbde7ca67c Mon Sep 17 00:00:00 2001 From: Colin Roberts Date: Wed, 13 Dec 2023 13:08:28 -0700 Subject: [PATCH 3/3] test: `CacheDB` serialization + deserialization --- crates/revm/src/db/in_memory_db.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/crates/revm/src/db/in_memory_db.rs b/crates/revm/src/db/in_memory_db.rs index 434c417b75..d93270b43b 100644 --- a/crates/revm/src/db/in_memory_db.rs +++ b/crates/revm/src/db/in_memory_db.rs @@ -456,4 +456,28 @@ mod tests { assert_eq!(new_state.storage(account, key0), Ok(U256::ZERO)); assert_eq!(new_state.storage(account, key1), Ok(value1)); } + + #[cfg(feature = "serde")] + #[test] + fn test_serialize_deserialize_cachedb() { + let account = Address::with_last_byte(69); + let nonce = 420; + let mut init_state = CacheDB::new(EmptyDB::default()); + init_state.insert_account_info( + account, + AccountInfo { + nonce, + ..Default::default() + }, + ); + + let serialized = serde_json::to_string(&init_state).unwrap(); + let deserialized: CacheDB = serde_json::from_str(&serialized).unwrap(); + + assert!(deserialized.accounts.get(&account).is_some()); + assert_eq!( + deserialized.accounts.get(&account).unwrap().info.nonce, + nonce + ); + } }