SurmountSystems / gabriel

'While I, Daniel, was watching the vision and trying to understand it, there before me stood one who looked like a man. And I heard a man’s voice from the Ulai calling, “Gabriel, tell this man the meaning of the vision.”'
6 stars 3 forks source link

Transaction Index #3

Open cryptoquick opened 1 month ago

cryptoquick commented 1 month ago

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.