Skip to content

Commit

Permalink
feat(prt): adapt new IDataProvider & constants parameterization
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenctw committed Dec 15, 2024
1 parent 0405697 commit 9d59195
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 47 deletions.
2 changes: 1 addition & 1 deletion cartesi-rollups/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ alloy = { version = "0.8.0", features = ["sol-types", "contract", "network", "re
anyhow = "1.0"
async-recursion = "1"
async-trait = "0.1.74"
cartesi-rollups-contracts = "2.0.0-rc.12"
cartesi-rollups-contracts = "=2.0.0-rc.12"
clap = { version = "4.5.7", features = ["derive", "env"] }
clap_derive = "=4.5.13"
futures = "0.3"
Expand Down
44 changes: 19 additions & 25 deletions cartesi-rollups/node/blockchain-reader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,18 @@ where
.map(|e| {
let epoch = Epoch {
epoch_number: e
.0
.epochNumber
.to_u64()
.expect("fail to convert epoch number"),
epoch_boundary: e
.0
.blockNumberUpperBound
input_index_boundary: e
.inputIndexUpperBound
.to_u64()
.expect("fail to convert epoch boundary"),
root_tournament: e.0.tournament.to_string(),
root_tournament: e.tournament.to_string(),
};
info!(
"epoch received: epoch_number {}, epoch_boundary {}, root_tournament {}",
epoch.epoch_number, epoch.epoch_boundary, epoch.root_tournament
"epoch received: epoch_number {}, input_index_boundary {}, root_tournament {}",
epoch.epoch_number, epoch.input_index_boundary, epoch.root_tournament
);
epoch
})
Expand Down Expand Up @@ -226,7 +224,7 @@ where
// iterate through newly sealed epochs, fill in the inputs accordingly
let inputs_of_epoch = self.construct_input_ids(
epoch.epoch_number,
epoch.epoch_boundary,
epoch.input_index_boundary,
&mut next_input_index_in_epoch,
&mut input_events_peekable,
);
Expand All @@ -251,22 +249,27 @@ where
fn construct_input_ids<'a>(
&self,
epoch_number: u64,
epoch_boundary: u64,
input_index_boundary: u64,
next_input_index_in_epoch: &mut u64,
input_events_peekable: &mut Peekable<impl Iterator<Item = &'a (InputAdded, u64)>>,
input_events_peekable: &mut Peekable<impl Iterator<Item = &'a InputAdded>>,
) -> Vec<Input> {
let mut inputs = vec![];

while let Some(input_added) = input_events_peekable.peek() {
if input_added.1 >= epoch_boundary {
if input_added
.index
.to_u64()
.expect("fail to convert input index")
>= input_index_boundary
{
break;
}
let input = Input {
id: InputId {
epoch_number,
input_index_in_epoch: *next_input_index_in_epoch,
},
data: input_added.0.input.to_vec(),
data: input_added.input.to_vec(),
};
info!(
"input received: epoch_number {}, input_index {}",
Expand Down Expand Up @@ -303,7 +306,7 @@ impl<E: SolEvent + Send + Sync> EventReader<E> {
prev_finalized: u64,
current_finalized: u64,
provider: &PartitionProvider,
) -> std::result::Result<Vec<(E, u64)>, ProviderErrors> {
) -> std::result::Result<Vec<E>, ProviderErrors> {
assert!(current_finalized > prev_finalized);

let logs = provider
Expand Down Expand Up @@ -340,7 +343,7 @@ impl PartitionProvider {
read_from: &Address,
start_block: u64,
end_block: u64,
) -> std::result::Result<Vec<(E, u64)>, Vec<Error>> {
) -> std::result::Result<Vec<E>, Vec<Error>> {
self.get_events_rec(topic1, read_from, start_block, end_block)
.await
}
Expand All @@ -352,7 +355,7 @@ impl PartitionProvider {
read_from: &Address,
start_block: u64,
end_block: u64,
) -> std::result::Result<Vec<(E, u64)>, Vec<Error>> {
) -> std::result::Result<Vec<E>, Vec<Error>> {
// TODO: partition log queries if range too large
let event = {
let mut e = Event::new_sol(&self.inner, read_from)
Expand All @@ -369,16 +372,7 @@ impl PartitionProvider {

match event.query().await {
Ok(l) => {
let logs = l
.into_iter()
.map(|x| {
(
x.0,
x.1.block_number
.expect("fail to get block number from event"),
)
})
.collect();
let logs = l.into_iter().map(|x| x.0).collect();

Ok(logs)
}
Expand Down
2 changes: 1 addition & 1 deletion cartesi-rollups/node/state-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub struct Input {
#[derive(Clone, Debug)]
pub struct Epoch {
pub epoch_number: u64,
pub epoch_boundary: u64,
pub input_index_boundary: u64,
pub root_tournament: String,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ mod tests {
.into_iter(),
[&Epoch {
epoch_number: 0,
epoch_boundary: 12,
input_index_boundary: 12,
root_tournament: String::new(),
}]
.into_iter(),
Expand Down
30 changes: 15 additions & 15 deletions cartesi-rollups/node/state-manager/src/sql/consensus_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub fn insert_epochs<'a>(

stmt.execute(params![
epoch.epoch_number,
epoch.epoch_boundary,
epoch.input_index_boundary,
epoch.root_tournament
])?;
next_epoch += 1;
Expand All @@ -196,15 +196,15 @@ pub fn insert_epochs<'a>(
fn insert_epoch_statement<'a>(conn: &'a rusqlite::Connection) -> Result<rusqlite::Statement<'a>> {
Ok(conn.prepare(
"\
INSERT INTO epochs (epoch_number, epoch_boundary, root_tournament) VALUES (?1, ?2, ?3)
INSERT INTO epochs (epoch_number, input_index_boundary, root_tournament) VALUES (?1, ?2, ?3)
",
)?)
}

pub fn last_sealed_epoch(conn: &rusqlite::Connection) -> Result<Option<Epoch>> {
let mut stmt = conn.prepare(
"\
SELECT epoch_number, epoch_boundary, root_tournament FROM epochs
SELECT epoch_number, input_index_boundary, root_tournament FROM epochs
ORDER BY epoch_number DESC
LIMIT 1
",
Expand All @@ -214,7 +214,7 @@ pub fn last_sealed_epoch(conn: &rusqlite::Connection) -> Result<Option<Epoch>> {
.query_row([], |row| {
Ok(Epoch {
epoch_number: row.get(0)?,
epoch_boundary: row.get(1)?,
input_index_boundary: row.get(1)?,
root_tournament: row.get(2)?,
})
})
Expand All @@ -224,7 +224,7 @@ pub fn last_sealed_epoch(conn: &rusqlite::Connection) -> Result<Option<Epoch>> {
pub fn epoch(conn: &rusqlite::Connection, epoch_number: u64) -> Result<Option<Epoch>> {
let mut stmt = conn.prepare(
"\
SELECT epoch_boundary, root_tournament FROM epochs
SELECT input_index_boundary, root_tournament FROM epochs
WHERE epoch_number = ?1
",
)?;
Expand All @@ -233,7 +233,7 @@ pub fn epoch(conn: &rusqlite::Connection, epoch_number: u64) -> Result<Option<Ep
.query_row(params![epoch_number], |row| {
Ok(Epoch {
epoch_number,
epoch_boundary: row.get(0)?,
input_index_boundary: row.get(0)?,
root_tournament: row.get(1)?,
})
})
Expand Down Expand Up @@ -538,7 +538,7 @@ mod epochs_tests {
&conn,
[&Epoch {
epoch_number: 1,
epoch_boundary: 0,
input_index_boundary: 0,
root_tournament: String::new(),
}]
.into_iter(),
Expand All @@ -555,7 +555,7 @@ mod epochs_tests {
&conn,
[&Epoch {
epoch_number: 0,
epoch_boundary: 0,
input_index_boundary: 0,
root_tournament: String::new(),
}]
.into_iter(),
Expand All @@ -569,7 +569,7 @@ mod epochs_tests {
&conn,
[&Epoch {
epoch_number: 0,
epoch_boundary: 0,
input_index_boundary: 0,
root_tournament: String::new(),
}]
.into_iter(),
Expand All @@ -584,7 +584,7 @@ mod epochs_tests {
let x: Vec<_> = (1..128)
.map(|i| Epoch {
epoch_number: i,
epoch_boundary: 0,
input_index_boundary: 0,
root_tournament: String::new(),
})
.collect();
Expand All @@ -597,17 +597,17 @@ mod epochs_tests {
[
&Epoch {
epoch_number: 128,
epoch_boundary: 0,
input_index_boundary: 0,
root_tournament: String::new(),
},
&Epoch {
epoch_number: 129,
epoch_boundary: 0,
input_index_boundary: 0,
root_tournament: String::new(),
},
&Epoch {
epoch_number: 131,
epoch_boundary: 0,
input_index_boundary: 0,
root_tournament: String::new(),
}
]
Expand All @@ -627,7 +627,7 @@ mod epochs_tests {
&conn,
[&Epoch {
epoch_number: 130,
epoch_boundary: 99,
input_index_boundary: 99,
root_tournament: tournament_address,
}]
.into_iter(),
Expand All @@ -638,7 +638,7 @@ mod epochs_tests {
epoch(&conn, 130),
Ok(Some(Epoch {
epoch_number: 130,
epoch_boundary: 99,
input_index_boundary: 99,
..
}))
));
Expand Down
2 changes: 1 addition & 1 deletion cartesi-rollups/node/state-manager/src/sql/migrations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CREATE TABLE computation_hashes (

CREATE TABLE epochs (
epoch_number INTEGER NOT NULL PRIMARY KEY,
epoch_boundary INTEGER NOT NULL,
input_index_boundary INTEGER NOT NULL,
root_tournament TEXT NOT NULL
);

Expand Down
3 changes: 2 additions & 1 deletion prt/client-lua/player/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ function StateFetcher:_capture_matches(tournament)
match.current_right = m[3]
match.running_leaf = bint(m[4])
match.current_height = tonumber(m[5])
match.level = tonumber(m[6])
match.log2_step = tonumber(m[6])
match.height = tonumber(m[7])

local leaf_cycle = self.reader:read_cycle(tournament.address, match.match_id_hash)
match.leaf_cycle = bint(leaf_cycle)
Expand Down
1 change: 0 additions & 1 deletion prt/client-rs/src/arena/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ pub struct MatchState {
pub right_node: Digest,
pub running_leaf_position: U256,
pub current_height: u64,
pub level: u64,
pub leaf_cycle: U256,
pub base_big_cycle: u64,
pub tournament_address: Address,
Expand Down
1 change: 0 additions & 1 deletion prt/client-rs/src/arena/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ impl StateReader {
running_leaf_position,
current_height: m.currentHeight,
tournament_address,
level: m.level,
leaf_cycle,
base_big_cycle,
inner_tournament: None,
Expand Down

0 comments on commit 9d59195

Please sign in to comment.