Merge pull request #1665 from mempool/simon/getrawtransaction-recursive-fix
Fix for transaction inputs being fetched recursively
This commit is contained in:
		
						commit
						d84ee4b264
					
				@ -2,7 +2,7 @@ import { IEsploraApi } from './esplora-api.interface';
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
export interface AbstractBitcoinApi {
 | 
					export interface AbstractBitcoinApi {
 | 
				
			||||||
  $getRawMempool(): Promise<IEsploraApi.Transaction['txid'][]>;
 | 
					  $getRawMempool(): Promise<IEsploraApi.Transaction['txid'][]>;
 | 
				
			||||||
  $getRawTransaction(txId: string, skipConversion?: boolean, addPrevout?: boolean, blockHash?: string): Promise<IEsploraApi.Transaction>;
 | 
					  $getRawTransaction(txId: string, skipConversion?: boolean, addPrevout?: boolean): Promise<IEsploraApi.Transaction>;
 | 
				
			||||||
  $getBlockHeightTip(): Promise<number>;
 | 
					  $getBlockHeightTip(): Promise<number>;
 | 
				
			||||||
  $getTxIdsForBlock(hash: string): Promise<string[]>;
 | 
					  $getTxIdsForBlock(hash: string): Promise<string[]>;
 | 
				
			||||||
  $getBlockHash(height: number): Promise<string>;
 | 
					  $getBlockHash(height: number): Promise<string>;
 | 
				
			||||||
 | 
				
			|||||||
@ -14,14 +14,31 @@ class BitcoinApi implements AbstractBitcoinApi {
 | 
				
			|||||||
    this.bitcoindClient = bitcoinClient;
 | 
					    this.bitcoindClient = bitcoinClient;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  $getRawTransaction(txId: string, skipConversion = false, addPrevout = false, blockHash?: string): Promise<IEsploraApi.Transaction> {
 | 
					  static convertBlock(block: IBitcoinApi.Block): IEsploraApi.Block {
 | 
				
			||||||
 | 
					    return {
 | 
				
			||||||
 | 
					      id: block.hash,
 | 
				
			||||||
 | 
					      height: block.height,
 | 
				
			||||||
 | 
					      version: block.version,
 | 
				
			||||||
 | 
					      timestamp: block.time,
 | 
				
			||||||
 | 
					      bits: parseInt(block.bits, 16),
 | 
				
			||||||
 | 
					      nonce: block.nonce,
 | 
				
			||||||
 | 
					      difficulty: block.difficulty,
 | 
				
			||||||
 | 
					      merkle_root: block.merkleroot,
 | 
				
			||||||
 | 
					      tx_count: block.nTx,
 | 
				
			||||||
 | 
					      size: block.size,
 | 
				
			||||||
 | 
					      weight: block.weight,
 | 
				
			||||||
 | 
					      previousblockhash: block.previousblockhash,
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  $getRawTransaction(txId: string, skipConversion = false, addPrevout = false): Promise<IEsploraApi.Transaction> {
 | 
				
			||||||
    // If the transaction is in the mempool we already converted and fetched the fee. Only prevouts are missing
 | 
					    // If the transaction is in the mempool we already converted and fetched the fee. Only prevouts are missing
 | 
				
			||||||
    const txInMempool = mempool.getMempool()[txId];
 | 
					    const txInMempool = mempool.getMempool()[txId];
 | 
				
			||||||
    if (txInMempool && addPrevout) {
 | 
					    if (txInMempool && addPrevout) {
 | 
				
			||||||
      return this.$addPrevouts(txInMempool);
 | 
					      return this.$addPrevouts(txInMempool);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return this.bitcoindClient.getRawTransaction(txId, true, blockHash)
 | 
					    return this.bitcoindClient.getRawTransaction(txId, true)
 | 
				
			||||||
      .then((transaction: IBitcoinApi.Transaction) => {
 | 
					      .then((transaction: IBitcoinApi.Transaction) => {
 | 
				
			||||||
        if (skipConversion) {
 | 
					        if (skipConversion) {
 | 
				
			||||||
          transaction.vout.forEach((vout) => {
 | 
					          transaction.vout.forEach((vout) => {
 | 
				
			||||||
@ -174,35 +191,18 @@ class BitcoinApi implements AbstractBitcoinApi {
 | 
				
			|||||||
      };
 | 
					      };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (transaction.confirmations) {
 | 
					    if (addPrevout) {
 | 
				
			||||||
      esploraTransaction = await this.$calculateFeeFromInputs(esploraTransaction, addPrevout);
 | 
					      if (transaction.confirmations) {
 | 
				
			||||||
    } else {
 | 
					        esploraTransaction = await this.$calculateFeeFromInputs(esploraTransaction);
 | 
				
			||||||
      esploraTransaction = await this.$appendMempoolFeeData(esploraTransaction);
 | 
					      } else {
 | 
				
			||||||
      if (addPrevout) {
 | 
					        esploraTransaction = await this.$appendMempoolFeeData(esploraTransaction);
 | 
				
			||||||
        esploraTransaction = await this.$calculateFeeFromInputs(esploraTransaction, addPrevout);
 | 
					        esploraTransaction = await this.$calculateFeeFromInputs(esploraTransaction);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return esploraTransaction;
 | 
					    return esploraTransaction;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  static convertBlock(block: IBitcoinApi.Block): IEsploraApi.Block {
 | 
					 | 
				
			||||||
    return {
 | 
					 | 
				
			||||||
      id: block.hash,
 | 
					 | 
				
			||||||
      height: block.height,
 | 
					 | 
				
			||||||
      version: block.version,
 | 
					 | 
				
			||||||
      timestamp: block.time,
 | 
					 | 
				
			||||||
      bits: parseInt(block.bits, 16),
 | 
					 | 
				
			||||||
      nonce: block.nonce,
 | 
					 | 
				
			||||||
      difficulty: block.difficulty,
 | 
					 | 
				
			||||||
      merkle_root: block.merkleroot,
 | 
					 | 
				
			||||||
      tx_count: block.nTx,
 | 
					 | 
				
			||||||
      size: block.size,
 | 
					 | 
				
			||||||
      weight: block.weight,
 | 
					 | 
				
			||||||
      previousblockhash: block.previousblockhash,
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  private translateScriptPubKeyType(outputType: string): string {
 | 
					  private translateScriptPubKeyType(outputType: string): string {
 | 
				
			||||||
    const map = {
 | 
					    const map = {
 | 
				
			||||||
      'pubkey': 'p2pk',
 | 
					      'pubkey': 'p2pk',
 | 
				
			||||||
@ -245,7 +245,7 @@ class BitcoinApi implements AbstractBitcoinApi {
 | 
				
			|||||||
      if (vin.prevout) {
 | 
					      if (vin.prevout) {
 | 
				
			||||||
        continue;
 | 
					        continue;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      const innerTx = await this.$getRawTransaction(vin.txid, false);
 | 
					      const innerTx = await this.$getRawTransaction(vin.txid, false, false);
 | 
				
			||||||
      vin.prevout = innerTx.vout[vin.vout];
 | 
					      vin.prevout = innerTx.vout[vin.vout];
 | 
				
			||||||
      this.addInnerScriptsToVin(vin);
 | 
					      this.addInnerScriptsToVin(vin);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -271,18 +271,16 @@ class BitcoinApi implements AbstractBitcoinApi {
 | 
				
			|||||||
    return this.bitcoindClient.getRawMemPool(true);
 | 
					    return this.bitcoindClient.getRawMemPool(true);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private async $calculateFeeFromInputs(transaction: IEsploraApi.Transaction, addPrevout: boolean): Promise<IEsploraApi.Transaction> {
 | 
					  private async $calculateFeeFromInputs(transaction: IEsploraApi.Transaction): Promise<IEsploraApi.Transaction> {
 | 
				
			||||||
    if (transaction.vin[0].is_coinbase) {
 | 
					    if (transaction.vin[0].is_coinbase) {
 | 
				
			||||||
      transaction.fee = 0;
 | 
					      transaction.fee = 0;
 | 
				
			||||||
      return transaction;
 | 
					      return transaction;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    let totalIn = 0;
 | 
					    let totalIn = 0;
 | 
				
			||||||
    for (const vin of transaction.vin) {
 | 
					    for (const vin of transaction.vin) {
 | 
				
			||||||
      const innerTx = await this.$getRawTransaction(vin.txid, !addPrevout);
 | 
					      const innerTx = await this.$getRawTransaction(vin.txid, false, false);
 | 
				
			||||||
      if (addPrevout) {
 | 
					      vin.prevout = innerTx.vout[vin.vout];
 | 
				
			||||||
        vin.prevout = innerTx.vout[vin.vout];
 | 
					      this.addInnerScriptsToVin(vin);
 | 
				
			||||||
        this.addInnerScriptsToVin(vin);
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      totalIn += innerTx.vout[vin.vout].value;
 | 
					      totalIn += innerTx.vout[vin.vout].value;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    const totalOut = transaction.vout.reduce((p, output) => p + output.value, 0);
 | 
					    const totalOut = transaction.vout.reduce((p, output) => p + output.value, 0);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user