ink icon indicating copy to clipboard operation
ink copied to clipboard

Enable own_code_hash() in unit testing environment

Open goastler opened this issue 2 years ago • 0 comments

Questions

  • Is this a bug concerning building of ink! smart contracts?
    • no
  • Is this a bug that appears when interacting with smart contracts via the Polkadot JS browser UI?
    • no

FAQ

  • Are you using the nightly Rust channel to compile your smart contract?
    • no
  • Are you using an up-to-date cargo-contract? Check via cargo contract --version.
    • cargo-contract-contract 2.0.0-rc-unknown-x86_64-unknown-linux-gnu
  • Are you using an up-to-date ink! version in your smart contract?
    • yes

Describe the bug When unit testing, I expect to be able to use own_code_hash() to test the set_code_hash() functionality. According to the error message, the own_code_hash() method has not been implemented in the test environment.

Unit test:

#[ink::test]
            fn test_set_code_hash() {

                // always set the caller to the unused account to start, avoid any mistakes with caller checks
                set_caller(default_unused_account());
                let accounts = default_accounts();

                let mut contract = default_contract();

                let new_code_hash = accounts.code_hashes[1];
                let old_code_hash = contract.env().own_code_hash().unwrap();
                assert_ne!(Hash::from(new_code_hash), old_code_hash);

                set_caller(accounts.admins[0]); // an account which does have permission to call set code hash

                assert_eq!(contract.set_code_hash(new_code_hash), Ok(()));

                assert_eq!(contract.env().own_code_hash().unwrap(), Hash::from(new_code_hash)); // hits error here
            }

Error:

failures:

---- prosopo::tests::tests_inner::test_set_code_hash stdout ----
thread 'prosopo::tests::tests_inner::test_set_code_hash' panicked at 'not implemented: off-chain environment does not support `own_code_hash`', /home/geopro/.cargo/git/checkouts/ink-b050680cd543eb5a/9ca19c4/crates/env/src/engine/off_chain/impls.rs:564:9

Expected behavior The own_code_hash() method to return a code hash address.

Screenshots

Additional context I think it would be really helpful from a testing point of view to have the code_hash mocked in the testing environment. We use set_code_hash() for doing contract upgrades in-place and the inability to test it fully unnerves me somewhat. However, I appreciate this isn't top priority of feature requests.

goastler avatar May 05 '23 09:05 goastler