paritytech / substrate

Substrate: The platform for blockchain innovators
Apache License 2.0
8.39k stars 2.65k forks source link

Fetching offchain storage value returns different value than the value fetched using localStorageGet function #14413

Open chirag-bgh opened 1 year ago

chirag-bgh commented 1 year ago

Is there an existing issue?

Experiencing problems? Have you tried our Stack Exchange first?

Description of bug

I need to get the rpc_url value set initially from the explorer. The localStorageGet rpc function returns the correct value of the rpc_url. This returns https://eth-mainnet.g.alchemy.com/v2/MY_API_KEY

But while fetching the rpc_url through the storage call like this:

    pub const ETHEREUM_EXECUTION_RPC: &[u8] = b"starknet::ETHEREUM_EXECUTION_RPC";
    let eth_execution_rpc_url = StorageValueRef::persistent(ETHEREUM_EXECUTION_RPC)
        .get::<Vec<u8>>()
        .map_err(|_| OffchainWorkerError::GetStorageFailed)?
        .ok_or(OffchainWorkerError::EthRpcNotSet)?;

     let endpoint: &str =
        core::str::from_utf8(&eth_execution_rpc_url).map_err(|_| OffchainWorkerError::FormatBytesFailed)?;

This returns endpoint = ttps://eth-mainnet.g.alche

Steps to reproduce

No response

chirag-bgh commented 1 year ago

@bkchr Do you have any insights on this?

chirag-bgh commented 1 year ago

Update: So setting the rpc_url as Bytes in the explorer works instead putting it as String

gui1117 commented 1 year ago

at a first glance it looks like the value is not encoded as a Vec<u8> but maybe as an u8 array of fixed size or something else.

Because the h seems to be interpreted as the compact length of the vec and thus is missing from the result and then the url is cut too short probably meaning the length of the vec is wrong, because it has been computed from h instead of the correct way.