Skip to content

Commit b46b723

Browse files
refactor: Move Block type into essential-node-types. Introduce BlockHeader. (#169)
* refactor: Move Block type into essential-node-types. Introduce BlockHeader. * refactor: Complete migration of Block type to essential-node-types * deps: bump base versions (#171) --------- Co-authored-by: Bikem <[email protected]>
1 parent daf062f commit b46b723

File tree

21 files changed

+303
-141
lines changed

21 files changed

+303
-141
lines changed

Cargo.lock

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ axum = "0.7.7"
1717
clap = { version = "4.5", features = ["derive"] }
1818
crossbeam = "0.8"
1919
dirs = "5"
20-
essential-check = "0.10.0"
21-
essential-hash = "0.8.0"
22-
essential-sign = "0.8.0"
23-
essential-types = "0.6.0"
20+
essential-check = "0.11.0"
21+
essential-hash = "0.9.0"
22+
essential-sign = "0.9.0"
23+
essential-types = "0.7.0"
2424
futures = "0.3.30"
2525
hex = "0.4.3"
2626
http = "1.1.0"

crates/node-api/src/endpoint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use axum::{
99
Json,
1010
};
1111
use essential_node::db;
12-
use essential_node_types::block_notify::BlockRx;
13-
use essential_types::{convert::word_from_bytes, Block, ContentAddress, Value, Word};
12+
use essential_node_types::{block_notify::BlockRx, Block};
13+
use essential_types::{convert::word_from_bytes, ContentAddress, Value, Word};
1414
use futures::{Stream, StreamExt};
1515
use serde::Deserialize;
1616
use thiserror::Error;

crates/node-api/tests/endpoints.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use essential_node::{self as node};
22
use essential_node_api as node_api;
3-
use essential_node_types::block_notify::BlockTx;
4-
use essential_types::{convert::bytes_from_word, Block, Value};
3+
use essential_node_types::{block_notify::BlockTx, Block};
4+
use essential_types::{convert::bytes_from_word, Value};
55
use futures::{StreamExt, TryStreamExt};
66
use tokio_util::{
77
bytes::{self, Buf},

crates/node-db/src/lib.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ pub use error::QueryError;
1515
use essential_hash::content_addr;
1616
#[doc(inline)]
1717
pub use essential_node_db_sql as sql;
18+
use essential_node_types::{block, Block, BlockHeader};
1819
use essential_types::{
1920
convert::{bytes_from_word, word_from_bytes},
2021
solution::{Mutation, Solution, SolutionSet},
21-
Block, ContentAddress, Hash, Key, PredicateAddress, Value, Word,
22+
ContentAddress, Hash, Key, PredicateAddress, Value, Word,
2223
};
2324
use futures::Stream;
2425
#[cfg(feature = "pool")]
@@ -71,14 +72,12 @@ pub fn create_tables(tx: &Transaction) -> rusqlite::Result<()> {
7172
/// Returns the `ContentAddress` of the inserted block.
7273
pub fn insert_block(tx: &Transaction, block: &Block) -> rusqlite::Result<ContentAddress> {
7374
// Insert the header.
74-
let secs = block.timestamp.as_secs();
75-
let nanos = block.timestamp.subsec_nanos() as u64;
75+
let secs = block.header.timestamp.as_secs();
76+
let nanos = block.header.timestamp.subsec_nanos() as u64;
7677
let solution_set_addrs: Vec<ContentAddress> =
7778
block.solution_sets.iter().map(content_addr).collect();
78-
let block_address = essential_hash::block_addr::from_block_and_solution_set_addrs_slice(
79-
block,
80-
&solution_set_addrs,
81-
);
79+
let block_address =
80+
block::addr::from_header_and_solution_set_addrs_slice(&block.header, &solution_set_addrs);
8281

8382
// TODO: Use real parent block address once blocks have parent addresses.
8483
let parent_block_address = ContentAddress([0; 32]);
@@ -88,7 +87,7 @@ pub fn insert_block(tx: &Transaction, block: &Block) -> rusqlite::Result<Content
8887
named_params! {
8988
":block_address": block_address.0,
9089
":parent_block_address": parent_block_address.0,
91-
":number": block.number,
90+
":number": block.header.number,
9291
":timestamp_secs": secs,
9392
":timestamp_nanos": nanos,
9493
},
@@ -307,7 +306,7 @@ pub fn query_state(
307306
pub fn get_block_header(
308307
conn: &Connection,
309308
block_address: &ContentAddress,
310-
) -> rusqlite::Result<Option<(Word, Duration)>> {
309+
) -> rusqlite::Result<Option<BlockHeader>> {
311310
conn.query_row(
312311
sql::query::GET_BLOCK_HEADER,
313312
named_params! {
@@ -318,7 +317,7 @@ pub fn get_block_header(
318317
let timestamp_secs: u64 = row.get("timestamp_secs")?;
319318
let timestamp_nanos: u32 = row.get("timestamp_nanos")?;
320319
let timestamp = Duration::new(timestamp_secs, timestamp_nanos);
321-
Ok((number, timestamp))
320+
Ok(BlockHeader { number, timestamp })
322321
},
323322
)
324323
.optional()
@@ -329,7 +328,7 @@ pub fn get_block(
329328
tx: &Transaction,
330329
block_address: &ContentAddress,
331330
) -> Result<Option<Block>, QueryError> {
332-
let Some((block_number, timestamp)) = get_block_header(tx, block_address)? else {
331+
let Some(header) = get_block_header(tx, block_address)? else {
333332
return Ok(None);
334333
};
335334
let mut stmt = tx.prepare(sql::query::GET_BLOCK)?;
@@ -344,8 +343,7 @@ pub fn get_block(
344343
)?;
345344

346345
let mut block = Block {
347-
number: block_number,
348-
timestamp,
346+
header,
349347
solution_sets: vec![],
350348
};
351349
for res in rows {
@@ -461,8 +459,10 @@ pub fn list_blocks(tx: &Transaction, block_range: Range<Word>) -> Result<Vec<Blo
461459
_ => {
462460
last_block_address = Some(block_address);
463461
blocks.push(Block {
464-
number: block_number,
465-
timestamp,
462+
header: BlockHeader {
463+
number: block_number,
464+
timestamp,
465+
},
466466
solution_sets: vec![],
467467
});
468468
blocks.last_mut().expect("last block must exist")
@@ -528,8 +528,10 @@ pub fn list_blocks_by_time(
528528
_ => {
529529
last_block_address = Some(block_address);
530530
blocks.push(Block {
531-
number: block_number,
532-
timestamp,
531+
header: BlockHeader {
532+
number: block_number,
533+
timestamp,
534+
},
533535
solution_sets: vec![],
534536
});
535537
blocks.last_mut().expect("last block must exist")
@@ -615,8 +617,10 @@ pub fn list_unchecked_blocks(
615617
_ => {
616618
last_block_address = Some(block_address);
617619
blocks.push(Block {
618-
number: block_number,
619-
timestamp,
620+
header: BlockHeader {
621+
number: block_number,
622+
timestamp,
623+
},
620624
solution_sets: vec![],
621625
});
622626
blocks.last_mut().expect("last block must exist")

crates/node-db/src/pool.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
66
use crate::{with_tx, AcquireConnection, AwaitNewBlock, QueryError};
77
use core::ops::Range;
8-
use essential_node_types::block_notify::BlockRx;
9-
use essential_types::{solution::SolutionSet, Block, ContentAddress, Key, Value, Word};
8+
use essential_node_types::{block_notify::BlockRx, Block};
9+
use essential_types::{solution::SolutionSet, ContentAddress, Key, Value, Word};
1010
use futures::Stream;
1111
use rusqlite_pool::tokio::{AsyncConnectionHandle, AsyncConnectionPool};
1212
use std::{path::PathBuf, sync::Arc, time::Duration};
@@ -240,11 +240,15 @@ impl ConnectionPool {
240240
let Some(addr) = crate::get_latest_finalized_block_address(&tx)? else {
241241
return Ok(None);
242242
};
243-
let Some((number, _)) = crate::get_block_header(&tx, &addr)? else {
243+
let Some(header) = crate::get_block_header(&tx, &addr)? else {
244244
return Ok(None);
245245
};
246-
let value =
247-
crate::finalized::query_state_inclusive_block(&tx, &contract_ca, &key, number)?;
246+
let value = crate::finalized::query_state_inclusive_block(
247+
&tx,
248+
&contract_ca,
249+
&key,
250+
header.number,
251+
)?;
248252
tx.finish()?;
249253
Ok(value)
250254
})

crates/node-db/tests/insert.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ fn test_insert_block() {
3737
let timestamp_nanos: u32 = row.get(2).expect("timestamp_nanos");
3838
let timestamp = Duration::new(timestamp_secs, timestamp_nanos);
3939

40-
assert_eq!(id, block.number);
41-
assert_eq!(timestamp, block.timestamp);
40+
assert_eq!(id, block.header.number);
41+
assert_eq!(timestamp, block.header.timestamp);
4242

4343
// Verify that the solution sets were inserted correctly
4444
for solution_set in block.solution_sets.iter() {
@@ -241,14 +241,14 @@ fn test_failed_block() {
241241
// Check failed blocks.
242242
let failed_blocks = node_db::list_failed_blocks(&conn, 0..(NUM_BLOCKS + 10)).unwrap();
243243
assert_eq!(failed_blocks.len(), 1);
244-
assert_eq!(failed_blocks[0].0, blocks[0].number);
244+
assert_eq!(failed_blocks[0].0, blocks[0].header.number);
245245
assert_eq!(failed_blocks[0].1, solution_set_addr);
246246

247247
// Same failed block should not be inserted again.
248248
node_db::insert_failed_block(&conn, &block_address, &solution_set_addr).unwrap();
249249
let failed_blocks = node_db::list_failed_blocks(&conn, 0..(NUM_BLOCKS + 10)).unwrap();
250250
assert_eq!(failed_blocks.len(), 1);
251-
assert_eq!(failed_blocks[0].0, blocks[0].number);
251+
assert_eq!(failed_blocks[0].0, blocks[0].header.number);
252252
assert_eq!(failed_blocks[0].1, solution_set_addr);
253253

254254
// Insert another failed block.
@@ -266,7 +266,7 @@ fn test_failed_block() {
266266
.unwrap();
267267

268268
assert_eq!(failed_blocks.len(), 2);
269-
assert_eq!(failed_blocks[1].0, blocks[1].number);
269+
assert_eq!(failed_blocks[1].0, blocks[1].header.number);
270270
assert_eq!(failed_blocks[1].1, solution_set_addr);
271271
}
272272

0 commit comments

Comments
 (0)