-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathmod.rs
More file actions
219 lines (194 loc) · 7.72 KB
/
Copy pathmod.rs
File metadata and controls
219 lines (194 loc) · 7.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
mod migrations;
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};
use miden_node_db::DatabaseError;
use miden_node_db::sqlite::{Database, ReadTx, WriteTx};
use miden_protocol::block::{BlockHeader, BlockNumber};
use miden_protocol::transaction::TransactionId;
use miden_protocol::utils::serde::Serializable;
use tracing::instrument;
use crate::COMPONENT;
use crate::db::migrations::{bootstrap_database, migrate_database, verify_latest_schema};
use crate::tx_validation::ValidatedTransaction;
/// Open a connection to the DB after verifying that it is at the latest schema version.
#[instrument(target = COMPONENT, skip_all)]
pub async fn load(database_filepath: PathBuf) -> Result<Database, DatabaseError> {
load_with_pool_size(database_filepath, miden_node_db::default_connection_pool_size()).await
}
/// Open a connection to the DB with a specific pool size after verifying that it is at the latest
/// schema version.
#[instrument(target = COMPONENT, skip_all)]
pub async fn load_with_pool_size(
database_filepath: PathBuf,
connection_pool_size: NonZeroUsize,
) -> Result<Database, DatabaseError> {
verify_latest_schema(&database_filepath)?;
open_with_pool_size(&database_filepath, connection_pool_size)
}
/// Creates a new database, applies all migrations, and opens a connection pool.
#[instrument(target = COMPONENT, skip_all)]
pub async fn setup(database_filepath: PathBuf) -> Result<Database, DatabaseError> {
setup_with_pool_size(database_filepath, miden_node_db::default_connection_pool_size()).await
}
/// Creates a new database with a specific pool size and applies all migrations.
#[instrument(target = COMPONENT, skip_all)]
pub async fn setup_with_pool_size(
database_filepath: PathBuf,
connection_pool_size: NonZeroUsize,
) -> Result<Database, DatabaseError> {
bootstrap_database(&database_filepath)?;
open_with_pool_size(&database_filepath, connection_pool_size)
}
/// Applies all pending migrations to an existing DB.
#[instrument(target = COMPONENT, skip_all)]
pub fn migrate(database_filepath: impl AsRef<Path>) -> Result<(), DatabaseError> {
migrate_database(database_filepath.as_ref())?;
Ok(())
}
fn open_with_pool_size(
database_filepath: &Path,
connection_pool_size: NonZeroUsize,
) -> Result<Database, DatabaseError> {
let db = Database::new_with_pool_size(database_filepath, connection_pool_size)?;
tracing::info!(
target: COMPONENT,
sqlite= %database_filepath.display(),
connection_pool_size = %connection_pool_size,
"Connected to the database"
);
Ok(db)
}
/// Inserts a new validated transaction into the database.
#[instrument(target = COMPONENT, skip_all, fields(tx_id = %tx_info.tx_id()), err)]
pub(crate) fn insert_transaction(
tx: &WriteTx<'_>,
tx_info: &ValidatedTransaction,
) -> Result<usize, DatabaseError> {
let id = tx_info.tx_id().to_bytes();
let block_num = i64::from(tx_info.block_num().as_u32());
let account_id = tx_info.account_id().to_bytes();
let account_delta = tx_info.account_delta().to_bytes();
let input_notes = tx_info.input_notes().to_bytes();
let output_notes = tx_info.output_notes().to_bytes();
let initial_account_hash = tx_info.initial_account_hash().to_bytes();
let final_account_hash = tx_info.final_account_hash().to_bytes();
let fee = tx_info.fee().amount().as_u64().to_le_bytes().to_vec();
tx.execute(
"INSERT INTO validated_transactions \
(id, block_num, account_id, account_delta, input_notes, output_notes, \
initial_account_hash, final_account_hash, fee) \
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9) \
ON CONFLICT DO NOTHING",
&[
&id,
&block_num,
&account_id,
&account_delta,
&input_notes,
&output_notes,
&initial_account_hash,
&final_account_hash,
&fee,
],
)
}
/// Scans the database for transaction Ids that do not exist.
///
/// If the resulting vector is empty, all supplied transaction ids have been validated in the past.
///
/// # Raw SQL
///
/// ```sql
/// SELECT EXISTS(
/// SELECT 1
/// FROM validated_transactions
/// WHERE id = ?
/// );
/// ```
#[instrument(target = COMPONENT, skip(tx), err)]
pub(crate) fn find_unvalidated_transactions(
tx: &ReadTx<'_>,
tx_ids: &[TransactionId],
) -> Result<Vec<TransactionId>, DatabaseError> {
let mut unvalidated_tx_ids = Vec::new();
for tx_id in tx_ids {
// Check whether each transaction id exists in the database.
let exists = tx.exists(
"SELECT EXISTS(SELECT 1 FROM validated_transactions WHERE id = ?1)",
&[&tx_id.to_bytes()],
)?;
// Record any transaction ids that do not exist.
if !exists {
unvalidated_tx_ids.push(*tx_id);
}
}
Ok(unvalidated_tx_ids)
}
/// Upserts a block header into the database.
///
/// Inserts a new row if no block header exists at the given block number, or replaces the
/// existing block header if one already exists.
#[instrument(target = COMPONENT, skip(tx, header), err)]
pub fn upsert_block_header(tx: &WriteTx<'_>, header: &BlockHeader) -> Result<(), DatabaseError> {
let block_num = i64::from(header.block_num().as_u32());
let block_header = header.to_bytes();
tx.execute(
"REPLACE INTO block_headers (block_num, block_header) VALUES (?1, ?2)",
&[&block_num, &block_header],
)?;
Ok(())
}
/// Loads the chain tip (block header with the highest block number) from the database.
///
/// Returns `None` if no block headers have been persisted (i.e. bootstrap has not been run).
#[instrument(target = COMPONENT, skip(tx), err)]
pub fn load_chain_tip(tx: &ReadTx<'_>) -> Result<Option<BlockHeader>, DatabaseError> {
tx.query_opt(
"SELECT block_header FROM block_headers ORDER BY block_num DESC LIMIT 1",
&[],
|row| row.get::<BlockHeader>(0),
)
}
/// Loads a block header by its block number.
///
/// Returns `None` if no block header exists at the given block number.
#[instrument(target = COMPONENT, skip(tx), err)]
pub fn load_block_header(
tx: &ReadTx<'_>,
block_num: BlockNumber,
) -> Result<Option<BlockHeader>, DatabaseError> {
tx.query_opt(
"SELECT block_header FROM block_headers WHERE block_num = ?1",
&[&i64::from(block_num.as_u32())],
|row| row.get::<BlockHeader>(0),
)
}
/// Returns the total number of validated transactions in the database.
#[instrument(target = COMPONENT, skip(tx), err)]
pub fn count_validated_transactions(tx: &ReadTx<'_>) -> Result<i64, DatabaseError> {
tx.count("SELECT COUNT(*) FROM validated_transactions", &[])
}
/// Returns the total number of signed blocks in the database.
#[instrument(target = COMPONENT, skip(tx), err)]
pub fn count_signed_blocks(tx: &ReadTx<'_>) -> Result<i64, DatabaseError> {
tx.count("SELECT COUNT(*) FROM block_headers", &[])
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn migrate_rejects_missing_database() {
let temp_dir = tempfile::tempdir().expect("failed to create temp directory");
let db_path = temp_dir.path().join("validator.sqlite3");
let err = migrate(db_path.clone()).expect_err("missing database should fail");
assert!(matches!(err, DatabaseError::Migration(_)), "unexpected error: {err:?}");
assert!(!db_path.exists());
}
#[tokio::test]
async fn setup_creates_database_that_load_accepts() {
let temp_dir = tempfile::tempdir().expect("failed to create temp directory");
let db_path = temp_dir.path().join("validator.sqlite3");
setup(db_path.clone()).await.expect("setup should bootstrap the database");
load(db_path).await.expect("load should accept a bootstrapped database");
}
}