Skip to content

Commit

Permalink
docs: ✏️ add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
waynewyang committed Dec 27, 2023
1 parent fa2726c commit 0868032
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import { RootController } from './root/root.controller';
import { RootService } from './root/root.service';
import { ChainsyncService } from './chainsync/chainsync.service';

/**
* Root module for the application.
*/
@Module({
imports: [],
controllers: [RootController],
Expand Down
17 changes: 15 additions & 2 deletions src/chainsync/chainsync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,38 @@

import { Injectable, OnModuleInit } from '@nestjs/common';

/**
* Service responsible for synchronizing with the blockchain.
*/
@Injectable()
export class ChainsyncService implements OnModuleInit {
/**
* Lifecycle hook, called once the module has been initialized.
*/
async onModuleInit() {
await this.startBackgroundTask();
}

/**
* Start the background task for continuous synchronization.
*/
private async startBackgroundTask() {
while (true) {
try {
console.log(
'Always craw the lastest tipset ,blockMessages and messages ',
'Always crawl the latest tipset, blockMessages, and messages.',
);
await this.delay(1000);
} catch (error) {
console.error('Error in background task:', error);
console.error('Error in the background task:', error);
}
}
}

/**
* Utility function to introduce a delay.
* @param ms - The delay time in milliseconds.
*/
private async delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
Expand Down
16 changes: 16 additions & 0 deletions src/config/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import {
AddressesFilterReplayStrategy,
} from '@unipackage/filecoin';

/**
* Configuration for a Filecoin network.
*/
export interface NetworkConfig {
apiAddress: string;
token: string;
Expand All @@ -37,6 +40,9 @@ export interface NetworkConfig {
};
}

/**
* Represents a connection to a Filecoin network.
*/
export class ChainNetwork {
rpc: ChainFilecoinRPC;
messageDatastore: MessageMongoDatastore;
Expand All @@ -45,6 +51,10 @@ export class ChainNetwork {
dataswapStartHeight: number;
chainService: ChainService;

/**
* Creates an instance of ChainNetwork.
* @param config - The network configuration.
*/
constructor(config: NetworkConfig) {
this.dataswapStartHeight = config.dataswapStartHeight;
this.rpc = new ChainFilecoinRPC({
Expand All @@ -67,13 +77,19 @@ export class ChainNetwork {
}
}

/**
* Calibration network configuration
*/
export const chainNetworkCalibration = new ChainNetwork({
apiAddress: process.env.CALIBRATION_LOTUS_API_ENDPOINT as string,
token: process.env.CALIBRATION_LOTUS_TOKEN as string,
mongoUrl: process.env.CALIBRATION_MONGO_URL as string,
dataswapStartHeight: 1,
});

/**
* Main network configuration
*/
export const chainNetwork = new ChainNetwork({
apiAddress: process.env.MAIN_LOTUS_API_ENDPOINT as string,
token: process.env.MAIN_LOTUS_TOKEN as string,
Expand Down
8 changes: 8 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

/**
* Application bootstrap function.
*/
async function bootstrap() {
// Create a NestJS application instance
const app = await NestFactory.create(AppModule);

// Start the application, listening on port 3000
await app.listen(3000);
}

// Call the bootstrap function to start the application
bootstrap();
12 changes: 12 additions & 0 deletions src/root/root.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,22 @@
import { Controller, Get, Param } from '@nestjs/common';
import { RootService } from './root.service';

/**
* Controller responsible for handling root-level requests.
*/
@Controller()
export class RootController {
/**
* Creates an instance of RootController.
* @param rootService - The RootService instance.
*/
constructor(private readonly rootService: RootService) {}

/**
* Handles GET requests for root-level resources with an identifier.
* @param param - Request parameters.
* @returns A string representing the response.
*/
@Get(':id')
getHello(@Param() param): string {
console.log(param.id);
Expand Down
7 changes: 7 additions & 0 deletions src/root/root.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@

import { Injectable } from '@nestjs/common';

/**
* Service responsible for providing root-level functionality.
*/
@Injectable()
export class RootService {
/**
* Gets a greeting message.
* @returns A string representing a greeting message.
*/
getHello(): string {
return 'Hello World!';
}
Expand Down

0 comments on commit 0868032

Please sign in to comment.