Not All Bytes Read Common Solutions
Asked Answered
S

1

10

In this post, I'll go through what causes the cryptic error message "Smart contract panicked: panicked at 'Cannot deserialize the contract state.: Custom { kind: InvalidData, error: "Not all bytes read" }".

When you deploy smart contracts and store information, the state is serialized and stored on disk. A problem arises when you deploy another smart contract to your account and you've changed important information. The NEAR Runtime will try to read your serialized state from disk and load it using whatever smart contract code is deployed to your account. If it can't figure out how to do this, it will output the error the Not all bytes read error message.

For an example of when this could happen, see the upgrade a contract section of the NFT zero to hero tutorial found in our DevDocs.

To read about the best practices and how to avoid such scenarios, refer to this detailed article.

Stingy answered 22/12, 2021 at 21:43 Comment(0)
P
6

As you pointed out, the problem is that if you build your contract using near-sdk, it will try to load the storage from to build the contract object on every function call. If the layout of the contract changed, deserializing the old storage with the new layout will fail.

Check this pattern to upgrade storage layout from a contract after upgrade. Using near-sdk-rs you should decorate a function with #[init(ignore_state)], and then this function won't load the state by default (you can do it manually though). The result from that function will be serialised and stored as the new state.

Pandect answered 3/1, 2022 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.