From 3d3a4381590ff584a22d08de6190d67df84003cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thoralf=20M=C3=BCller?= Date: Sat, 24 Jun 2023 10:21:49 +0200 Subject: [PATCH] Update examples and tests --- .../nodejs/examples/client/06-simple-block.ts | 5 ++- .../nodejs/examples/client/08-data-block.ts | 12 +----- bindings/nodejs/tests/client/examples.spec.ts | 43 ++++++------------- .../tests/client/messageMethods.spec.ts | 28 ++++++------ .../python/examples/client/06_simple_block.py | 5 ++- .../python/examples/client/08_data_block.py | 5 +-- .../python/examples/client/post_raw_block.py | 4 +- .../examples/client/submit_and_read_block.py | 7 ++- 8 files changed, 42 insertions(+), 67 deletions(-) diff --git a/bindings/nodejs/examples/client/06-simple-block.ts b/bindings/nodejs/examples/client/06-simple-block.ts index 221b9e280c..2087347bc9 100644 --- a/bindings/nodejs/examples/client/06-simple-block.ts +++ b/bindings/nodejs/examples/client/06-simple-block.ts @@ -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( diff --git a/bindings/nodejs/examples/client/08-data-block.ts b/bindings/nodejs/examples/client/08-data-block.ts index 96dd2f4480..a90fde62e5 100644 --- a/bindings/nodejs/examples/client/08-data-block.ts +++ b/bindings/nodejs/examples/client/08-data-block.ts @@ -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( diff --git a/bindings/nodejs/tests/client/examples.spec.ts b/bindings/nodejs/tests/client/examples.spec.ts index 0e2a61cef4..2701ea0a20 100644 --- a/bindings/nodejs/tests/client/examples.spec.ts +++ b/bindings/nodejs/tests/client/examples.spec.ts @@ -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'; @@ -139,27 +139,26 @@ 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(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]); @@ -167,22 +166,4 @@ describe.skip('Main examples', () => { 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(); - }); }); diff --git a/bindings/nodejs/tests/client/messageMethods.spec.ts b/bindings/nodejs/tests/client/messageMethods.spec.ts index d5cecaf0f1..85c52cf4aa 100644 --- a/bindings/nodejs/tests/client/messageMethods.spec.ts +++ b/bindings/nodejs/tests/client/messageMethods.spec.ts @@ -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({ @@ -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]); @@ -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. diff --git a/bindings/python/examples/client/06_simple_block.py b/bindings/python/examples/client/06_simple_block.py index 54753d5fd4..11d426d7bd 100644 --- a/bindings/python/examples/client/06_simple_block.py +++ b/bindings/python/examples/client/06_simple_block.py @@ -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 @@ -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]}') \ No newline at end of file diff --git a/bindings/python/examples/client/08_data_block.py b/bindings/python/examples/client/08_data_block.py index 1b57b74ac6..f7a7a9658c 100644 --- a/bindings/python/examples/client/08_data_block.py +++ b/bindings/python/examples/client/08_data_block.py @@ -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 @@ -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]}') diff --git a/bindings/python/examples/client/post_raw_block.py b/bindings/python/examples/client/post_raw_block.py index 102bb33234..57dd3842fa 100644 --- a/bindings/python/examples/client/post_raw_block.py +++ b/bindings/python/examples/client/post_raw_block.py @@ -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 @@ -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 diff --git a/bindings/python/examples/client/submit_and_read_block.py b/bindings/python/examples/client/submit_and_read_block.py index 2d94e01f94..23ea0fa2dd 100644 --- a/bindings/python/examples/client/submit_and_read_block.py +++ b/bindings/python/examples/client/submit_and_read_block.py @@ -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 @@ -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]