-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add bitcoin dataloader cache and optimize transactionsCountIn24…
…Hours field
- Loading branch information
Showing
13 changed files
with
367 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { BitcoinApiModule } from 'src/core/bitcoin-api/bitcoin-api.module'; | ||
import { BitcoinBlockLoader, BitcoinBlockTransactionsLoader } from './block.dataloader'; | ||
import { BitcoinBlockResolver } from './block.resolver'; | ||
import { BitcoinBlockLoader } from './dataloader/block.loader'; | ||
import { BitcoinBlockTransactionsLoader } from './dataloader/block-transactions.loader'; | ||
import { BitcoinBlockTxidsLoader } from './dataloader/block-txids.loader'; | ||
|
||
@Module({ | ||
imports: [BitcoinApiModule], | ||
providers: [BitcoinBlockResolver, BitcoinBlockLoader, BitcoinBlockTransactionsLoader], | ||
exports: [BitcoinBlockLoader, BitcoinBlockTransactionsLoader], | ||
providers: [ | ||
BitcoinBlockResolver, | ||
BitcoinBlockLoader, | ||
BitcoinBlockTransactionsLoader, | ||
BitcoinBlockTxidsLoader, | ||
], | ||
exports: [BitcoinBlockLoader, BitcoinBlockTransactionsLoader, BitcoinBlockTxidsLoader], | ||
}) | ||
export class BitcoinBlockModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { Logger } from '@nestjs/common'; | ||
import { BitcoinApiService } from 'src/core/bitcoin-api/bitcoin-api.service'; | ||
import * as BitcoinApi from 'src/core/bitcoin-api/bitcoin-api.schema'; | ||
import { Cache } from '@nestjs/cache-manager'; | ||
|
||
export abstract class BitcoinBaseLoader { | ||
protected logger = new Logger(BitcoinBaseLoader.name); | ||
abstract bitcoinApiService: BitcoinApiService; | ||
abstract cacheManager: Cache; | ||
|
||
private async getBlockHashByHeight(height: number): Promise<string> { | ||
const cacheKey = `bitcoinApiService#getBlockHeight:${height}`; | ||
const cached = await this.cacheManager.get<string>(cacheKey); | ||
if (cached) { | ||
return cached; | ||
} | ||
const hash = await this.bitcoinApiService.getBlockHeight({ height }); | ||
await this.cacheManager.set(cacheKey, hash); | ||
return hash; | ||
} | ||
|
||
private async getBlockByHash(hash: string): Promise<BitcoinApi.Block> { | ||
const cacheKey = `bitcoinApiService#getBlock:${hash}`; | ||
const cached = await this.cacheManager.get<BitcoinApi.Block>(cacheKey); | ||
if (cached) { | ||
return cached; | ||
} | ||
const block = await this.bitcoinApiService.getBlock({ hash }); | ||
const ttl = Date.now() - block.timestamp * 1000; | ||
await this.cacheManager.set(cacheKey, block, ttl); | ||
return block; | ||
} | ||
|
||
private async getBlockTxsByHash( | ||
hash: string, | ||
startIndex: number, | ||
): Promise<BitcoinApi.Transaction[]> { | ||
const cacheKey = `bitcoinApiService#getBlockTxs:${hash}:${startIndex}`; | ||
const cached = await this.cacheManager.get<BitcoinApi.Transaction[]>(cacheKey); | ||
if (cached) { | ||
return cached; | ||
} | ||
const txs = await this.bitcoinApiService.getBlockTxs({ hash, startIndex }); | ||
await this.cacheManager.set(cacheKey, txs); | ||
return txs; | ||
} | ||
|
||
private async getBlockTxidsByHash(hash: string): Promise<string[]> { | ||
const cacheKey = `bitcoinApiService#getBlockTxids:${hash}`; | ||
const cached = await this.cacheManager.get<string[]>(cacheKey); | ||
if (cached) { | ||
return cached; | ||
} | ||
const txids = await this.bitcoinApiService.getBlockTxids({ hash }); | ||
await this.cacheManager.set(cacheKey, txids); | ||
return txids; | ||
} | ||
|
||
protected async getBlock(hashOrHeight: string): Promise<BitcoinApi.Block> { | ||
if (hashOrHeight.startsWith('0')) { | ||
const block = await this.getBlockByHash(hashOrHeight); | ||
return block; | ||
} | ||
const height = parseInt(hashOrHeight, 10); | ||
const hash = await this.getBlockHashByHeight(height); | ||
const block = await this.getBlockByHash(hash); | ||
return block; | ||
} | ||
|
||
protected async getBlockTxs( | ||
hashOrHeight: string, | ||
startIndex: number, | ||
): Promise<BitcoinApi.Transaction[]> { | ||
if (hashOrHeight.startsWith('0')) { | ||
return this.getBlockTxsByHash(hashOrHeight, startIndex); | ||
} | ||
const height = parseInt(hashOrHeight, 10); | ||
const hash = await this.getBlockHashByHeight(height); | ||
const txs = await this.getBlockTxsByHash(hash, startIndex); | ||
return txs; | ||
} | ||
|
||
protected async getBlockTxids(hashOrHeight: string): Promise<string[]> { | ||
if (hashOrHeight.startsWith('0')) { | ||
const txs = await this.getBlockTxsByHash(hashOrHeight, 0); | ||
return txs.map((tx) => tx.txid); | ||
} | ||
const height = parseInt(hashOrHeight, 10); | ||
const hash = await this.getBlockHashByHeight(height); | ||
const txids = await this.getBlockTxidsByHash(hash); | ||
return txids; | ||
} | ||
} | ||
|
48 changes: 48 additions & 0 deletions
48
backend/src/modules/bitcoin/block/dataloader/block-transactions.loader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import DataLoader from 'dataloader'; | ||
import { Inject, Injectable, Logger } from '@nestjs/common'; | ||
import { NestDataLoader } from '@applifting-io/nestjs-dataloader'; | ||
import { DataLoaderResponse } from 'src/common/type/dataloader'; | ||
import { BitcoinApiService } from 'src/core/bitcoin-api/bitcoin-api.service'; | ||
import * as BitcoinApi from 'src/core/bitcoin-api/bitcoin-api.schema'; | ||
import { CACHE_MANAGER, Cache } from '@nestjs/cache-manager'; | ||
import { BitcoinBaseLoader } from './base'; | ||
|
||
export interface BitcoinBlockTransactionsLoaderParams { | ||
hash?: string; | ||
height?: number; | ||
startIndex?: number; | ||
} | ||
|
||
@Injectable() | ||
export class BitcoinBlockTransactionsLoader | ||
extends BitcoinBaseLoader | ||
implements NestDataLoader<BitcoinBlockTransactionsLoaderParams, BitcoinApi.Transaction[] | null> | ||
{ | ||
protected logger = new Logger(BitcoinBlockTransactionsLoader.name); | ||
|
||
constructor( | ||
public bitcoinApiService: BitcoinApiService, | ||
@Inject(CACHE_MANAGER) public cacheManager: Cache, | ||
) { | ||
super(); | ||
} | ||
|
||
public getBatchFunction() { | ||
return async (batchProps: BitcoinBlockTransactionsLoaderParams[]) => { | ||
this.logger.debug(`Loading bitcoin block transactions`); | ||
const results = await Promise.allSettled( | ||
batchProps.map(async ({ hash, height, startIndex }) => | ||
this.getBlockTxs(hash || height.toString(), startIndex), | ||
), | ||
); | ||
return results.map((result) => (result.status === 'fulfilled' ? result.value : null)); | ||
}; | ||
} | ||
} | ||
export type BitcoinBlockTransactionsLoaderType = DataLoader< | ||
BitcoinBlockTransactionsLoaderParams, | ||
BitcoinApi.Transaction[] | null | ||
>; | ||
export type BitcoinBlockTransactionsLoaderResponse = | ||
DataLoaderResponse<BitcoinBlockTransactionsLoader>; | ||
|
39 changes: 39 additions & 0 deletions
39
backend/src/modules/bitcoin/block/dataloader/block-txids.loader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import DataLoader from 'dataloader'; | ||
import { Inject, Injectable, Logger } from '@nestjs/common'; | ||
import { NestDataLoader } from '@applifting-io/nestjs-dataloader'; | ||
import { DataLoaderResponse } from 'src/common/type/dataloader'; | ||
import { BitcoinApiService } from 'src/core/bitcoin-api/bitcoin-api.service'; | ||
import { CACHE_MANAGER, Cache } from '@nestjs/cache-manager'; | ||
import { BitcoinBaseLoader } from './base'; | ||
|
||
export interface BitcoinBlockTxidsLoaderParams { | ||
hash?: string; | ||
height?: number; | ||
} | ||
|
||
@Injectable() | ||
export class BitcoinBlockTxidsLoader | ||
extends BitcoinBaseLoader | ||
implements NestDataLoader<BitcoinBlockTxidsLoaderParams, string[] | null> | ||
{ | ||
protected logger = new Logger(BitcoinBlockTxidsLoader.name); | ||
|
||
constructor( | ||
public bitcoinApiService: BitcoinApiService, | ||
@Inject(CACHE_MANAGER) public cacheManager: Cache, | ||
) { | ||
super(); | ||
} | ||
|
||
public getBatchFunction() { | ||
return async (keys: BitcoinBlockTxidsLoaderParams[]) => { | ||
this.logger.debug(`Loading bitcoin block transactions`); | ||
const results = await Promise.allSettled( | ||
keys.map(async ({ hash, height }) => this.getBlockTxids(hash || height.toString())), | ||
); | ||
return results.map((result) => (result.status === 'fulfilled' ? result.value : null)); | ||
}; | ||
} | ||
} | ||
export type BitcoinBlockTxidsLoaderType = DataLoader<BitcoinBlockTxidsLoaderParams, string[]>; | ||
export type BitcoinBlockTxidsLoaderResponse = DataLoaderResponse<BitcoinBlockTxidsLoader>; |
Oops, something went wrong.