From 9d59195c7dade8e8570a868697c66fe671ad8ea0 Mon Sep 17 00:00:00 2001 From: Stephen Chen <20940639+stephenctw@users.noreply.github.com> Date: Sun, 15 Dec 2024 10:59:56 +0800 Subject: [PATCH] feat(prt): adapt new `IDataProvider` & constants parameterization --- cartesi-rollups/node/Cargo.toml | 2 +- .../node/blockchain-reader/src/lib.rs | 44 ++++++++----------- cartesi-rollups/node/state-manager/src/lib.rs | 2 +- .../src/persistent_state_access.rs | 2 +- .../state-manager/src/sql/consensus_data.rs | 30 ++++++------- .../node/state-manager/src/sql/migrations.sql | 2 +- prt/client-lua/player/state.lua | 3 +- prt/client-rs/src/arena/arena.rs | 1 - prt/client-rs/src/arena/reader.rs | 1 - 9 files changed, 40 insertions(+), 47 deletions(-) diff --git a/cartesi-rollups/node/Cargo.toml b/cartesi-rollups/node/Cargo.toml index 2beb706f..e891b58e 100644 --- a/cartesi-rollups/node/Cargo.toml +++ b/cartesi-rollups/node/Cargo.toml @@ -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" diff --git a/cartesi-rollups/node/blockchain-reader/src/lib.rs b/cartesi-rollups/node/blockchain-reader/src/lib.rs index ca15f276..41a44ed6 100644 --- a/cartesi-rollups/node/blockchain-reader/src/lib.rs +++ b/cartesi-rollups/node/blockchain-reader/src/lib.rs @@ -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 }) @@ -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, ); @@ -251,14 +249,19 @@ 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>, + input_events_peekable: &mut Peekable>, ) -> Vec { 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 { @@ -266,7 +269,7 @@ where 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 {}", @@ -303,7 +306,7 @@ impl EventReader { prev_finalized: u64, current_finalized: u64, provider: &PartitionProvider, - ) -> std::result::Result, ProviderErrors> { + ) -> std::result::Result, ProviderErrors> { assert!(current_finalized > prev_finalized); let logs = provider @@ -340,7 +343,7 @@ impl PartitionProvider { read_from: &Address, start_block: u64, end_block: u64, - ) -> std::result::Result, Vec> { + ) -> std::result::Result, Vec> { self.get_events_rec(topic1, read_from, start_block, end_block) .await } @@ -352,7 +355,7 @@ impl PartitionProvider { read_from: &Address, start_block: u64, end_block: u64, - ) -> std::result::Result, Vec> { + ) -> std::result::Result, Vec> { // TODO: partition log queries if range too large let event = { let mut e = Event::new_sol(&self.inner, read_from) @@ -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) } diff --git a/cartesi-rollups/node/state-manager/src/lib.rs b/cartesi-rollups/node/state-manager/src/lib.rs index 5ec7eee6..0305eb2f 100644 --- a/cartesi-rollups/node/state-manager/src/lib.rs +++ b/cartesi-rollups/node/state-manager/src/lib.rs @@ -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, } diff --git a/cartesi-rollups/node/state-manager/src/persistent_state_access.rs b/cartesi-rollups/node/state-manager/src/persistent_state_access.rs index 2d97c763..fdcfba89 100644 --- a/cartesi-rollups/node/state-manager/src/persistent_state_access.rs +++ b/cartesi-rollups/node/state-manager/src/persistent_state_access.rs @@ -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(), diff --git a/cartesi-rollups/node/state-manager/src/sql/consensus_data.rs b/cartesi-rollups/node/state-manager/src/sql/consensus_data.rs index 7cff76b7..55531875 100644 --- a/cartesi-rollups/node/state-manager/src/sql/consensus_data.rs +++ b/cartesi-rollups/node/state-manager/src/sql/consensus_data.rs @@ -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; @@ -196,7 +196,7 @@ pub fn insert_epochs<'a>( fn insert_epoch_statement<'a>(conn: &'a rusqlite::Connection) -> Result> { 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) ", )?) } @@ -204,7 +204,7 @@ fn insert_epoch_statement<'a>(conn: &'a rusqlite::Connection) -> Result Result> { 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 ", @@ -214,7 +214,7 @@ pub fn last_sealed_epoch(conn: &rusqlite::Connection) -> Result> { .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)?, }) }) @@ -224,7 +224,7 @@ pub fn last_sealed_epoch(conn: &rusqlite::Connection) -> Result> { pub fn epoch(conn: &rusqlite::Connection, epoch_number: u64) -> Result> { let mut stmt = conn.prepare( "\ - SELECT epoch_boundary, root_tournament FROM epochs + SELECT input_index_boundary, root_tournament FROM epochs WHERE epoch_number = ?1 ", )?; @@ -233,7 +233,7 @@ pub fn epoch(conn: &rusqlite::Connection, epoch_number: u64) -> Result = (1..128) .map(|i| Epoch { epoch_number: i, - epoch_boundary: 0, + input_index_boundary: 0, root_tournament: String::new(), }) .collect(); @@ -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(), } ] @@ -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(), @@ -638,7 +638,7 @@ mod epochs_tests { epoch(&conn, 130), Ok(Some(Epoch { epoch_number: 130, - epoch_boundary: 99, + input_index_boundary: 99, .. })) )); diff --git a/cartesi-rollups/node/state-manager/src/sql/migrations.sql b/cartesi-rollups/node/state-manager/src/sql/migrations.sql index ca527382..1f85f256 100644 --- a/cartesi-rollups/node/state-manager/src/sql/migrations.sql +++ b/cartesi-rollups/node/state-manager/src/sql/migrations.sql @@ -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 ); diff --git a/prt/client-lua/player/state.lua b/prt/client-lua/player/state.lua index a96bfac5..900326d2 100644 --- a/prt/client-lua/player/state.lua +++ b/prt/client-lua/player/state.lua @@ -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) diff --git a/prt/client-rs/src/arena/arena.rs b/prt/client-rs/src/arena/arena.rs index 679a189d..e83072b5 100644 --- a/prt/client-rs/src/arena/arena.rs +++ b/prt/client-rs/src/arena/arena.rs @@ -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, diff --git a/prt/client-rs/src/arena/reader.rs b/prt/client-rs/src/arena/reader.rs index fa68a091..df53b8de 100644 --- a/prt/client-rs/src/arena/reader.rs +++ b/prt/client-rs/src/arena/reader.rs @@ -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,