Skip to content

Commit 59f7202

Browse files
authored
1718 new net p1 code review and test thorblock package (#1836)
* feat: added support for raw and expanded block requests * fix: fixed blockId instantiation * fix: fixed naming and missing things * chore: comment to add gas unit class when available * feat: spllited RetrieveBlock into 3 different classes/files and added negative tests * fix: matched errors on expected test failures and increased code coverage * fix: fixed regex in tests
1 parent 98b6859 commit 59f7202

13 files changed

+551
-65
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
1-
import { Address, ThorId, UInt, Units, VTHO } from '@vechain/sdk-core';
1+
import { Address, BlockId, ThorId, UInt, Units, VTHO } from '@vechain/sdk-core';
22

3-
class RegularBlockResponse {
3+
interface CommmonBlockResponseJSON {
4+
number: number;
5+
id: string;
6+
size: number;
7+
parentID: string;
8+
timestamp: number;
9+
gasLimit: number;
10+
beneficiary: string;
11+
gasUsed: number;
12+
totalScore: number;
13+
txsRoot: string;
14+
txsFeatures: number;
15+
stateRoot: string;
16+
receiptsRoot: string;
17+
com: boolean;
18+
signer: string;
19+
isTrunk: boolean;
20+
isFinalized: boolean;
21+
}
22+
23+
class CommmonBlockResponse {
424
readonly number: UInt;
5-
readonly id: ThorId;
25+
readonly id: BlockId;
626
readonly size: UInt;
7-
readonly parentID: ThorId;
8-
readonly timestamp: bigint;
27+
readonly parentID: BlockId;
28+
readonly timestamp: UInt;
29+
// to do: use the Gas unit class when it will be available
930
readonly gasLimit: VTHO;
1031
readonly beneficiary: Address;
1132
readonly gasUsed: VTHO;
@@ -18,14 +39,13 @@ class RegularBlockResponse {
1839
readonly signer: Address;
1940
readonly isTrunk: boolean;
2041
readonly isFinalized: boolean;
21-
readonly transactions: ThorId[];
2242

23-
constructor(json: RegularBlockResponseJSON) {
43+
constructor(json: CommmonBlockResponseJSON) {
2444
this.number = UInt.of(json.number);
25-
this.id = ThorId.of(json.id);
45+
this.id = BlockId.of(json.id);
2646
this.size = UInt.of(json.size);
27-
this.parentID = ThorId.of(json.parentID);
28-
this.timestamp = json.timestamp;
47+
this.parentID = BlockId.of(json.parentID);
48+
this.timestamp = UInt.of(json.timestamp);
2949
this.gasLimit = VTHO.of(json.gasLimit, Units.wei);
3050
this.beneficiary = Address.of(json.beneficiary);
3151
this.gasUsed = VTHO.of(json.gasUsed, Units.wei);
@@ -38,18 +58,15 @@ class RegularBlockResponse {
3858
this.signer = Address.of(json.signer);
3959
this.isTrunk = json.isTrunk;
4060
this.isFinalized = json.isFinalized;
41-
this.transactions = json.transactions.map(
42-
(txId: string): ThorId => ThorId.of(txId)
43-
);
4461
}
4562

46-
toJSON(): RegularBlockResponseJSON {
63+
toJSON(): CommmonBlockResponseJSON {
4764
return {
4865
number: this.number.valueOf(),
4966
id: this.id.toString(),
5067
size: this.size.valueOf(),
5168
parentID: this.parentID.toString(),
52-
timestamp: this.timestamp,
69+
timestamp: this.timestamp.valueOf(),
5370
gasLimit: Number(this.gasLimit.wei),
5471
beneficiary: this.beneficiary.toString(),
5572
gasUsed: Number(this.gasUsed.wei),
@@ -61,33 +78,9 @@ class RegularBlockResponse {
6178
com: this.com,
6279
signer: this.signer.toString(),
6380
isTrunk: this.isTrunk,
64-
isFinalized: this.isFinalized,
65-
transactions: this.transactions.map((txId: ThorId) =>
66-
txId.toString()
67-
)
68-
} satisfies RegularBlockResponseJSON;
81+
isFinalized: this.isFinalized
82+
} satisfies CommmonBlockResponseJSON;
6983
}
7084
}
7185

72-
interface RegularBlockResponseJSON {
73-
number: number;
74-
id: string;
75-
size: number;
76-
parentID: string;
77-
timestamp: bigint;
78-
gasLimit: number;
79-
beneficiary: string;
80-
gasUsed: number;
81-
totalScore: number;
82-
txsRoot: string;
83-
txsFeatures: number;
84-
stateRoot: string;
85-
receiptsRoot: string;
86-
com: boolean;
87-
signer: string;
88-
isTrunk: boolean;
89-
isFinalized: boolean;
90-
transactions: string[];
91-
}
92-
93-
export { RegularBlockResponse, type RegularBlockResponseJSON };
86+
export { type CommmonBlockResponseJSON, CommmonBlockResponse };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { BlockId } from '@vechain/sdk-core';
2+
import {
3+
GetTxResponse,
4+
type GetTxResponseJSON,
5+
Receipt,
6+
type ReceiptJSON
7+
} from '../transactions';
8+
import {
9+
CommmonBlockResponse,
10+
type CommmonBlockResponseJSON
11+
} from './CommonBlockResponse';
12+
13+
class TransactionWithOutputs {
14+
readonly transaction: Omit<GetTxResponse, 'meta'>;
15+
readonly receipt: Receipt;
16+
17+
constructor(json: TransactionWithOutputsJSON) {
18+
const transactionWithoutMeta = new GetTxResponse({
19+
...json,
20+
meta: {
21+
blockID: BlockId.of(0).fit(64).toString(),
22+
blockNumber: 0,
23+
blockTimestamp: 0n
24+
}
25+
});
26+
27+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
28+
const { meta, ...transactionWithoutMetaNoMeta } =
29+
transactionWithoutMeta;
30+
this.transaction = {
31+
...transactionWithoutMetaNoMeta,
32+
toJSON: () => transactionWithoutMeta.toJSON()
33+
} satisfies Omit<GetTxResponse, 'meta'>;
34+
this.receipt = new Receipt(json);
35+
}
36+
37+
toJSON(): TransactionWithOutputsJSON {
38+
return {
39+
...this.transaction.toJSON(),
40+
...this.receipt.toJSON()
41+
} satisfies TransactionWithOutputsJSON;
42+
}
43+
}
44+
45+
type TransactionWithOutputsJSON = Omit<GetTxResponseJSON, 'meta'> & ReceiptJSON;
46+
47+
class ExpandedBlockResponse extends CommmonBlockResponse {
48+
readonly transactions: TransactionWithOutputs[];
49+
50+
constructor(json: ExpandedBlockResponseJSON) {
51+
super(json);
52+
this.transactions = json.transactions.map(
53+
(txId: TransactionWithOutputsJSON): TransactionWithOutputs =>
54+
new TransactionWithOutputs(txId)
55+
);
56+
}
57+
58+
toJSON(): ExpandedBlockResponseJSON {
59+
return {
60+
...super.toJSON(),
61+
transactions: this.transactions.map((tx: TransactionWithOutputs) =>
62+
tx.toJSON()
63+
)
64+
};
65+
}
66+
}
67+
68+
interface ExpandedBlockResponseJSON extends CommmonBlockResponseJSON {
69+
transactions: TransactionWithOutputsJSON[];
70+
}
71+
72+
export { ExpandedBlockResponse, type ExpandedBlockResponseJSON };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Hex } from '@vechain/sdk-core';
2+
3+
class RawBlockResponse {
4+
readonly raw: Hex;
5+
6+
constructor(json: RawBlockResponseJSON) {
7+
this.raw = Hex.of(json.raw);
8+
}
9+
10+
toJSON(): RawBlockResponseJSON {
11+
return {
12+
raw: this.raw.toString()
13+
} satisfies RawBlockResponseJSON;
14+
}
15+
}
16+
17+
interface RawBlockResponseJSON {
18+
raw: string;
19+
}
20+
21+
export { RawBlockResponse, type RawBlockResponseJSON };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { TxId } from '@vechain/sdk-core';
2+
import {
3+
CommmonBlockResponse,
4+
type CommmonBlockResponseJSON
5+
} from './CommonBlockResponse';
6+
7+
class RegularBlockResponse extends CommmonBlockResponse {
8+
readonly transactions: TxId[];
9+
10+
constructor(json: RegularBlockResponseJSON) {
11+
super(json);
12+
this.transactions = json.transactions.map(
13+
(txId: string): TxId => TxId.of(txId)
14+
);
15+
}
16+
17+
toJSON(): RegularBlockResponseJSON {
18+
return {
19+
...super.toJSON(),
20+
transactions: this.transactions.map((txId) => txId.toString())
21+
};
22+
}
23+
}
24+
25+
interface RegularBlockResponseJSON extends CommmonBlockResponseJSON {
26+
transactions: string[];
27+
}
28+
29+
export { RegularBlockResponse, type RegularBlockResponseJSON };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { type HttpClient, type HttpPath } from '../../http';
2+
import { type ThorRequest } from '../ThorRequest';
3+
import { type ThorResponse } from '../ThorResponse';
4+
import {
5+
ExpandedBlockResponse,
6+
type ExpandedBlockResponseJSON
7+
} from './ExpandedBlockResponse';
8+
import { type Revision } from '@vechain/sdk-core';
9+
10+
class RetrieveExpandedBlock
11+
implements ThorRequest<RetrieveExpandedBlock, ExpandedBlockResponse>
12+
{
13+
public readonly path: RetrieveExpandedBlockPath;
14+
15+
constructor(path: RetrieveExpandedBlockPath) {
16+
this.path = path;
17+
}
18+
19+
async askTo(
20+
httpClient: HttpClient
21+
): Promise<ThorResponse<RetrieveExpandedBlock, ExpandedBlockResponse>> {
22+
const response = await httpClient.get(this.path, {
23+
query: '?expanded=true'
24+
});
25+
const responseJSON =
26+
(await response.json()) as ExpandedBlockResponseJSON;
27+
return {
28+
request: this,
29+
response: new ExpandedBlockResponse(responseJSON)
30+
};
31+
}
32+
33+
static of(revision: Revision): RetrieveExpandedBlock {
34+
return new RetrieveExpandedBlock(
35+
new RetrieveExpandedBlockPath(revision)
36+
);
37+
}
38+
}
39+
40+
class RetrieveExpandedBlockPath implements HttpPath {
41+
readonly revision: Revision;
42+
43+
constructor(revision: Revision) {
44+
this.revision = revision;
45+
}
46+
47+
get path(): string {
48+
return `/blocks/${this.revision}`;
49+
}
50+
}
51+
52+
export { RetrieveExpandedBlock, RetrieveExpandedBlockPath };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { type HttpClient, type HttpPath } from '../../http';
2+
import { type ThorRequest } from '../ThorRequest';
3+
import { type ThorResponse } from '../ThorResponse';
4+
import { RawBlockResponse, type RawBlockResponseJSON } from './RawBlockReponse';
5+
import { type Revision } from '@vechain/sdk-core';
6+
7+
class RetrieveRawBlock
8+
implements ThorRequest<RetrieveRawBlock, RawBlockResponse>
9+
{
10+
public readonly path: RetrieveRawBlockPath;
11+
12+
constructor(path: RetrieveRawBlockPath) {
13+
this.path = path;
14+
}
15+
16+
async askTo(
17+
httpClient: HttpClient
18+
): Promise<ThorResponse<RetrieveRawBlock, RawBlockResponse>> {
19+
const response = await httpClient.get(this.path, {
20+
query: '?raw=true'
21+
});
22+
const responseJSON = (await response.json()) as RawBlockResponseJSON;
23+
return {
24+
request: this,
25+
response: new RawBlockResponse(responseJSON)
26+
};
27+
}
28+
29+
static of(revision: Revision): RetrieveRawBlock {
30+
return new RetrieveRawBlock(new RetrieveRawBlockPath(revision));
31+
}
32+
}
33+
34+
class RetrieveRawBlockPath implements HttpPath {
35+
readonly revision: Revision;
36+
37+
constructor(revision: Revision) {
38+
this.revision = revision;
39+
}
40+
41+
get path(): string {
42+
return `/blocks/${this.revision}`;
43+
}
44+
}
45+
46+
export { RetrieveRawBlock, RetrieveRawBlockPath };

packages/thorest/src/thor/blocks/RetrieveBlock.ts packages/thorest/src/thor/blocks/RetrieveRegularBlock.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,36 @@ import { type ThorResponse } from '../ThorResponse';
44
import {
55
RegularBlockResponse,
66
type RegularBlockResponseJSON
7-
} from './RegularBlockResponse.';
7+
} from './RegularBlockResponse';
88
import { type Revision } from '@vechain/sdk-core';
99

10-
class RetrieveBlock
11-
implements ThorRequest<RetrieveBlock, RegularBlockResponse>
10+
class RetrieveRegularBlock
11+
implements ThorRequest<RetrieveRegularBlock, RegularBlockResponse>
1212
{
13-
public readonly path: RetrieveBlockPath;
13+
public readonly path: RetrieveRegularBlockPath;
1414

15-
constructor(path: RetrieveBlockPath) {
15+
constructor(path: RetrieveRegularBlockPath) {
1616
this.path = path;
1717
}
1818

1919
async askTo(
2020
httpClient: HttpClient
21-
): Promise<ThorResponse<RetrieveBlock, RegularBlockResponse>> {
21+
): Promise<ThorResponse<RetrieveRegularBlock, RegularBlockResponse>> {
2222
const response = await httpClient.get(this.path, { query: '' });
23-
const responseBody =
23+
const responseJSON =
2424
(await response.json()) as RegularBlockResponseJSON;
2525
return {
2626
request: this,
27-
response: new RegularBlockResponse(responseBody)
27+
response: new RegularBlockResponse(responseJSON)
2828
};
2929
}
3030

31-
static of(revision: Revision): RetrieveBlock {
32-
return new RetrieveBlock(new RetrieveBlockPath(revision));
31+
static of(revision: Revision): RetrieveRegularBlock {
32+
return new RetrieveRegularBlock(new RetrieveRegularBlockPath(revision));
3333
}
3434
}
3535

36-
class RetrieveBlockPath implements HttpPath {
36+
class RetrieveRegularBlockPath implements HttpPath {
3737
readonly revision: Revision;
3838

3939
constructor(revision: Revision) {
@@ -45,4 +45,4 @@ class RetrieveBlockPath implements HttpPath {
4545
}
4646
}
4747

48-
export { RetrieveBlock, RetrieveBlockPath };
48+
export { RetrieveRegularBlock, RetrieveRegularBlockPath };

0 commit comments

Comments
 (0)