0.7.1 for Real ! - it was 0.7.0 before, not 0.7.1 !!
This commit is contained in:
246
kb_lib/src/db/queries/chain_instruction.rs
Normal file
246
kb_lib/src/db/queries/chain_instruction.rs
Normal file
@@ -0,0 +1,246 @@
|
||||
// file: kb_lib/src/db/queries/chain_instruction.rs
|
||||
|
||||
//! Queries for `kb_chain_instructions`.
|
||||
|
||||
/// Inserts one normalized chain instruction row.
|
||||
pub async fn insert_chain_instruction(
|
||||
database: &crate::KbDatabase,
|
||||
dto: &crate::KbChainInstructionDto,
|
||||
) -> Result<i64, crate::KbError> {
|
||||
let instruction_index_result = i64::from(dto.instruction_index);
|
||||
let inner_instruction_index = match dto.inner_instruction_index {
|
||||
Some(inner_instruction_index) => Some(i64::from(inner_instruction_index)),
|
||||
None => None,
|
||||
};
|
||||
let stack_height = match dto.stack_height {
|
||||
Some(stack_height) => Some(i64::from(stack_height)),
|
||||
None => None,
|
||||
};
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO kb_chain_instructions (
|
||||
transaction_id,
|
||||
parent_instruction_id,
|
||||
instruction_index,
|
||||
inner_instruction_index,
|
||||
program_id,
|
||||
program_name,
|
||||
stack_height,
|
||||
accounts_json,
|
||||
data_json,
|
||||
parsed_type,
|
||||
parsed_json,
|
||||
created_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
"#,
|
||||
)
|
||||
.bind(dto.transaction_id)
|
||||
.bind(dto.parent_instruction_id)
|
||||
.bind(instruction_index_result)
|
||||
.bind(inner_instruction_index)
|
||||
.bind(dto.program_id.clone())
|
||||
.bind(dto.program_name.clone())
|
||||
.bind(stack_height)
|
||||
.bind(dto.accounts_json.clone())
|
||||
.bind(dto.data_json.clone())
|
||||
.bind(dto.parsed_type.clone())
|
||||
.bind(dto.parsed_json.clone())
|
||||
.bind(dto.created_at.to_rfc3339())
|
||||
.execute(pool)
|
||||
.await;
|
||||
let insert_result = match query_result {
|
||||
Ok(insert_result) => insert_result,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot insert kb_chain_instructions on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
}
|
||||
};
|
||||
Ok(insert_result.last_insert_rowid())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Lists instructions for one transaction ordered from outer to inner.
|
||||
pub async fn list_chain_instructions_by_transaction_id(
|
||||
database: &crate::KbDatabase,
|
||||
transaction_id: i64,
|
||||
) -> Result<std::vec::Vec<crate::KbChainInstructionDto>, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbChainInstructionEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
transaction_id,
|
||||
parent_instruction_id,
|
||||
instruction_index,
|
||||
inner_instruction_index,
|
||||
program_id,
|
||||
program_name,
|
||||
stack_height,
|
||||
accounts_json,
|
||||
data_json,
|
||||
parsed_type,
|
||||
parsed_json,
|
||||
created_at
|
||||
FROM kb_chain_instructions
|
||||
WHERE transaction_id = ?
|
||||
ORDER BY instruction_index ASC, inner_instruction_index ASC, id ASC
|
||||
"#,
|
||||
)
|
||||
.bind(transaction_id)
|
||||
.fetch_all(pool)
|
||||
.await;
|
||||
let entities = match query_result {
|
||||
Ok(entities) => entities,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot list kb_chain_instructions for transaction_id '{}' on sqlite: {}",
|
||||
transaction_id, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
let mut dtos = std::vec::Vec::new();
|
||||
for entity in entities {
|
||||
let dto_result = crate::KbChainInstructionDto::try_from(entity);
|
||||
let dto = match dto_result {
|
||||
Ok(dto) => dto,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
dtos.push(dto);
|
||||
}
|
||||
Ok(dtos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Deletes all instructions for one transaction id.
|
||||
pub async fn delete_chain_instructions_by_transaction_id(
|
||||
database: &crate::KbDatabase,
|
||||
transaction_id: i64,
|
||||
) -> Result<(), crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query(
|
||||
r#"
|
||||
DELETE FROM kb_chain_instructions
|
||||
WHERE transaction_id = ?
|
||||
"#,
|
||||
)
|
||||
.bind(transaction_id)
|
||||
.execute(pool)
|
||||
.await;
|
||||
if let Err(error) = query_result {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot delete kb_chain_instructions for transaction_id '{}' on sqlite: {}",
|
||||
transaction_id, error
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
async fn make_database() -> crate::KbDatabase {
|
||||
let tempdir = tempfile::tempdir().expect("tempdir must succeed");
|
||||
let database_path = tempdir.path().join("chain_instruction.sqlite3");
|
||||
let config = crate::KbDatabaseConfig {
|
||||
enabled: true,
|
||||
backend: crate::KbDatabaseBackend::Sqlite,
|
||||
sqlite: crate::KbSqliteDatabaseConfig {
|
||||
path: database_path.to_string_lossy().to_string(),
|
||||
create_if_missing: true,
|
||||
busy_timeout_ms: 5000,
|
||||
max_connections: 1,
|
||||
auto_initialize_schema: true,
|
||||
use_wal: true,
|
||||
},
|
||||
};
|
||||
crate::KbDatabase::connect_and_initialize(&config)
|
||||
.await
|
||||
.expect("database init must succeed")
|
||||
}
|
||||
|
||||
async fn make_transaction(database: &crate::KbDatabase) -> i64 {
|
||||
let dto = crate::KbChainTransactionDto::new(
|
||||
"sig-chain-instruction-1".to_string(),
|
||||
None,
|
||||
None,
|
||||
Some("mainnet_public_http".to_string()),
|
||||
Some("legacy".to_string()),
|
||||
None,
|
||||
None,
|
||||
r#"{"transaction":{"message":{"instructions":[]}}}"#.to_string(),
|
||||
);
|
||||
crate::upsert_chain_transaction(database, &dto)
|
||||
.await
|
||||
.expect("chain transaction upsert must succeed")
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn chain_instruction_insert_list_delete_works() {
|
||||
let database = make_database().await;
|
||||
let transaction_id = make_transaction(&database).await;
|
||||
let outer_dto = crate::KbChainInstructionDto::new(
|
||||
transaction_id,
|
||||
None,
|
||||
0,
|
||||
None,
|
||||
Some(crate::SPL_TOKEN_PROGRAM_ID.to_string()),
|
||||
Some("spl-token".to_string()),
|
||||
Some(1),
|
||||
r#"["AccountA","AccountB"]"#.to_string(),
|
||||
Some(r#""raw-data-outer""#.to_string()),
|
||||
Some("transfer".to_string()),
|
||||
Some(r#"{"type":"transfer","info":{"amount":"10"}}"#.to_string()),
|
||||
);
|
||||
let outer_instruction_id = crate::insert_chain_instruction(&database, &outer_dto)
|
||||
.await
|
||||
.expect("outer instruction insert must succeed");
|
||||
assert!(outer_instruction_id > 0);
|
||||
let inner_dto = crate::KbChainInstructionDto::new(
|
||||
transaction_id,
|
||||
Some(outer_instruction_id),
|
||||
0,
|
||||
Some(0),
|
||||
Some(crate::SPL_TOKEN_PROGRAM_ID.to_string()),
|
||||
Some("spl-token".to_string()),
|
||||
Some(2),
|
||||
r#"["InnerA","InnerB"]"#.to_string(),
|
||||
Some(r#""raw-data-inner""#.to_string()),
|
||||
Some("mintTo".to_string()),
|
||||
Some(r#"{"type":"mintTo","info":{"amount":"5"}}"#.to_string()),
|
||||
);
|
||||
let inner_instruction_id = crate::insert_chain_instruction(&database, &inner_dto)
|
||||
.await
|
||||
.expect("inner instruction insert must succeed");
|
||||
assert!(inner_instruction_id > outer_instruction_id);
|
||||
let listed = crate::list_chain_instructions_by_transaction_id(&database, transaction_id)
|
||||
.await
|
||||
.expect("chain instruction list must succeed");
|
||||
assert_eq!(listed.len(), 2);
|
||||
assert_eq!(listed[0].parent_instruction_id, None);
|
||||
assert_eq!(listed[0].instruction_index, 0);
|
||||
assert_eq!(listed[0].inner_instruction_index, None);
|
||||
assert_eq!(listed[0].parsed_type, Some("transfer".to_string()));
|
||||
assert_eq!(listed[1].parent_instruction_id, Some(outer_instruction_id));
|
||||
assert_eq!(listed[1].instruction_index, 0);
|
||||
assert_eq!(listed[1].inner_instruction_index, Some(0));
|
||||
assert_eq!(listed[1].parsed_type, Some("mintTo".to_string()));
|
||||
crate::delete_chain_instructions_by_transaction_id(&database, transaction_id)
|
||||
.await
|
||||
.expect("chain instruction delete must succeed");
|
||||
let listed_after_delete =
|
||||
crate::list_chain_instructions_by_transaction_id(&database, transaction_id)
|
||||
.await
|
||||
.expect("chain instruction list after delete must succeed");
|
||||
assert_eq!(listed_after_delete.len(), 0);
|
||||
}
|
||||
}
|
||||
221
kb_lib/src/db/queries/chain_slot.rs
Normal file
221
kb_lib/src/db/queries/chain_slot.rs
Normal file
@@ -0,0 +1,221 @@
|
||||
// file: kb_lib/src/db/queries/chain_slot.rs
|
||||
|
||||
//! Queries for `kb_chain_slots`.
|
||||
|
||||
/// Inserts or updates one normalized chain slot row.
|
||||
pub async fn upsert_chain_slot(
|
||||
database: &crate::KbDatabase,
|
||||
dto: &crate::KbChainSlotDto,
|
||||
) -> Result<u64, crate::KbError> {
|
||||
let slot_result = i64::try_from(dto.slot);
|
||||
let slot = match slot_result {
|
||||
Ok(slot) => slot,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot convert chain slot '{}' to i64: {}",
|
||||
dto.slot, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
let parent_slot = match dto.parent_slot {
|
||||
Some(parent_slot) => {
|
||||
let parent_slot_result = i64::try_from(parent_slot);
|
||||
match parent_slot_result {
|
||||
Ok(parent_slot) => Some(parent_slot),
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot convert chain parent_slot '{}' to i64: {}",
|
||||
parent_slot, error
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO kb_chain_slots (
|
||||
slot,
|
||||
parent_slot,
|
||||
block_time_unix,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
ON CONFLICT(slot) DO UPDATE SET
|
||||
parent_slot = excluded.parent_slot,
|
||||
block_time_unix = excluded.block_time_unix,
|
||||
updated_at = excluded.updated_at
|
||||
"#,
|
||||
)
|
||||
.bind(slot)
|
||||
.bind(parent_slot)
|
||||
.bind(dto.block_time_unix)
|
||||
.bind(dto.created_at.to_rfc3339())
|
||||
.bind(dto.updated_at.to_rfc3339())
|
||||
.execute(pool)
|
||||
.await;
|
||||
if let Err(error) = query_result {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot upsert kb_chain_slots on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
}
|
||||
Ok(dto.slot)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads one chain slot row by slot number.
|
||||
pub async fn get_chain_slot(
|
||||
database: &crate::KbDatabase,
|
||||
slot: u64,
|
||||
) -> Result<std::option::Option<crate::KbChainSlotDto>, crate::KbError> {
|
||||
let slot_result = i64::try_from(slot);
|
||||
let slot_i64 = match slot_result {
|
||||
Ok(slot_i64) => slot_i64,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot convert requested chain slot '{}' to i64: {}",
|
||||
slot, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbChainSlotEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
slot,
|
||||
parent_slot,
|
||||
block_time_unix,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM kb_chain_slots
|
||||
WHERE slot = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
)
|
||||
.bind(slot_i64)
|
||||
.fetch_optional(pool)
|
||||
.await;
|
||||
let entity_option = match query_result {
|
||||
Ok(entity_option) => entity_option,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot fetch kb_chain_slots for slot '{}' on sqlite: {}",
|
||||
slot, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
match entity_option {
|
||||
Some(entity) => {
|
||||
let dto_result = crate::KbChainSlotDto::try_from(entity);
|
||||
match dto_result {
|
||||
Ok(dto) => Ok(Some(dto)),
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Lists recent chain slots ordered from newest to oldest.
|
||||
pub async fn list_recent_chain_slots(
|
||||
database: &crate::KbDatabase,
|
||||
limit: u32,
|
||||
) -> Result<std::vec::Vec<crate::KbChainSlotDto>, crate::KbError> {
|
||||
if limit == 0 {
|
||||
return Ok(std::vec::Vec::new());
|
||||
}
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbChainSlotEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
slot,
|
||||
parent_slot,
|
||||
block_time_unix,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM kb_chain_slots
|
||||
ORDER BY slot DESC
|
||||
LIMIT ?
|
||||
"#,
|
||||
)
|
||||
.bind(i64::from(limit))
|
||||
.fetch_all(pool)
|
||||
.await;
|
||||
let entities = match query_result {
|
||||
Ok(entities) => entities,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot list kb_chain_slots on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
}
|
||||
};
|
||||
let mut dtos = std::vec::Vec::new();
|
||||
for entity in entities {
|
||||
let dto_result = crate::KbChainSlotDto::try_from(entity);
|
||||
let dto = match dto_result {
|
||||
Ok(dto) => dto,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
dtos.push(dto);
|
||||
}
|
||||
Ok(dtos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
async fn make_database() -> crate::KbDatabase {
|
||||
let tempdir = tempfile::tempdir().expect("tempdir must succeed");
|
||||
let database_path = tempdir.path().join("chain_slot.sqlite3");
|
||||
let config = crate::KbDatabaseConfig {
|
||||
enabled: true,
|
||||
backend: crate::KbDatabaseBackend::Sqlite,
|
||||
sqlite: crate::KbSqliteDatabaseConfig {
|
||||
path: database_path.to_string_lossy().to_string(),
|
||||
create_if_missing: true,
|
||||
busy_timeout_ms: 5000,
|
||||
max_connections: 1,
|
||||
auto_initialize_schema: true,
|
||||
use_wal: true,
|
||||
},
|
||||
};
|
||||
crate::KbDatabase::connect_and_initialize(&config)
|
||||
.await
|
||||
.expect("database init must succeed")
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn chain_slot_roundtrip_works() {
|
||||
let database = make_database().await;
|
||||
let dto = crate::KbChainSlotDto::new(424242, Some(424241), Some(1_777_777_777));
|
||||
let slot = crate::upsert_chain_slot(&database, &dto)
|
||||
.await
|
||||
.expect("chain slot upsert must succeed");
|
||||
assert_eq!(slot, 424242);
|
||||
let fetched = crate::get_chain_slot(&database, 424242)
|
||||
.await
|
||||
.expect("chain slot fetch must succeed")
|
||||
.expect("chain slot must exist");
|
||||
assert_eq!(fetched.slot, 424242);
|
||||
assert_eq!(fetched.parent_slot, Some(424241));
|
||||
assert_eq!(fetched.block_time_unix, Some(1_777_777_777));
|
||||
let listed = crate::list_recent_chain_slots(&database, 10)
|
||||
.await
|
||||
.expect("chain slot list must succeed");
|
||||
assert_eq!(listed.len(), 1);
|
||||
assert_eq!(listed[0].slot, 424242);
|
||||
assert_eq!(listed[0].parent_slot, Some(424241));
|
||||
assert_eq!(listed[0].block_time_unix, Some(1_777_777_777));
|
||||
}
|
||||
}
|
||||
265
kb_lib/src/db/queries/chain_transaction.rs
Normal file
265
kb_lib/src/db/queries/chain_transaction.rs
Normal file
@@ -0,0 +1,265 @@
|
||||
// file: kb_lib/src/db/queries/chain_transaction.rs
|
||||
|
||||
//! Queries for `kb_chain_transactions`.
|
||||
|
||||
/// Inserts or updates one normalized chain transaction row.
|
||||
pub async fn upsert_chain_transaction(
|
||||
database: &crate::KbDatabase,
|
||||
dto: &crate::KbChainTransactionDto,
|
||||
) -> Result<i64, crate::KbError> {
|
||||
let slot_i64 = match dto.slot {
|
||||
Some(slot) => {
|
||||
let slot_result = i64::try_from(slot);
|
||||
match slot_result {
|
||||
Ok(slot) => Some(slot),
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot convert chain transaction slot '{}' to i64: {}",
|
||||
slot, error
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO kb_chain_transactions (
|
||||
signature,
|
||||
slot,
|
||||
block_time_unix,
|
||||
source_endpoint_name,
|
||||
version_text,
|
||||
err_json,
|
||||
meta_json,
|
||||
transaction_json,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(signature) DO UPDATE SET
|
||||
slot = excluded.slot,
|
||||
block_time_unix = excluded.block_time_unix,
|
||||
source_endpoint_name = excluded.source_endpoint_name,
|
||||
version_text = excluded.version_text,
|
||||
err_json = excluded.err_json,
|
||||
meta_json = excluded.meta_json,
|
||||
transaction_json = excluded.transaction_json,
|
||||
updated_at = excluded.updated_at
|
||||
"#,
|
||||
)
|
||||
.bind(dto.signature.clone())
|
||||
.bind(slot_i64)
|
||||
.bind(dto.block_time_unix)
|
||||
.bind(dto.source_endpoint_name.clone())
|
||||
.bind(dto.version_text.clone())
|
||||
.bind(dto.err_json.clone())
|
||||
.bind(dto.meta_json.clone())
|
||||
.bind(dto.transaction_json.clone())
|
||||
.bind(dto.created_at.to_rfc3339())
|
||||
.bind(dto.updated_at.to_rfc3339())
|
||||
.execute(pool)
|
||||
.await;
|
||||
if let Err(error) = query_result {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot upsert kb_chain_transactions on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
}
|
||||
let id_result = sqlx::query_scalar::<sqlx::Sqlite, i64>(
|
||||
r#"
|
||||
SELECT id
|
||||
FROM kb_chain_transactions
|
||||
WHERE signature = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
)
|
||||
.bind(dto.signature.clone())
|
||||
.fetch_one(pool)
|
||||
.await;
|
||||
match id_result {
|
||||
Ok(id) => Ok(id),
|
||||
Err(error) => Err(crate::KbError::Db(format!(
|
||||
"cannot fetch kb_chain_transactions id for signature '{}' on sqlite: {}",
|
||||
dto.signature, error
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads one chain transaction row by signature.
|
||||
pub async fn get_chain_transaction_by_signature(
|
||||
database: &crate::KbDatabase,
|
||||
signature: &str,
|
||||
) -> Result<std::option::Option<crate::KbChainTransactionDto>, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbChainTransactionEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
signature,
|
||||
slot,
|
||||
block_time_unix,
|
||||
source_endpoint_name,
|
||||
version_text,
|
||||
err_json,
|
||||
meta_json,
|
||||
transaction_json,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM kb_chain_transactions
|
||||
WHERE signature = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
)
|
||||
.bind(signature.to_string())
|
||||
.fetch_optional(pool)
|
||||
.await;
|
||||
let entity_option = match query_result {
|
||||
Ok(entity_option) => entity_option,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot fetch kb_chain_transactions for signature '{}' on sqlite: {}",
|
||||
signature, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
match entity_option {
|
||||
Some(entity) => {
|
||||
let dto_result = crate::KbChainTransactionDto::try_from(entity);
|
||||
match dto_result {
|
||||
Ok(dto) => Ok(Some(dto)),
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Lists recent chain transactions ordered from newest to oldest.
|
||||
pub async fn list_recent_chain_transactions(
|
||||
database: &crate::KbDatabase,
|
||||
limit: u32,
|
||||
) -> Result<std::vec::Vec<crate::KbChainTransactionDto>, crate::KbError> {
|
||||
if limit == 0 {
|
||||
return Ok(std::vec::Vec::new());
|
||||
}
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbChainTransactionEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
id,
|
||||
signature,
|
||||
slot,
|
||||
block_time_unix,
|
||||
source_endpoint_name,
|
||||
version_text,
|
||||
err_json,
|
||||
meta_json,
|
||||
transaction_json,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM kb_chain_transactions
|
||||
ORDER BY id DESC
|
||||
LIMIT ?
|
||||
"#,
|
||||
)
|
||||
.bind(i64::from(limit))
|
||||
.fetch_all(pool)
|
||||
.await;
|
||||
let entities = match query_result {
|
||||
Ok(entities) => entities,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot list kb_chain_transactions on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
}
|
||||
};
|
||||
let mut dtos = std::vec::Vec::new();
|
||||
for entity in entities {
|
||||
let dto_result = crate::KbChainTransactionDto::try_from(entity);
|
||||
let dto = match dto_result {
|
||||
Ok(dto) => dto,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
dtos.push(dto);
|
||||
}
|
||||
Ok(dtos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
async fn make_database() -> crate::KbDatabase {
|
||||
let tempdir = tempfile::tempdir().expect("tempdir must succeed");
|
||||
let database_path = tempdir.path().join("chain_transaction.sqlite3");
|
||||
let config = crate::KbDatabaseConfig {
|
||||
enabled: true,
|
||||
backend: crate::KbDatabaseBackend::Sqlite,
|
||||
sqlite: crate::KbSqliteDatabaseConfig {
|
||||
path: database_path.to_string_lossy().to_string(),
|
||||
create_if_missing: true,
|
||||
busy_timeout_ms: 5000,
|
||||
max_connections: 1,
|
||||
auto_initialize_schema: true,
|
||||
use_wal: true,
|
||||
},
|
||||
};
|
||||
crate::KbDatabase::connect_and_initialize(&config)
|
||||
.await
|
||||
.expect("database init must succeed")
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn chain_transaction_roundtrip_works() {
|
||||
let database = make_database().await;
|
||||
let slot_dto = crate::KbChainSlotDto::new(515151, Some(515150), Some(1_700_000_001));
|
||||
crate::upsert_chain_slot(&database, &slot_dto)
|
||||
.await
|
||||
.expect("chain slot upsert must succeed");
|
||||
let dto = crate::KbChainTransactionDto::new(
|
||||
"sig-chain-transaction-1".to_string(),
|
||||
Some(515151),
|
||||
Some(1_700_000_001),
|
||||
Some("helius_primary_http".to_string()),
|
||||
Some("0".to_string()),
|
||||
None,
|
||||
Some(r#"{"fee":5000}"#.to_string()),
|
||||
r#"{"slot":515151,"transaction":{"message":{"instructions":[]}}}"#.to_string(),
|
||||
);
|
||||
let transaction_id = crate::upsert_chain_transaction(&database, &dto)
|
||||
.await
|
||||
.expect("chain transaction upsert must succeed");
|
||||
assert!(transaction_id > 0);
|
||||
let fetched =
|
||||
crate::get_chain_transaction_by_signature(&database, "sig-chain-transaction-1")
|
||||
.await
|
||||
.expect("chain transaction fetch must succeed")
|
||||
.expect("chain transaction must exist");
|
||||
assert_eq!(fetched.id, Some(transaction_id));
|
||||
assert_eq!(fetched.signature, "sig-chain-transaction-1");
|
||||
assert_eq!(fetched.slot, Some(515151));
|
||||
assert_eq!(fetched.block_time_unix, Some(1_700_000_001));
|
||||
assert_eq!(
|
||||
fetched.source_endpoint_name,
|
||||
Some("helius_primary_http".to_string())
|
||||
);
|
||||
assert_eq!(fetched.version_text, Some("0".to_string()));
|
||||
assert_eq!(fetched.meta_json, Some(r#"{"fee":5000}"#.to_string()));
|
||||
let listed = crate::list_recent_chain_transactions(&database, 10)
|
||||
.await
|
||||
.expect("chain transaction list must succeed");
|
||||
assert_eq!(listed.len(), 1);
|
||||
assert_eq!(listed[0].signature, "sig-chain-transaction-1");
|
||||
assert_eq!(listed[0].slot, Some(515151));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user