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

chore(sequencer)!: put blocks and deposits to non-verified storage (ENG-812) #1525

Merged
merged 10 commits into from
Sep 26, 2024
63 changes: 32 additions & 31 deletions crates/astria-sequencer/src/api_state_ext.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noot Can I ask you to double check these keys? I think I recall (but might be mistaken) that you wanted plaintext/human readable keys.

I don't think we ever formalized this, but I would like you to give your explicit approval here.

Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,28 @@ use cnidarium::{
use prost::Message;
use tracing::instrument;

fn block_hash_by_height_key(height: u64) -> String {
format!("blockhash/{height}")
fn block_hash_by_height_key(height: u64) -> Vec<u8> {
[b"blockhash/".as_slice(), &height.to_le_bytes()].concat()
}

fn sequencer_block_header_by_hash_key(hash: &[u8]) -> String {
format!("blockheader/{}", crate::utils::Hex(hash))
fn sequencer_block_header_by_hash_key(hash: &[u8]) -> Vec<u8> {
[b"blockheader/", hash].concat()
}

fn rollup_data_by_hash_and_rollup_id_key(hash: &[u8], rollup_id: &RollupId) -> String {
format!("rollupdata/{}/{}", crate::utils::Hex(hash), rollup_id)
fn rollup_data_by_hash_and_rollup_id_key(hash: &[u8], rollup_id: &RollupId) -> Vec<u8> {
[b"rollupdata/", hash, rollup_id.as_ref()].concat()
}

fn rollup_ids_by_hash_key(hash: &[u8]) -> String {
format!("rollupids/{}", crate::utils::Hex(hash))
fn rollup_ids_by_hash_key(hash: &[u8]) -> Vec<u8> {
[b"rollupids/", hash].concat()
}

fn rollup_transactions_proof_by_hash_key(hash: &[u8]) -> String {
format!("rolluptxsproof/{}", crate::utils::Hex(hash))
fn rollup_transactions_proof_by_hash_key(hash: &[u8]) -> Vec<u8> {
[b"rolluptxsproof/", hash].concat()
}

fn rollup_ids_proof_by_hash_key(hash: &[u8]) -> String {
format!("rollupidsproof/{}", crate::utils::Hex(hash))
fn rollup_ids_proof_by_hash_key(hash: &[u8]) -> Vec<u8> {
[b"rollupidsproof/", hash].concat()
}

#[derive(BorshSerialize, BorshDeserialize)]
Expand Down Expand Up @@ -139,7 +139,7 @@ pub(crate) trait StateReadExt: StateRead {
async fn get_block_hash_by_height(&self, height: u64) -> Result<[u8; 32]> {
let key = block_hash_by_height_key(height);
let Some(hash) = self
.get_raw(&key)
.nonverifiable_get_raw(&key)
.await
.map_err(anyhow_to_eyre)
.wrap_err("failed to read block hash by height from state")?
Expand All @@ -160,7 +160,7 @@ pub(crate) trait StateReadExt: StateRead {
) -> Result<SequencerBlockHeader> {
let key = sequencer_block_header_by_hash_key(hash);
let Some(header_bytes) = self
.get_raw(&key)
.nonverifiable_get_raw(&key)
.await
.map_err(anyhow_to_eyre)
.wrap_err("failed to read raw sequencer block from state")?
Expand All @@ -179,7 +179,7 @@ pub(crate) trait StateReadExt: StateRead {
async fn get_rollup_ids_by_block_hash(&self, hash: &[u8]) -> Result<Vec<RollupId>> {
let key = rollup_ids_by_hash_key(hash);
let Some(rollup_ids_bytes) = self
.get_raw(&key)
.nonverifiable_get_raw(&key)
.await
.map_err(anyhow_to_eyre)
.wrap_err("failed to read rollup IDs by block hash from state")?
Expand All @@ -195,7 +195,7 @@ pub(crate) trait StateReadExt: StateRead {
#[instrument(skip_all)]
async fn get_sequencer_block_by_hash(&self, hash: &[u8]) -> Result<SequencerBlock> {
let Some(header_bytes) = self
.get_raw(&sequencer_block_header_by_hash_key(hash))
.nonverifiable_get_raw(&sequencer_block_header_by_hash_key(hash))
.await
.map_err(anyhow_to_eyre)
.wrap_err("failed to read raw sequencer block from state")?
Expand All @@ -214,10 +214,11 @@ pub(crate) trait StateReadExt: StateRead {
let mut rollup_transactions = Vec::with_capacity(rollup_ids.len());
for id in &rollup_ids {
let key = rollup_data_by_hash_and_rollup_id_key(hash, id);
let raw =
self.get_raw(&key).await.map_err(anyhow_to_eyre).wrap_err(
"failed to read rollup data by block hash and rollup ID from state",
)?;
let raw = self
.nonverifiable_get_raw(&key)
.await
.map_err(anyhow_to_eyre)
.context("failed to read rollup data by block hash and rollup ID from state")?;
if let Some(raw) = raw {
let raw = raw.as_slice();
let rollup_data = raw::RollupTransactions::decode(raw)
Expand All @@ -227,7 +228,7 @@ pub(crate) trait StateReadExt: StateRead {
}

let Some(rollup_transactions_proof) = self
.get_raw(&rollup_transactions_proof_by_hash_key(hash))
.nonverifiable_get_raw(&rollup_transactions_proof_by_hash_key(hash))
.await
.map_err(anyhow_to_eyre)
.wrap_err("failed to read rollup transactions proof by block hash from state")?
Expand All @@ -240,7 +241,7 @@ pub(crate) trait StateReadExt: StateRead {
.wrap_err("failed to decode rollup transactions proof from raw bytes")?;

let Some(rollup_ids_proof) = self
.get_raw(&rollup_ids_proof_by_hash_key(hash))
.nonverifiable_get_raw(&rollup_ids_proof_by_hash_key(hash))
.await
.map_err(anyhow_to_eyre)
.wrap_err("failed to read rollup IDs proof by block hash from state")?
Expand Down Expand Up @@ -284,7 +285,7 @@ pub(crate) trait StateReadExt: StateRead {
) -> Result<RollupTransactions> {
let key = rollup_data_by_hash_and_rollup_id_key(hash, rollup_id);
let Some(bytes) = self
.get_raw(&key)
.nonverifiable_get_raw(&key)
.await
.map_err(anyhow_to_eyre)
.wrap_err("failed to read rollup data by block hash and rollup ID from state")?
Expand All @@ -306,7 +307,7 @@ pub(crate) trait StateReadExt: StateRead {
hash: &[u8],
) -> Result<(primitiveRaw::Proof, primitiveRaw::Proof)> {
let Some(rollup_transactions_proof) = self
.get_raw(&rollup_transactions_proof_by_hash_key(hash))
.nonverifiable_get_raw(&rollup_transactions_proof_by_hash_key(hash))
.await
.map_err(anyhow_to_eyre)
.wrap_err("failed to read rollup transactions proof by block hash from state")?
Expand All @@ -319,7 +320,7 @@ pub(crate) trait StateReadExt: StateRead {
.wrap_err("failed to decode rollup transactions proof from raw bytes")?;

let Some(rollup_ids_proof) = self
.get_raw(&rollup_ids_proof_by_hash_key(hash))
.nonverifiable_get_raw(&rollup_ids_proof_by_hash_key(hash))
.await
.map_err(anyhow_to_eyre)
.wrap_err("failed to read rollup IDs proof by block hash from state")?
Expand Down Expand Up @@ -348,7 +349,7 @@ pub(crate) trait StateWriteExt: StateWrite {
// 6. block hash to rollup IDs proof

let key = block_hash_by_height_key(block.height().into());
self.put_raw(key, block.block_hash().to_vec());
self.nonverifiable_put_raw(key, block.block_hash().to_vec());

let rollup_ids = block
.rollup_transactions()
Expand All @@ -359,7 +360,7 @@ pub(crate) trait StateWriteExt: StateWrite {

let key = rollup_ids_by_hash_key(&block.block_hash());

self.put_raw(
self.nonverifiable_put_raw(
key,
borsh::to_vec(&RollupIdSeq(rollup_ids))
.wrap_err("failed to serialize rollup IDs list")?,
Expand All @@ -374,18 +375,18 @@ pub(crate) trait StateWriteExt: StateWrite {
rollup_ids_proof,
} = block.into_parts();
let header = header.into_raw();
self.put_raw(key, header.encode_to_vec());
self.nonverifiable_put_raw(key, header.encode_to_vec());

for (id, rollup_data) in rollup_transactions {
let key = rollup_data_by_hash_and_rollup_id_key(&block_hash, &id);
self.put_raw(key, rollup_data.into_raw().encode_to_vec());
self.nonverifiable_put_raw(key, rollup_data.into_raw().encode_to_vec());
}

let key = rollup_transactions_proof_by_hash_key(&block_hash);
self.put_raw(key, rollup_transactions_proof.into_raw().encode_to_vec());
self.nonverifiable_put_raw(key, rollup_transactions_proof.into_raw().encode_to_vec());

let key = rollup_ids_proof_by_hash_key(&block_hash);
self.put_raw(key, rollup_ids_proof.into_raw().encode_to_vec());
self.nonverifiable_put_raw(key, rollup_ids_proof.into_raw().encode_to_vec());

Ok(())
}
Expand Down
29 changes: 7 additions & 22 deletions crates/astria-sequencer/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,7 @@ impl App {
self.metrics
.record_proposal_transactions(signed_txs_included.len());

let deposits = self
.state
.get_block_deposits()
.await
.wrap_err("failed to get block deposits in prepare_proposal")?;
let deposits = self.state.get_cached_block_deposits();
self.metrics.record_proposal_deposits(deposits.len());

// generate commitment to sequence::Actions and deposits and commitment to the rollup IDs
Expand Down Expand Up @@ -444,11 +440,7 @@ impl App {
);
self.metrics.record_proposal_transactions(signed_txs.len());

let deposits = self
.state
.get_block_deposits()
.await
.wrap_err("failed to get block deposits in process_proposal")?;
let deposits = self.state.get_cached_block_deposits();
self.metrics.record_proposal_deposits(deposits.len());

let GeneratedCommitments {
Expand Down Expand Up @@ -871,21 +863,14 @@ impl App {

let end_block = self.end_block(height.value(), sudo_address).await?;

// get and clear block deposits from state
// get deposits for this block from state's ephemeral cache and put them to storage.
let mut state_tx = StateDelta::new(self.state.clone());
let deposits = self
.state
.get_block_deposits()
.await
.wrap_err("failed to get block deposits in end_block")?;
state_tx
.clear_block_deposits()
.await
.wrap_err("failed to clear block deposits")?;
let deposits_in_this_block = self.state.get_cached_block_deposits();
debug!(
deposits = %telemetry::display::json(&deposits),
deposits = %telemetry::display::json(&deposits_in_this_block),
"got block deposits from state"
);
state_tx.put_deposits(deposits_in_this_block.clone());

let sequencer_block = SequencerBlock::try_from_block_info_and_data(
block_hash,
Expand All @@ -898,7 +883,7 @@ impl App {
.into_iter()
.map(std::convert::Into::into)
.collect(),
deposits,
deposits_in_this_block,
)
.wrap_err("failed to convert block info and data to SequencerBlock")?;
state_tx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,36 @@ source: crates/astria-sequencer/src/app/tests_breaking_changes.rs
expression: app.app_hash.as_bytes()
---
[
18,
206,
200,
101,
160,
129,
124,
188,
249,
181,
44,
218,
209,
251,
208,
98,
119,
122,
211,
105,
201,
75,
214,
56,
145,
255,
37,
50,
148,
173,
225,
73,
8,
85,
223,
87,
239,
132,
48,
208,
77,
23,
229,
208,
0,
79,
13,
53,
156
108,
163,
67,
97,
222,
83,
63,
46,
116,
31,
142,
68
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,36 @@ source: crates/astria-sequencer/src/app/tests_breaking_changes.rs
expression: app.app_hash.as_bytes()
---
[
113,
62,
40,
174,
69,
178,
233,
224,
39,
210,
192,
217,
27,
49,
166,
136,
209,
172,
147,
223,
184,
187,
228,
8,
178,
5,
178,
0,
236,
111,
231,
207,
235,
81,
163,
103,
135,
254,
99,
63,
197,
35,
165,
202,
177,
78,
238,
106,
144,
250,
69,
90,
192,
129,
187
161,
77,
92,
49,
147,
119,
69
]
Loading
Loading