NEAR-Edu / near-certification-tools

2 stars 2 forks source link

Cannot deserialize the contract state.: Custom { kind: InvalidInput, error: "Unexpected length of input" } #58

Closed ryancwalsh closed 2 years ago

ryancwalsh commented 2 years ago

I rebuilt and redeployed the contract to mainnet and then tried to use the new cert_delete function.

 ~/code/NCD/near-certification-tools/data-contract/ [main*] NEAR_ENV=mainnet ./deploy.sh certificates.unv.near -f
This account already has a deployed contract [ 5QGLCjUe1Qm6xsZdQzLqqSAnZpPF68ynxgAoZf8KhPvV ]. Do you want to proceed? (y/n) y
Starting deployment. Account id: certificates.unv.near, node: https://rpc.mainnet.near.org, helper: https://helper.mainnet.near.org, file: ./target/wasm32-unknown-unknown/release/near_certification_tools.wasm
Transaction Id 7uGvH4FP9F9Hrcwwzkh8afLZS4G1QUUFjuQXV8fK7A4R
To see the transaction in the transaction explorer, please open this url in your browser
https://explorer.mainnet.near.org/transactions/7uGvH4FP9F9Hrcwwzkh8afLZS4G1QUUFjuQXV8fK7A4R
Done deploying to certificates.unv.near
 ~/code/NCD/near-certification-tools/data-contract/ [main*] NEAR_ENV=mainnet near call certificates.unv.near cert_delete '{ "token_id": "e73ed3f56a6e4a209044f0e4f4bca4cc"}' --account-id ryancwalsh.near --depositYocto 1 --gas 300000000000000
Scheduling a call: certificates.unv.near.cert_delete({ "token_id": "e73ed3f56a6e4a209044f0e4f4bca4cc"}) with attached 0.000000000000000000000001 NEAR
Doing account.functionCall()
Receipts: 3aPiZFm7ENK7zAyeeWxcAQVsw7zq5M23pkV7E3t7RUMr, Geqot8y3aHbcshChxkhps6rE4V1yLM1RGLxSRQt61vqA
    Failure [certificates.unv.near]: Error: {"index":0,"kind":{"ExecutionError":"Smart contract panicked: panicked at 'Cannot deserialize the contract state.: Custom { kind: InvalidInput, error: \"Unexpected length of input\" }', /Users/rcwalsh/.cargo/registry/src/github.com-1ecc6299db9ec823/near-sdk-4.0.0-pre.7/src/environment/env.rs:688:46"}}
ServerTransactionError: {"index":0,"kind":{"ExecutionError":"Smart contract panicked: panicked at 'Cannot deserialize the contract state.: Custom { kind: InvalidInput, error: \"Unexpected length of input\" }', /Users/rcwalsh/.cargo/registry/src/github.com-1ecc6299db9ec823/near-sdk-4.0.0-pre.7/src/environment/env.rs:688:46"}}
    at Object.parseResultError (/usr/local/lib/node_modules/near-cli/node_modules/near-api-js/lib/utils/rpc_errors.js:31:29)
    at Account.signAndSendTransactionV2 (/usr/local/lib/node_modules/near-cli/node_modules/near-api-js/lib/account.js:160:36)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async scheduleFunctionCall (/usr/local/lib/node_modules/near-cli/commands/call.js:57:38)
    at async Object.handler (/usr/local/lib/node_modules/near-cli/utils/exit-on-error.js:52:9) {
  type: 'FunctionCallError',
  context: undefined,
  index: 0,
  kind: {
    ExecutionError: `Smart contract panicked: panicked at 'Cannot deserialize the contract state.: Custom { kind: InvalidInput, error: "Unexpected length of input" }', /Users/rcwalsh/.cargo/registry/src/github.com-1ecc6299db9ec823/near-sdk-4.0.0-pre.7/src/environment/env.rs:688:46`
  },
  transaction_outcome: {
    block_hash: 'FQvxXdzMa9Yep6TepDf7KptXdoq1eNM9r2PrQQwdXevo',
    id: 'BBREaueyLHnhw7GGquGr7CpkQvoQNpUctfup6VBQBi47',
    outcome: {
      executor_id: 'ryancwalsh.near',
      gas_burnt: 2428050684172,
      logs: [],
      metadata: [Object],
      receipt_ids: [Array],
      status: [Object],
      tokens_burnt: '242805068417200000000'
    },
    proof: [ [Object], [Object], [Object], [Object], [Object], [Object] ]
  }
}
NEAR_ENV=mainnet near state certificates.unv.near                                                                                                                          
Account certificates.unv.near
{
  amount: '99872509835780252200000000',
  block_hash: 'ER9ZGebcSeG3HYc9W9SqTkwGoPb7KbZLjnZqzzQZCToL',
  block_height: 64955225,
  code_hash: '5QGLCjUe1Qm6xsZdQzLqqSAnZpPF68ynxgAoZf8KhPvV',
  locked: '0',
  storage_paid_at: 0,
  storage_usage: 683176,
  formattedAmount: '99.8725098357802522'
}

From searching, I found these answers, which all seem to suggest that maybe the easiest solution would be for Sherif to delete and recreate this account (otherwise, you might need to code some kind of complicated data migration?):

  1. https://stackoverflow.com/questions/60767120/getting-cannot-deserialize-the-contract-state-when-calling-rust-init-function
  2. https://stackoverflow.com/questions/70455438/not-all-bytes-read-common-solutions
  3. https://stackoverflow.com/questions/71943459/how-can-i-fix-error-happened-while-deserializing-the-module-error

Thoughts?

encody commented 2 years ago

Ach, yeah, that's because I changed the state storage layout so it is incompatible with the old contract's state.

There are two solutions:

  1. Delete the account entirely and redeploy the contract.
  2. I can implement a migrate function.
ryancwalsh commented 2 years ago

@encody If the migrate function is quick / easy enough, it might be a valuable learning experience for us. Otherwise, let me know, and I can ask Sherif to delete and recreate the account and send me a new key.

encody commented 2 years ago

Addressed in f5c1536e715c0895b43fa3afde0fe2c49588f8c5

Messed up the commit message; I forgot that saying something like #58 ... comments out the line instead of referencing the issue 🤦‍♀️