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

feat: add redis cache and unimplemented/missing fields #21

Merged
merged 9 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@
"postinstall": "npx prisma generate"
},
"dependencies": {
"@apollo/server": "^4.10.4",
"@applifting-io/nestjs-dataloader": "^1.1.5",
"@cell-studio/mempool.js": "^2.4.0",
"@ckb-lumos/bi": "^0.23.0",
"@ckb-lumos/lumos": "^0.23.0",
"@ckb-lumos/rpc": "^0.23.0",
"@nestjs/apollo": "^12.2.0",
"@nestjs/cache-manager": "^2.2.2",
"@nestjs/common": "^10.0.0",
"@nestjs/config": "^3.2.3",
"@nestjs/core": "^10.0.0",
"@nestjs/graphql": "^12.2.0",
"@nestjs/mercurius": "^12.2.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/platform-fastify": "^10.3.10",
"@ntegral/nestjs-sentry": "^4.0.1",
"@prisma/client": "^5.16.2",
"@rgbpp-sdk/btc": "^0.0.0-snap-20240727021715",
Expand All @@ -42,10 +42,16 @@
"@sentry/profiling-node": "^8.17.0",
"@types/ws": "^8.5.11",
"axios": "^1.7.2",
"cache-manager": "^5.7.1",
"cache-manager": "^5.2.3",
"cache-manager-redis-yet": "^4.1.2",
"dataloader": "^2.2.2",
"fastify": "^4.28.1",
"graphql": "^16.9.0",
"lodash": "^4.17.21",
"mercurius": "^14.1.0",
"mercurius-cache": "^6.0.1",
"nestjs-cacheable": "^1.0.0",
"redis": "^4.6.7",
"reflect-metadata": "^0.2.0",
"rpc-websockets": "^7.11.2",
"rxjs": "^7.8.1",
Expand Down
22 changes: 18 additions & 4 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { APP_INTERCEPTOR } from '@nestjs/core';
import { CacheModule } from '@nestjs/cache-manager';
import { CacheModule, CacheStore } from '@nestjs/cache-manager';
import { HttpException, Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { SentryInterceptor, SentryModule } from '@ntegral/nestjs-sentry';
import { nodeProfilingIntegration } from '@sentry/profiling-node';
import { envSchema } from './env';
import type { RedisClientOptions } from 'redis';
import { redisStore } from 'cache-manager-redis-yet';
import { Env, envSchema } from './env';
import { CoreModule } from './core/core.module';
import { ApiModule } from './modules/api.module';
import { CacheableModule } from 'nestjs-cacheable';

@Module({
imports: [
Expand All @@ -20,7 +23,7 @@ import { ApiModule } from './modules/api.module';
}),
SentryModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
useFactory: async (configService: ConfigService<Env>) => ({
dsn: configService.get('SENTRY_DSN'),
environment: configService.get('NODE_ENV'),
tracesSampleRate: 0.1,
Expand All @@ -33,8 +36,19 @@ import { ApiModule } from './modules/api.module';
}),
inject: [ConfigService],
}),
CacheModule.register({
CacheableModule.register(),
CacheModule.registerAsync<RedisClientOptions>({
isGlobal: true,
imports: [ConfigModule],
useFactory: async (configService: ConfigService<Env>) => {
const store = await redisStore({
url: configService.get('REDIS_URL'),
}) as unknown as CacheStore;
return {
store,
};
},
inject: [ConfigService],
}),
CoreModule,
ApiModule,
Expand Down
2 changes: 2 additions & 0 deletions backend/src/common/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ONE_MONTH_MS = 30 * 24 * 60 * 60 * 1000;
export const TEN_MINUTES_MS = 10 * 60 * 1000;
35 changes: 27 additions & 8 deletions backend/src/core/bitcoin-api/bitcoin-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { IBitcoinDataProvider } from './bitcoin-api.interface';
import { ElectrsService } from './provider/electrs.service';
import { MempoolService } from './provider/mempool.service';
import { ChainInfo } from './bitcoin-api.schema';
import { Cacheable } from 'nestjs-cacheable';
import { ONE_MONTH_MS, TEN_MINUTES_MS } from 'src/common/date';

type MethodParameters<T, K extends keyof T> = T[K] extends (...args: infer P) => any ? P : never;
type MethodReturnType<T, K extends keyof T> = T[K] extends (...args: any[]) => infer R ? R : never;
Expand Down Expand Up @@ -182,14 +184,15 @@ export class BitcoinApiService {
return this.call('getAddressTxs', { address, afterTxid });
}

@Cacheable({
key: ({ txid }) => `getTx:${txid}`,
namespace: 'bitcoinApiService',
ttl: ONE_MONTH_MS,
})
public async getTx({ txid }: { txid: string }) {
return this.call('getTx', { txid });
}

public async getTxHex({ txid }: { txid: string }) {
return this.call('getTxHex', { txid });
}

public async getTxOutSpend({ txid, vout }: { txid: string; vout: number }) {
return this.call('getTxOutSpend', { txid, vout });
}
Expand All @@ -198,22 +201,38 @@ export class BitcoinApiService {
return this.call('getTxOutSpends', { txid });
}

@Cacheable({
key: ({ hash }) => `getBlock:${hash}`,
namespace: 'bitcoinApiService',
ttl: ONE_MONTH_MS,
Flouse marked this conversation as resolved.
Show resolved Hide resolved
})
public async getBlock({ hash }: { hash: string }) {
return this.call('getBlock', { hash });
}

@Cacheable({
key: ({ hash, startIndex }) => `getBlockTxs:${hash}:${startIndex}`,
namespace: 'bitcoinApiService',
ttl: ONE_MONTH_MS,
Flouse marked this conversation as resolved.
Show resolved Hide resolved
})
public async getBlockTxs({ hash, startIndex }: { hash: string; startIndex?: number }) {
return this.call('getBlockTxs', { hash, startIndex });
}

@Cacheable({
key: ({ height }) => `getBlockHeight:${height}`,
namespace: 'bitcoinApiService',
ttl: TEN_MINUTES_MS,
})
public async getBlockHeight({ height }: { height: number }) {
return this.call('getBlockHeight', { height });
}

public async getBlockHeader({ hash }: { hash: string }) {
return this.call('getBlockHeader', { hash });
}

@Cacheable({
key: ({ hash }) => `getBlockTxids:${hash}`,
namespace: 'bitcoinApiService',
ttl: ONE_MONTH_MS,
})
public async getBlockTxids({ hash }: { hash: string }) {
return this.call('getBlockTxids', { hash });
}
Expand Down
14 changes: 14 additions & 0 deletions backend/src/core/ckb-rpc/ckb-rpc-websocket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Env } from 'src/env';
import {
Block,
BlockEconomicState,
GetCellsResult,
GetTransactionsResult,
SearchKey,
TransactionWithStatusResponse,
Expand Down Expand Up @@ -66,4 +67,17 @@ export class CkbRpcWebsocketService {
const transactions = await this.websocket.call('get_transactions', [searchKey, order, limit]);
return transactions as GetTransactionsResult;
}

public async getCells(
searchKey: SearchKey,
order: 'asc' | 'desc',
limit: string,
after?: string,
): Promise<GetCellsResult> {
this.logger.debug(
`get_cells - searchKey: ${JSON.stringify(searchKey)}, order: ${order}, limit: ${limit}, after: ${after}`,
);
const cells = await this.websocket.call('get_cells', [searchKey, order, limit, after]);
return cells as GetCellsResult;
}
}
14 changes: 14 additions & 0 deletions backend/src/core/ckb-rpc/ckb-rpc.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,17 @@ export interface GetTransactionsResult {
tx_index: string;
}[];
}

export interface GetCellsResult {
last_cursor: string;
objects: {
block_number: string;
out_point: {
index: string;
tx_hash: string;
};
output: Output;
output_data: string;
tx_index: string;
}[];
}
5 changes: 4 additions & 1 deletion backend/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ import { NetworkType } from './constants';

export const envSchema = z
.object({
NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
NETWORK: z.enum([NetworkType.mainnet, NetworkType.testnet]).default(NetworkType.testnet),
ENABLED_GRAPHQL_PLAYGROUND: z.boolean().default(true),

// DATABASE_URL: z.string(),
// REDIS_URL: z.string(),
REDIS_URL: z.string(),

BITCOIN_PRIMARY_DATA_PROVIDER: z.enum(['mempool', 'electrs']),

CKB_EXPLORER_API_URL: z.string(),
CKB_RPC_WEBSOCKET_URL: z.string(),

SENTRY_DSN: z.string().optional(),
})
.and(
z.union([
Expand Down
3 changes: 2 additions & 1 deletion backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
await app.listen(3000);
}
bootstrap();
16 changes: 11 additions & 5 deletions backend/src/modules/api.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,33 @@ import { APP_INTERCEPTOR } from '@nestjs/core';
import { ConfigService } from '@nestjs/config';
import { GraphQLModule } from '@nestjs/graphql';
import { SentryService } from '@ntegral/nestjs-sentry';
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { MercuriusDriver, MercuriusDriverConfig } from '@nestjs/mercurius';
import { DataLoaderInterceptor } from '@applifting-io/nestjs-dataloader';
import { Env } from 'src/env';
import { CkbModule } from './ckb/ckb.module';
import { RgbppModule } from './rgbpp/rgbpp.module';
import { BitcoinModule } from './bitcoin/bitcoin.module';
import { FastifyReply, FastifyRequest } from 'fastify';

@Module({
imports: [
GraphQLModule.forRootAsync<ApolloDriverConfig>({
driver: ApolloDriver,
GraphQLModule.forRootAsync<MercuriusDriverConfig>({
driver: MercuriusDriver,
inject: [ConfigService, SentryService],
useFactory: async (configService: ConfigService<Env>, sentryService: SentryService) => ({
playground: configService.get('ENABLED_GRAPHQL_PLAYGROUND'),
installSubscriptionHandlers: true,
introspection: true,
graphiql: true,
autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
buildSchemaOptions: {
dateScalarMode: 'timestamp',
},
context: () => {
context: (req: FastifyRequest, res: FastifyReply) => {
if (req.method === 'GET') {
res.redirect(302, '/graphiql');
return;
}
return {
span: sentryService.instance().startInactiveSpan({
op: 'gql',
Expand All @@ -45,4 +51,4 @@ import { BitcoinModule } from './bitcoin/bitcoin.module';
},
],
})
export class ApiModule {}
export class ApiModule { }
2 changes: 1 addition & 1 deletion backend/src/modules/bitcoin/bitcoin.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ import { BitcoinOutputModule } from './output/output.module';
],
providers: [BitcoinResolver],
})
export class BitcoinModule {}
export class BitcoinModule { }
27 changes: 22 additions & 5 deletions backend/src/modules/bitcoin/bitcoin.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Float, Query, ResolveField, Resolver } from '@nestjs/graphql';
import { Float, Parent, Query, ResolveField, Resolver } from '@nestjs/graphql';
import { BitcoinApiService } from 'src/core/bitcoin-api/bitcoin-api.service';
import { BitcoinBaseChainInfo, BitcoinChainInfo, BitcoinFees } from './bitcoin.model';
import { Loader } from '@applifting-io/nestjs-dataloader';
import { BitcoinBlockTxidsLoader, BitcoinBlockTxidsLoaderType } from './block/dataloader/block-txids.loader';

// 60 * 24 = 1440 minutes
const BLOCK_NUMBER_OF_24_HOURS = 144;

@Resolver(() => BitcoinChainInfo)
export class BitcoinResolver {
constructor(private bitcoinApiService: BitcoinApiService) {}
constructor(private bitcoinApiService: BitcoinApiService) { }

@Query(() => BitcoinChainInfo, { name: 'btcChainInfo' })
public async chainInfo(): Promise<BitcoinBaseChainInfo> {
Expand All @@ -13,9 +18,21 @@ export class BitcoinResolver {
}

@ResolveField(() => Float)
public async transactionsCountIn24Hours(): Promise<number> {
// TODO: implement this resolver
return 0;
public async transactionsCountIn24Hours(
@Parent() chainInfo: BitcoinBaseChainInfo,
@Loader(BitcoinBlockTxidsLoader) blockTxidsLoader: BitcoinBlockTxidsLoaderType,
): Promise<number> {
const blockNumbers = Array.from(
{ length: BLOCK_NUMBER_OF_24_HOURS },
(_, i) => chainInfo.tipBlockHeight - i,
);
const txidsCollection = await blockTxidsLoader.loadMany(
blockNumbers.map((blockNumber) => ({ height: blockNumber })),
);
const count = txidsCollection
.map((txs) => (txs instanceof Array ? txs : []))
.reduce((acc, txs) => acc + txs?.length ?? 0, 0);
return count;
}

@ResolveField(() => BitcoinFees)
Expand Down
66 changes: 0 additions & 66 deletions backend/src/modules/bitcoin/block/block.dataloader.ts

This file was deleted.

Loading
Loading