aeternity / aepp-calldata-js

Aeternity data serialization library
ISC License
3 stars 4 forks source link

When ACI and data returned from contract diverge, the library returns a very confusing message #169

Closed loxs closed 3 months ago

loxs commented 2 years ago

Currently when we call a contract with the wrong ACI, we get an error like Invalid FATE prefix: 0b10011111 Instead the library should return something like "Data does not fit the ACI provided".

dincho commented 2 years ago

Please provide minimal working example, using the lib

loxs commented 2 years ago
import axios from 'axios';
import aeppSdkJs from '@aeternity/aepp-sdk';
const { AeSdk, Node, MemoryAccount, generateKeyPair } = aeppSdkJs;

const MAIN_STAKING_ADDR = 'ct_LRbi65kmLtE7YMkG6mvG5TxAXTsPJDZjAtsPuaXtRyPA7gnfJ';

const MAIN_STAKING_MASTER =
    'https://raw.githubusercontent.com/aeternity/aeternity/master/test/contracts/MainStaking.aes';
const MAIN_STAKING_OLD =
    'https://raw.githubusercontent.com/aeternity/aeternity/eeb35410bf3708f62a1d146e1c151b70daf67f89/test/contracts/MainStaking.aes';

const COMPILER_URL = 'https://compiler.aeternity.io';
const NODE_URL = 'https://testnet-hc.aeternity.io/';

const callContract = async () => {
    //
    // Change this to MAIN_STAKING_MASTER to have a working/non-crashing version
    //
    const mainStakingSource = await (await axios.get(MAIN_STAKING_OLD)).data.trim();
    const node = new Node(NODE_URL);
    const aeSdk = new AeSdk({
        nodes: [{ name: 'hyper_chain2', instance: node }],
        compilerUrl: COMPILER_URL
    });
    const keypair = generateKeyPair();
    const senderAccount = new MemoryAccount({ keypair });
    await aeSdk.addAccount(senderAccount, { select: true });

    const mainStaking = await aeSdk.getContractInstance({ source: mainStakingSource });
    const ms = await aeSdk.getContractInstance({
        aci: mainStaking._aci,
        contractAddress: MAIN_STAKING_ADDR
    });
    const resp = await ms.call('get_state', [], { callStatic: true });
    console.log(resp);
};

callContract();
loxs commented 2 years ago

Here is the output when you run it via node scriptName.js

/Users/loxs/aeternity/aepp-hc-ui/node_modules/.pnpm/@aeternity+aepp-calldata@1.2.0/node_modules/@aeternity/aepp-calldata/src/Serializers/IntSerializer.js:74
        throw new FatePrefixError(prefix)
              ^

FatePrefixError: Invalid FATE prefix: 0b10011111
    at IntSerializer.deserializeStream (/Users/loxs/aeternity/aepp-hc-ui/node_modules/.pnpm/@aeternity+aepp-calldata@1.2.0/node_modules/@aeternity/aepp-calldata/src/Serializers/IntSerializer.js:74:15)
    at Serializer.deserializeStream (/Users/loxs/aeternity/aepp-hc-ui/node_modules/.pnpm/@aeternity+aepp-calldata@1.2.0/node_modules/@aeternity/aepp-calldata/src/Serializer.js:98:42)
    at TupleSerializer.deserializeStream (/Users/loxs/aeternity/aepp-hc-ui/node_modules/.pnpm/@aeternity+aepp-calldata@1.2.0/node_modules/@aeternity/aepp-calldata/src/Serializers/TupleSerializer.js:60:48)
    at Serializer.deserializeStream (/Users/loxs/aeternity/aepp-hc-ui/node_modules/.pnpm/@aeternity+aepp-calldata@1.2.0/node_modules/@aeternity/aepp-calldata/src/Serializer.js:98:42)
    at ListSerializer.deserializeStream (/Users/loxs/aeternity/aepp-hc-ui/node_modules/.pnpm/@aeternity+aepp-calldata@1.2.0/node_modules/@aeternity/aepp-calldata/src/Serializers/ListSerializer.js:55:48)
    at Serializer.deserializeStream (/Users/loxs/aeternity/aepp-hc-ui/node_modules/.pnpm/@aeternity+aepp-calldata@1.2.0/node_modules/@aeternity/aepp-calldata/src/Serializer.js:98:42)
    at TupleSerializer.deserializeStream (/Users/loxs/aeternity/aepp-hc-ui/node_modules/.pnpm/@aeternity+aepp-calldata@1.2.0/node_modules/@aeternity/aepp-calldata/src/Serializers/TupleSerializer.js:60:48)
    at TupleSerializer.deserialize (/Users/loxs/aeternity/aepp-hc-ui/node_modules/.pnpm/@aeternity+aepp-calldata@1.2.0/node_modules/@aeternity/aepp-calldata/src/Serializers/BaseSerializer.js:13:37)
    at Serializer.deserialize (/Users/loxs/aeternity/aepp-hc-ui/node_modules/.pnpm/@aeternity+aepp-calldata@1.2.0/node_modules/@aeternity/aepp-calldata/src/Serializer.js:85:42)
    at Encoder.decode (/Users/loxs/aeternity/aepp-hc-ui/node_modules/.pnpm/@aeternity+aepp-calldata@1.2.0/node_modules/@aeternity/aepp-calldata/src/Encoder.js:101:47) {
  prefix: 159
}

Node.js v18.5.0
dincho commented 2 years ago

Perhaps I wasn't very clear, minimal example/reproduction steps using this library ONLY.

I'll take a look anyway someday, but having lib only minimal example would speedup the fix/triage.

loxs commented 2 years ago

Currently I have no idea how to do that. If I go to the extent of figuring out how to produce such a thing, I'll probably be able to debug/fix it myself. It's of course not impossible that I actually go out and do it, will let you know if I head that direction.