Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Runtime API: aggregate block messages | Introduce Starknet OS output #1160

Merged
merged 24 commits into from
Oct 11, 2023
Merged

Conversation

m-kus
Copy link
Contributor

@m-kus m-kus commented Sep 30, 2023

  1. Messages that are sent to L1 or consumed from L1 during a particular block now can be aggregated via runtime API
  2. StarknetOsOutput type was introduced with a binary codec compatible with Solidity contract (tested on mainnet data)

Related to #1133

On the client we can get program output roughly as follows:

    fn get_snos_program_output(&self, block_id: BlockId) -> RpcResult<StarknetOsOutput> {
        let substrate_block_hash = self.substrate_block_hash_from_starknet_block(block_id).map_err(|e| {
            error!("'{e}'");
            StarknetRpcApiError::BlockNotFound
        })?;

        let block = get_block_by_block_hash(self.client.as_ref(), substrate_block_hash).unwrap_or_default();

        let old_root = if block.header().block_number > 0 {
            let parent_block_hash = (TryInto::<FieldElement>::try_into(block.header().parent_block_hash)).unwrap();
            let substrate_parent_block_hash =
                self.substrate_block_hash_from_starknet_block(BlockId::Hash(parent_block_hash)).map_err(|e| {
                    error!("'{e}'");
                    StarknetRpcApiError::BlockNotFound
                })?;

            let parent_block =
                get_block_by_block_hash(self.client.as_ref(), substrate_parent_block_hash).unwrap_or_default();
            parent_block.header().global_state_root.into()
        } else {
            FieldElement::default()
        };

        let block_extrinsics = self
            .client
            .block_body(substrate_block_hash)
            .map_err(|e| {
                error!("'{e}'");
                StarknetRpcApiError::InternalServerError
            })?
            .ok_or(StarknetRpcApiError::BlockNotFound)?;
        let chain_id = self.chain_id()?.0.into();

        let (messages_to_l1, messages_to_l2) = self
            .client
            .runtime_api()
            .get_starknet_messages(substrate_block_hash, block_extrinsics, chain_id)
            .map_err(|e| {
                error!("'{e}'");
                StarknetRpcApiError::InternalServerError
            })?;

        Ok(StarknetOsOutput {
            prev_state_root: old_root,
            new_state_root: block.header().global_state_root.into(),
            block_number: block.header().block_number,
            block_hash: block.header().hash::<H>().into(),
            config_hash: StarkFelt::ZERO,
            messages_to_l1,
            messages_to_l2,
        })
    }

@m-kus m-kus changed the title Get Starknet OS program output Runtime API: aggregate block messages | Introduce Starknet OS types Oct 1, 2023
@m-kus m-kus marked this pull request as ready for review October 1, 2023 15:24
@m-kus m-kus changed the title Runtime API: aggregate block messages | Introduce Starknet OS types Runtime API: aggregate block messages | Introduce Starknet OS output Oct 1, 2023
Copy link
Collaborator

@EvolveArt EvolveArt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left few comments

crates/runtime/src/lib.rs Show resolved Hide resolved
starknet-rpc-test/src/constants.rs Outdated Show resolved Hide resolved
starknet-rpc-test/src/constants.rs Outdated Show resolved Hide resolved
starknet-rpc-test/get_transaction_receipt.rs Outdated Show resolved Hide resolved
starknet-rpc-test/get_transaction_receipt.rs Outdated Show resolved Hide resolved
starknet-rpc-test/get_transaction_receipt.rs Outdated Show resolved Hide resolved
@tdelabro tdelabro requested a review from drspacemn October 2, 2023 13:24
@tdelabro
Copy link
Collaborator

tdelabro commented Oct 3, 2023

@m-kus I merged the other one, you should rebase more probably cherry-pick your commits and push force on this one :)

crates/pallets/starknet/src/runtime_api.rs Show resolved Hide resolved
crates/primitives/snos-output/src/codec.rs Outdated Show resolved Hide resolved
crates/primitives/snos-output/src/lib.rs Show resolved Hide resolved
crates/primitives/snos-output/src/codec.rs Outdated Show resolved Hide resolved
crates/primitives/snos-output/src/codec.rs Outdated Show resolved Hide resolved
crates/primitives/snos-output/src/tests.rs Outdated Show resolved Hide resolved
crates/primitives/snos-output/src/codec.rs Outdated Show resolved Hide resolved
crates/primitives/snos-output/src/tests.rs Outdated Show resolved Hide resolved
crates/primitives/snos-output/src/codec.rs Outdated Show resolved Hide resolved
crates/primitives/snos-output/src/codec.rs Outdated Show resolved Hide resolved
crates/primitives/snos-output/src/codec.rs Show resolved Hide resolved
crates/primitives/snos-output/src/codec.rs Outdated Show resolved Hide resolved
crates/primitives/snos-output/src/tests.rs Outdated Show resolved Hide resolved
crates/primitives/snos-output/src/tests.rs Outdated Show resolved Hide resolved
crates/primitives/snos-output/src/codec.rs Outdated Show resolved Hide resolved
crates/primitives/snos-output/src/felt_reader.rs Outdated Show resolved Hide resolved
crates/primitives/snos-output/src/codec.rs Outdated Show resolved Hide resolved
@tdelabro tdelabro merged commit f3a1ae8 into keep-starknet-strange:l1-l2-messaging Oct 11, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Oct 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants