Skip to content

Commit

Permalink
feat: Voter registration endpoint (#403)
Browse files Browse the repository at this point in the history
* fix CardanoStakeAddress error handling

* refactor, add sync_state_get endpoint

* refactor types

* refactor

* add block_hash validation

* wip

* wip

* wip

* wip

* add check_network fn

* fix

* fix schematisis test

* try

* wip

* try

* try

* try

* try

* wip

* try

* try

* fix

* update Network

* add test_utxo test

* try

* fix

* try

* fix

* wip

* fix

* fix docket-compose.yml file

* try

* try

* fix

* try

* try

* try

* try

* wip

* fix

* wip

* try

* try

* wip

* try

* try

* revert

* wip

* wip

* wip

* fix

* fix

* fix

* remove mithril_snapshot loader

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* add stake addr bech32 encode utils function

* wip

* wip

* update indexing of the utxo data

* fix spelling

* wip

* wip

* finish utxo test

* fix deny

* fix check

* fix

* fix

* update earthly builder versions

* wip

* ignore test_utxo.py in CI

* dont ignore tests

* add date_time_to_slot_number_get endpoint

* add sql queries

* fix

* update slot info, fix follower indexing block time issue

* add previous slot info field

* fix

* refactor

* fix sync_state_get

* wip

* fix check

* try

* fix

* finish slot_info test, fix queries

* fix

* cleanup

* wip

* wip

* wip

* increase max_response time

* add new registration endpoint

* add sql query

* update slot_info examples

* cleanup get slot info db functions

* wip

* make templated sql query

* refactor

* refactor

* refactor

* refactor, add new API shema Hash type

* fix

* fix Hash parsing

* wip

* wip

* wip

* add integration test

* fix sqlfluff

* wip

* wip

* fix query

* wip

* refactor

* wip

* wip

* wip

* fix

* wip

* cleanup

---------

Co-authored-by: Oleksandr Prokhorenko <djminikin@gmail.com>
  • Loading branch information
Mr-Leshiy and minikin authored Apr 12, 2024
1 parent 19e3714 commit 326b838
Show file tree
Hide file tree
Showing 31 changed files with 1,074 additions and 368 deletions.
3 changes: 0 additions & 3 deletions catalyst-gateway/bin/src/event_db/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ pub(crate) enum Error {
/// JSON Parsing error
#[error("Unable to parse database data: {0}")]
JsonParseIssue(String),
#[error("Decode Error: {0}")]
/// Unable to decode hex
DecodeHex(String),
/// Unable to extract policy assets
#[error("Unable parse assets: {0}")]
AssetParsingIssue(String),
Expand Down
10 changes: 5 additions & 5 deletions catalyst-gateway/bin/src/event_db/follower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub type SlotNumber = i64;
/// Epoch
pub type EpochNumber = i64;
/// Block hash
pub type BlockHash = String;
pub type BlockHash = Vec<u8>;
/// Unique follower id
pub type MachineId = String;

Expand Down Expand Up @@ -100,7 +100,7 @@ impl EventDB {
&network.to_string(),
&epoch_no,
&block_time,
&hex::decode(block_hash).map_err(|e| Error::DecodeHex(e.to_string()))?,
&block_hash,
])
.await?;

Expand All @@ -125,7 +125,7 @@ impl EventDB {
};

let slot_number: SlotNumber = row.try_get(SLOT_NO_COLUMN)?;
let block_hash = hex::encode(row.try_get::<_, Vec<u8>>(BLOCK_HASH_COLUMN)?);
let block_hash = row.try_get(BLOCK_HASH_COLUMN)?;
let block_time = row.try_get(BLOCK_TIME_COLUMN)?;
Ok((slot_number, block_hash, block_time))
}
Expand All @@ -146,7 +146,7 @@ impl EventDB {
};

let slot_no = row.try_get(SLOT_NO_COLUMN)?;
let block_hash = hex::encode(row.try_get::<_, Vec<u8>>(BLOCK_HASH_COLUMN)?);
let block_hash = row.try_get(BLOCK_HASH_COLUMN)?;
let last_updated = row.try_get(ENDED_COLUMN)?;

Ok((slot_no, block_hash, last_updated))
Expand Down Expand Up @@ -176,7 +176,7 @@ impl EventDB {
&machine_id,
&slot_no,
&network.to_string(),
&hex::decode(block_hash).map_err(|e| Error::DecodeHex(e.to_string()))?,
&block_hash,
&update,
])
.await?;
Expand Down
104 changes: 49 additions & 55 deletions catalyst-gateway/bin/src/event_db/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,63 +14,57 @@ pub(crate) type StakeAmount = i64;

impl EventDB {
/// Index utxo data
pub(crate) async fn index_utxo_data(
&self, txs: Vec<MultiEraTx<'_>>, slot_no: SlotNumber, network: Network,
) -> Result<(), Error> {
pub(crate) async fn index_utxo_data(&self, tx: &MultiEraTx<'_>) -> Result<(), Error> {
let conn = self.pool.get().await?;

for tx in txs {
let tx_hash = tx.hash();
self.index_txn_data(tx_hash.as_slice(), slot_no, network)
let tx_hash = tx.hash();

// index outputs
for (index, tx_out) in tx.outputs().iter().enumerate() {
// extract assets
let assets = serde_json::to_value(parse_policy_assets(&tx_out.non_ada_assets()))
.map_err(|e| Error::AssetParsingIssue(format!("Asset parsing issue {e}")))?;

let stake_address = match tx_out
.address()
.map_err(|e| Error::Unknown(format!("Address issue {e}")))?
{
Address::Shelley(address) => address.try_into().ok(),
Address::Stake(stake_address) => Some(stake_address),
Address::Byron(_) => None,
};
let stake_credential = stake_address.map(|val| val.payload().as_hash().to_vec());

let _rows = conn
.query(
include_str!("../../../event-db/queries/utxo/insert_utxo.sql"),
&[
&i32::try_from(index).map_err(|e| Error::Unknown(e.to_string()))?,
&tx_hash.as_slice(),
&i64::try_from(tx_out.lovelace_amount())
.map_err(|e| Error::Unknown(e.to_string()))?,
&stake_credential,
&assets,
],
)
.await?;
}
// update outputs with inputs
for tx_in in tx.inputs() {
let output = tx_in.output_ref();
let output_tx_hash = output.hash();
let out_index = output.index();

let _rows = conn
.query(
include_str!("../../../event-db/queries/utxo/update_utxo.sql"),
&[
&tx_hash.as_slice(),
&output_tx_hash.as_slice(),
&i32::try_from(out_index).map_err(|e| Error::Unknown(e.to_string()))?,
],
)
.await?;

// index outputs
for (index, tx_out) in tx.outputs().iter().enumerate() {
// extract assets
let assets = serde_json::to_value(parse_policy_assets(&tx_out.non_ada_assets()))
.map_err(|e| Error::AssetParsingIssue(format!("Asset parsing issue {e}")))?;

let stake_address = match tx_out
.address()
.map_err(|e| Error::Unknown(format!("Address issue {e}")))?
{
Address::Shelley(address) => address.try_into().ok(),
Address::Stake(stake_address) => Some(stake_address),
Address::Byron(_) => None,
};
let stake_credential = stake_address.map(|val| val.payload().as_hash().to_vec());

let _rows = conn
.query(
include_str!("../../../event-db/queries/utxo/insert_utxo.sql"),
&[
&i32::try_from(index).map_err(|e| Error::Unknown(e.to_string()))?,
&tx_hash.as_slice(),
&i64::try_from(tx_out.lovelace_amount())
.map_err(|e| Error::Unknown(e.to_string()))?,
&stake_credential,
&assets,
],
)
.await?;
}
// update outputs with inputs
for tx_in in tx.inputs() {
let output = tx_in.output_ref();
let output_tx_hash = output.hash();
let out_index = output.index();

let _rows = conn
.query(
include_str!("../../../event-db/queries/utxo/update_utxo.sql"),
&[
&tx_hash.as_slice(),
&output_tx_hash.as_slice(),
&i32::try_from(out_index).map_err(|e| Error::Unknown(e.to_string()))?,
],
)
.await?;
}
}

Ok(())
Expand All @@ -94,7 +88,7 @@ impl EventDB {

/// Get total utxo amount
pub(crate) async fn total_utxo_amount(
&self, stake_credential: StakeCredential<'_>, network: Network, slot_num: SlotNumber,
&self, stake_credential: StakeCredential, network: Network, slot_num: SlotNumber,
) -> Result<(StakeAmount, SlotNumber), Error> {
let conn = self.pool.get().await?;

Expand Down
148 changes: 0 additions & 148 deletions catalyst-gateway/bin/src/event_db/voter_registration.rs

This file was deleted.

Loading

0 comments on commit 326b838

Please sign in to comment.