Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transaction Index #3

Open
cryptoquick opened this issue Sep 24, 2024 · 0 comments
Open

Transaction Index #3

cryptoquick opened this issue Sep 24, 2024 · 0 comments

Comments

@cryptoquick
Copy link
Member

#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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant