fn process_tx<D: BatchDatabase>(
        &self,
        database: &mut D,
        tx: &Transaction,
        height: Option<u32>,
        timestamp: Option<u64>,
        internal_max_deriv: &mut Option<u32>,
        external_max_deriv: &mut Option<u32>,
    ) -> Result<(), Error> {
        let mut updates = database.begin_batch();

        let mut incoming: u64 = 0;
        let mut outgoing: u64 = 0;

        let mut inputs_sum: u64 = 0;
        let mut outputs_sum: u64 = 0;

        // look for our own inputs
        for (i, input) in tx.input.iter().enumerate() {
            if let Some(previous_output) = database.get_previous_output(&input.previous_output)? {
                inputs_sum += previous_output.value;

                if database.is_mine(&previous_output.script_pubkey)? {
                    outgoing += previous_output.value;

                    debug!("{} input #{} is mine, removing from utxo", tx.txid(), i);
                    updates.del_utxo(&input.previous_output)?;
                }
            }
        }

        for (i, output) in tx.output.iter().enumerate() {
            // to compute the fees later
            outputs_sum += output.value;

            // this output is ours, we have a path to derive it
            if let Some((keychain, child)) =
                database.get_path_from_script_pubkey(&output.script_pubkey)?
            {
                debug!("{} output #{} is mine, adding utxo", tx.txid(), i);
                updates.set_utxo(&LocalUtxo {
                    outpoint: OutPoint::new(tx.txid(), i as u32),
                    txout: output.clone(),
                    keychain,
                })?;
                incoming += output.value;

                if keychain == KeychainKind::Internal
                    && (internal_max_deriv.is_none() || child > internal_max_deriv.unwrap_or(0))
                {
                    *internal_max_deriv = Some(child);
                } else if keychain == KeychainKind::External
                    && (external_max_deriv.is_none() || child > external_max_deriv.unwrap_or(0))
                {
                    *external_max_deriv = Some(child);
                }
            }
        }

        if incoming > 0 || outgoing > 0 {
            let tx = TransactionDetails {
                txid: tx.txid(),
                transaction: Some(tx.clone()),
                received: incoming,
                sent: outgoing,
                confirmation_time: ConfirmationTime::new(height, timestamp),
                verified: height.is_some(),
                fee: Some(inputs_sum.saturating_sub(outputs_sum)),
            };

            info!("Saving tx {}", tx.txid);
            updates.set_tx(&tx)?;
        }

        database.commit_batch(updates)?;

        Ok(())
    }
/// Process a transaction by looking for inputs that spend from a UTXO in the database or
/// outputs that send funds to a know script_pubkey.
Process a transaction by looking for inputs that spend from a UTXO in the database or
outputs that send funds to a know script_pubkey.
Process a transaction by looking for inputs that spend from a UTXO in the database or outputs that send funds to a know script_pubkey .
fn process_tx<D: BatchDatabase>(
        &self,
        database: &mut D,
        tx: &Transaction,
        height: Option<u32>,
        timestamp: Option<u64>,
        internal_max_deriv: &mut Option<u32>,
        external_max_deriv: &mut Option<u32>,
    ) -> Result<(), Error> {
        let mut updates = database.begin_batch();
        let mut incoming: u64 = 0;
        let mut outgoing: u64 = 0;
        let mut inputs_sum: u64 = 0;
        let mut outputs_sum: u64 = 0;
        // look for our own inputs
        for (i, input) in tx.input.iter().enumerate() {
            if let Some(previous_output) = database.get_previous_output(&input.previous_output)? {
                inputs_sum += previous_output.value;
                if database.is_mine(&previous_output.script_pubkey)? {
                    outgoing += previous_output.value;
                    debug!("{} input #{} is mine, removing from utxo", tx.txid(), i);
                    updates.del_utxo(&input.previous_output)?;
                }
            }
        }
        for (i, output) in tx.output.iter().enumerate() {
            // to compute the fees later
            outputs_sum += output.value;
            // this output is ours, we have a path to derive it
            if let Some((keychain, child)) =
                database.get_path_from_script_pubkey(&output.script_pubkey)?
            {
                debug!("{} output #{} is mine, adding utxo", tx.txid(), i);
                updates.set_utxo(&LocalUtxo {
                    outpoint: OutPoint::new(tx.txid(), i as u32),
                    txout: output.clone(),
                    keychain,
                })?;
                incoming += output.value;
                if keychain == KeychainKind::Internal
                    && (internal_max_deriv.is_none() || child > internal_max_deriv.unwrap_or(0))
                {
                    *internal_max_deriv = Some(child);
                } else if keychain == KeychainKind::External
                    && (external_max_deriv.is_none() || child > external_max_deriv.unwrap_or(0))
                {
                    *external_max_deriv = Some(child);
                }
            }
        }
        if incoming > 0 || outgoing > 0 {
            let tx = TransactionDetails {
                txid: tx.txid(),
                transaction: Some(tx.clone()),
                received: incoming,
                sent: outgoing,
                confirmation_time: ConfirmationTime::new(height, timestamp),
                verified: height.is_some(),
                fee: Some(inputs_sum.saturating_sub(outputs_sum)),
            };
            info!("Saving tx {}", tx.txid);
            updates.set_tx(&tx)?;
        }
        database.commit_batch(updates)?;
        Ok(())
    }
fn process_tx < D : BatchDatabase > ( & self , database : & mut D , tx : & Transaction , height : Option < u32 > , timestamp : Option < u64 > , internal_max_deriv : & mut Option < u32 > , external_max_deriv : & mut Option < u32 > , ) -> Result < ( ) , Error > { let mut updates = database . begin_batch ( ) ; let mut incoming : u64 = 0 ; let mut outgoing : u64 = 0 ; let mut inputs_sum : u64 = 0 ; let mut outputs_sum : u64 = 0 ; // look for our own inputs for ( i , input ) in tx . input . iter ( ) . enumerate ( ) { if let Some ( previous_output ) = database . get_previous_output ( & input . previous_output ) ? { inputs_sum += previous_output . value ; if database . is_mine ( & previous_output . script_pubkey ) ? { outgoing += previous_output . value ; debug ! ( " " tx txid ( ) i ) ; updates . del_utxo ( & input . previous_output ) ? ; } } } for ( i , output ) in tx . output . iter ( ) . enumerate ( ) { // to compute the fees later outputs_sum += output . value ; // this output is ours, we have a path to derive it if let Some ( ( keychain , child ) ) = database . get_path_from_script_pubkey ( & output . script_pubkey ) ? { debug ! ( " " tx txid ( ) i ) ; updates . set_utxo ( & LocalUtxo { outpoint : OutPoint :: new ( tx . txid ( ) , i as u32 ) , txout : output . clone ( ) , keychain , } ) ? ; incoming += output . value ; if keychain == KeychainKind :: Internal && ( internal_max_deriv . is_none ( ) || child > internal_max_deriv . unwrap_or ( 0 ) ) { * internal_max_deriv = Some ( child ) ; } else if keychain == KeychainKind :: External && ( external_max_deriv . is_none ( ) || child > external_max_deriv . unwrap_or ( 0 ) ) { * external_max_deriv = Some ( child ) ; } } } if incoming > 0 || outgoing > 0 { let tx = TransactionDetails { txid : tx . txid ( ) , transaction : Some ( tx . clone ( ) ) , received : incoming , sent : outgoing , confirmation_time : ConfirmationTime :: new ( height , timestamp ) , verified : height . is_some ( ) , fee : Some ( inputs_sum . saturating_sub ( outputs_sum ) ) , } ; info ! ( " " tx txid ) ; updates . set_tx ( & tx ) ? ; } database . commit_batch ( updates ) ? ; Ok ( ( ) ) }
Process a transaction by looking for inputs that spend from a UTXO in the database or
outputs that send funds to a know script_pubkey.
Process a transaction by looking for inputs that spend from a UTXO in the database or outputs that send funds to a know script_pubkey .
&self
database
&mut D
tx
&Transaction
height
Option<u32>
timestamp
Option<u64>
internal_max_deriv
&mut Option<u32>
external_max_deriv
&mut Option<u32>
&self
database
&mut D
tx
&Transaction
height
Option<u32>
timestamp
Option<u64>
internal_max_deriv
&mut Option<u32>
external_max_deriv
&mut Option<u32>