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

feat(cat-gateway): CIP36 Registration queries #933

Merged
merged 49 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
5403663
feat(barebones): scylla queries
cong-or Sep 27, 2024
8cee464
Merge branch 'main' into feat-latest_registrations
cong-or Sep 28, 2024
8f41614
feat(barebones): scylla queries
cong-or Sep 28, 2024
f293514
feat(barebones): scylla queries
cong-or Sep 29, 2024
5df6a3a
feat(barebones): scylla queries
cong-or Sep 29, 2024
b307125
feat(barebones): scylla queries
cong-or Sep 29, 2024
30f3d3f
feat(latest registration endpoints): given stake addr and stake key hash
cong-or Sep 30, 2024
649b0fe
feat(latest registration endpoints): given stake addr and stake key hash
cong-or Sep 30, 2024
221c07d
feat(latest registration endpoints): given stake addr and stake key hash
cong-or Sep 30, 2024
3a6150b
feat(latest registration endpoints): given stake addr and stake key hash
cong-or Sep 30, 2024
375801f
feat(latest registration endpoints): given stake addr and stake key hash
cong-or Sep 30, 2024
883b71e
feat(latest registration endpoints): given stake addr and stake key hash
cong-or Sep 30, 2024
01e38b3
feat(latest registration endpoints): given stake addr and stake key hash
cong-or Sep 30, 2024
c4e9741
feat(latest registration endpoints): given stake addr and stake key hash
cong-or Oct 1, 2024
c343d0e
feat(latest registration endpoints): given stake addr and stake key hash
cong-or Oct 1, 2024
e9797d7
feat(latest registration endpoints): given stake addr and stake key hash
cong-or Oct 2, 2024
ea81080
feat(latest registration endpoints): given stake addr and stake key hash
cong-or Oct 2, 2024
1ad3ea1
feat(latest registration endpoints): given stake addr and stake key hash
cong-or Oct 2, 2024
305bc0e
feat(latest registration endpoints): given stake addr and stake key hash
cong-or Oct 2, 2024
526de9a
refactor(stake addr voting key association): check pair is active
cong-or Oct 2, 2024
21ed8be
feat(invalids): add error reporting in resp
cong-or Oct 2, 2024
8a67bab
feat(error reporting): invalids
cong-or Oct 3, 2024
f20e71f
feat(error reporting): invalids
cong-or Oct 3, 2024
83db4e5
Merge branch 'main' into feat-latest_registrations
cong-or Oct 3, 2024
c92ece2
refactor(docs): clean
cong-or Oct 3, 2024
a5ce6ab
Merge branch 'main' into feat-latest_registrations
cong-or Oct 3, 2024
d87066c
refactor(housekeeping): clean
cong-or Oct 3, 2024
f4c63b1
Merge branch 'main' into feat-latest_registrations
stevenj Oct 3, 2024
5a3729c
Merge branch 'main' into feat-latest_registrations
cong-or Oct 3, 2024
fd5b3cb
feat(error reporting): vote key
cong-or Oct 3, 2024
91f7628
feat(error reporting): vote key
cong-or Oct 3, 2024
7855f89
feat(error reporting): vote key
cong-or Oct 3, 2024
3381900
fix(code formatting): format
cong-or Oct 3, 2024
125d2dc
style(spelling): cspell
cong-or Oct 3, 2024
7fae701
Merge branch 'main' into feat-latest_registrations
stevenj Oct 4, 2024
6810108
fix(cat-gateway): spelling
stevenj Oct 4, 2024
e872455
fix(cat-gateway): docs
stevenj Oct 4, 2024
70dcb28
fix(cat-gateway): generated dart openapi files
stevenj Oct 4, 2024
3e9cc3d
Merge branch 'main' into feat-latest_registrations
cong-or Oct 4, 2024
9b4c00b
ci(docs): lints
cong-or Oct 4, 2024
0f5a4e1
Merge branch 'main' into feat-latest_registrations
cong-or Oct 4, 2024
600c5fe
ci(docs): lints
cong-or Oct 4, 2024
ea5bc09
Merge branch 'feat-latest_registrations' of https://github.com/input-…
cong-or Oct 4, 2024
9d0ce3e
fix(cat-gateway): bump cache version
stevenj Oct 4, 2024
00351e2
Merge branch 'feat-latest_registrations' of github.com:input-output-h…
stevenj Oct 4, 2024
15196cf
docs(cat-gateway): fix docs
stevenj Oct 4, 2024
4b1d67a
Merge branch 'main' into feat-latest_registrations
stevenj Oct 4, 2024
2be4089
ci(oai lints): add missing endpoint annotations
cong-or Oct 4, 2024
6c87d9d
Merge branch 'feat-latest_registrations' of https://github.com/input-…
cong-or Oct 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .config/dictionaries/project.dic
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ adminer
afinet
androidx
anypolicy
appbar
appspot
Arbritrary
ARGB
Expand Down Expand Up @@ -294,4 +295,3 @@ xctestrun
xcworkspace
xvfb
yoroi
appbar
2 changes: 1 addition & 1 deletion catalyst-gateway/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ COPY_SRC:

# builder : Set up our target toolchains, and copy our files.
builder:
DO rust-ci+SETUP
DO rust-ci+SETUP --CACHE_VERSION="00002"

# sync-cfg: Synchronize local config with CI version.
# Must be run by the developer manually.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SELECT error_report,
stake_address,
vote_key,
payment_address,
is_payable,
cip36
FROM cip36_registration_invalid
WHERE stake_address = :stake_address
AND slot_no >= :slot_no
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SELECT stake_address,
nonce,
slot_no,
txn,
vote_key,
payment_address,
is_payable,
cip36
FROM cip36_registration
WHERE stake_address = :stake_address
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT stake_address
FROM stake_registration
WHERE stake_hash = :stake_hash;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT stake_address
FROM cip36_registration_for_vote_key
WHERE vote_key = :vote_key
ALLOW FILTERING;
46 changes: 42 additions & 4 deletions catalyst-gateway/bin/src/db/index/queries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
//!
//! This improves query execution time.

pub(crate) mod registrations;
pub(crate) mod staked_ada;
pub(crate) mod sync_status;

use std::{fmt::Debug, sync::Arc};

use anyhow::{bail, Context};
use crossbeam_skiplist::SkipMap;
use registrations::{
get_from_stake_addr::GetRegistrationQuery, get_from_stake_hash::GetStakeAddrQuery,
get_from_vote_key::GetStakeAddrFromVoteKeyQuery, get_invalid::GetInvalidRegistrationQuery,
};
use scylla::{
batch::Batch, prepared_statement::PreparedStatement, serialize::row::SerializeRow,
transport::iterator::RowIterator, QueryResult, Session,
Expand Down Expand Up @@ -56,9 +61,17 @@ pub(crate) enum PreparedQuery {
/// All prepared SELECT query statements (return data).
pub(crate) enum PreparedSelectQuery {
/// Get TXO by stake address query.
GetTxoByStakeAddress,
TxoByStakeAddress,
/// Get TXI by transaction hash query.
GetTxiByTransactionHash,
TxiByTransactionHash,
/// Get Registrations
RegistrationFromStakeAddr,
/// Get invalid Registration
InvalidRegistrationsFromStakeAddr,
/// Get stake addr from stake hash
StakeAddrFromStakeHash,
/// Get stake addr from vote key
StakeAddrFromVoteKey,
}

/// All prepared UPSERT query statements (inserts/updates a single value of data).
Expand Down Expand Up @@ -94,6 +107,14 @@ pub(crate) struct PreparedQueries {
txo_by_stake_address_query: PreparedStatement,
/// Get TXI by transaction hash.
txi_by_txn_hash_query: PreparedStatement,
/// Get registrations
registration_from_stake_addr_query: PreparedStatement,
/// stake addr from stake hash
stake_addr_from_stake_hash_query: PreparedStatement,
/// stake addr from vote key
stake_addr_from_vote_key_query: PreparedStatement,
/// Get invalid registrations
invalid_registrations_from_stake_addr_query: PreparedStatement,
/// Insert Sync Status update.
sync_status_insert: PreparedStatement,
}
Expand All @@ -120,6 +141,11 @@ impl PreparedQueries {
UpdateTxoSpentQuery::prepare_batch(session.clone(), cfg).await;
let txo_by_stake_address_query = GetTxoByStakeAddressQuery::prepare(session.clone()).await;
let txi_by_txn_hash_query = GetTxiByTxnHashesQuery::prepare(session.clone()).await;
let registration_from_stake_addr_query =
GetRegistrationQuery::prepare(session.clone()).await;
let stake_addr_from_stake_hash = GetStakeAddrQuery::prepare(session.clone()).await;
let stake_addr_from_vote_key = GetStakeAddrFromVoteKeyQuery::prepare(session.clone()).await;
let invalid_registrations = GetInvalidRegistrationQuery::prepare(session.clone()).await;
let sync_status_insert = SyncStatusInsertQuery::prepare(session).await;

let (
Expand Down Expand Up @@ -148,6 +174,10 @@ impl PreparedQueries {
txo_spent_update_queries: txo_spent_update_queries?,
txo_by_stake_address_query: txo_by_stake_address_query?,
txi_by_txn_hash_query: txi_by_txn_hash_query?,
registration_from_stake_addr_query: registration_from_stake_addr_query?,
stake_addr_from_stake_hash_query: stake_addr_from_stake_hash?,
stake_addr_from_vote_key_query: stake_addr_from_vote_key?,
invalid_registrations_from_stake_addr_query: invalid_registrations?,
sync_status_insert: sync_status_insert?,
})
}
Expand Down Expand Up @@ -223,8 +253,16 @@ impl PreparedQueries {
) -> anyhow::Result<RowIterator>
where P: SerializeRow {
let prepared_stmt = match select_query {
PreparedSelectQuery::GetTxoByStakeAddress => &self.txo_by_stake_address_query,
PreparedSelectQuery::GetTxiByTransactionHash => &self.txi_by_txn_hash_query,
PreparedSelectQuery::TxoByStakeAddress => &self.txo_by_stake_address_query,
PreparedSelectQuery::TxiByTransactionHash => &self.txi_by_txn_hash_query,
PreparedSelectQuery::RegistrationFromStakeAddr => {
&self.registration_from_stake_addr_query
},
PreparedSelectQuery::StakeAddrFromStakeHash => &self.stake_addr_from_stake_hash_query,
PreparedSelectQuery::StakeAddrFromVoteKey => &self.stake_addr_from_vote_key_query,
PreparedSelectQuery::InvalidRegistrationsFromStakeAddr => {
&self.invalid_registrations_from_stake_addr_query
},
};

session
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//! Get stake addr registrations

use std::sync::Arc;

use scylla::{
prepared_statement::PreparedStatement, transport::iterator::TypedRowIterator, SerializeRow,
Session,
};
use tracing::error;

use crate::db::index::{
queries::{PreparedQueries, PreparedSelectQuery},
session::CassandraSession,
};

/// Get registrations from stake addr query.
const GET_REGISTRATIONS_FROM_STAKE_ADDR_QUERY: &str =
include_str!("../cql/get_registrations_w_stake_addr.cql");

/// Get registration
#[derive(SerializeRow)]
pub(crate) struct GetRegistrationParams {
/// Stake address.
pub stake_address: Vec<u8>,
}

impl GetRegistrationParams {
/// Create a new instance of [`GetRegistrationParams`]
pub(crate) fn new(stake_addr: Vec<u8>) -> GetRegistrationParams {
Self {
stake_address: stake_addr,
}
}
}

/// Get registration given stake addr or vote key
#[allow(clippy::expect_used)]
mod result {
use scylla::FromRow;

/// Get registration query result.
#[derive(FromRow)]
pub(crate) struct GetRegistrationQuery {
/// Full Stake Address (not hashed, 32 byte ED25519 Public key).
pub stake_address: Vec<u8>,
/// Nonce value after normalization.
pub nonce: num_bigint::BigInt,
/// Slot Number the cert is in.
pub slot_no: num_bigint::BigInt,
/// Transaction Index.
pub txn: i16,
/// Voting Public Key
pub vote_key: Vec<u8>,
/// Full Payment Address (not hashed, 32 byte ED25519 Public key).
pub payment_address: Vec<u8>,
/// Is the stake address a script or not.
pub is_payable: bool,
/// Is the Registration CIP36 format, or CIP15
pub cip36: bool,
}
}
/// Get registration query.
pub(crate) struct GetRegistrationQuery;

impl GetRegistrationQuery {
/// Prepares a get registration query.
pub(crate) async fn prepare(session: Arc<Session>) -> anyhow::Result<PreparedStatement> {
let get_registrations_query = PreparedQueries::prepare(
session,
GET_REGISTRATIONS_FROM_STAKE_ADDR_QUERY,
scylla::statement::Consistency::All,
true,
)
.await;

if let Err(ref error) = get_registrations_query {
error!(error=%error, "Failed to prepare get registration query.");
};

get_registrations_query
}

/// Executes get registration info for given stake addr query.
pub(crate) async fn execute(
session: &CassandraSession, params: GetRegistrationParams,
) -> anyhow::Result<TypedRowIterator<result::GetRegistrationQuery>> {
let iter = session
.execute_iter(PreparedSelectQuery::RegistrationFromStakeAddr, params)
.await?
.into_typed::<result::GetRegistrationQuery>();

Ok(iter)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//! Get stake addr from stake hash

use std::sync::Arc;

use scylla::{
prepared_statement::PreparedStatement, transport::iterator::TypedRowIterator, SerializeRow,
Session,
};
use tracing::error;

use crate::db::index::{
queries::{PreparedQueries, PreparedSelectQuery},
session::CassandraSession,
};

/// Get stake addr from stake hash query string.
const GET_STAKE_ADDR_FROM_STAKE_HASH: &str =
include_str!("../cql/get_stake_addr_w_stake_key_hash.cql");

/// Get stake addr
#[derive(SerializeRow)]
pub(crate) struct GetStakeAddrParams {
/// Stake hash.
pub stake_hash: Vec<u8>,
}

impl GetStakeAddrParams {
/// Create a new instance of [`GetStakeAddrParams`]
pub(crate) fn new(stake_hash: Vec<u8>) -> GetStakeAddrParams {
Self { stake_hash }
}
}

/// Get stake addr from stake hash query string.
#[allow(clippy::expect_used)]
mod result {
use scylla::FromRow;

/// Get stake addr from stake hash query result.
#[derive(FromRow)]
pub(crate) struct GetStakeAddrQuery {
/// Full Stake Address (not hashed, 32 byte ED25519 Public key).
pub stake_address: Vec<u8>,
}
}
/// Get registration query.
pub(crate) struct GetStakeAddrQuery;

impl GetStakeAddrQuery {
/// Prepares a get get stake addr from stake hash query.
pub(crate) async fn prepare(session: Arc<Session>) -> anyhow::Result<PreparedStatement> {
let get_stake_addr_query = PreparedQueries::prepare(
session,
GET_STAKE_ADDR_FROM_STAKE_HASH,
scylla::statement::Consistency::All,
true,
)
.await;

if let Err(ref error) = get_stake_addr_query {
error!(error=%error, "Failed to prepare get stake addr query.");
};

get_stake_addr_query
}

/// Executes a get txi by transaction hashes query.
pub(crate) async fn execute(
session: &CassandraSession, params: GetStakeAddrParams,
) -> anyhow::Result<TypedRowIterator<result::GetStakeAddrQuery>> {
let iter = session
.execute_iter(PreparedSelectQuery::StakeAddrFromStakeHash, params)
.await?
.into_typed::<result::GetStakeAddrQuery>();

Ok(iter)
}
}
Loading
Loading