3

I'm learning how to upgrade ink! contract,and have some question:

Eg:old contractA, storage define is

#[ink(storage)
pub struct MyContract {
    a: u32,
}

new contractB,storage define is

#[ink(storage)
pub struct MyContract {
    a: u32,
    b: String,
}

Suppose now contractA rootKey(contract) value is 0x01,use set_code_hash to upgrade contractA to contractB

Then call contractA,when message call,will first invoke get_contract_storage to get rootKey(contract) value(is 0x01),use this value to decode new contract storage struct(contractB storage), maybe get error

Error { cause: None, desc: "Not enough data to fill buffer" }), desc: "Could not decode `MyContract::b`" }

MessageDecoder define as

impl ::ink::reflect::ExecuteDispatchable for __ink_MessageDecoder {
        #[allow(clippy::nonminimal_bool, clippy::let_unit_value)]
        fn execute_dispatchable(
            self,
        ) -> ::core::result::Result<(), ::ink::reflect::DispatchError> {
            let key = <Flipper as ::ink::storage::traits::StorageKey>::KEY;
            let mut contract: ::core::mem::ManuallyDrop<Flipper> = ::core::mem::ManuallyDrop::new(
                match ::ink::env::get_contract_storage(&key) { // get contract value and decode
                    ::core::result::Result::Ok(
                        ::core::option::Option::Some(value),
                    ) => value,
                    ::core::result::Result::Ok(::core::option::Option::None) => {
                        ::core::panicking::panic_fmt(
                            format_args!("storage entry was empty"),
                        );
                    }
                    ::core::result::Result::Err(_) => {
                        ::core::panicking::panic_fmt(
                            format_args!("could not properly decode storage entry"),
                        );
                    }
                },
            );

So why there wont has scale::Decode error,how set_code_hash works to achieve Storage Compatibility,is there some step I've missed?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.