hyperledger / iroha-javascript

JavaScript library for Iroha, a Distributed Ledger Technology (blockchain) platform.
https://wiki.hyperledger.org/display/iroha
Apache License 2.0
94 stars 64 forks source link

Error in ensureDomainExistence function: "Decode data for discriminant 85 is undefined" #126

Closed 6r1d closed 1 year ago

6r1d commented 2 years ago

Today, Youssef Mohammad reported an error occuring with Iroha-JS in Iroha Telegram.

Log ```text js file:///C:/Users/ASUS/Desktop/crowd-funding/node_modules/@scale-codec/core/dist/lib.mjs:583 throw new Error(`Decode data for discriminant ${discriminant} is undefined; decoders schema: ${formatDecoders(decoders)}`); ^ Error: Decode data for discriminant 85 is undefined; decoders schema: 0 => Decode(...), 1 => Signature(...), 2 => Permission(...), 3 => Evaluate(...), 4 => Find(...), 5 => Conversion(...) at decodeEnum (file:///C:/Users/ASUS/Desktop/crowd-funding/node_modules/@scale-codec/core/dist/lib.mjs:583:11) at file:///C:/Users/ASUS/Desktop/crowd-funding/node_modules/@scale-codec/core/dist/lib.mjs:590:22 at trackDecode (file:///C:/Users/ASUS/Desktop/crowd-funding/node_modules/@scale-codec/definition-runtime/dist/lib.mjs:19:61) at decodeTracked (file:///C:/Users/ASUS/Desktop/crowd-funding/node_modules/@scale-codec/definition-runtime/dist/lib.mjs:238:37) at WalkerImpl.decode (file:///C:/Users/ASUS/Desktop/crowd-funding/node_modules/@scale-codec/core/dist/lib.mjs:16:19) at Function.fromBuffer (file:///C:/Users/ASUS/Desktop/crowd-funding/node_modules/@scale-codec/definition-runtime/dist/lib.mjs:243:37) at Client.request (file:///C:/Users/ASUS/Desktop/crowd-funding/node_modules/@iroha2/client/dist/lib.mjs:350:42) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async ensureDomainExistence (file:///C:/Users/ASUS/Desktop/crowd-funding/client.js:120:18) at async file:///C:/Users/ASUS/Desktop/crowd-funding/client.js:136:1 Node.js v18.9.0 ```

Details reported:

6r1d commented 2 years ago

Youssef also reported a decoded error: Unhandled rejection: WarpQueryError(Decode(ParityScale)).

It was displayed by modifying a file: node_modules\@iroha2\client\dist\lib.mjs

Modified code:

    async request(query, params) {
        const scope = createScope();
        const { createHash } = useCryptoAssertive();
        const url = this.forceGetApiURL();
        const accountId = this.forceGetAccountId();
        const keyPair = this.forceGetKeyPair();
        const payload = QueryPayload({
            query,
            account_id: accountId,
            timestamp_ms: BigInt(Date.now()),
            filter: params?.filter ?? PredicateBox('Raw', Predicate('Pass')),
        });
        try {
            let queryBytes,queryString;
            scope.run(() => {
                const payloadHash = collect(createHash(QueryPayload.toBuffer(payload)));
                const signature = makeSignature(keyPair, payloadHash.bytes());
                queryString = VersionedSignedQueryRequest('V1', SignedQueryRequest({ payload, signature }))
                console.log(SignedQueryRequest({ payload, signature }))
                queryBytes = VersionedSignedQueryRequest.toBuffer(VersionedSignedQueryRequest('V1', SignedQueryRequest({ payload, signature })));
            });
            const response = await this.forceGetFetch()(url + ENDPOINT_QUERY, {
                method: 'POST',
                body: queryBytes,
            }).then();

            /* ADDED BY YOUSSEF MOHAMMAD TO DECODE RUST ERROR MESSAGE */

            console.log(await response.text())

            /*  */
            const buffer = await response.arrayBuffer();
            const decoder = new TextDecoder("utf-8");
            console.log("QS",queryBytes, queryBytes.length, decoder.decode(queryBytes))

            const bytes = new Uint8Array(buffer);
            fs.writeFileSync('./iroha-query.bin',queryBytes)
            if (response.status === 200) {
                // OK
                const value = VersionedPaginatedQueryResult.fromBuffer(bytes).as('V1');
                return Enum.variant('Ok', value);
            }
            else {
                // ERROR
                try {
                  const error = QueryError.fromBuffer(bytes);
                  return Enum.variant('Err', error);  
                } catch(e) {
                  const decoder = new TextDecoder("utf-8");
                  //throw new Error(decoder.decode(bytes));
                  return decoder.decode(bytes);
                }
            }
        }
        finally {
            scope.free();
        }
    }
0x009922 commented 1 year ago

@6r1d, please mention me directly and add iroha2 label to the issue so it can be more accessible for my attention. Thanks.

In general, I think it is a versioning issue. @iroha2/client and @iroha2/data-model v2.0.2 targets Iroha 2 RC6 (previous LTS): https://github.com/hyperledger/iroha/commit/75da907f66d5270f407a50e06bc76cec41d3d409.

I am not sure that iroha2-stable is the same.

0x009922 commented 1 year ago

@6r1d, I am likely to close the issue due to its irrelevance.

6r1d commented 1 year ago

Sure, but hopefully it will not repeat. I am not sure personally, so it would help if you explain a bit.

0x009922 commented 1 year ago

I think it is simply a compatibility issue.

However, I think it might be useful to implement this: