Skip to content

Commit

Permalink
Chain state module (#598)
Browse files Browse the repository at this point in the history
* WIP: Add optimsitic sync

* WIP: implement chain state module

* WIP: Add attester incentives

* Can compile

* Does compile

* Start working on attester incentives

* Implement attestation processing

* Finish unbonding

* Improve events

* Rename sov-attester-incentives to optimistic-workflow

* Refactor

* Start working on apply_sync_data_blob

* Refactoring apply_tx_blob

* Refactoring files

* Rewriting unbonding

* Refactor attestation processing

* Refactor process challenge

* Refactoring

* Fixing compile issues

* Does compile

* Everything compiles

* Add 2 phase unbonding back

* Updating tests

* Changing bonding proof

* Changing bonding proof

* Fixing get_with_proof

* Fixing chain updates

* Change STF interface to work on slot level

* Removing Misbehavour hint from parameters
* Fixing prover
* Update docs

* Adding chain state

* Starting chain state

* Fix lint

* Fixing warnings

* Integrating slot hooks

* Populating todos

* Adding data generation for different modules

* Implementing automatic bank data generation

* Fixing stf

* Compiles

* Fixing chain-state test, adding EncodeCall trait

* Fixing lints

* Chain state testing

* Simple chain state module tests

* All tests pass

* Fixing lints

* Fixing comments PR

* Fixing comments PR

* nit fix

* Fixing PR comments

* Deleting borsh compat

* Adding back github and config files

* Specifying commit for jmt

* Update module-system/sov-state/src/storage.rs

Co-authored-by: Nikolai Golub <nikolai@sovlabs.io>

* Fixing comments PR

* Update adapters/risc0/src/host.rs

Co-authored-by: Nikolai Golub <nikolai@sovlabs.io>

* Update adapters/risc0/src/host.rs

Co-authored-by: Nikolai Golub <nikolai@sovlabs.io>

* Fixing error handling

* Fixing lints

* Fixing lints

* Fix lints

* Update rollup-interface/src/state_machine/mocks.rs

Co-authored-by: Nikolai Golub <nikolai@sovlabs.io>

* Addressing PR comments

* Including PR comments

* Update Cargo.toml

Co-authored-by: Nikolai Golub <nikolai@sovlabs.io>

* Update adapters/risc0/src/guest.rs

Co-authored-by: Nikolai Golub <nikolai@sovlabs.io>

* Update adapters/risc0/src/host.rs

Co-authored-by: Nikolai Golub <nikolai@sovlabs.io>

* Update adapters/risc0/src/host.rs

Co-authored-by: Nikolai Golub <nikolai@sovlabs.io>

* Adding back genesis and call

* Fix lints

* Lint

* Restoring default context

* Fixing imports

* Fixing prover compiles

* Fix prover compiles

* Fixing end slot hook

* Reorganizing integration tests

* Reorganizing integration tests

* Adding unit tests for chain-state

* Update chain state tests

* Update apply slot signature

* Fix demo-prover

* Update adapters/risc0/src/guest.rs

Co-authored-by: Nikolai Golub <nikolai@sovlabs.io>

* Update adapters/risc0/src/host.rs

Co-authored-by: Nikolai Golub <nikolai@sovlabs.io>

* Fix lints

* Format demo-prover

* Fix feature data generators

* Update examples/demo-stf/Cargo.toml

Co-authored-by: Nikolai Golub <nikolai@sovlabs.io>

* Update adapters/risc0/src/guest.rs

Co-authored-by: Nikolai Golub <nikolai@sovlabs.io>

* Fixing pr comments

* Fix lint

* Removing call from chain-state because provided by blanket

---------

Co-authored-by: Preston Evans <preston.b.evans@gmail.com>
Co-authored-by: Nikolai Golub <nikolai@sovlabs.io>
  • Loading branch information
3 people committed Sep 14, 2023
1 parent 73005f9 commit 0487050
Show file tree
Hide file tree
Showing 98 changed files with 2,643 additions and 1,045 deletions.
47 changes: 45 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ members = [
"module-system/sov-modules-api",
"module-system/module-schemas",
"module-system/utils/sov-first-read-last-write-cache",
"module-system/utils/sov-data-generators",
"module-system/module-implementations/sov-accounts",
"module-system/module-implementations/sov-bank",
"module-system/module-implementations/sov-chain-state",
"module-system/module-implementations/sov-blob-storage",
"module-system/module-implementations/sov-evm",
"module-system/module-implementations/sov-prover-incentives",
Expand All @@ -48,7 +50,8 @@ rust-version = "1.66"

[workspace.dependencies]
# Dependencies maintained by sovereign
jmt = "0.6.0"
# TODO: replace by release number once available on crates.io: tracking issue https://github.com/Sovereign-Labs/sovereign-sdk/issues/632
jmt = { git = "https://github.com/penumbra-zone/jmt", commit = "46b4b00" }

# External dependencies
async-trait = "0.1.71"
Expand Down
27 changes: 26 additions & 1 deletion adapters/celestia/src/celestia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use prost::bytes::Buf;
use prost::Message;
use serde::{Deserialize, Serialize};
use sov_rollup_interface::da::{BlockHeaderTrait as BlockHeader, CountedBufReader};
use sov_rollup_interface::services::da::SlotData;
use sov_rollup_interface::AddressTrait;
pub use tendermint::block::Header as TendermintHeader;
use tendermint::block::Height;
Expand All @@ -29,7 +30,7 @@ use crate::pfb::{BlobTx, MsgPayForBlobs, Tx};
use crate::shares::{read_varint, BlobIterator, BlobRefIterator, NamespaceGroup};
use crate::utils::BoxError;
use crate::verifier::address::CelestiaAddress;
use crate::verifier::{TmHash, PFB_NAMESPACE};
use crate::verifier::{ChainValidityCondition, TmHash, PFB_NAMESPACE};

#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
pub struct MarshalledDataAvailabilityHeader {
Expand Down Expand Up @@ -303,6 +304,30 @@ impl BlockHeader for CelestiaHeader {
}
}

/// We implement [`SlotData`] for [`CelestiaHeader`] in a similar fashion as for [`FilteredCelestiaBlock`]
impl SlotData for CelestiaHeader {
type BlockHeader = CelestiaHeader;
type Cond = ChainValidityCondition;

fn hash(&self) -> [u8; 32] {
match self.header.hash() {
tendermint::Hash::Sha256(h) => h,
tendermint::Hash::None => unreachable!("tendermint::Hash::None should not be possible"),
}
}

fn header(&self) -> &Self::BlockHeader {
self
}

fn validity_condition(&self) -> ChainValidityCondition {
ChainValidityCondition {
prev_hash: *self.header().prev_hash().inner(),
block_hash: <Self as SlotData>::hash(self),
}
}
}

#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
pub struct CelestiaVersion {
pub block: u32,
Expand Down
11 changes: 10 additions & 1 deletion adapters/celestia/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use base64::Engine;
use borsh::{BorshDeserialize, BorshSerialize};
pub use nmt_rs::NamespaceId;
use serde::{Deserialize, Serialize};
use sov_rollup_interface::da::BlockHeaderTrait;
use sov_rollup_interface::services::da::SlotData;
use sov_rollup_interface::Bytes;
use tendermint::crypto::default::Sha256;
Expand All @@ -14,7 +15,7 @@ use tendermint::merkle;
use crate::pfb::MsgPayForBlobs;
use crate::shares::{NamespaceGroup, Share};
use crate::utils::BoxError;
use crate::verifier::PARITY_SHARES_NAMESPACE;
use crate::verifier::{ChainValidityCondition, PARITY_SHARES_NAMESPACE};
use crate::{CelestiaHeader, TxPosition};

#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
Expand Down Expand Up @@ -81,6 +82,7 @@ pub struct FilteredCelestiaBlock {

impl SlotData for FilteredCelestiaBlock {
type BlockHeader = CelestiaHeader;
type Cond = ChainValidityCondition;

fn hash(&self) -> [u8; 32] {
match self.header.header.hash() {
Expand All @@ -92,6 +94,13 @@ impl SlotData for FilteredCelestiaBlock {
fn header(&self) -> &Self::BlockHeader {
&self.header
}

fn validity_condition(&self) -> ChainValidityCondition {
ChainValidityCondition {
prev_hash: *self.header().prev_hash().inner(),
block_hash: self.hash(),
}
}
}

impl FilteredCelestiaBlock {
Expand Down
20 changes: 16 additions & 4 deletions adapters/celestia/src/verifier/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use borsh::{BorshDeserialize, BorshSerialize};
use nmt_rs::NamespaceId;
use serde::{Deserialize, Serialize};
use sov_rollup_interface::da::{
Expand Down Expand Up @@ -97,14 +98,27 @@ impl DaSpec for CelestiaSpec {
type CompletenessProof = Vec<RelevantRowProof>;

type ChainParams = RollupParams;

type ValidityCondition = ChainValidityCondition;
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RollupParams {
pub namespace: NamespaceId,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
Serialize,
Deserialize,
Hash,
BorshDeserialize,
BorshSerialize,
)]
/// A validity condition expressing that a chain of DA layer blocks is contiguous and canonical
pub struct ChainValidityCondition {
pub prev_hash: [u8; 32],
Expand All @@ -131,8 +145,6 @@ impl da::DaVerifier for CelestiaVerifier {

type Error = ValidationError;

type ValidityCondition = ChainValidityCondition;

fn new(params: <Self::Spec as DaSpec>::ChainParams) -> Self {
Self {
rollup_namespace: params.namespace,
Expand All @@ -145,7 +157,7 @@ impl da::DaVerifier for CelestiaVerifier {
txs: &[<Self::Spec as DaSpec>::BlobTransaction],
inclusion_proof: <Self::Spec as DaSpec>::InclusionMultiProof,
completeness_proof: <Self::Spec as DaSpec>::CompletenessProof,
) -> Result<Self::ValidityCondition, Self::Error> {
) -> Result<<Self::Spec as DaSpec>::ValidityCondition, Self::Error> {
// Validate that the provided DAH is well-formed
block_header.validate_dah()?;
let validity_condition = ChainValidityCondition {
Expand Down
16 changes: 14 additions & 2 deletions adapters/risc0/src/guest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[cfg(target_os = "zkvm")]
use risc0_zkvm::guest::env;
use sov_rollup_interface::zk::{Zkvm, ZkvmGuest};
use sov_rollup_interface::AddressTrait;

use crate::Risc0MethodId;

Expand Down Expand Up @@ -37,7 +38,18 @@ impl Zkvm for Risc0Guest {
_serialized_proof: &'a [u8],
_code_commitment: &Self::CodeCommitment,
) -> Result<&'a [u8], Self::Error> {
// Implement this method once risc0 supports recursion
todo!()
// Implement this method once risc0 supports recursion: issue #633
todo!("Implement once risc0 supports recursion: https://github.com/Sovereign-Labs/sovereign-sdk/issues/633")
}

fn verify_and_extract_output<
C: sov_rollup_interface::zk::ValidityCondition,
Add: AddressTrait,
>(
_serialized_proof: &[u8],
_code_commitment: &Self::CodeCommitment,
) -> Result<sov_rollup_interface::zk::StateTransition<C, Add>, Self::Error> {
// Implement this method once risc0 supports recursion: issue https://github.com/Sovereign-Labs/sovereign-sdk/issues/633
todo!("Implement once risc0 supports recursion: https://github.com/Sovereign-Labs/sovereign-sdk/issues/633")
}
}
23 changes: 23 additions & 0 deletions adapters/risc0/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use risc0_zkvm::{
Executor, ExecutorEnvBuilder, LocalExecutor, SegmentReceipt, Session, SessionReceipt,
};
use sov_rollup_interface::zk::{Zkvm, ZkvmHost};
use sov_rollup_interface::AddressTrait;

use crate::Risc0MethodId;

Expand Down Expand Up @@ -55,6 +56,16 @@ impl<'prover> Zkvm for Risc0Host<'prover> {
) -> Result<&'a [u8], Self::Error> {
verify_from_slice(serialized_proof, code_commitment)
}

fn verify_and_extract_output<
C: sov_rollup_interface::zk::ValidityCondition,
Add: AddressTrait,
>(
_serialized_proof: &[u8],
_code_commitment: &Self::CodeCommitment,
) -> Result<sov_rollup_interface::zk::StateTransition<C, Add>, Self::Error> {
todo!("Implement once risc0 supports recursion, issue https://github.com/Sovereign-Labs/sovereign-sdk/issues/633")
}
}

pub struct Risc0Verifier;
Expand All @@ -70,6 +81,18 @@ impl Zkvm for Risc0Verifier {
) -> Result<&'a [u8], Self::Error> {
verify_from_slice(serialized_proof, code_commitment)
}

fn verify_and_extract_output<
C: sov_rollup_interface::zk::ValidityCondition,
Add: AddressTrait,
>(
_serialized_proof: &[u8],
_code_commitment: &Self::CodeCommitment,
) -> Result<sov_rollup_interface::zk::StateTransition<C, Add>, Self::Error> {
// Method to implement: not clear how to deserialize the proof output.
// Issue https://github.com/Sovereign-Labs/sovereign-sdk/issues/621
todo!("not clear how to deserialize the proof output. Issue https://github.com/Sovereign-Labs/sovereign-sdk/issues/621")
}
}

fn verify_from_slice<'a>(
Expand Down
1 change: 1 addition & 0 deletions examples/demo-nft-module/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ sov-state = { path = "../../module-system/sov-state", default-features = false }

[dev-dependencies]
sov-rollup-interface = { path = "../../rollup-interface" }
sov-data-generators = { path = "../../module-system/utils/sov-data-generators" }
tempfile = { workspace = true }


Expand Down
Loading

0 comments on commit 0487050

Please sign in to comment.