For non Bitcoin network, run legacy API code, but keep the same endpoint
This commit is contained in:
		
							parent
							
								
									8f57272ea0
								
							
						
					
					
						commit
						88fba3f506
					
				@ -336,8 +336,8 @@ class Server {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    this.app
 | 
			
		||||
      .get(config.MEMPOOL.API_URL_PREFIX + 'blocks', routes.getBlocks)
 | 
			
		||||
      .get(config.MEMPOOL.API_URL_PREFIX + 'blocks/:height', routes.getBlocks)
 | 
			
		||||
      .get(config.MEMPOOL.API_URL_PREFIX + 'blocks', routes.getBlocks.bind(routes))
 | 
			
		||||
      .get(config.MEMPOOL.API_URL_PREFIX + 'blocks/:height', routes.getBlocks.bind(routes))
 | 
			
		||||
      .get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash', routes.getBlock);
 | 
			
		||||
 | 
			
		||||
    if (config.MEMPOOL.BACKEND !== 'esplora') {
 | 
			
		||||
 | 
			
		||||
@ -81,7 +81,7 @@ export interface TransactionStripped {
 | 
			
		||||
 | 
			
		||||
export interface BlockExtension {
 | 
			
		||||
  totalFees?: number;
 | 
			
		||||
  medianFee?: number; // Actually the median fee rate that we compute ourself
 | 
			
		||||
  medianFee?: number;
 | 
			
		||||
  feeRange?: number[];
 | 
			
		||||
  reward?: number;
 | 
			
		||||
  coinbaseTx?: TransactionMinerInfo;
 | 
			
		||||
 | 
			
		||||
@ -722,14 +722,52 @@ class Routes {
 | 
			
		||||
 | 
			
		||||
  public async getBlocks(req: Request, res: Response) {
 | 
			
		||||
    try {
 | 
			
		||||
      const height = req.params.height === undefined ? undefined : parseInt(req.params.height, 10);
 | 
			
		||||
      res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString());
 | 
			
		||||
      res.json(await blocks.$getBlocks(height, 15));
 | 
			
		||||
      if (['mainnet', 'testnet', 'signet', 'regtest'].includes(config.MEMPOOL.NETWORK)) { // Bitcoin
 | 
			
		||||
        const height = req.params.height === undefined ? undefined : parseInt(req.params.height, 10);
 | 
			
		||||
        res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString());
 | 
			
		||||
        res.json(await blocks.$getBlocks(height, 15));
 | 
			
		||||
      } else { // Liquid, Bisq
 | 
			
		||||
        return await this.getLegacyBlocks(req, res);
 | 
			
		||||
      }
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
      res.status(500).send(e instanceof Error ? e.message : e);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public async getLegacyBlocks(req: Request, res: Response) {
 | 
			
		||||
    try {
 | 
			
		||||
      const returnBlocks: IEsploraApi.Block[] = [];
 | 
			
		||||
      const fromHeight = parseInt(req.params.height, 10) || blocks.getCurrentBlockHeight();
 | 
			
		||||
 | 
			
		||||
      // Check if block height exist in local cache to skip the hash lookup
 | 
			
		||||
      const blockByHeight = blocks.getBlocks().find((b) => b.height === fromHeight);
 | 
			
		||||
      let startFromHash: string | null = null;
 | 
			
		||||
      if (blockByHeight) {
 | 
			
		||||
        startFromHash = blockByHeight.id;
 | 
			
		||||
      } else {
 | 
			
		||||
        startFromHash = await bitcoinApi.$getBlockHash(fromHeight);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      let nextHash = startFromHash;
 | 
			
		||||
      for (let i = 0; i < 10 && nextHash; i++) {
 | 
			
		||||
        const localBlock = blocks.getBlocks().find((b) => b.id === nextHash);
 | 
			
		||||
        if (localBlock) {
 | 
			
		||||
          returnBlocks.push(localBlock);
 | 
			
		||||
          nextHash = localBlock.previousblockhash;
 | 
			
		||||
        } else {
 | 
			
		||||
          const block = await bitcoinApi.$getBlock(nextHash);
 | 
			
		||||
          returnBlocks.push(block);
 | 
			
		||||
          nextHash = block.previousblockhash;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString());
 | 
			
		||||
      res.json(returnBlocks);
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
      res.status(500).send(e instanceof Error ? e.message : e);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  public async getBlockTransactions(req: Request, res: Response) {
 | 
			
		||||
    try {
 | 
			
		||||
      loadingIndicators.setProgress('blocktxs-' + req.params.hash, 0);
 | 
			
		||||
 | 
			
		||||
@ -15,7 +15,7 @@ export function prepareBlock(block: any): BlockExtended {
 | 
			
		||||
    weight: block.weight,
 | 
			
		||||
    previousblockhash: block.previousblockhash,
 | 
			
		||||
    extras: {
 | 
			
		||||
      coinbaseRaw: block.coinbase_raw ?? block.extras.coinbaseRaw,
 | 
			
		||||
      coinbaseRaw: block.coinbase_raw ?? block.extras?.coinbaseRaw,
 | 
			
		||||
      medianFee: block.medianFee ?? block.median_fee ?? block.extras?.medianFee,
 | 
			
		||||
      feeRange: block.feeRange ?? block.fee_span,
 | 
			
		||||
      reward: block.reward ?? block?.extras?.reward,
 | 
			
		||||
 | 
			
		||||
@ -87,9 +87,11 @@ export class BlocksList implements OnInit {
 | 
			
		||||
            return blocks[0];
 | 
			
		||||
          }
 | 
			
		||||
          this.blocksCount = Math.max(this.blocksCount, blocks[1][0].height) + 1;
 | 
			
		||||
          // @ts-ignore: Need to add an extra field for the template
 | 
			
		||||
          blocks[1][0].extras.pool.logo = `./resources/mining-pools/` +
 | 
			
		||||
            blocks[1][0].extras.pool.name.toLowerCase().replace(' ', '').replace('.', '') + '.svg';
 | 
			
		||||
          if (this.stateService.env.MINING_DASHBOARD) {
 | 
			
		||||
            // @ts-ignore: Need to add an extra field for the template
 | 
			
		||||
            blocks[1][0].extras.pool.logo = `./resources/mining-pools/` +
 | 
			
		||||
              blocks[1][0].extras.pool.name.toLowerCase().replace(' ', '').replace('.', '') + '.svg';
 | 
			
		||||
          }
 | 
			
		||||
          acc.unshift(blocks[1][0]);
 | 
			
		||||
          acc = acc.slice(0, this.widget ? 6 : 15);
 | 
			
		||||
          return acc;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user