Skip to content

Commit

Permalink
Feat: getCleanStatus(), startClean() and stopClean() (#97)
Browse files Browse the repository at this point in the history
* Added getCleanStatus(), startClean() and stopClean()

* used getBankPV in payment handler
  • Loading branch information
tomijaga authored Mar 31, 2021
1 parent 0705481 commit 3037330
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 43 deletions.
71 changes: 66 additions & 5 deletions docs/bank.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,6 @@ console.log(res);

> If you don't understand upgradeRequest and upgradeNotice, check out the [documentation](https://thenewboston.com/guide/resync-process) at thenewboston.com

## Getting the Bank's Primary Validator

Simply use the `Bank.getBankPV` method which returns the formatted url of the Primary Validator
Expand All @@ -526,6 +525,7 @@ Use the `Bank.getTxFee` method to get the transaction fee
const bank = new tnb.Bank("http://143.110.137.54");
console.log(await bank.getTxFee());
// 1
```

## Crawl

Expand Down Expand Up @@ -554,9 +554,9 @@ console.log(crawlStatus);
To initiate a network crawl we need to send a request to the bank using the `startCrawl()` method

```ts
const bankNetworkId = new Account("BankNetworkIdSigingKey");
const account = new Account("BankNetworkIdSigingKey");

const response = bank.startCrawl(bankNetworkId);
const response = bank.startCrawl(account);

console.log(response);

Expand All @@ -574,9 +574,9 @@ console.log(response);
To stop the network crawl process we can send a request to the bank using the `stopCrawl()` method

```ts
const bankNetworkId = new Account("BankNetworkIdSigingKey");
const account = new Account("BankNetworkIdSigingKey");

const response = bank.stopCrawl(bankNetworkId);
const response = bank.stopCrawl(account);

console.log(response);

Expand All @@ -587,5 +587,66 @@ console.log(response);
// port: 80,
// protocol: 'http'
// }
```

## Clean

A network clean is the process of updating the network (mainly to remove disconected nodes). A clean can be triggered by any client, given that it knows the Node's signing key.

### Retrieve Clean Status

To retrieve the current clean status of the bank we can use the `getCleanStatus()` method

```ts
const cleanStatus = bank.getCleanStatus();

console.log(cleanStatus);

// {
// clean_last_completed: '2021-03-29 14:07:26.218216+00:00',
// clean_status: 'cleaning',
// ip_address: '18.218.193.164',
// port: 80,
// protocol: 'http'
// }
```

### Start Clean

To initiate a network clean we need to send a request to the bank using the `startClean()` method

```ts
const account = new Account("BankNetworkIdSigingKey");

const response = bank.startClean(account);

console.log(response);

// {
// clean_last_completed: '2021-03-29 14:07:26.218216+00:00',
// clean_status: 'cleaning',
// ip_address: '18.218.193.164',
// port: 80,
// protocol: 'http'
// }
```

### Stopping Clean

To stop the network clean process we can send a request to the bank using the `stopClean()` method

```ts
const account = new Account("BankNetworkIdSigingKey");

const response = bank.stopClean(account);

console.log(response);

// {
// clean_last_completed: '2021-03-29 14:20:29.265859+00:00',
// clean_status: 'stop_requested',
// ip_address: '18.218.193.164',
// port: 80,
// protocol: 'http'
// }
```
70 changes: 66 additions & 4 deletions docs/confirmation-validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ console.log(crawlStatus);
To initiate a network crawl we need to send a request to the bank using the `startCrawl()` method

```ts
const CV_NetworkId = new Account("BankNetworkIdSigingKey");
const account = new Account("CV_NetworkIdSigningKey");

const response = CV.startCrawl(CV_NetworkId);
const response = CV.startCrawl(account);

console.log(response);

Expand All @@ -174,9 +174,9 @@ console.log(response);
To stop the network crawl process we can send a request to the bank using the `stopCrawl()` method

```ts
const CV_NetworkId = new Account("BankNetworkIdSigingKey");
const account = new Account("CV_NetworkIdSigningKey");

const response = CV.stopCrawl(CV_NetworkId);
const response = CV.stopCrawl(account);

console.log(response);

Expand All @@ -188,3 +188,65 @@ console.log(response);
// protocol: 'http'
// }
```

## Clean

A network clean is the process of updating the network (mainly to remove disconected nodes). A clean can be triggered by any client, given that it knows the Node's signing key.

### Retrieve Clean Status

To retrieve the current clean status of the bank we can use the `getCleanStatus()` method

```ts
const cleanStatus = CV.getCleanStatus();

console.log(cleanStatus);

// {
// clean_last_completed: '2021-03-29 14:07:26.218216+00:00',
// clean_status: 'cleaning',
// ip_address: '18.218.193.164',
// port: 80,
// protocol: 'http'
// }
```

### Start Clean

To initiate a network clean we need to send a request to the bank using the `startClean()` method

```ts
const account = new Account("CV_NetworkIdSigningKey");

const response = CV.startClean(account);

console.log(response);

// {
// clean_last_completed: '2021-03-29 14:07:26.218216+00:00',
// clean_status: 'cleaning',
// ip_address: '18.218.193.164',
// port: 80,
// protocol: 'http'
// }
```

### Stopping Clean

To stop the network clean process we can send a request to the bank using the `stopClean()` method

```ts
const account = new Account("CV_NetworkIdSigningKey");

const response = CV.stopClean(account);

console.log(response);

// {
// clean_last_completed: '2021-03-29 14:20:29.265859+00:00',
// clean_status: 'stop_requested',
// ip_address: '18.218.193.164',
// port: 80,
// protocol: 'http'
// }
```
45 changes: 39 additions & 6 deletions src/bank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ import type {
PaginatedEntryMetadata,
PaginatedBlockEntry,
PaginatedValidatorEntry,
CleanResponse,
CleanData,
CrawlData,
} from "./models";
import type { Account } from "./account";
import { CrawlResponse } from "./models/responses/generic/crawl";

/** Used for creating banks and sending requests easily to that specific bank server node. */
export class Bank extends ServerNode {
Expand Down Expand Up @@ -84,27 +88,56 @@ export class Bank extends ServerNode {

/** Gets the current crawl status */
async getCrawlStatus() {
return await super.getData("/crawl");
return await super.getData<CrawlResponse>("/crawl");
}

/**
* Sends a Post Request to the bank to start crawl process
* @param account An Account created with the Network Id Signing key of the current Bank
*/
async startCrawl(account: Account) {
const command: CrawlCommand = "start";

return await super._postCrawl(command, account);
return await super.postData<CleanResponse>(
"/crawl",
account.createSignedMessage<CrawlData>({ crawl: "start" })
);
}

/**
* Sends a Post Request to the bank to start crawl process
* @param account An Account created with the Network Id Signing key of the current Bank
*/
async stopCrawl(account: Account) {
const command: CrawlCommand = "stop";
return await super.postData<CleanResponse>(
"/crawl",
account.createSignedMessage<CrawlData>({ crawl: "stop" })
);
}

/** Gets the current clean status */
async getCleanStatus() {
return await super.getData<CleanResponse>("/clean");
}

/**
* Sends a Post Request to the bank to start clean process
* @param account An Account created with the Network Id Signing key of the current Bank
*/
async startClean(account: Account) {
return await super.postData<CleanResponse>(
"/clean",
account.createSignedMessage<CleanData>({ clean: "start" })
);
}

return await super._postCrawl(command, account);
/**
* Sends a Post Request to the bank to start clean process
* @param account An Account created with the Network Id Signing key of the current Bank
*/
async stopClean(account: Account) {
return await super.postData<CleanResponse>(
"/clean",
account.createSignedMessage<CleanData>({ clean: "stop" })
);
}

/**
Expand Down
45 changes: 37 additions & 8 deletions src/confirmation-validator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Validator } from "./validator";
import type { Account } from "./account";
import type { ConfirmationValidatorConfigResponse, CrawlData, CrawlCommand } from "./models";
import type { ConfirmationValidatorConfigResponse, CrawlData, CrawlCommand, CleanResponse, CleanData } from "./models";

/** Used for connecting with and using confirmation validator server nodes. */
export class ConfirmationValidator extends Validator {
Expand All @@ -15,23 +15,52 @@ export class ConfirmationValidator extends Validator {
}

/**
* Sends a Post Request to the bank to start crawl process
* Sends a Post Request to the confirmation validator to start crawl process
* @param account An Account created with the Network Id Signing key of the current Confirmation Validator
*/
async startCrawl(account: Account) {
const command: CrawlCommand = "start";

return await super._postCrawl(command, account);
return await super.postData<CleanResponse>(
"/crawl",
account.createSignedMessage<CleanData>({ clean: "start" })
);
}

/**
* Sends a Post Request to the bank to start crawl process
* Sends a Post Request to the confirmation validator to start crawl process
* @param account An Account created with the Network Id Signing key of the current Confirmation Validator
*/
async stopCrawl(account: Account) {
const command: CrawlCommand = "stop";
return await super.postData<CleanResponse>(
"/crawl",
account.createSignedMessage<CleanData>({ clean: "stop" })
);
}

/** Gets the current clean status */
async getCleanStatus() {
return await super.getData<CleanResponse>("/clean");
}

/**
* Sends a Post Request to the confirmation validator to start clean process
* @param account An Account created with the Network Id Signing key of the current Confirmation Validator
*/
async startClean(account: Account) {
return await super.postData<CleanResponse>(
"/clean",
account.createSignedMessage<CleanData>({ clean: "start" })
);
}

return await super._postCrawl(command, account);
/**
* Sends a Post Request to the confirmation validator to start clean process
* @param account An Account created with the Network Id Signing key of the current Confirmation Validator
*/
async stopClean(account: Account) {
return await super.postData<CleanResponse>(
"/clean",
account.createSignedMessage<CleanData>({ clean: "stop" })
);
}

// TODO: POST /confirmation_blocks
Expand Down
7 changes: 7 additions & 0 deletions src/models/clean-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** The model for crawl status. */
export type CleanCommand = "start" | "stop";

/** The model for crawl data. */
export interface CleanData {
clean: CleanCommand;
}
1 change: 1 addition & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type { AccountData } from "./account-data";
export type { BlockData } from "./block-data";
export type { BlockMessage } from "./block-message";
export type { CrawlCommand, CrawlData } from "./crawl-data";
export type { CleanCommand, CleanData } from "./clean-data";
export type { ServerNodeOptions } from "./server-node-options";
export type { PaymentHandlerOptions } from "./payment-handler-options";
export type { AccountPaymentHandlerOptions } from "./account-payment-handler-options";
Expand Down
10 changes: 10 additions & 0 deletions src/models/responses/generic/clean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Origin, Port, Protocol } from "../constants";

/** The response model for a crawl request. */
export interface CleanResponse {
clean_last_completed: string;
clean_status: string;
ip_address: Origin;
port: Port;
protocol: Protocol;
}
1 change: 1 addition & 0 deletions src/models/responses/generic/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export type { AccountBalanceLockResponse } from "./account-balance-lock";
export type { AccountBalanceResponse } from "./account-balance";
export type { CleanResponse } from "./clean";
9 changes: 3 additions & 6 deletions src/payment-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ export class PaymentHandler {
if (!this.bankConfig) {
return;
}
const { ip_address: ip, port, protocol } = this.bankConfig.primary_validator!;
let url = `${protocol}://${ip}`;
if (port !== null) {
url += `:${port}`;
}
this.primaryValidator = new PrimaryValidator(url);

this.primaryValidator = await this.bank.getBankPV();

const config = await this.primaryValidator
.getConfig()
.catch((err) => throwError("Failed to load the primary validator's config.", err));
Expand Down
Loading

0 comments on commit 3037330

Please sign in to comment.