Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/nightly' into preston/kernel-state3
Browse files Browse the repository at this point in the history
  • Loading branch information
preston-evans98 committed Dec 22, 2023
2 parents eb9e213 + 4497a7f commit 798dbbc
Show file tree
Hide file tree
Showing 37 changed files with 929 additions and 429 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Rust

# On Rust, GitHub Actions, and caching
# ===========
# Here's a list of things to keep in mind if you find yourself maintaing this
# Here's a list of things to keep in mind if you find yourself maintaining this
# CI:
#
# https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#matching-a-cache-key
Expand All @@ -26,7 +26,7 @@ name: Rust
# - Learn cache invalidation rules of `Swatinem/rust-cache` before making
# changes, e.g. what happens when `rustc --version` changes or `Cargo.lock`
# changes (or is missing).
# - The jobs dependency tree is the way it is to accomodate for sharing caches,
# - The jobs dependency tree is the way it is to accommodate for sharing caches,
# not necessarily because it makes logical sense to run one job after the
# other. This is due to the fact that we can't share caches between jobs that
# run in parallel.
Expand Down Expand Up @@ -156,7 +156,7 @@ jobs:
hack:
name: features
# `cargo-hack` uses the same profile as `cargo check` and doesn't require
# building dependencies, only chceking them, so we can share caches
# building dependencies, only checking them, so we can share caches
# effectively.
needs: check
runs-on: buildjet-8vcpu-ubuntu-2204
Expand Down
15 changes: 4 additions & 11 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ borsh = { version = "0.10.3", default-features = false }
# TODO: Consider replacing this serialization format
# https://github.com/Sovereign-Labs/sovereign-sdk/issues/283
bincode = "1.3.3"
bcs = "0.1.5"
bcs = "0.1.6"
byteorder = { version = "1.5.0", default-features = false }
bytes = { version = "1.2.1", default-features = false }
digest = { version = "0.10.6", default-features = false, features = ["alloc"] }
Expand Down
3 changes: 2 additions & 1 deletion adapters/avail/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ reqwest = { version = "0.11", features = ["json"], optional = true }
thiserror = { workspace = true }
sp-keyring = { version = "24", optional = true }
sp-core = { version = "21", optional = true }
hex = { workspace = true }

[features]
default = ["native"]
Expand All @@ -48,4 +49,4 @@ native = [
"dep:sp-keyring",
"dep:sp-core",
"sov-rollup-interface/native"
]
]
6 changes: 6 additions & 0 deletions adapters/avail/src/spec/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ impl AvailHash {

impl BlockHashTrait for AvailHash {}

impl core::fmt::Display for AvailHash {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "0x{}", hex::encode(self.0))
}
}

impl AsRef<[u8]> for AvailHash {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
Expand Down
6 changes: 6 additions & 0 deletions adapters/celestia/src/verifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ impl AsRef<[u8]> for TmHash {
}
}

impl core::fmt::Display for TmHash {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "0x{}", hex::encode(self.0))
}
}

impl TmHash {
pub fn inner(&self) -> &[u8; 32] {
match self.0 {
Expand Down
66 changes: 52 additions & 14 deletions adapters/mock-da/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ impl MockDaService {
}
}

/// Get sequencer address
pub fn get_sequencer_address(&self) -> MockAddress {
self.sequencer_da_address
}

/// Change number of wait attempts before giving up on waiting for block
pub fn set_wait_attempts(&mut self, wait_attempts: usize) {
self.wait_attempts = wait_attempts;
}

async fn wait_for_height(&self, height: u64) -> anyhow::Result<()> {
// Waits self.wait_attempts * 10ms to get block at height
for _ in 0..self.wait_attempts {
Expand Down Expand Up @@ -725,24 +735,15 @@ mod tests {
assert!(has_planned_fork.is_some());
}

// 1 -> 2 -> 3.1 -> 4.1
// \ -> 3.2 -> 4.2

da.send_transaction(&[1, 2, 3, 4]).await.unwrap();
da.send_transaction(&[4, 5, 6, 7]).await.unwrap();
da.send_transaction(&[8, 9, 0, 1]).await.unwrap();

let block_1_before = da.get_block_at(1).await.unwrap();
let block_2_before = da.get_block_at(2).await.unwrap();
assert_eq!(
block_1_before.header().hash(),
block_2_before.header().prev_hash()
);
assert_consecutive_blocks(&block_1_before, &block_2_before);
let block_3_before = da.get_block_at(3).await.unwrap();
assert_eq!(
block_2_before.header().hash(),
block_3_before.header().prev_hash()
);
assert_consecutive_blocks(&block_2_before, &block_3_before);
let block_4 = da.get_block_at(4).await.unwrap();
{
let has_planned_fork = da.planned_fork.lock().unwrap();
Expand All @@ -752,11 +753,48 @@ mod tests {
// Fork is happening!
assert_ne!(block_3_before.header().hash(), block_4.header().prev_hash());
let block_3_after = da.get_block_at(3).await.unwrap();
assert_eq!(block_3_after.header().hash(), block_4.header().prev_hash());
assert_consecutive_blocks(&block_3_after, &block_4);
assert_consecutive_blocks(&block_2_before, &block_3_after);
}

#[tokio::test]
async fn test_planned_reorg_shorter() {
let mut da = MockDaService::with_finality(MockAddress::new([1; 32]), 4);
da.wait_attempts = 2;
// Planned for will replace blocks at height 3 and 4
let planned_fork =
PlannedFork::new(4, 2, vec![vec![13, 13, 13, 13], vec![14, 14, 14, 14]]);
da.set_planned_fork(planned_fork).await.unwrap();

da.send_transaction(&[1, 1, 1, 1]).await.unwrap();
da.send_transaction(&[2, 2, 2, 2]).await.unwrap();
da.send_transaction(&[3, 3, 3, 3]).await.unwrap();
da.send_transaction(&[4, 4, 4, 4]).await.unwrap();
da.send_transaction(&[5, 5, 5, 5]).await.unwrap();

let block_1_before = da.get_block_at(1).await.unwrap();
let block_2_before = da.get_block_at(2).await.unwrap();
assert_consecutive_blocks(&block_1_before, &block_2_before);
let block_3_before = da.get_block_at(3).await.unwrap();
assert_consecutive_blocks(&block_2_before, &block_3_before);
let block_4 = da.get_block_at(4).await.unwrap();
assert_ne!(block_4.header().prev_hash(), block_3_before.header().hash());
let block_1_after = da.get_block_at(1).await.unwrap();
let block_2_after = da.get_block_at(2).await.unwrap();
let block_3_after = da.get_block_at(3).await.unwrap();
assert_consecutive_blocks(&block_3_after, &block_4);
assert_consecutive_blocks(&block_2_after, &block_3_after);
assert_consecutive_blocks(&block_1_after, &block_2_after);

let block_5 = da.get_block_at(5).await;
assert_eq!(
block_2_before.header().hash(),
block_3_after.header().prev_hash()
"No block at height=5 has been sent in 20ms",
block_5.unwrap_err().to_string()
);
}
}

fn assert_consecutive_blocks(block1: &MockBlock, block2: &MockBlock) {
assert_eq!(block2.header().prev_hash(), block1.header().hash())
}
}
6 changes: 6 additions & 0 deletions adapters/mock-da/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ impl Debug for MockHash {
}
}

impl core::fmt::Display for MockHash {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "0x{}", hex::encode(self.0))
}
}

impl AsRef<[u8]> for MockHash {
fn as_ref(&self) -> &[u8] {
&self.0
Expand Down
77 changes: 65 additions & 12 deletions adapters/mock-zkvm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#![deny(missing_docs)]
#![doc = include_str!("../README.md")]

use std::collections::VecDeque;
use std::io::Write;
use std::sync::{Arc, Condvar, Mutex};

use anyhow::ensure;
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};
use sov_rollup_interface::zk::Matches;
use sov_rollup_interface::da::BlockHeaderTrait;
use sov_rollup_interface::zk::{Matches, StateTransitionData, ValidityCondition};

/// A mock commitment to a particular zkVM program.
#[derive(Debug, Clone, PartialEq, Eq, BorshDeserialize, BorshSerialize, Serialize, Deserialize)]
Expand Down Expand Up @@ -91,20 +93,31 @@ impl Notifier {
}

/// A mock implementing the zkVM trait.
#[derive(Clone, Default)]
pub struct MockZkvm {
#[derive(Clone)]
pub struct MockZkvm<ValidityCond> {
worker_thread_notifier: Notifier,
committed_data: VecDeque<Vec<u8>>,
validity_condition: ValidityCond,
}

impl MockZkvm {
impl<ValidityCond> MockZkvm<ValidityCond> {
/// Creates a new MockZkvm
pub fn new(validity_condition: ValidityCond) -> Self {
Self {
worker_thread_notifier: Default::default(),
committed_data: Default::default(),
validity_condition,
}
}

/// Simulates zk proof generation.
pub fn make_proof(&self) {
// We notify the worket thread.
self.worker_thread_notifier.notify();
}
}

impl sov_rollup_interface::zk::Zkvm for MockZkvm {
impl<ValidityCond: ValidityCondition> sov_rollup_interface::zk::Zkvm for MockZkvm<ValidityCond> {
type CodeCommitment = MockCodeCommitment;

type Error = anyhow::Error;
Expand All @@ -123,30 +136,65 @@ impl sov_rollup_interface::zk::Zkvm for MockZkvm {
}

fn verify_and_extract_output<
Add: sov_rollup_interface::RollupAddress,
Da: sov_rollup_interface::da::DaSpec,
Root: serde::Serialize + serde::de::DeserializeOwned,
>(
serialized_proof: &[u8],
code_commitment: &Self::CodeCommitment,
) -> Result<sov_rollup_interface::zk::StateTransition<Da, Add, Root>, Self::Error> {
) -> Result<sov_rollup_interface::zk::StateTransition<Da, Root>, Self::Error> {
let output = Self::verify(serialized_proof, code_commitment)?;
Ok(bincode::deserialize(output)?)
}
}

impl sov_rollup_interface::zk::ZkvmHost for MockZkvm {
impl<ValidityCond: ValidityCondition> sov_rollup_interface::zk::ZkvmHost
for MockZkvm<ValidityCond>
{
type Guest = MockZkGuest;

fn add_hint<T: Serialize>(&mut self, _item: T) {}
fn add_hint<T: Serialize>(&mut self, item: T) {
let hint = bincode::serialize(&item).unwrap();
let proof_info = ProofInfo {
hint,
validity_condition: self.validity_condition,
};

let data = bincode::serialize(&proof_info).unwrap();
self.committed_data.push_back(data)
}

fn simulate_with_hints(&mut self) -> Self::Guest {
MockZkGuest {}
}

fn run(&mut self, _with_proof: bool) -> Result<sov_rollup_interface::zk::Proof, anyhow::Error> {
self.worker_thread_notifier.wait();
Ok(sov_rollup_interface::zk::Proof::Empty)
let data = self.committed_data.pop_front().unwrap_or_default();
Ok(sov_rollup_interface::zk::Proof::PublicInput(data))
}

fn extract_output<
Da: sov_rollup_interface::da::DaSpec,
Root: Serialize + serde::de::DeserializeOwned,
>(
proof: &sov_rollup_interface::zk::Proof,
) -> Result<sov_rollup_interface::zk::StateTransition<Da, Root>, Self::Error> {
match proof {
sov_rollup_interface::zk::Proof::PublicInput(pub_input) => {
let data: ProofInfo<Da::ValidityCondition> = bincode::deserialize(pub_input)?;
let st: StateTransitionData<Root, (), Da> = bincode::deserialize(&data.hint)?;

Ok(sov_rollup_interface::zk::StateTransition {
initial_state_root: st.initial_state_root,
final_state_root: st.final_state_root,
slot_hash: st.da_block_header.hash(),
validity_condition: data.validity_condition,
})
}
sov_rollup_interface::zk::Proof::Full(_) => {
panic!("Mock DA doesn't generate real proofs")
}
}
}
}

Expand All @@ -166,13 +214,12 @@ impl sov_rollup_interface::zk::Zkvm for MockZkGuest {
}

fn verify_and_extract_output<
Add: sov_rollup_interface::RollupAddress,
Da: sov_rollup_interface::da::DaSpec,
Root: Serialize + serde::de::DeserializeOwned,
>(
_serialized_proof: &[u8],
_code_commitment: &Self::CodeCommitment,
) -> Result<sov_rollup_interface::zk::StateTransition<Da, Add, Root>, Self::Error> {
) -> Result<sov_rollup_interface::zk::StateTransition<Da, Root>, Self::Error> {
unimplemented!()
}
}
Expand All @@ -187,6 +234,12 @@ impl sov_rollup_interface::zk::ZkvmGuest for MockZkGuest {
}
}

#[derive(Debug, Serialize, Deserialize)]
struct ProofInfo<ValidityCond> {
hint: Vec<u8>,
validity_condition: ValidityCond,
}

#[test]
fn test_mock_proof_round_trip() {
let proof = MockProof {
Expand Down
Loading

0 comments on commit 798dbbc

Please sign in to comment.