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/Node: Block wrapper and Basic block parents #1001

Merged
merged 46 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
c69f741
added parents
Aug 10, 2023
b5e8077
format
Aug 10, 2023
98d6d6e
wrapper
Aug 10, 2023
15cdb4b
bigint rename
Aug 10, 2023
aadb3e1
wrapper imports
Aug 10, 2023
aeca172
fmt
Aug 10, 2023
53a7e3c
renamed
Aug 10, 2023
d49b1be
examples
Aug 10, 2023
0d9794d
removed boxedslice
Aug 10, 2023
3f8cee6
ids and getters
Aug 10, 2023
3f2923a
readonly
Aug 10, 2023
a65433c
removed unused deps
Aug 10, 2023
1bd035c
fmt...
Aug 10, 2023
5fe919f
readonly, removed getters
Aug 11, 2023
8c77ada
renamed to Block
Aug 11, 2023
06035d1
updated Slotcommit and u64 usage, renamed some files
Aug 14, 2023
01cf0b9
fixed file name
Aug 14, 2023
1cb6685
removed unused blokc tyle
Aug 16, 2023
7e98a86
Update bindings/nodejs/lib/types/block/core/block.ts
kwek20 Aug 15, 2023
f1b69be
rewrote a bit
Aug 21, 2023
b41e6b9
lint
Aug 21, 2023
a497015
moved comments to block instead of parents
Aug 22, 2023
6e08683
init new
Sep 14, 2023
745119e
examples and types
Sep 15, 2023
2d98f89
format
Sep 15, 2023
2764e0d
removed type from basic
Sep 15, 2023
62e6f9d
review
Sep 15, 2023
b0b4920
Merge branch '2.0' into node/block-wrapper
Sep 15, 2023
ce8f4aa
comment
Sep 18, 2023
41f88a0
circle begone
Sep 18, 2023
7fd85bd
Merge branch 'node/block-wrapper' of https://github.com/kwek20/iota-s…
Sep 18, 2023
6360bd0
fmt
Sep 18, 2023
8930beb
fmt
Sep 18, 2023
14b9588
revert merge
Sep 18, 2023
1ead6a6
readonly
Sep 18, 2023
603d837
Merge branch '2.0' into node/block-wrapper
Sep 18, 2023
05ad296
removed numbers
Sep 18, 2023
2eeafa8
Merge branch 'node/block-wrapper' of https://github.com/kwek20/iota-s…
Sep 18, 2023
bcd8b30
Merge branch '2.0' into node/block-wrapper
Sep 18, 2023
5234e92
u64
Sep 19, 2023
d83b414
.
Sep 19, 2023
b398f6e
comment
Sep 19, 2023
7555ce3
good catch
Sep 19, 2023
bf79652
Merge branch '2.0' into node/block-wrapper
Sep 19, 2023
30a2c79
update import again
Sep 19, 2023
ed64bbc
Update bindings/nodejs/lib/types/block/core/index.ts
thibault-martinez Sep 19, 2023
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: 6 additions & 3 deletions bindings/nodejs/examples/client/08-data-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ async function run() {
const fetchedBlock = await client.getBlock(blockIdAndBlock[0]);
console.log('Block data: ', fetchedBlock);

if (fetchedBlock.payload instanceof TaggedDataPayload) {
const payload = fetchedBlock.payload as TaggedDataPayload;
console.log('Decoded data:', hexToUtf8(payload.data));
if (fetchedBlock.isBasic()) {
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
const basic = fetchedBlock.asBasic();
if (basic.payload instanceof TaggedDataPayload) {
const payload = basic.payload as TaggedDataPayload;
console.log('Decoded data:', hexToUtf8(payload.data));
}
}
} catch (error) {
console.error('Error: ', error);
Expand Down
11 changes: 7 additions & 4 deletions bindings/nodejs/examples/client/10-mqtt.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright 2021-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { Block, Client, initLogger } from '@iota/sdk';
import { plainToInstance } from 'class-transformer';
import { Client, initLogger, parseBlockWrapper } from '@iota/sdk';

require('dotenv').config({ path: '.env' });

Expand Down Expand Up @@ -34,8 +33,12 @@ async function run() {

const parsed = JSON.parse(data);
if (parsed.topic == 'blocks') {
const block = plainToInstance(Block, JSON.parse(parsed.payload));
console.log('payload:', block.payload);
const block = parseBlockWrapper(JSON.parse(parsed.payload));

if (block.isBasic()) {
const basic = block.asBasic();
console.log('payload:', basic.payload);
}
}
};

Expand Down
1 change: 0 additions & 1 deletion bindings/nodejs/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
},
"dependencies": {
"@iota/sdk": "link:../",
"class-transformer": "^0.5.1",
"dotenv": "^16.0.0",
"ts-node": "^10.9.1"
},
Expand Down
36 changes: 21 additions & 15 deletions bindings/nodejs/lib/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import {
UnlockCondition,
Payload,
TransactionPayload,
parseBlockWrapper,
BlockWrapper,
} from '../types/block';
import { HexEncodedString } from '../utils';
import {
Expand Down Expand Up @@ -168,16 +170,16 @@ export class Client {
* @param blockId The corresponding block ID of the requested block.
* @returns The requested block.
*/
async getBlock(blockId: BlockId): Promise<Block> {
async getBlock(blockId: BlockId): Promise<BlockWrapper> {
const response = await this.methodHandler.callMethod({
name: 'getBlock',
data: {
blockId,
},
});

const parsed = JSON.parse(response) as Response<Block>;
return plainToInstance(Block, parsed.payload);
const parsed = JSON.parse(response) as Response<BlockWrapper>;
return parseBlockWrapper(parsed.payload);
}

/**
Expand Down Expand Up @@ -271,15 +273,17 @@ export class Client {
* @param payload The payload to post.
* @returns The block ID followed by the block containing the payload.
*/
async postBlockPayload(payload: Payload): Promise<[BlockId, Block]> {
async postBlockPayload(payload: Payload): Promise<[BlockId, BlockWrapper]> {
const response = await this.methodHandler.callMethod({
name: 'postBlockPayload',
data: {
payload,
},
});
const parsed = JSON.parse(response) as Response<[BlockId, Block]>;
const block = plainToInstance(Block, parsed.payload[1]);
const parsed = JSON.parse(response) as Response<
[BlockId, BlockWrapper]
>;
const block = parseBlockWrapper(parsed.payload[1]);
return [parsed.payload[0], block];
}

Expand Down Expand Up @@ -417,15 +421,15 @@ export class Client {
* @param transactionId The ID of the transaction.
* @returns The included block that contained the transaction.
*/
async getIncludedBlock(transactionId: string): Promise<Block> {
async getIncludedBlock(transactionId: string): Promise<BlockWrapper> {
const response = await this.methodHandler.callMethod({
name: 'getIncludedBlock',
data: {
transactionId,
},
});
const parsed = JSON.parse(response) as Response<Block>;
return plainToInstance(Block, parsed.payload);
const parsed = JSON.parse(response) as Response<BlockWrapper>;
return parseBlockWrapper(parsed.payload);
}

/**
Expand All @@ -434,15 +438,17 @@ export class Client {
* @param transactionId The ID of the transaction.
* @returns The included block that contained the transaction.
*/
async getIncludedBlockMetadata(transactionId: string): Promise<Block> {
async getIncludedBlockMetadata(
transactionId: string,
): Promise<BlockWrapper> {
const response = await this.methodHandler.callMethod({
name: 'getIncludedBlockMetadata',
data: {
transactionId,
},
});
const parsed = JSON.parse(response) as Response<Block>;
return plainToInstance(Block, parsed.payload);
const parsed = JSON.parse(response) as Response<BlockWrapper>;
return parseBlockWrapper(parsed.payload);
}

/**
Expand Down Expand Up @@ -661,15 +667,15 @@ export class Client {
* @param blockIds An array of `BlockId`s.
* @returns An array of corresponding blocks.
*/
async findBlocks(blockIds: BlockId[]): Promise<Block[]> {
async findBlocks(blockIds: BlockId[]): Promise<BlockWrapper[]> {
const response = await this.methodHandler.callMethod({
name: 'findBlocks',
data: {
blockIds,
},
});
const parsed = JSON.parse(response) as Response<Block[]>;
return plainToInstance(Block, parsed.payload);
const parsed = JSON.parse(response) as Response<BlockWrapper[]>;
return parsed.payload.map((p) => parseBlockWrapper(p));
}

/**
Expand Down
32 changes: 0 additions & 32 deletions bindings/nodejs/lib/types/block/block.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { AccountId } from '../..';
import { u16 } from '../../utils/type_aliases';
import { u16 } from '../../utils/type-aliases';
import { SlotCommitmentId } from '../slot';

enum ContextInputType {
Expand Down
38 changes: 38 additions & 0 deletions bindings/nodejs/lib/types/block/core/basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { Payload, PayloadDiscriminator } from '../payload';
import { Type } from 'class-transformer';
import { StrongParents, WeakParents, ShallowLikeParents } from '../parents';
import { Block } from './block';
import { u64 } from '../../utils';

/**
* Basic Block layout.
*/
export class BasicBlock extends Block {
/**
* Blocks that are strongly directly approved.
*/
readonly strongParents!: StrongParents;
/**
* Blocks that are weakly directly approved.
*/
readonly weakParents!: WeakParents;
/**
* Blocks that are directly referenced to adjust opinion.
*/
readonly shallowLikeParents!: ShallowLikeParents;
/**
* The payload contents.
*/
@Type(() => Payload, {
discriminator: PayloadDiscriminator,
})
readonly payload?: Payload;
/**
* The amount of mana the Account identified by IssuerID is at most
* willing to burn for this block.
*/
readonly burnedMana!: u64;
}
43 changes: 43 additions & 0 deletions bindings/nodejs/lib/types/block/core/block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { BasicBlock } from './basic';
/**
* All of the block types.
*/
export enum BlockType {
/// A Basic block.
Basic = 0,
}

export abstract class Block {
readonly type: BlockType;

kwek20 marked this conversation as resolved.
Show resolved Hide resolved
/**
* @param type The type of Block.
*/
constructor(type: BlockType) {
this.type = type;
}

/**
* Checks whether the block is a `BasicBlock`.
* @returns true if it is, otherwise false
*/
isBasic(): boolean {
return this.type === BlockType.Basic;
}

/**
* Gets the block as an actual `BasicBlock`.
* NOTE: Will throw an error if the block is not a `BasicBlock`.
* @returns The block
*/
asBasic(): BasicBlock {
if (this.isBasic()) {
return this as unknown as BasicBlock;
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
} else {
throw new Error('invalid downcast of non-BasicBlock');
}
}
}
15 changes: 15 additions & 0 deletions bindings/nodejs/lib/types/block/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { BlockType } from './block';
import { BasicBlock } from './basic';

export * from './block';
export * from './basic';
export * from './wrapper';

// Here because in block.ts it causes a circular dependency
export const BlockDiscriminator = {
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
property: 'type',
subTypes: [{ value: BasicBlock, name: BlockType.Basic as any }],
};
Loading