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: added getCrawlStatus #91

Merged
merged 5 commits into from
Mar 29, 2021
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
9 changes: 1 addition & 8 deletions docs/account.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,13 @@ Account.isValidPair("SIGNING_KEY", "ACCOUNT_NUMBER"); // returns true only if th

## Using Signed Data and Signed Messages

We have already talked about creating signatures, so let's learn how we can apply them to creating signed data and signed messages. Signed data and signed messages are very similar, with the only difference being that signed messages have an extra `node_identifier` property. Here is an example of us creating signed data and a signed message:
We have already talked about creating signatures, so let's learn how we can apply them to creating signed messages. Here is an example of us creating a signed message:

> Note that all of the `signature` and `node_identifier` properties that we are generating are _almost_ random as we are not passing any arguments into the `Account` class.

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

account.createSignedData({ name: "Bacon" });

// {
// data: { name: 'Bacon' },
// signature: '68202fd5336c57dd42ba116fbf4154b7ef797473c7bc04949fef943c37b7b448ababf22c94711cd5f0fc603f5bd7d10d4e96dff9c876599de9fe887dfffe6d01'
// }

account.createSignedMessage({ name: "Tuna" });

// {
Expand Down
63 changes: 63 additions & 0 deletions docs/bank.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ 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 @@ -525,4 +526,66 @@ 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

A network crawl is the process of browsing nodes in order to discover new one. A crawl can be triggered by any client, given that it knows the Node's signing key.

### Retrieve Crawl Status

To retrieve the current crawl status of the bank we can use the `getCrawlStatus()` method

```ts
const crawlStatus = bank.getCrawlStatus();

console.log(crawlStatus);

// {
// crawl_last_completed: '2021-03-29 14:07:26.218216+00:00',
// crawl_status: 'crawling',
// ip_address: '18.218.193.164',
// port: 80,
// protocol: 'http'
// }
```

### Start Crawl

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 response = bank.startCrawl(bankNetworkId);

console.log(response);

// {
// crawl_last_completed: '2021-03-29 14:07:26.218216+00:00',
// crawl_status: 'crawling',
// ip_address: '18.218.193.164',
// port: 80,
// protocol: 'http'
// }
```

### Stopping Crawl

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 response = bank.stopCrawl(bankNetworkId);

console.log(response);

// {
// crawl_last_completed: '2021-03-29 14:20:29.265859+00:00',
// crawl_status: 'stop_requested',
// ip_address: '18.218.193.164',
// port: 80,
// protocol: 'http'
// }

```
73 changes: 65 additions & 8 deletions docs/confirmation-validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

In this section, we will look at the methods of the Primary Validator class, which extends the Validator class.

>It is recommended that you read the [documentation for the `Validator` Class](#Validator) Class first, to understand the base methods before reading this section.
> It is recommended that you read the [documentation for the `Validator` Class](#Validator) Class first, to understand the base methods before reading this section.

## Get Bank Confirmation Services
## Get Bank Confirmation Services

Confirmation Services are services rendered by a CV for validating a Bank's transactions

We can see what Banks are currently using a Confirmation Validators services and the details attached by using the `getBankConfirmationServices()` method

We can see what Banks are currently using a Confirmation Validators services and the details attached by using the `getBankConfirmationServices()` method

```ts
const CV = new tnb.ConfirmationValidator("http://54.177.174.219");

// retrive confirmation services handled by CV
const paginationOptions = { limit: 2, offset: 0 }
const paginationOptions = { limit: 2, offset: 0 };
const confirmationServices = await CV.getBankConfirmationServices(paginationOptions);

console.log(confirmationServices);
Expand Down Expand Up @@ -84,16 +83,15 @@ The Upgrade Request can be sent using the `sendUpgradeRequest()` method
const CV = new tnb.ConfirmationValidator("http://54.177.174.219");

//the node identifier of the confirmation validator that is receiving the upgrade notice
const nodeIdentifier = "9bfa37627e2dba0ae48165b219e76ceaba036b3db8e84108af73a1cce01fad35"
const nodeIdentifier = "9bfa37627e2dba0ae48165b219e76ceaba036b3db8e84108af73a1cce01fad35";

//Account of current CV
const account = new tnb.Account(CV.getConfig().account_number, "fakeSigningKeyHex");
const account = new tnb.Account(CV.getConfig().account_number, "fakeSigningKeyHex");

const response = await CV.sendUpgradeRequest(nodeIdentifier, account);

console.log(response);


// Status Code 200 if the CV Accepted the request and upgraded to a Primary Validator
// Status Code 400 if the CV Rejected the request

Expand Down Expand Up @@ -129,5 +127,64 @@ console.log(response);
*/
```

## Crawl

A network crawl is the process of browsing nodes in order to discover new one. A crawl can be triggered by any client, given that it knows the Node's signing key.

### Retrieve Crawl Status

To retrieve the current crawl status of the bank we can use the `getCrawlStatus()` method

```ts
const crawlStatus = CV.getCrawlStatus();

console.log(crawlStatus);

// {
// crawl_last_completed: '2021-03-29 14:07:26.218216+00:00',
// crawl_status: 'crawling',
// ip_address: '18.218.193.164',
// port: 80,
// protocol: 'http'
// }
```

### Start Crawl

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 response = CV.startCrawl(CV_NetworkId);

console.log(response);

// {
// crawl_last_completed: '2021-03-29 14:07:26.218216+00:00',
// crawl_status: 'crawling',
// ip_address: '18.218.193.164',
// port: 80,
// protocol: 'http'
// }
```

### Stopping Crawl

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 response = CV.stopCrawl(CV_NetworkId);

console.log(response);

// {
// crawl_last_completed: '2021-03-29 14:20:29.265859+00:00',
// crawl_status: 'stop_requested',
// ip_address: '18.218.193.164',
// port: 80,
// protocol: 'http'
// }
```
19 changes: 3 additions & 16 deletions src/account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createAccountData, uint8arrayToHex, hexToUint8Array } from "./utils";
import type { BlockData, BlockMessage, SignedData, SignedMessage, Transaction } from "./models";
import type { BlockData, BlockMessage, SignedMessage, Transaction } from "./models";
import { sign } from "tweetnacl";

type AccountKeys = [Uint8Array, Uint8Array];
Expand Down Expand Up @@ -77,29 +77,16 @@ export class Account {
return signature.substring(0, 128);
}

/**
* Creates a signed data object.
* @param data the data to be used to generate the signature
* @returns the signed data object
*/
createSignedData<T>(data: T): SignedData<T> {
return {
data,
signature: this.createSignature(JSON.stringify(data)),
};
}

/**
* Creates a signed data message with the given data.
* @param data the data to be passed along in the message
* @returns the signed message
*/
createSignedMessage<T>(data: T): SignedMessage<T> {
const { data: _data, signature } = this.createSignedData(data);
return {
data: _data,
message: data,
node_identifier: this.accountNumberHex,
signature,
signature: this.createSignature(JSON.stringify(data)),
};
}

Expand Down
26 changes: 26 additions & 0 deletions src/bank.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ServerNode } from "./server-node";
import { PrimaryValidator } from "./primary-validator";
import type {
CrawlCommand,
PaginationOptions,
BankConfigResponse,
Transaction,
Expand Down Expand Up @@ -81,6 +82,31 @@ export class Bank extends ServerNode {
return await super.getData<BankConfigResponse>("/config");
}

/** Gets the current crawl status */
async getCrawlStatus() {
return await super.getData("/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);
}

/**
* 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._postCrawl(command, account);
}

/**
* Gets the confirmation blocks for the given bank.
* @param options The optional object for the pagination options.
Expand Down
27 changes: 26 additions & 1 deletion 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 } from "./models";
import type { ConfirmationValidatorConfigResponse, CrawlData, CrawlCommand } from "./models";

/** Used for connecting with and using confirmation validator server nodes. */
export class ConfirmationValidator extends Validator {
Expand All @@ -9,6 +9,31 @@ export class ConfirmationValidator extends Validator {
return await super.getData("/bank_confirmation_services");
}

/** Gets the current crawl status */
async getCrawlStatus() {
return await super.getData("/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 Confirmation Validator
*/
async startCrawl(account: Account) {
const command: CrawlCommand = "start";

return await super._postCrawl(command, account);
}

/**
* 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 Confirmation Validator
*/
async stopCrawl(account: Account) {
const command: CrawlCommand = "stop";

return await super._postCrawl(command, account);
}

// TODO: POST /confirmation_blocks

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

/** The model for crawl data. */
export interface CrawlData {
crawl: CrawlCommand;
}
2 changes: 1 addition & 1 deletion src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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 { ServerNodeOptions } from "./server-node-options";
export type { PaymentHandlerOptions } from "./payment-handler-options";
export type { AccountPaymentHandlerOptions } from "./account-payment-handler-options";
export type { PaginationOptions } from "./pagination-options";
export type { SignedData } from "./signed-data";
export type { SignedMessage } from "./signed-message";
export type { Transaction } from "./transaction";
export * from "./responses";
4 changes: 2 additions & 2 deletions src/models/responses/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type NodeType = "BANK";
export type NodeType = "BANK" | "CONFIRMATION_VALIDATOR" | "PRIMARY_VALIDATOR";
export type Version = "v1.0";
export type Protocol = "http" | "https";
export type Port = string;
export type Port = number;
export type Hex = string;
export type Trust = string;
export type Origin = string;
Expand Down
10 changes: 10 additions & 0 deletions src/models/responses/generic/crawl.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 CrawlResponse {
crawl_last_completed: string;
crawl_status: string;
ip_address: Origin;
port: Port;
protocol: Protocol;
}
7 changes: 4 additions & 3 deletions src/models/signed-message.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SignedData } from "./signed-data";
/** The interface for the signed message that can send data to the server nodes. */

/** The interface for user authenticated messages. */
export interface SignedMessage<T> extends SignedData<T> {
export interface SignedMessage<T> {
message: T;
node_identifier: string;
signature: string;
}
Loading