Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollups meta step #82

Merged
merged 23 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
95f5909
feat: create compute/rollups meta step functions
stephenctw Oct 15, 2024
ea95109
fix(measure): missing input span, update measured constants
stephenctw Oct 16, 2024
632f6f6
feat: handle inputs in compute clients
stephenctw Oct 24, 2024
7057874
chore: rename `dispute` to `compute`
stephenctw Oct 24, 2024
cca08b7
dep: bump `step` to include `send-cmio-response`
stephenctw Oct 27, 2024
901da71
feat(prt): complete rollups meta step
stephenctw Oct 31, 2024
c4f1c08
fix(prt-contracts): use custom error to reduce code size
stephenctw Nov 19, 2024
5820fd1
feat(prt-rollups): add integration test with fixes
stephenctw Dec 2, 2024
db8cd47
chore(prt-rollups): move rollups tests to `prt/test/rollups`
stephenctw Dec 9, 2024
6961fe4
fix(prt): make verbosity configurable and fix compute access test
stephenctw Dec 9, 2024
fe9c552
fix(prt-rollups): use correct initial state and prepare step logs
stephenctw Dec 9, 2024
61b71fd
fix: `fake_commitment` fail to join tournament
stephenctw Dec 9, 2024
ea9ce91
dep: bump `alloy` to 0.8.0
stephenctw Dec 11, 2024
8735197
fix(prt-client): wrong `machine.run` cycle calculation
stephenctw Dec 11, 2024
906b5d7
chore(prt): improve logging
stephenctw Dec 11, 2024
5356960
feat(prt-client): improve snapshots
stephenctw Dec 11, 2024
ebd05bb
chore(prt-tests): improve Makefile
stephenctw Dec 11, 2024
e61d968
chore: update root .dockerignore file
stephenctw Dec 11, 2024
ea06864
feat(prt): complete meta-step implementation
stephenctw Dec 11, 2024
f10a103
chore(prt): cleanups, logging and namings
stephenctw Dec 12, 2024
e0926f2
dep: bump `rollups-contracts` to `2.0.0-rc.12`
stephenctw Dec 12, 2024
b4a1339
fix(prt): use correct parameters to `gio` call
stephenctw Dec 12, 2024
4d00d15
chore(prt-contracts): improve logging
stephenctw Dec 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.vscode
**/target/
**/snapshots/
/**/target/
/**/snapshots/
Cargo.lock
/**/compute_data/
/**/rollups_data/
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ target/
snapshots/
common-rs/Cargo.lock
prt/client-rs/Cargo.lock
prt/lua_poc/outputs/
prt/lua_poc/pixels/
prt/tests/compute/outputs/
prt/tests/compute/pixels/
node_modules
**/contract-bindings/src/contract
**/contract-bindings/Cargo.lock
**/compute_data/
**/rollups_data/
2 changes: 1 addition & 1 deletion cartesi-rollups/contract-bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ readme = "README.md"
repository = "https://github.com/cartesi/dave"

[dependencies]
alloy = { version = "0.3.1", features = ["sol-types", "contract"] }
alloy = { version = "0.8.0", features = ["sol-types", "contract"] }

15 changes: 15 additions & 0 deletions cartesi-rollups/contracts/deploy_anvil.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

set -euo pipefail

INITIAL_HASH=`xxd -p -c32 "${MACHINE_PATH}/hash"`

export PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

forge script \
script/DaveConsensus.s.sol \
--fork-url "http://127.0.0.1:8545" \
--broadcast \
--sig "run(bytes32)" \
"${INITIAL_HASH}" \
-vvvv
2 changes: 2 additions & 0 deletions cartesi-rollups/contracts/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ allow_paths = [
'../../machine/step/',
]

solc-version = "0.8.27"

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
26 changes: 26 additions & 0 deletions cartesi-rollups/contracts/script/DaveConsensus.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// (c) Cartesi and individual authors (see AUTHORS)
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)

pragma solidity ^0.8.17;

import {Script} from "forge-std/Script.sol";

import {Machine} from "prt-contracts/Machine.sol";

import "prt-contracts/tournament/factories/MultiLevelTournamentFactory.sol";
import "rollups-contracts/inputs/InputBox.sol";
import "src/DaveConsensus.sol";

contract DaveConcensusScript is Script {
function run(Machine.Hash initialHash) external {
vm.startBroadcast(vm.envUint("PRIVATE_KEY"));

InputBox inputBox = new InputBox();
MultiLevelTournamentFactory factory = new MultiLevelTournamentFactory(
new TopTournamentFactory(), new MiddleTournamentFactory(), new BottomTournamentFactory()
);
new DaveConsensus(inputBox, address(0x0), factory, initialHash);

vm.stopBroadcast();
}
}
3 changes: 2 additions & 1 deletion cartesi-rollups/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ cartesi-dave-contracts = { path = "../contract-bindings" }
cartesi-dave-merkle = { path = "../../common-rs/merkle" }
cartesi-prt-core = { path = "../../prt/client-rs" }

alloy = { version = "0.3.1", features = ["sol-types", "contract", "network", "reqwest", "signers", "signer-local"] }
alloy = { version = "0.8.0", features = ["sol-types", "contract", "network", "reqwest", "signers", "signer-local"] }
anyhow = "1.0"
async-recursion = "1"
async-trait = "0.1.74"
Expand All @@ -44,5 +44,6 @@ clap = { version = "4.5.7", features = ["derive", "env"] }
clap_derive = "=4.5.13"
futures = "0.3"
log = "0.4"
num-traits = "0.2.19"
thiserror = "1.0"
tokio = { version = "1", features = ["full"] }
4 changes: 2 additions & 2 deletions cartesi-rollups/node/Dockerfile.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ RUN curl https://sh.rustup.rs -sSf | bash -s -- -y

ENV PATH="/root/.cargo/bin:${PATH}"

COPY --from=ethereum/solc:0.8.23 /usr/bin/solc /usr/bin/solc
COPY --from=ethereum/solc:0.8.27 /usr/bin/solc /usr/bin/solc
RUN chmod u+x /usr/bin/solc

WORKDIR /app

RUN wget https://github.com/cartesi/image-kernel/releases/download/v0.20.0/linux-6.5.13-ctsi-1-v0.20.0.bin \
-O ./linux.bin
RUN wget https://github.com/cartesi/machine-emulator-tools/releases/download/v0.15.0/rootfs-tools-v0.15.0.ext2 \
RUN wget https://github.com/cartesi/machine-emulator-tools/releases/download/v0.16.1/rootfs-tools-v0.16.1.ext2 \
-O ./rootfs.ext2

RUN cartesi-machine --ram-image=./linux.bin \
Expand Down
11 changes: 6 additions & 5 deletions cartesi-rollups/node/blockchain-reader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ repository = { workspace = true }

[dependencies]
rollups-state-manager = { workspace = true }
alloy = { workspace = true }
async-recursion = { workspace = true }
cartesi-dave-contracts = { workspace = true }
cartesi-rollups-contracts = { workspace = true }

alloy = { workspace = true }
alloy-rpc-types-eth = "0.8.0"
async-recursion = { workspace = true }
clap = { workspace = true }
clap_derive = { workspace = true }
log = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }

alloy-rpc-types-eth = "0.3.1"
num-traits = "0.2.19"
num-traits = { workspace = true }
125 changes: 76 additions & 49 deletions cartesi-rollups/node/blockchain-reader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::error::{ProviderErrors, Result};
use alloy::{
contract::{Error, Event},
eips::BlockNumberOrTag::Finalized,
hex::ToHexExt,
providers::{
network::primitives::BlockTransactionsKind, Provider, ProviderBuilder, RootProvider,
},
Expand All @@ -17,8 +18,10 @@ use alloy_rpc_types_eth::Topic;
use async_recursion::async_recursion;
use clap::Parser;
use error::BlockchainReaderError;
use log::{info, trace};
use num_traits::cast::ToPrimitive;
use std::{
iter::Peekable,
marker::{Send, Sync},
str::FromStr,
sync::Arc,
Expand All @@ -29,18 +32,21 @@ use cartesi_dave_contracts::daveconsensus::DaveConsensus::EpochSealed;
use cartesi_rollups_contracts::inputbox::InputBox::InputAdded;
use rollups_state_manager::{Epoch, Input, InputId, StateManager};

const DEVNET_CONSENSUS_ADDRESS: &str = "0x5FC8d32690cc91D4c39d9d3abcBD16989F875707";
const DEVNET_INPUT_BOX_ADDRESS: &str = "0x5FbDB2315678afecb367f032d93F642f64180aa3";

#[derive(Debug, Clone, Parser)]
#[command(name = "cartesi_rollups_config")]
#[command(about = "Addresses of Cartesi Rollups")]
pub struct AddressBook {
/// address of app
#[arg(long, env)]
#[arg(long, env, default_value_t = Address::ZERO)]
app: Address,
/// address of Dave consensus
#[arg(long, env)]
#[arg(long, env, default_value = DEVNET_CONSENSUS_ADDRESS)]
pub consensus: Address,
/// address of input box
#[arg(long, env)]
#[arg(long, env, default_value = DEVNET_INPUT_BOX_ADDRESS)]
input_box: Address,
}

Expand Down Expand Up @@ -85,9 +91,11 @@ where
pub async fn start(&mut self) -> Result<(), SM> {
loop {
let current_block = self.provider.latest_finalized_block().await?;
self.advance(self.prev_block, current_block).await?;
self.prev_block = current_block;

if current_block > self.prev_block {
self.advance(self.prev_block, current_block).await?;
self.prev_block = current_block;
}
tokio::time::sleep(self.sleep_duration).await;
}
}
Expand Down Expand Up @@ -116,13 +124,23 @@ where
.collect_sealed_epochs(prev_block, current_block)
.await?;

let last_sealed_epoch_opt = self
.state_manager
.last_sealed_epoch()
.map_err(|e| BlockchainReaderError::StateManagerError(e))?;
let mut merged_sealed_epochs = Vec::new();
if let Some(last_sealed_epoch) = last_sealed_epoch_opt {
merged_sealed_epochs.push(last_sealed_epoch);
}
merged_sealed_epochs.extend(sealed_epochs.clone());
let merged_sealed_epochs_iter = merged_sealed_epochs
.iter()
.collect::<Vec<&Epoch>>()
.into_iter();

// read inputs from blockchain
let inputs = self
.collect_inputs(
prev_block,
current_block,
sealed_epochs.iter().collect::<Vec<&Epoch>>().into_iter(),
)
.collect_inputs(prev_block, current_block, merged_sealed_epochs_iter)
.await?;

Ok((inputs, sealed_epochs))
Expand All @@ -144,18 +162,25 @@ where
)
.await?
.iter()
.map(|e| Epoch {
epoch_number: e
.0
.epochNumber
.to_u64()
.expect("fail to convert epoch number"),
epoch_boundary: e
.0
.blockNumberUpperBound
.to_u64()
.expect("fail to convert epoch boundary"),
root_tournament: e.0.tournament.to_string(),
.map(|e| {
let epoch = Epoch {
epoch_number: e
.0
.epochNumber
.to_u64()
.expect("fail to convert epoch number"),
epoch_boundary: e
.0
.blockNumberUpperBound
.to_u64()
.expect("fail to convert epoch boundary"),
root_tournament: e.0.tournament.to_string(),
};
info!(
"epoch received: epoch_number {}, epoch_boundary {}, root_tournament {}",
epoch.epoch_number, epoch.epoch_boundary, epoch.root_tournament
);
epoch
})
.collect())
}
Expand Down Expand Up @@ -193,61 +218,63 @@ where
};

let mut inputs = vec![];
let mut input_events_iter = input_events.iter();
let mut input_events_peekable = input_events.iter().peekable();
for epoch in sealed_epochs_iter {
if last_epoch_number > epoch.epoch_number {
continue;
}
// iterate through newly sealed epochs, fill in the inputs accordingly
let inputs_of_epoch = self
.construct_input_ids(
epoch.epoch_number,
epoch.epoch_boundary,
&mut next_input_index_in_epoch,
&mut input_events_iter,
)
.await;
let inputs_of_epoch = self.construct_input_ids(
epoch.epoch_number,
epoch.epoch_boundary,
&mut next_input_index_in_epoch,
&mut input_events_peekable,
);

inputs.extend(inputs_of_epoch);
last_epoch_number = epoch.epoch_number + 1;
}

// all remaining inputs belong to an epoch that's not sealed yet
let inputs_of_epoch = self
.construct_input_ids(
last_epoch_number,
u64::MAX,
&mut next_input_index_in_epoch,
&mut input_events_iter,
)
.await;
let inputs_of_epoch = self.construct_input_ids(
last_epoch_number,
u64::MAX,
&mut next_input_index_in_epoch,
&mut input_events_peekable,
);

inputs.extend(inputs_of_epoch);

Ok(inputs)
}

async fn construct_input_ids(
fn construct_input_ids<'a>(
&self,
epoch_number: u64,
epoch_boundary: u64,
next_input_index_in_epoch: &mut u64,
input_events_iter: &mut impl Iterator<Item = &(InputAdded, u64)>,
input_events_peekable: &mut Peekable<impl Iterator<Item = &'a (InputAdded, u64)>>,
) -> Vec<Input> {
let mut inputs = vec![];

while input_events_iter
.peekable()
.peek()
.expect("fail to get peek next input")
.1
< epoch_boundary
{
while let Some(input_added) = input_events_peekable.peek() {
if input_added.1 >= epoch_boundary {
break;
}
let input = Input {
id: InputId {
epoch_number,
input_index_in_epoch: *next_input_index_in_epoch,
},
data: input_events_iter.next().unwrap().0.input.to_vec(),
data: input_added.0.input.to_vec(),
};
info!(
"input received: epoch_number {}, input_index {}",
input.id.epoch_number, input.id.input_index_in_epoch,
);
trace!("input data 0x{}", input.data.encode_hex());

input_events_peekable.next();
*next_input_index_in_epoch += 1;
inputs.push(input);
}
Expand Down
1 change: 1 addition & 0 deletions cartesi-rollups/node/compute-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ cartesi-prt-core = { workspace = true }
rollups-state-manager = { workspace = true }

alloy = { workspace = true }
log = { workspace = true }
Loading