From 4056b15882baac15746563958a60531636977f94 Mon Sep 17 00:00:00 2001 From: joshieDo <93316087+joshieDo@users.noreply.github.com> Date: Fri, 12 May 2023 18:29:56 +0100 Subject: [PATCH] fix: remove `Account` length from compact on `AccountBeforeTx` (#2622) --- .../storage/db/src/tables/models/accounts.rs | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/crates/storage/db/src/tables/models/accounts.rs b/crates/storage/db/src/tables/models/accounts.rs index 2141ea60e1..8389c97257 100644 --- a/crates/storage/db/src/tables/models/accounts.rs +++ b/crates/storage/db/src/tables/models/accounts.rs @@ -7,13 +7,15 @@ use crate::{ table::{Decode, Encode}, Error, }; -use reth_codecs::Compact; +use bytes::Buf; +use reth_codecs::{derive_arbitrary, Compact}; use reth_primitives::{Account, Address, BlockNumber}; use serde::{Deserialize, Serialize}; /// Account as it is saved inside [`AccountChangeSet`][crate::tables::AccountChangeSet]. /// /// [`Address`] is the subkey. +#[derive_arbitrary(compact)] #[derive(Debug, Default, Clone, Eq, PartialEq, Serialize)] pub struct AccountBeforeTx { /// Address for the account. Acts as `DupSort::SubKey`. @@ -32,16 +34,29 @@ impl Compact for AccountBeforeTx { { // for now put full bytes and later compress it. buf.put_slice(&self.address.to_fixed_bytes()[..]); - self.info.to_compact(buf) + 32 + + let mut acc_len = 0; + if let Some(account) = self.info { + acc_len = account.to_compact(buf); + } + acc_len + 20 } - fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) + fn from_compact(mut buf: &[u8], len: usize) -> (Self, &[u8]) where Self: Sized, { let address = Address::from_slice(&buf[..20]); - let (info, out) = >::from_compact(&buf[20..], len - 20); - (Self { address, info }, out) + buf.advance(20); + + let mut info = None; + if len - 20 > 0 { + let (acc, advanced_buf) = Account::from_compact(buf, len - 20); + buf = advanced_buf; + info = Some(acc); + } + + (Self { address, info }, buf) } }