From 7592ea72b85a474bd82b40fc3db9857148342b38 Mon Sep 17 00:00:00 2001 From: elv-preethi <39742400+elv-preethi@users.noreply.github.com> Date: Mon, 8 Jul 2024 15:55:36 -0700 Subject: [PATCH 1/3] update testScripts and publish content method --- src/client/ContentManagement.js | 61 ++++++++++-------- testScripts/BasicObjectOperations.js | 93 ++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 25 deletions(-) create mode 100644 testScripts/BasicObjectOperations.js diff --git a/src/client/ContentManagement.js b/src/client/ContentManagement.js index 85476138..8c21db75 100644 --- a/src/client/ContentManagement.js +++ b/src/client/ContentManagement.js @@ -1006,7 +1006,8 @@ exports.PublishContentVersion = async function({objectId, versionHash, awaitComm }); const abi = await this.ContractAbi({id: objectId}); - const fromBlock = commit.blockNumber + 1; + const fromBlock = commit.blockNumber - 10; // due to block re-org + const objectHash = await this.ExtractValueFromEvent({ abi, event: commit, @@ -1023,18 +1024,21 @@ exports.PublishContentVersion = async function({objectId, versionHash, awaitComm throw Error(`Pending version hash mismatch on ${objectId}: expected ${objectHash}, currently ${pendingHash}`); } + let confirmCommitFound = false; if(awaitCommitConfirmation) { this.Log(`Awaiting commit confirmation for ${objectHash}`); const pollingInterval = this.ethClient.Provider().pollingInterval || 500; + const startTime = Date.now(); + const duration = 2 * 60 * 1000; // 2 minutes - // eslint-disable-next-line no-constant-condition - while(true) { + while(Date.now() - startTime < duration) { await new Promise(resolve => setTimeout(resolve, pollingInterval)); const events = await this.ContractEvents({ contractAddress: this.utils.HashToAddress(objectId), abi, fromBlock, + topics: [ Ethers.utils.id("VersionConfirm(address,address,string)") ], count: 1000 }); @@ -1045,39 +1049,46 @@ exports.PublishContentVersion = async function({objectId, versionHash, awaitComm if(confirmEvent) { // Found confirmation this.Log(`Commit confirmed on chain: ${objectHash}`); + confirmCommitFound=true; break; } } + + if(!confirmCommitFound){ + this.Log(`Commit with hash ${objectHash} was not confirmed on chain, due to confirmCommit event not found within the duration (${duration})`); + } + } - // APIv2 ensure the fabric API returns the correct hash - if(awaitCommitConfirmation) { - const pollingInterval = 500; // ms - let tries = 20; - while(tries > 0) { - let h; - - try { - h = await this.LatestVersionHashV2({objectId}); - - if(h === versionHash) { - this.Log(`Commit confirmed on fabric node: ${versionHash}`); - break; - } else { + if(confirmCommitFound) { + // APIv2 ensure the fabric API returns the correct hash + if(awaitCommitConfirmation) { + const pollingInterval = 500; // ms + let tries = 20; + while(tries > 0) { + let h; + + try { + h = await this.LatestVersionHashV2({objectId}); + + if(h === versionHash) { + this.Log(`Commit confirmed on fabric node: ${versionHash}`); + break; + } else { + tries--; + await new Promise(resolve => setTimeout(resolve, pollingInterval)); + } + } catch(error) { + if(error.status !== 404) { + throw error; + } + tries--; await new Promise(resolve => setTimeout(resolve, pollingInterval)); } - } catch(error) { - if(error.status !== 404) { - throw error; - } - - tries--; - await new Promise(resolve => setTimeout(resolve, pollingInterval)); } } } - }; /** diff --git a/testScripts/BasicObjectOperations.js b/testScripts/BasicObjectOperations.js new file mode 100644 index 00000000..3fab31d4 --- /dev/null +++ b/testScripts/BasicObjectOperations.js @@ -0,0 +1,93 @@ +/* eslint-disable no-console */ + +const { ElvClient } = require("../src/ElvClient"); + +const configUrl = "https://test.net955205.contentfabric.io/config"; +const libraryId = "ilibE6vhm2YCR6vZCtW9mope6Dbo2Tn"; // to create and edit content + +const Tool = async () => { + + const client = await ElvClient.FromConfigurationUrl({configUrl}); + + let wallet = client.GenerateWallet(); + let signer = wallet.AddAccount({ + privateKey: process.env.PRIVATE_KEY + }); + + client.SetSigner({signer}); + client.ToggleLogging(true); + + + try { + let start = new Date().getTime(); + const {objectId, writeToken} = await client.CreateContentObject({ + libraryId: libraryId, + options: { + meta: {commit: "Create Content-" + start.toString()} + } + }); + let end = new Date().getTime(); + let timeDifference = end - start; + console.log("content object completed after: ", timeDifference, "ms"); + + start = new Date().getTime(); + const {hash} = await client.FinalizeContentObject({ + libraryId, + objectId, + writeToken + }); + end = new Date(); + timeDifference = end - start; + console.log("finalize object completed after: ", timeDifference, "ms"); + + start = new Date().getTime(); + const editResponse = await client.EditContentObject({ + libraryId, + objectId, + }); + end = new Date().getTime(); + timeDifference = end - start; + console.log("edit object completed after: ", timeDifference, "ms"); + + const metadata = { + description: "edit content", + }; + + start = new Date().getTime(); + await client.ReplaceMetadata({ + libraryId, + objectId, + writeToken: editResponse.write_token, + metadata + }); + end = new Date().getTime(); + timeDifference = end - start; + console.log("replace metadata completed after: ", timeDifference, "ms"); + + + start = new Date().getTime(); + await client.FinalizeContentObject({ + libraryId, + objectId, + writeToken: editResponse.write_token, + }); + end = new Date().getTime(); + timeDifference = end - start; + console.log("finalize object completed after: ", timeDifference, "ms"); + + console.log(`\nSuccessfully created new content version: ${hash}`); + } catch(error) { + console.error("Error creating content object:"); + console.error(error); + } +}; + +const privateKey = process.env.PRIVATE_KEY; +if(!privateKey) { + console.error("PRIVATE_KEY environment variable must be specified"); + return; +} + +Tool(); + + From 006b7b62e44e949421e6561b5be9281c3b3ffdf5 Mon Sep 17 00:00:00 2001 From: elv-preethi <39742400+elv-preethi@users.noreply.github.com> Date: Tue, 9 Jul 2024 13:24:09 -0700 Subject: [PATCH 2/3] rm timeout --- src/client/ContentManagement.js | 55 ++++++++++++++------------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/src/client/ContentManagement.js b/src/client/ContentManagement.js index 8c21db75..83a6de9f 100644 --- a/src/client/ContentManagement.js +++ b/src/client/ContentManagement.js @@ -1024,14 +1024,12 @@ exports.PublishContentVersion = async function({objectId, versionHash, awaitComm throw Error(`Pending version hash mismatch on ${objectId}: expected ${objectHash}, currently ${pendingHash}`); } - let confirmCommitFound = false; if(awaitCommitConfirmation) { this.Log(`Awaiting commit confirmation for ${objectHash}`); const pollingInterval = this.ethClient.Provider().pollingInterval || 500; - const startTime = Date.now(); - const duration = 2 * 60 * 1000; // 2 minutes - while(Date.now() - startTime < duration) { + // eslint-disable-next-line no-constant-condition + while(true) { await new Promise(resolve => setTimeout(resolve, pollingInterval)); const events = await this.ContractEvents({ @@ -1049,43 +1047,36 @@ exports.PublishContentVersion = async function({objectId, versionHash, awaitComm if(confirmEvent) { // Found confirmation this.Log(`Commit confirmed on chain: ${objectHash}`); - confirmCommitFound=true; break; } } - - if(!confirmCommitFound){ - this.Log(`Commit with hash ${objectHash} was not confirmed on chain, due to confirmCommit event not found within the duration (${duration})`); - } - } - if(confirmCommitFound) { - // APIv2 ensure the fabric API returns the correct hash - if(awaitCommitConfirmation) { - const pollingInterval = 500; // ms - let tries = 20; - while(tries > 0) { - let h; - - try { - h = await this.LatestVersionHashV2({objectId}); - - if(h === versionHash) { - this.Log(`Commit confirmed on fabric node: ${versionHash}`); - break; - } else { - tries--; - await new Promise(resolve => setTimeout(resolve, pollingInterval)); - } - } catch(error) { - if(error.status !== 404) { - throw error; - } + // APIv2 ensure the fabric API returns the correct hash + if(awaitCommitConfirmation) { + const pollingInterval = 500; // ms + let tries = 20; + while(tries > 0) { + let h; + + try { + h = await this.LatestVersionHashV2({objectId}); + + if(h === versionHash) { + this.Log(`Commit confirmed on fabric node: ${versionHash}`); + break; + } else { tries--; await new Promise(resolve => setTimeout(resolve, pollingInterval)); } + } catch(error) { + if(error.status !== 404) { + throw error; + } + + tries--; + await new Promise(resolve => setTimeout(resolve, pollingInterval)); } } } From 569f27e48ba58fa82e12448b52590603fa77b0c5 Mon Sep 17 00:00:00 2001 From: elv-preethi <39742400+elv-preethi@users.noreply.github.com> Date: Fri, 12 Jul 2024 11:47:52 -0700 Subject: [PATCH 3/3] make changes as per comment --- src/client/ContentManagement.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/client/ContentManagement.js b/src/client/ContentManagement.js index 83a6de9f..b5034e50 100644 --- a/src/client/ContentManagement.js +++ b/src/client/ContentManagement.js @@ -1006,7 +1006,7 @@ exports.PublishContentVersion = async function({objectId, versionHash, awaitComm }); const abi = await this.ContractAbi({id: objectId}); - const fromBlock = commit.blockNumber - 10; // due to block re-org + const fromBlock = commit.blockNumber - 30; // due to block re-org const objectHash = await this.ExtractValueFromEvent({ abi, @@ -1052,7 +1052,6 @@ exports.PublishContentVersion = async function({objectId, versionHash, awaitComm } } - // APIv2 ensure the fabric API returns the correct hash if(awaitCommitConfirmation) { const pollingInterval = 500; // ms