From 2086e37c1b1061bb36d8a3d187e9e75d43e77972 Mon Sep 17 00:00:00 2001 From: Stanislav Jakuschevskij Date: Mon, 17 Feb 2025 18:28:33 +0100 Subject: [PATCH] Fix failing ci pipeline with js chaincode The Javascript asset-transfer-basic chaincode stores AppraisedValue and Size as string types instead of number types. This leads to an issue when used with a Go client application where assets are unmarshaled into an Asset type where AppraisedValue and Size are of uint64 type. This change makes sure that AppraisedValue and Size are stringified as numbers. To prevent the pipeline from failing when the expected error occurs a sentinel error was created and checked against in the entry point. Signed-off-by: Stanislav Jakuschevskij --- .../chaincode-javascript/lib/assetTransfer.js | 4 ++-- off_chain_data/application-go/app.go | 5 +++++ off_chain_data/application-go/store.go | 6 ++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js b/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js index a68b75c6..d50554fc 100644 --- a/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js +++ b/asset-transfer-basic/chaincode-javascript/lib/assetTransfer.js @@ -79,9 +79,9 @@ class AssetTransfer extends Contract { const asset = { ID: id, Color: color, - Size: size, + Size: Number(size), Owner: owner, - AppraisedValue: appraisedValue, + AppraisedValue: Number(appraisedValue), }; // we insert data in alphabetic order using 'json-stringify-deterministic' and 'sort-keys-recursive' await ctx.stub.putState(id, Buffer.from(stringify(sortKeysRecursive(asset)))); diff --git a/off_chain_data/application-go/app.go b/off_chain_data/application-go/app.go index fa49de02..4469df2c 100644 --- a/off_chain_data/application-go/app.go +++ b/off_chain_data/application-go/app.go @@ -39,6 +39,11 @@ func main() { command := allCommands[name] if err := command(client); err != nil { + if errors.Is(err, errExpected) { + fmt.Println(err) + return + } + panic(err) } } diff --git a/off_chain_data/application-go/store.go b/off_chain_data/application-go/store.go index ccc90e5d..704e7632 100644 --- a/off_chain_data/application-go/store.go +++ b/off_chain_data/application-go/store.go @@ -14,7 +14,7 @@ var simulatedFailureCount = getSimulatedFailureCount() var transactionCount uint = 0 // Used only to simulate failures // Apply writes for a given transaction to off-chain data store, ideally in a single operation for fault tolerance. -type writer = func(data ledgerUpdate) error +type writer = func(ledgerUpdate) error // Ledger update made by a specific transaction. type ledgerUpdate struct { @@ -71,10 +71,12 @@ func applyWritesToOffChainStore(data ledgerUpdate) error { return nil } +var errExpected = errors.New("expected error: simulated write failure") + func simulateFailureIfRequired() error { if simulatedFailureCount > 0 && transactionCount >= simulatedFailureCount { transactionCount = 0 - return errors.New("expected error: simulated write failure") + return errExpected } transactionCount += 1