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

dev: starknet_getStateUpdate improvement ✨ #773

Closed
EvolveArt opened this issue Jun 27, 2023 · 32 comments
Closed

dev: starknet_getStateUpdate improvement ✨ #773

EvolveArt opened this issue Jun 27, 2023 · 32 comments
Labels
backlog Ready to be picked enhancement New feature or request good first issue Good for newcomers testing

Comments

@EvolveArt
Copy link
Collaborator

EvolveArt commented Jun 27, 2023

Current Behavior

getStateUpdate RPC endpoint is mocked.

Ok(StateUpdate {
            block_hash: block.header().hash(*self.hasher).into(),
            new_root: block.header().global_state_root.into(),
            old_root,
            state_diff: StateDiff {
                storage_diffs: Vec::new(),
                deprecated_declared_classes: Vec::new(),
                declared_classes: Vec::new(),
                deployed_contracts: Vec::new(),
                replaced_classes: Vec::new(),
                nonces: Vec::new(),
            },
        })

Expected Behavior

Make it work as expected, following pathfinder implementation.

@EvolveArt EvolveArt added enhancement New feature or request good first issue Good for newcomers testing labels Jun 27, 2023
@dbejarano820
Copy link
Contributor

Is this possible with the current implementation of the rpc?

@nikania
Copy link
Contributor

nikania commented Jul 3, 2023

@EvolveArt it seems like rpc endpoint always returns empty state_diff

state_diff: StateDiff {
storage_diffs: Vec::new(),
deprecated_declared_classes: Vec::new(),
declared_classes: Vec::new(),
deployed_contracts: Vec::new(),
replaced_classes: Vec::new(),
nonces: Vec::new(),
},

therefore not much to test

@danilowhk
Copy link
Contributor

I can take this one

@MdTeach
Copy link
Contributor

MdTeach commented Jul 10, 2023

@danilowhk #836 refactored the integration test structure. I think you will be working on this test file. Could you check once.

@AbdelStark
Copy link
Contributor

I can take this one

hey, any ETA ?

@danilowhk
Copy link
Contributor

I had a look but wasn't able to actually dig into it, if someone wants to take it please give to them. Apologize for the late notice.

@chee-chyuan
Copy link

throwing in some of my ideas which could be 100% . happy to get corrected

  1. should we follow pathfinder's implementation, that would mean that we will need to sync state_update while syncing block. As pathfinder is a client and not a sequencer, it takes no responsibility in executing transactions and proposing new blocks, and thus it gets the state_update from the feeder gateway. code below.
#[tracing::instrument(skip(self))]
    async fn state_update(&self, block: BlockId) -> Result<StateUpdate, SequencerError> {
        let state_update: reply::StateUpdate = self
            .feeder_gateway_request()
            .get_state_update()
            .with_block(block)
            .with_retry(self.retry)
            .get()
            .await?;

        Ok(state_update.into())
  1. with madara being a sequencer, when proposing a block, there needs to have a way to store the state update for that block too. and then, share state_update for the specific block via rpc. with my shallow understanding of substrate, im guessing that there is a need to modify the block-proposer crate to do so.

  2. after which, when in sync_block, somehow get state_update along with our block info (still figuring out how this part works)

@rkdud007
Copy link
Contributor

rkdud007 commented Aug 9, 2023

Hey if this issue is not assigned to someone, can i take it?

@AbdelStark
Copy link
Contributor

yes please

@rkdud007
Copy link
Contributor

rkdud007 commented Aug 11, 2023

Before I jump on the code, any feedback appreciated ( cc. @abdelhamidbakhta, @EvolveArt).

Approach Suggestion:

  1. Retrieve state_diff of Starknet from storage_change ( Can get it from Substrate's blockbuilder ).
  2. When syncing a new block, obtain state_diff using storage_change before storing it in the Madara backend database.
  3. Introduce a field in the Madara DB for storing state_diff.
  4. Through rpc, access Starknet's state_diff from the Madara database, which was stored during block syncing.

I'm not deeply familiar with the substrate storage system, but I saw related discussions in the TG group chat. @abdelhamidbakhta, any insights on retrieving state_diff from storage_changes would be helpful. 🙏🏻

@tdelabro tdelabro added the backlog Ready to be picked label Aug 11, 2023
@tdelabro tdelabro added this to Madara Aug 11, 2023
@tdelabro tdelabro moved this to 📋 Backlog in Madara Aug 11, 2023
@tdelabro tdelabro moved this from 📋 Backlog to 🏗 In progress in Madara Aug 11, 2023
@tdelabro
Copy link
Collaborator

@rkdud007 Did you do any progress on this one?

@rkdud007
Copy link
Contributor

hey sorry for delay, was paused for a moments handling recently. Will share shortly :)

@tdelabro
Copy link
Collaborator

Sure! Do you need help on this or all good? I just saw we didn't receive responses to your question. Did you find out by yourself?

@rkdud007
Copy link
Contributor

Yes! If I could get more information about this question would be very helpful :) still bit confuse at how to combine pallet’s storage_change and starknet state_diff part :/

@lana-shanghai
Copy link
Contributor

Yes! If I could get more information about this question would be very helpful :) still bit confuse at how to combine pallet’s storage_change and starknet state_diff part :/

Hey I will first do this issue #1081 and then you can wrap up the RPC task, is that's OK with you!

@rkdud007
Copy link
Contributor

Ah there is an issue for this, sure @lana-shanghai ! :))

@github-actions
Copy link

There hasn't been any activity on this issue recently, and in order to prioritize active issues, it will be marked as stale.
Please make sure to update to the latest version and check if that solves the issue. Let us know if that works for you by leaving a 👍
Because this issue is marked as stale, it will be closed and locked in 7 days if no further activity occurs.
Thank you for your contributions!

@github-actions github-actions bot added the stale label Oct 14, 2023
Copy link

There hasn't been any activity on this issue recently, and in order to prioritize active issues, it will be marked as stale.
Please make sure to update to the latest version and check if that solves the issue. Let us know if that works for you by leaving a 👍
Because this issue is marked as stale, it will be closed and locked in 7 days if no further activity occurs.
Thank you for your contributions!

@github-actions github-actions bot added the stale label Nov 15, 2023
@tdelabro tdelabro moved this from 🏗 In progress to 🔖 Ready in Madara Nov 15, 2023
@tdelabro tdelabro removed this from Madara Nov 15, 2023
@tdelabro
Copy link
Collaborator

blocked by #1390

@github-actions github-actions bot removed the stale label Jan 28, 2024
@tdelabro
Copy link
Collaborator

will use the code developed for #1372

Copy link

There hasn't been any activity on this issue recently, and in order to prioritize active issues, it will be marked as stale.
Please make sure to update to the latest version and check if that solves the issue. Let us know if that works for you by leaving a 👍
Because this issue is marked as stale, it will be closed and locked in 7 days if no further activity occurs.
Thank you for your contributions!

@github-actions github-actions bot added the stale label Feb 28, 2024
@neelkamath
Copy link

Do we know when the state update API will be fixed? I heard of the following issues:

In the madara release-v0.7.0, the getStateUpdate interface is not implemented. Although the latest version of madara code implements this interface, the logic is also incorrect. The most critical issues are:
1. StateRoot calculation is not currently correct.
2. The state diff includes some states belonging to Substrate but not to StarkNet.

@github-actions github-actions bot removed the stale label Apr 30, 2024
@tdelabro
Copy link
Collaborator

@antiyro and deoxys should fix the 1. soon.
2. I had no idea about that, will have to double check and fix it

@jbmkahdeksan
Copy link

hi!
Is this available to contribute?
I am interested, what do I need to know?

Copy link

There hasn't been any activity on this issue recently, and in order to prioritize active issues, it will be marked as stale.
Please make sure to update to the latest version and check if that solves the issue. Let us know if that works for you by leaving a 👍
Because this issue is marked as stale, it will be closed and locked in 7 days if no further activity occurs.
Thank you for your contributions!

@github-actions github-actions bot added the stale label Jun 20, 2024
@github-actions github-actions bot removed the stale label Jun 28, 2024
@gerceboss
Copy link

@EvolveArt, would like to take up this issue if it's still relevant, I am new to Cairo and Starknet , might need help as well.

Copy link

onlydustapp bot commented Jul 25, 2024

Hey @gerceboss!
Thanks for showing interest.
We've created an application for you to contribute to Madara.
Go check it out on OnlyDust!

@leykhan16
Copy link

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

I'm a recent graduate of computer science, Alx Africa certified software engineer, ICP/ Dacade certified blockchain developer and Rust (smart contracts ) expert.

How I plan on tackling this issue

Understand the current implementation
Understand pathfinders implementation
Implementation of missing logic
Test the implementation

@nelsonaty
Copy link

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

I have a strong foundation in software development with a focus on blockchain technology and decentralized applications. My experience includes working with smart contracts, blockchain infrastructure, and various consensus mechanisms. I have also worked on integrating blockchain networks with front-end and back-end applications, providing end-to-end solutions for decentralized projects.

Relevant Skills:

Proficient in blockchain development (Solidity, Rust, etc.)
Experience with blockchain frameworks and tools (Ethereum, Polkadot, StarkNet, etc.)
Knowledge of smart contract auditing and security
Strong problem-solving and debugging skills
Familiarity with RPC (Remote Procedure Call) endpoints and blockchain APIs

How I plan on tackling this issue

Understanding the Requirements:
The given snippet suggests that the tStateUpdate RPC endpoint is mocked, and the goal is to make it work as expected, following the pathfinder implementation.

Steps to Address the Problem:

Study Pathfinder Implementation:

Review the pathfinder codebase to understand how they handle the tStateUpdate endpoint.
Identify the key components and logic used in pathfinder for state updates.
Analyze the Mock Implementation:

Examine the existing mocked implementation to identify what aspects are missing or incorrect.
Understand the structure of the StateUpdate and its components (block_hash, new_root, old_root, state_diff).
Design a Working Implementation:

Plan how to transition from the mock to a functional implementation.
Ensure that the necessary data (e.g., block header, global state root, state differences) is accurately retrieved and processed.
Implement the Changes:

Replace the mocked components with real data processing and retrieval.
Ensure that the block_hash, new_root, old_root, and state_diff are correctly computed and formatted.
Testing and Validation:

Write unit tests to verify the correctness of the StateUpdate implementation.
Perform integration tests to ensure the endpoint functions as expected within the larger application.
Documentation and Best Practices:

Document the changes and the rationale behind them.
Follow best practices for code quality, security, and performance.

@YashBit
Copy link

YashBit commented Jul 30, 2024

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

NYU CS
Cambridge MPhil (Deferred)

Rust Programmer
Grant - PSE Ethereum Foundation

How I plan on tackling this issue

  • Review the StateUpdate Structure: Check how the StateUpdate response is constructed in the Pathfinder implementation. Specifically, examine how block_hash, new_root, old_root, and state_diff are derived and populated.

  • Understand Data Sources: Identify where and how data like block.header().hash, block.header().global_state_root, and old_root are sourced and used in the actual implementation.

  • Study the StateDiff Components: Review how StateDiff is populated, including storage_diffs, deprecated_declared_classes, declared_classes, deployed_contracts, replaced_classes, and nonces. Ensure you understand the data and formats expected for each component.

  • Consult Documentation: Refer to any available documentation or source code comments that describe the expected behavior and data structures for getStateUpdate.


@FranCapi2005
Copy link

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

I have experience with rust projects and RPC protocol

How I plan on tackling this issue

// Definition of necessary structures
#[derive(Serialize, Deserialize)]
pub struct StateUpdate {
pub block_hash: String,
pub new_root: String,
pub old_root: String,
pub state_diff: StateDiff,
}

#[derive(Serialize, Deserialize)]
pub struct StateDiff {
pub storage_diffs: Vec,
pub deprecated_declared_classes: Vec,
pub declared_classes: Vec,
pub deployed_contracts: Vec,
pub replaced_classes: Vec,
pub nonces: Vec,
}

#[derive(Serialize, Deserialize)]
pub struct StorageDiff {
// Fields as required
}

#[derive(Serialize, Deserialize)]
pub struct DeclaredClass {
// Fields as required
}

#[derive(Serialize, Deserialize)]
pub struct DeployedContract {
// Fields as required
}

#[derive(Serialize, Deserialize)]
pub struct ReplacedClass {
// Fields as required
}

#[derive(Serialize, Deserialize)]
pub struct Nonce {
// Fields as required
}

// Main function to get the state update
pub async fn get_state_update(&self, block_hash: String) -> Result<StateUpdate, Error> {
let block = self.chain.get_block_by_hash(&block_hash).await?;
let old_root = self.chain.get_previous_state_root(&block_hash).await?;
let state_diff = self.chain.get_state_diff(&block_hash).await?;

Ok(StateUpdate {
    block_hash: block.hash,
    new_root: block.new_root,
    old_root,
    state_diff,
})

}

// Implementation of Chain structure with necessary methods
impl Chain {
pub async fn get_state_diff(&self, block_hash: &str) -> Result<StateDiff, Error> {
let storage_diffs = self.get_storage_diffs(block_hash).await?;
let deprecated_declared_classes = self.get_deprecated_declared_classes(block_hash).await?;
let declared_classes = self.get_declared_classes(block_hash).await?;
let deployed_contracts = self.get_deployed_contracts(block_hash).await?;
let replaced_classes = self.get_replaced_classes(block_hash).await?;
let nonces = self.get_nonces(block_hash).await?;

    Ok(StateDiff {
        storage_diffs,
        deprecated_declared_classes,
        declared_classes,
        deployed_contracts,
        replaced_classes,
        nonces,
    })
}

// Methods to get the actual data from the blockchain
pub async fn get_storage_diffs(&self, block_hash: &str) -> Result<Vec<StorageDiff>, Error> {
    // Logic to get storage diffs
}

pub async fn get_deprecated_declared_classes(&self, block_hash: &str) -> Result<Vec<DeclaredClass>, Error> {
    // Logic to get deprecated declared classes
}

pub async fn get_declared_classes(&self, block_hash: &str) -> Result<Vec<DeclaredClass>, Error> {
    // Logic to get declared classes
}

pub async fn get_deployed_contracts(&self, block_hash: &str) -> Result<Vec<DeployedContract>, Error> {
    // Logic to get deployed contracts
}

pub async fn get_replaced_classes(&self, block_hash: &str) -> Result<Vec<ReplacedClass>, Error> {
    // Logic to get replaced classes
}

pub async fn get_nonces(&self, block_hash: &str) -> Result<Vec<Nonce>, Error> {
    // Logic to get nonces
}

}

@tdelabro
Copy link
Collaborator

tdelabro commented Aug 2, 2024

repository archived in favor of https://github.com/madara-alliance/madara

@tdelabro tdelabro closed this as not planned Won't fix, can't repro, duplicate, stale Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backlog Ready to be picked enhancement New feature or request good first issue Good for newcomers testing
Projects
None yet
Development

No branches or pull requests