pub fn apply_state_update(&self, block_number: u32) -> QueryResult<()> {
        log::info!("Applying state update for block: {}", block_number);
        self.0.conn().transaction(|| {
            // 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).
            let account_balance_diff = account_balance_updates::table
                .filter(account_balance_updates::block_number.eq(&(i64::from(block_number))))
                .load::<StorageAccountUpdate>(self.0.conn())?;

            let account_creation_diff = account_creates::table
                .filter(account_creates::block_number.eq(&(i64::from(block_number))))
                .load::<StorageAccountCreation>(self.0.conn())?;

            let account_change_pubkey_diff = account_pubkey_updates::table
                .filter(account_pubkey_updates::block_number.eq(&(i64::from(block_number))))
                .load::<StorageAccountPubkeyUpdate>(self.0.conn())?;

            // Collect the updates into one list of `StorageAccountDiff`.
            let account_updates: Vec<StorageAccountDiff> = {
                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_change_pubkey_diff
                        .into_iter()
                        .map(StorageAccountDiff::from),
                );
                account_diff.sort_by(StorageAccountDiff::cmp_order);
                account_diff
            };

            log::debug!("Sorted account update list: {:?}", account_updates);

            // Then go through the collected list of changes and apply them by one.
            for acc_update in account_updates.into_iter() {
                match acc_update {
                    StorageAccountDiff::BalanceUpdate(upd) => {
                        let storage_balance = StorageBalance {
                            coin_id: upd.coin_id,
                            account_id: upd.account_id,
                            balance: upd.new_balance.clone(),
                        };
                        insert_into(balances::table)
                            .values(&storage_balance)
                            .on_conflict((balances::coin_id, balances::account_id))
                            .do_update()
                            .set(balances::balance.eq(upd.new_balance))
                            .execute(self.0.conn())?;

                        update(accounts::table.filter(accounts::id.eq(upd.account_id)))
                            .set((
                                accounts::last_block.eq(upd.block_number),
                                accounts::nonce.eq(upd.new_nonce),
                            ))
                            .execute(self.0.conn())?;
                    }

                    StorageAccountDiff::Create(upd) => {
                        let storage_account = StorageAccount {
                            id: upd.account_id,
                            last_block: upd.block_number,
                            nonce: upd.nonce,
                            address: upd.address,
                            pubkey_hash: PubKeyHash::default().data.to_vec(),
                        };
                        insert_into(accounts::table)
                            .values(&storage_account)
                            .execute(self.0.conn())?;
                    }
                    StorageAccountDiff::Delete(upd) => {
                        delete(accounts::table.filter(accounts::id.eq(upd.account_id)))
                            .execute(self.0.conn())?;
                    }
                    StorageAccountDiff::ChangePubKey(upd) => {
                        update(accounts::table.filter(accounts::id.eq(upd.account_id)))
                            .set((
                                accounts::last_block.eq(upd.block_number),
                                accounts::nonce.eq(upd.new_nonce),
                                accounts::pubkey_hash.eq(upd.new_pubkey_hash),
                            ))
                            .execute(self.0.conn())?;
                    }
                }
            }

            Ok(())
        })
    }
/// Applies the previously stored list of account changes to the stored state.
Applies the previously stored list of account changes to the stored state.
Applies the previously stored list of account changes to the stored state .
pub fn apply_state_update(&self, block_number: u32) -> QueryResult<()> {
        log::info!("Applying state update for block: {}", block_number);
        self.0.conn().transaction(|| {
            // 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).
            let account_balance_diff = account_balance_updates::table
                .filter(account_balance_updates::block_number.eq(&(i64::from(block_number))))
                .load::<StorageAccountUpdate>(self.0.conn())?;
            let account_creation_diff = account_creates::table
                .filter(account_creates::block_number.eq(&(i64::from(block_number))))
                .load::<StorageAccountCreation>(self.0.conn())?;
            let account_change_pubkey_diff = account_pubkey_updates::table
                .filter(account_pubkey_updates::block_number.eq(&(i64::from(block_number))))
                .load::<StorageAccountPubkeyUpdate>(self.0.conn())?;
            // Collect the updates into one list of `StorageAccountDiff`.
            let account_updates: Vec<StorageAccountDiff> = {
                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_change_pubkey_diff
                        .into_iter()
                        .map(StorageAccountDiff::from),
                );
                account_diff.sort_by(StorageAccountDiff::cmp_order);
                account_diff
            };
            log::debug!("Sorted account update list: {:?}", account_updates);
            // Then go through the collected list of changes and apply them by one.
            for acc_update in account_updates.into_iter() {
                match acc_update {
                    StorageAccountDiff::BalanceUpdate(upd) => {
                        let storage_balance = StorageBalance {
                            coin_id: upd.coin_id,
                            account_id: upd.account_id,
                            balance: upd.new_balance.clone(),
                        };
                        insert_into(balances::table)
                            .values(&storage_balance)
                            .on_conflict((balances::coin_id, balances::account_id))
                            .do_update()
                            .set(balances::balance.eq(upd.new_balance))
                            .execute(self.0.conn())?;
                        update(accounts::table.filter(accounts::id.eq(upd.account_id)))
                            .set((
                                accounts::last_block.eq(upd.block_number),
                                accounts::nonce.eq(upd.new_nonce),
                            ))
                            .execute(self.0.conn())?;
                    }
                    StorageAccountDiff::Create(upd) => {
                        let storage_account = StorageAccount {
                            id: upd.account_id,
                            last_block: upd.block_number,
                            nonce: upd.nonce,
                            address: upd.address,
                            pubkey_hash: PubKeyHash::default().data.to_vec(),
                        };
                        insert_into(accounts::table)
                            .values(&storage_account)
                            .execute(self.0.conn())?;
                    }
                    StorageAccountDiff::Delete(upd) => {
                        delete(accounts::table.filter(accounts::id.eq(upd.account_id)))
                            .execute(self.0.conn())?;
                    }
                    StorageAccountDiff::ChangePubKey(upd) => {
                        update(accounts::table.filter(accounts::id.eq(upd.account_id)))
                            .set((
                                accounts::last_block.eq(upd.block_number),
                                accounts::nonce.eq(upd.new_nonce),
                                accounts::pubkey_hash.eq(upd.new_pubkey_hash),
                            ))
                            .execute(self.0.conn())?;
                    }
                }
            }
            Ok(())
        })
    }
pub fn apply_state_update ( & self , block_number : u32 ) -> QueryResult < ( ) > { log :: info ! ( " " block_number ) ; self . 0 . conn ( ) . transaction ( | | { // 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). let account_balance_diff = account_balance_updates :: table . filter ( account_balance_updates :: block_number . eq ( & ( i64 :: from ( block_number ) ) ) ) . load :: < StorageAccountUpdate > ( self . 0 . conn ( ) ) ? ; let account_creation_diff = account_creates :: table . filter ( account_creates :: block_number . eq ( & ( i64 :: from ( block_number ) ) ) ) . load :: < StorageAccountCreation > ( self . 0 . conn ( ) ) ? ; let account_change_pubkey_diff = account_pubkey_updates :: table . filter ( account_pubkey_updates :: block_number . eq ( & ( i64 :: from ( block_number ) ) ) ) . load :: < StorageAccountPubkeyUpdate > ( self . 0 . conn ( ) ) ? ; // Collect the updates into one list of `StorageAccountDiff`. let account_updates : Vec < StorageAccountDiff > = { 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_change_pubkey_diff . into_iter ( ) . map ( StorageAccountDiff :: from ) , ) ; account_diff . sort_by ( StorageAccountDiff :: cmp_order ) ; account_diff } ; log :: debug ! ( " " account_updates ) ; // Then go through the collected list of changes and apply them by one. for acc_update in account_updates . into_iter ( ) { match acc_update { StorageAccountDiff :: BalanceUpdate ( upd ) => { let storage_balance = StorageBalance { coin_id : upd . coin_id , account_id : upd . account_id , balance : upd . new_balance . clone ( ) , } ; insert_into ( balances :: table ) . values ( & storage_balance ) . on_conflict ( ( balances :: coin_id , balances :: account_id ) ) . do_update ( ) . set ( balances :: balance . eq ( upd . new_balance ) ) . execute ( self . 0 . conn ( ) ) ? ; update ( accounts :: table . filter ( accounts :: id . eq ( upd . account_id ) ) ) . set ( ( accounts :: last_block . eq ( upd . block_number ) , accounts :: nonce . eq ( upd . new_nonce ) , ) ) . execute ( self . 0 . conn ( ) ) ? ; } StorageAccountDiff :: Create ( upd ) => { let storage_account = StorageAccount { id : upd . account_id , last_block : upd . block_number , nonce : upd . nonce , address : upd . address , pubkey_hash : PubKeyHash :: default ( ) . data . to_vec ( ) , } ; insert_into ( accounts :: table ) . values ( & storage_account ) . execute ( self . 0 . conn ( ) ) ? ; } StorageAccountDiff :: Delete ( upd ) => { delete ( accounts :: table . filter ( accounts :: id . eq ( upd . account_id ) ) ) . execute ( self . 0 . conn ( ) ) ? ; } StorageAccountDiff :: ChangePubKey ( upd ) => { update ( accounts :: table . filter ( accounts :: id . eq ( upd . account_id ) ) ) . set ( ( accounts :: last_block . eq ( upd . block_number ) , accounts :: nonce . eq ( upd . new_nonce ) , accounts :: pubkey_hash . eq ( upd . new_pubkey_hash ) , ) ) . execute ( self . 0 . conn ( ) ) ? ; } } } Ok ( ( ) ) } ) }
Applies the previously stored list of account changes to the stored state.
Applies the previously stored list of account changes to the stored state .
&self
block_number
u32