Skip to content

Transaction Index #3

@cryptoquick

Description

@cryptoquick

#2 gets us much closer, but it's undercounting coins because it's not using the original improved algorithm:

    // For each block, account for P2PK coins
    for height in resume_height..tip_height {
        let hash = rpc.get_block_hash(height)?;
        let block = rpc.get_block(&hash)?;

        // Account for the new P2PK coins
        for tx in block.txdata.iter() {
            for outpoint in &tx.output {
                if outpoint.script_pubkey.is_p2pk() {
                    p2pk_addresses += 1;
                    p2pk_coins += outpoint.value.to_btc();
                }
            }

            // If the transaction is not coinbase, account for the spent coins
            if !tx.is_coinbase() {
                for txin in &tx.input {
                    let txid = txin.previous_output.txid;
                    let vout = txin.previous_output.vout;
                    let prev_tx = rpc.get_raw_transaction(&txid, None)?;

                    // Check if the specific output being spent was P2PK
                    if let Some(prev_output) = prev_tx.output.get(vout as usize) {
                        if prev_output.script_pubkey.is_p2pk() {
                            p2pk_addresses -= 1;
                            p2pk_coins -= prev_output.value.to_btc();
                        }
                    }
                }
            }
        }
    }

For this, we need to create a BTreeMap<[u8; 32], bool>, with the bool as to whether the prevout is a P2PK spend script. This is more memory efficient than storing the entire spend script. Ideally we put only txs that contain a P2PK spend script into the BTreeMap.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions