Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add serde derives for CacheDB under "serde" flag #911

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/revm/src/db/emptydb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub type EmptyDB = EmptyDBTyped<Infallible>;
/// 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<E> {
_phantom: PhantomData<E>,
}
Expand Down
27 changes: 27 additions & 0 deletions crates/revm/src/db/in_memory_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub type InMemoryDB = CacheDB<EmptyDB>;
/// 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<ExtDB: DatabaseRef> {
/// 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`.
Expand Down Expand Up @@ -288,6 +289,7 @@ impl<ExtDB: DatabaseRef> DatabaseRef for CacheDB<ExtDB> {
}

#[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.
Expand Down Expand Up @@ -330,6 +332,7 @@ impl From<AccountInfo> 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.
Expand Down Expand Up @@ -453,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<EmptyDB> = serde_json::from_str(&serialized).unwrap();

assert!(deserialized.accounts.get(&account).is_some());
assert_eq!(
deserialized.accounts.get(&account).unwrap().info.nonce,
nonce
);
}
}
Loading