cat_gateway/db/index/block/txo/mod.rs
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
//! Insert TXO Indexed Data Queries.
//!
//! Note, there are multiple ways TXO Data is indexed and they all happen in here.
mod insert_txo;
mod insert_txo_asset;
mod insert_unstaked_txo;
mod insert_unstaked_txo_asset;
use std::sync::Arc;
use scylla::Session;
use tracing::{error, warn};
use crate::{
db::index::{
queries::{FallibleQueryTasks, PreparedQuery, SizedBatch},
session::CassandraSession,
},
service::utilities::convert::from_saturating,
settings::cassandra_db,
};
/// This is used to indicate that there is no stake address.
const NO_STAKE_ADDRESS: &[u8] = &[];
/// Insert TXO Query and Parameters
///
/// There are multiple possible parameters to a query, which are represented separately.
#[allow(dead_code)]
pub(crate) struct TxoInsertQuery {
/// Staked TXO Data Parameters
staked_txo: Vec<insert_txo::Params>,
/// Unstaked TXO Data Parameters
unstaked_txo: Vec<insert_unstaked_txo::Params>,
/// Staked TXO Asset Data Parameters
staked_txo_asset: Vec<insert_txo_asset::Params>,
/// Unstaked TXO Asset Data Parameters
unstaked_txo_asset: Vec<insert_unstaked_txo_asset::Params>,
}
impl TxoInsertQuery {
/// Create a new Insert TXO Query Batch
pub(crate) fn new() -> Self {
TxoInsertQuery {
staked_txo: Vec::new(),
unstaked_txo: Vec::new(),
staked_txo_asset: Vec::new(),
unstaked_txo_asset: Vec::new(),
}
}
/// Prepare Batch of Insert TXI Index Data Queries
pub(crate) async fn prepare_batch(
session: &Arc<Session>, cfg: &cassandra_db::EnvVars,
) -> anyhow::Result<(SizedBatch, SizedBatch, SizedBatch, SizedBatch)> {
let txo_staked_insert_batch = insert_txo::Params::prepare_batch(session, cfg).await;
let txo_unstaked_insert_batch =
insert_unstaked_txo::Params::prepare_batch(session, cfg).await;
let txo_staked_asset_insert_batch =
insert_txo_asset::Params::prepare_batch(session, cfg).await;
let txo_unstaked_asset_insert_batch =
insert_unstaked_txo_asset::Params::prepare_batch(session, cfg).await;
Ok((
txo_staked_insert_batch?,
txo_unstaked_insert_batch?,
txo_staked_asset_insert_batch?,
txo_unstaked_asset_insert_batch?,
))
}
/// Extracts a stake address from a TXO if possible.
/// Returns None if it is not possible.
/// If we want to index, but can not determine a stake key hash, then return a Vec
/// with a single 0 byte. This is because the index DB needs data in the
/// primary key, so we use a single byte of 0 to indicate that there is no
/// stake address, and still have a primary key on the table. Otherwise return the
/// stake key hash as a vec of 28 bytes.
fn extract_stake_address(
txo: &pallas::ledger::traverse::MultiEraOutput<'_>, slot_no: u64, txn_id: &str,
) -> Option<(Vec<u8>, String)> {
let stake_address = match txo.address() {
Ok(address) => {
match address {
// Byron addresses do not have stake addresses and are not supported.
pallas::ledger::addresses::Address::Byron(_) => {
return None;
},
pallas::ledger::addresses::Address::Shelley(address) => {
let address_string = match address.to_bech32() {
Ok(address) => address,
Err(error) => {
// Shouldn't happen, but if it does error and don't index.
error!(error=%error, slot=slot_no, txn=txn_id,"Error converting to bech32: skipping.");
return None;
},
};
match address.delegation() {
pallas::ledger::addresses::ShelleyDelegationPart::Script(hash)
| pallas::ledger::addresses::ShelleyDelegationPart::Key(hash) => {
(hash.to_vec(), address_string)
},
pallas::ledger::addresses::ShelleyDelegationPart::Pointer(_pointer) => {
// These are not supported from Conway, so we don't support them
// either.
(NO_STAKE_ADDRESS.to_vec(), address_string)
},
pallas::ledger::addresses::ShelleyDelegationPart::Null => {
(NO_STAKE_ADDRESS.to_vec(), address_string)
},
}
},
pallas::ledger::addresses::Address::Stake(_) => {
// This should NOT appear in a TXO, so report if it does. But don't index it
// as a stake address.
warn!(
slot = slot_no,
txn = txn_id,
"Unexpected Stake address found in TXO. Refusing to index."
);
return None;
},
}
},
Err(error) => {
// This should not ever happen.
error!(error=%error, slot = slot_no, txn = txn_id, "Failed to get Address from TXO. Skipping TXO.");
return None;
},
};
Some(stake_address)
}
/// Index the transaction Inputs.
pub(crate) fn index(
&mut self, txs: &pallas::ledger::traverse::MultiEraTx<'_>, slot_no: u64, txn_hash: &[u8],
txn: i16,
) {
let txn_id = hex::encode_upper(txn_hash);
// Accumulate all the data we want to insert from this transaction here.
for (txo_index, txo) in txs.outputs().iter().enumerate() {
// This will only return None if the TXO is not to be indexed (Byron Addresses)
let Some((stake_address, address)) = Self::extract_stake_address(txo, slot_no, &txn_id)
else {
continue;
};
let staked = stake_address != NO_STAKE_ADDRESS;
let txo_index = from_saturating(txo_index);
if staked {
let params = insert_txo::Params::new(
&stake_address,
slot_no,
txn,
txo_index,
&address,
txo.lovelace_amount(),
txn_hash,
);
self.staked_txo.push(params);
} else {
let params = insert_unstaked_txo::Params::new(
txn_hash,
txo_index,
slot_no,
txn,
&address,
txo.lovelace_amount(),
);
self.unstaked_txo.push(params);
}
for asset in txo.non_ada_assets() {
let policy_id = asset.policy().to_vec();
for policy_asset in asset.assets() {
if policy_asset.is_output() {
let asset_name = policy_asset.name();
let value = policy_asset.any_coin();
if staked {
let params = insert_txo_asset::Params::new(
&stake_address,
slot_no,
txn,
txo_index,
&policy_id,
asset_name,
value,
);
self.staked_txo_asset.push(params);
} else {
let params = insert_unstaked_txo_asset::Params::new(
txn_hash, txo_index, &policy_id, asset_name, slot_no, txn, value,
);
self.unstaked_txo_asset.push(params);
}
} else {
error!("Minting MultiAsset in TXO.");
}
}
}
}
}
/// Index the transaction Inputs.
///
/// Consumes `self` and returns a vector of futures.
pub(crate) fn execute(self, session: &Arc<CassandraSession>) -> FallibleQueryTasks {
let mut query_handles: FallibleQueryTasks = Vec::new();
if !self.staked_txo.is_empty() {
let inner_session = session.clone();
query_handles.push(tokio::spawn(async move {
inner_session
.execute_batch(PreparedQuery::TxoAdaInsertQuery, self.staked_txo)
.await
}));
}
if !self.unstaked_txo.is_empty() {
let inner_session = session.clone();
query_handles.push(tokio::spawn(async move {
inner_session
.execute_batch(PreparedQuery::UnstakedTxoAdaInsertQuery, self.unstaked_txo)
.await
}));
}
if !self.staked_txo_asset.is_empty() {
let inner_session = session.clone();
query_handles.push(tokio::spawn(async move {
inner_session
.execute_batch(PreparedQuery::TxoAssetInsertQuery, self.staked_txo_asset)
.await
}));
}
if !self.unstaked_txo_asset.is_empty() {
let inner_session = session.clone();
query_handles.push(tokio::spawn(async move {
inner_session
.execute_batch(
PreparedQuery::UnstakedTxoAssetInsertQuery,
self.unstaked_txo_asset,
)
.await
}));
}
query_handles
}
}