pub fn load_state_diff(
        &self,
        from_block: u32,
        to_block: Option<u32>,
    ) -> QueryResult<Option<(u32, AccountUpdates)>> {
        self.0.conn().transaction(|| {
            // Resolve the end of range: if it was not provided, we have to fetch
            // the latest committed block.
            let to_block_resolved = if let Some(to_block) = to_block {
                to_block
            } else {
                let last_block = blocks::table
                    .select(max(blocks::number))
                    .first::<Option<i64>>(self.0.conn())?;
                last_block.map(|n| n as u32).unwrap_or(0)
            };

            // Determine the order: are we going forward or backwards.
            // Depending on that, determine the start/end of the block range as well.
            let (time_forward, start_block, end_block) = (
                from_block <= to_block_resolved,
                cmp::min(from_block, to_block_resolved),
                cmp::max(from_block, to_block_resolved),
            );

            // Collect the stored updates. This includes collecting entries from three tables:
            // `account_creates` (for creating/removing accounts),
            // `account_balance_updates` (for changing the balance of accounts),
            // `account_pubkey_updates` (for changing the accounts public keys).
            // The updates are loaded for the given blocks range.
            let account_balance_diff = account_balance_updates::table
                .filter(
                    account_balance_updates::block_number
                        .gt(&(i64::from(start_block)))
                        .and(account_balance_updates::block_number.le(&(i64::from(end_block)))),
                )
                .load::<StorageAccountUpdate>(self.0.conn())?;
            let account_creation_diff = account_creates::table
                .filter(
                    account_creates::block_number
                        .gt(&(i64::from(start_block)))
                        .and(account_creates::block_number.le(&(i64::from(end_block)))),
                )
                .load::<StorageAccountCreation>(self.0.conn())?;
            let account_pubkey_diff = account_pubkey_updates::table
                .filter(
                    account_pubkey_updates::block_number
                        .gt(&(i64::from(start_block)))
                        .and(account_pubkey_updates::block_number.le(&(i64::from(end_block)))),
                )
                .load::<StorageAccountPubkeyUpdate>(self.0.conn())?;

            log::debug!(
                "Loading state diff: forward: {}, start_block: {}, end_block: {}, unbounded: {}",
                time_forward,
                start_block,
                end_block,
                to_block.is_none()
            );
            log::debug!("Loaded account balance diff: {:#?}", account_balance_diff);
            log::debug!("Loaded account creation diff: {:#?}", account_creation_diff);

            // Fold the updates into one list and determine the actual last block
            // (since user-provided one may not exist yet).
            let (mut account_updates, last_block) = {
                let mut account_diff = Vec::new();
                account_diff.extend(
                    account_balance_diff
                        .into_iter()
                        .map(StorageAccountDiff::from),
                );
                account_diff.extend(
                    account_creation_diff
                        .into_iter()
                        .map(StorageAccountDiff::from),
                );
                account_diff.extend(
                    account_pubkey_diff
                        .into_iter()
                        .map(StorageAccountDiff::from),
                );
                let last_block = account_diff
                    .iter()
                    .map(|acc| acc.block_number())
                    .max()
                    .unwrap_or(0);
                account_diff.sort_by(StorageAccountDiff::cmp_order);
                (
                    account_diff
                        .into_iter()
                        .map(|d| d.into())
                        .collect::<AccountUpdates>(),
                    last_block as u32,
                )
            };

            // Reverse the blocks order if needed.
            if !time_forward {
                reverse_updates(&mut account_updates);
            }

            // Determine the block number which state will be obtained after
            // applying the changes.
            let block_after_updates = if time_forward {
                last_block
            } else {
                start_block
            };

            // We don't want to return an empty list to avoid the confusion, so return
            // `None` if there are no changes.
            if !account_updates.is_empty() {
                Ok(Some((block_after_updates, account_updates)))
            } else {
                Ok(None)
            }
        })
    }
/// Returns the list of updates, and the block number such that if we apply
/// these updates to the state of the block #(from_block), we will obtain state of the block
/// #(returned block number).
/// Returned block number is either `to_block`, latest committed block before `to_block`.
/// If `to_block` is `None`, then it will be assumed to be the number of the latest committed
/// block.
Returns the list of updates, and the block number such that if we apply
these updates to the state of the block #(from_block), we will obtain state of the block
Returns the list of updates and the block number such that if we apply these updates to the state of the block # ( from_block ) we will obtain state of the block
pub fn load_state_diff(
        &self,
        from_block: u32,
        to_block: Option<u32>,
    ) -> QueryResult<Option<(u32, AccountUpdates)>> {
        self.0.conn().transaction(|| {
            // Resolve the end of range: if it was not provided, we have to fetch
            // the latest committed block.
            let to_block_resolved = if let Some(to_block) = to_block {
                to_block
            } else {
                let last_block = blocks::table
                    .select(max(blocks::number))
                    .first::<Option<i64>>(self.0.conn())?;
                last_block.map(|n| n as u32).unwrap_or(0)
            };
            // Determine the order: are we going forward or backwards.
            // Depending on that, determine the start/end of the block range as well.
            let (time_forward, start_block, end_block) = (
                from_block <= to_block_resolved,
                cmp::min(from_block, to_block_resolved),
                cmp::max(from_block, to_block_resolved),
            );
            // Collect the stored updates. This includes collecting entries from three tables:
            // `account_creates` (for creating/removing accounts),
            // `account_balance_updates` (for changing the balance of accounts),
            // `account_pubkey_updates` (for changing the accounts public keys).
            // The updates are loaded for the given blocks range.
            let account_balance_diff = account_balance_updates::table
                .filter(
                    account_balance_updates::block_number
                        .gt(&(i64::from(start_block)))
                        .and(account_balance_updates::block_number.le(&(i64::from(end_block)))),
                )
                .load::<StorageAccountUpdate>(self.0.conn())?;
            let account_creation_diff = account_creates::table
                .filter(
                    account_creates::block_number
                        .gt(&(i64::from(start_block)))
                        .and(account_creates::block_number.le(&(i64::from(end_block)))),
                )
                .load::<StorageAccountCreation>(self.0.conn())?;
            let account_pubkey_diff = account_pubkey_updates::table
                .filter(
                    account_pubkey_updates::block_number
                        .gt(&(i64::from(start_block)))
                        .and(account_pubkey_updates::block_number.le(&(i64::from(end_block)))),
                )
                .load::<StorageAccountPubkeyUpdate>(self.0.conn())?;
            log::debug!(
                "Loading state diff: forward: {}, start_block: {}, end_block: {}, unbounded: {}",
                time_forward,
                start_block,
                end_block,
                to_block.is_none()
            );
            log::debug!("Loaded account balance diff: {:#?}", account_balance_diff);
            log::debug!("Loaded account creation diff: {:#?}", account_creation_diff);
            // Fold the updates into one list and determine the actual last block
            // (since user-provided one may not exist yet).
            let (mut account_updates, last_block) = {
                let mut account_diff = Vec::new();
                account_diff.extend(
                    account_balance_diff
                        .into_iter()
                        .map(StorageAccountDiff::from),
                );
                account_diff.extend(
                    account_creation_diff
                        .into_iter()
                        .map(StorageAccountDiff::from),
                );
                account_diff.extend(
                    account_pubkey_diff
                        .into_iter()
                        .map(StorageAccountDiff::from),
                );
                let last_block = account_diff
                    .iter()
                    .map(|acc| acc.block_number())
                    .max()
                    .unwrap_or(0);
                account_diff.sort_by(StorageAccountDiff::cmp_order);
                (
                    account_diff
                        .into_iter()
                        .map(|d| d.into())
                        .collect::<AccountUpdates>(),
                    last_block as u32,
                )
            };
            // Reverse the blocks order if needed.
            if !time_forward {
                reverse_updates(&mut account_updates);
            }
            // Determine the block number which state will be obtained after
            // applying the changes.
            let block_after_updates = if time_forward {
                last_block
            } else {
                start_block
            };
            // We don't want to return an empty list to avoid the confusion, so return
            // `None` if there are no changes.
            if !account_updates.is_empty() {
                Ok(Some((block_after_updates, account_updates)))
            } else {
                Ok(None)
            }
        })
    }
pub fn load_state_diff ( & self , from_block : u32 , to_block : Option < u32 > , ) -> QueryResult < Option < ( u32 , AccountUpdates ) > > { self . 0 . conn ( ) . transaction ( | | { // Resolve the end of range: if it was not provided, we have to fetch // the latest committed block. let to_block_resolved = if let Some ( to_block ) = to_block { to_block } else { let last_block = blocks :: table . select ( max ( blocks :: number ) ) . first :: < Option < i64 > > ( self . 0 . conn ( ) ) ? ; last_block . map ( | n | n as u32 ) . unwrap_or ( 0 ) } ; // Determine the order: are we going forward or backwards. // Depending on that, determine the start/end of the block range as well. let ( time_forward , start_block , end_block ) = ( from_block <= to_block_resolved , cmp :: min ( from_block , to_block_resolved ) , cmp :: max ( from_block , to_block_resolved ) , ) ; // Collect the stored updates. This includes collecting entries from three tables: // `account_creates` (for creating/removing accounts), // `account_balance_updates` (for changing the balance of accounts), // `account_pubkey_updates` (for changing the accounts public keys). // The updates are loaded for the given blocks range. let account_balance_diff = account_balance_updates :: table . filter ( account_balance_updates :: block_number . gt ( & ( i64 :: from ( start_block ) ) ) . and ( account_balance_updates :: block_number . le ( & ( i64 :: from ( end_block ) ) ) ) , ) . load :: < StorageAccountUpdate > ( self . 0 . conn ( ) ) ? ; let account_creation_diff = account_creates :: table . filter ( account_creates :: block_number . gt ( & ( i64 :: from ( start_block ) ) ) . and ( account_creates :: block_number . le ( & ( i64 :: from ( end_block ) ) ) ) , ) . load :: < StorageAccountCreation > ( self . 0 . conn ( ) ) ? ; let account_pubkey_diff = account_pubkey_updates :: table . filter ( account_pubkey_updates :: block_number . gt ( & ( i64 :: from ( start_block ) ) ) . and ( account_pubkey_updates :: block_number . le ( & ( i64 :: from ( end_block ) ) ) ) , ) . load :: < StorageAccountPubkeyUpdate > ( self . 0 . conn ( ) ) ? ; log :: debug ! ( " " time_forward start_block end_block to_block is_none ( ) ) ; log :: debug ! ( " " account_balance_diff ) ; log :: debug ! ( " " account_creation_diff ) ; // Fold the updates into one list and determine the actual last block // (since user-provided one may not exist yet). let ( mut account_updates , last_block ) = { let mut account_diff = Vec :: new ( ) ; account_diff . extend ( account_balance_diff . into_iter ( ) . map ( StorageAccountDiff :: from ) , ) ; account_diff . extend ( account_creation_diff . into_iter ( ) . map ( StorageAccountDiff :: from ) , ) ; account_diff . extend ( account_pubkey_diff . into_iter ( ) . map ( StorageAccountDiff :: from ) , ) ; let last_block = account_diff . iter ( ) . map ( | acc | acc . block_number ( ) ) . max ( ) . unwrap_or ( 0 ) ; account_diff . sort_by ( StorageAccountDiff :: cmp_order ) ; ( account_diff . into_iter ( ) . map ( | d | d . into ( ) ) . collect :: < AccountUpdates > ( ) , last_block as u32 , ) } ; // Reverse the blocks order if needed. if ! time_forward { reverse_updates ( & mut account_updates ) ; } // Determine the block number which state will be obtained after // applying the changes. let block_after_updates = if time_forward { last_block } else { start_block } ; // We don't want to return an empty list to avoid the confusion, so return // `None` if there are no changes. if ! account_updates . is_empty ( ) { Ok ( Some ( ( block_after_updates , account_updates ) ) ) } else { Ok ( None ) } } ) }
Returns the list of updates, and the block number such that if we apply
these updates to the state of the block #(from_block), we will obtain state of the block
Returns the list of updates and the block number such that if we apply these updates to the state of the block # ( from_block ) we will obtain state of the block
&self
from_block
u32
to_block
Option<u32>
(returned block number).
Returned block number is either `to_block`, latest committed block before `to_block`.
If `to_block` is `None`, then it will be assumed to be the number of the latest committed
block.
Returned block number is either ` to_block ` latest committed block before ` to_block ` . If ` to_block ` is ` None ` then it will be assumed to be the number of the latest committed block .