Skip to content

Commit

Permalink
Update examples and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoralf-M committed Jun 26, 2023
1 parent 098d3ef commit 3d3a438
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 67 deletions.
5 changes: 4 additions & 1 deletion bindings/nodejs/examples/client/06-simple-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ async function run() {

try {
// Create block with no payload
const blockIdAndBlock = await client.buildAndPostBlock();
// TODO: have a way in the bindings to send an empty block
const blockIdAndBlock = await client.postBlockPayload(
new TaggedDataPayload(utf8ToHex('Hello'), utf8ToHex('Tangle')),
);
console.log('Block:', blockIdAndBlock, '\n');

console.log(
Expand Down
12 changes: 2 additions & 10 deletions bindings/nodejs/examples/client/08-data-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,10 @@ async function run() {
nodes: [process.env.NODE_URL],
});

const options = {
tag: utf8ToHex('Hello'),
data: utf8ToHex('Tangle'),
};
try {
const mnemonic = Utils.generateMnemonic();
const secretManager = { mnemonic: mnemonic };

// Create block with tagged payload
const blockIdAndBlock = await client.buildAndPostBlock(
secretManager,
options,
const blockIdAndBlock = await client.postBlockPayload(
new TaggedDataPayload(utf8ToHex('Hello'), utf8ToHex('Tangle')),
);

console.log(
Expand Down
43 changes: 12 additions & 31 deletions bindings/nodejs/tests/client/examples.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { describe, it } from '@jest/globals';
import { Client, utf8ToHex, Utils, Block, OutputResponse, SecretManager, TaggedDataPayload, CommonOutput } from '../../';
import { Client, utf8ToHex, Utils, OutputResponse, SecretManager, TaggedDataPayload, CommonOutput } from '../../';
import '../customMatchers';
import 'dotenv/config';
import * as addressOutputs from '../fixtures/addressOutputs.json';
Expand Down Expand Up @@ -139,50 +139,31 @@ describe.skip('Main examples', () => {
).toBe(200);
});

it('sends a block', async () => {
const blockIdAndBlock = await client.buildAndPostBlock();
// TODO: have a way in the bindings to send an empty block
// it('sends a block', async () => {
// const blockIdAndBlock = await client.buildAndPostBlock();

expect(blockIdAndBlock[0]).toBeValidBlockId();
});
// expect(blockIdAndBlock[0]).toBeValidBlockId();
// });

it('gets block data', async () => {
const blockIdAndBlock = await client.buildAndPostBlock();
const tips = await client.getTips();

const blockData = await client.getBlock(blockIdAndBlock[0]);
const blockMetadata = await client.getBlockMetadata(blockIdAndBlock[0]);
const blockData = await client.getBlock(tips[0]);
const blockId = Utils.blockId(blockData)
expect(tips[0]).toStrictEqual(blockId);

expect(blockData).toStrictEqual<Block>(blockIdAndBlock[1]);
const blockMetadata = await client.getBlockMetadata(tips[0]);
expect(blockMetadata.blockId).toBeValidBlockId();
});

it('sends a block with a tagged data payload', async () => {
const blockIdAndBlock = await client.buildAndPostBlock(secretManager, {
tag: utf8ToHex('Hello'),
data: utf8ToHex('Tangle'),
});
const blockIdAndBlock = await client.postBlockPayload(new TaggedDataPayload( utf8ToHex('Hello'), utf8ToHex('Tangle')));

const fetchedBlock = await client.getBlock(blockIdAndBlock[0]);

expect(fetchedBlock.payload).toStrictEqual(
new TaggedDataPayload( utf8ToHex('Hello'), utf8ToHex('Tangle'))
);
});

it('sends a transaction', async () => {
const addresses = await new SecretManager(secretManager).generateEd25519Addresses({
range: {
start: 1,
end: 2,
},
});

const blockIdAndBlock = await client.buildAndPostBlock(secretManager, {
output: {
address: addresses[0],
amount: '1000000',
},
});

expect(blockIdAndBlock[0]).toBeValidBlockId();
});
});
28 changes: 14 additions & 14 deletions bindings/nodejs/tests/client/messageMethods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { describe, it } from '@jest/globals';
import 'reflect-metadata';
import 'dotenv/config';

import { Client } from '../../lib/client';
import { Client, utf8ToHex, TaggedDataPayload } from '../../';
import '../customMatchers';

const client = new Client({
Expand All @@ -20,7 +20,7 @@ const client = new Client({
// Skip for CI
describe.skip('Block methods', () => {
it('sends a block raw', async () => {
const blockIdAndBlock = await client.buildAndPostBlock();
const blockIdAndBlock = await client.postBlockPayload(new TaggedDataPayload( utf8ToHex('Hello'), utf8ToHex('Tangle')));

const blockId = await client.postBlockRaw(blockIdAndBlock[1]);

Expand All @@ -35,52 +35,52 @@ describe.skip('Block methods', () => {
});

it('gets block as raw bytes', async () => {
const blockIdAndBlock = await client.buildAndPostBlock();
const tips = await client.getTips();

const blockRaw = await client.getBlockRaw(blockIdAndBlock[0]);
const blockRaw = await client.getBlockRaw(tips[0]);

expect(blockRaw).toBeDefined();
});

it('promotes a block', async () => {
const blockIdAndBlock = await client.buildAndPostBlock();
const tips = await client.getTips();

// Promote a block without checking if it should be promoted
const promoteUnchecked = await client.promoteUnchecked(
blockIdAndBlock[0],
tips[0],
);

expect(promoteUnchecked[1].parents).toContain(blockIdAndBlock[0]);
expect(promoteUnchecked[1].parents).toContain(tips[0]);

// Returns expected error: no need to promote or reattach.
await expect(client.promote(blockIdAndBlock[0])).rejects.toMatch(
await expect(client.promote(tips[0])).rejects.toMatch(
'NoNeedPromoteOrReattach',
);
});

it('reattaches a block', async () => {
const blockIdAndBlock = await client.buildAndPostBlock();
const tips = await client.getTips();

// Reattach a block without checking if it should be reattached
const reattachUnchecked = await client.reattachUnchecked(
blockIdAndBlock[0],
tips[0],
);

expect(reattachUnchecked[0]).toBeValidBlockId();
expect(reattachUnchecked[1]).toBeDefined();

// Returns expected error: no need to promote or reattach.
await expect(client.reattach(blockIdAndBlock[0])).rejects.toMatch(
await expect(client.reattach(tips[0])).rejects.toMatch(
'NoNeedPromoteOrReattach',
);
});

// Skip by default, retryUntilIncluded can be slow
it.skip('retries a block', async () => {
const blockIdAndBlock = await client.buildAndPostBlock();
const blockId = await client.postBlock(blockIdAndBlock[1]);
const tips = await client.getTips();
const blockId = await client.postBlock(tips[1]);

expect(blockIdAndBlock[0]).toBe(blockId);
expect(tips[0]).toBe(blockId);

// Retries (promotes or reattaches) a block for provided block id until it's included
// (referenced by a milestone). Default interval is 5 seconds and max attempts is 40.
Expand Down
5 changes: 3 additions & 2 deletions bindings/python/examples/client/06_simple_block.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from iota_sdk import Client
from iota_sdk import Client, TaggedDataPayload, utf8_to_hex
from dotenv import load_dotenv
import os

Expand All @@ -10,5 +10,6 @@
client = Client(nodes=[node_url])

# Create and post a block without payload
block = client.build_and_post_block()
# TODO: have a way in the bindings to send an empty block
block = client.submit_payload(TaggedDataPayload(utf8_to_hex("tag"), utf8_to_hex("data")))
print(f'Empty block sent: {os.environ["EXPLORER_URL"]}/block/{block[0]}')
5 changes: 2 additions & 3 deletions bindings/python/examples/client/08_data_block.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from iota_sdk import Client, utf8_to_hex, hex_to_utf8
from iota_sdk import Client, utf8_to_hex, hex_to_utf8, TaggedDataPayload
from dotenv import load_dotenv
import json
import os
Expand All @@ -11,8 +11,7 @@
client = Client(nodes=[node_url])

# Create and post a block with a tagged data payload
block = client.build_and_post_block(
tag=utf8_to_hex('hello'), data=utf8_to_hex('hello'))
block = client.submit_payload(TaggedDataPayload(utf8_to_hex("tag"), utf8_to_hex("data")))

print(f'Data block sent: {os.environ["EXPLORER_URL"]}/block/{block[0]}')

Expand Down
4 changes: 2 additions & 2 deletions bindings/python/examples/client/post_raw_block.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from iota_sdk import Client, MnemonicSecretManager
from iota_sdk import Client, TaggedDataPayload, utf8_to_hex
from dotenv import load_dotenv
import os

Expand All @@ -10,7 +10,7 @@
client = Client(nodes=[node_url])

# Create and post a block without payload
block_id = client.build_and_post_block()[0]
block_id = block = client.submit_payload(TaggedDataPayload(utf8_to_hex("tag"), utf8_to_hex("data")))[0]
blockBytes = client.get_block_raw(block_id)

# Post raw block
Expand Down
7 changes: 3 additions & 4 deletions bindings/python/examples/client/submit_and_read_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# Import the python iota client
# Make sure you have first installed it with `pip install iota_sdk`
from iota_sdk import Client, hex_to_utf8, utf8_to_hex
from iota_sdk import Client, hex_to_utf8, utf8_to_hex, TaggedDataPayload
from dotenv import load_dotenv
import os

Expand Down Expand Up @@ -51,12 +51,11 @@
# Step 2: Submit your block to the Shimmer test network
########################################################
# A block must be built, to which the payload is attached.
# The build_and_post_block function handles this task.
# The submit_payload function handles this task.

# Create and post a block with a tagged data payload
# The client returns your block data (blockIdAndBlock)
blockIdAndBlock = client.build_and_post_block(
secret_manager=None, tag=tag_hex, data=message_hex)
blockIdAndBlock = client.submit_payload(TaggedDataPayload(utf8_to_hex("tag"), utf8_to_hex("data")))

block_id = blockIdAndBlock[0]
block = blockIdAndBlock[1]
Expand Down

0 comments on commit 3d3a438

Please sign in to comment.