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 <stas@two-giants.com>
This commit is contained in:
Stanislav Jakuschevskij 2025-02-17 18:28:33 +01:00
parent 034e9b08c0
commit 2086e37c1b
No known key found for this signature in database
GPG key ID: 78195D2E6998E2EB
3 changed files with 11 additions and 4 deletions

View file

@ -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))));

View file

@ -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)
}
}

View file

@ -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