Skip to content

Commit

Permalink
feat: Added registration block time and slot number to the snapshot_t…
Browse files Browse the repository at this point in the history
…ool output
  • Loading branch information
FelipeRosa committed Jul 24, 2023
1 parent bc02061 commit 25aa133
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/voting-tools-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ proptest = { workspace = true }
test-strategy = "0.2"

cddl = "0.9.1"
postgres = {version = "0.19.4" , features = ["with-serde_json-1"] }
postgres = {version = "0.19.4" , features = ["with-serde_json-1", "with-chrono-0_4"] }
cryptoxide = "0.4.2"

rust_decimal_macros = "1.29"
Expand Down
6 changes: 4 additions & 2 deletions src/voting-tools-rs/src/data/arbitrary.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::{
validation::{cbor::cbor_to_bytes, hash::hash},
Sig, Signature, VotingKey, VotingPurpose,
Sig, Signature, SlotNo, VotingKey, VotingPurpose,
};

use super::{
Nonce, PubKey, Registration, RewardsAddress, SignedRegistration, StakeKeyHex, TxId,
VotingKeyHex,
};
use cardano_serialization_lib::chain_crypto::{AsymmetricKey, Ed25519, SigningAlgorithm};
use chrono::NaiveDateTime;
use proptest::{arbitrary::StrategyFor, prelude::*, strategy::Map};

type Inputs = (Nonce, RewardsAddress, VotingKeyHex, [u8; 32], TxId);
Expand Down Expand Up @@ -67,7 +68,8 @@ pub(crate) fn arbitrary_signed_registration(
signature,
stake_key_hash: vec![0; 29],
tx_id,
slot: 12345,
slot: SlotNo(12345),
block_time: NaiveDateTime::MIN,
}
}

Expand Down
18 changes: 16 additions & 2 deletions src/voting-tools-rs/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
verify::{is_valid_rewards_address, stake_key_hash, StakeKeyHash},
};
use bytekind::{Bytes, HexString};
use chrono::NaiveDateTime;
use ciborium::value::Value;
use clap::builder::OsStr;
use hex::FromHexError;
Expand Down Expand Up @@ -150,7 +151,10 @@ pub struct SignedRegistration {
pub tx_id: TxId,

/// The slot the registration was found in.
pub slot: u64,
pub slot: SlotNo,

/// The block time for the block the registration was found in.
pub block_time: NaiveDateTime,
}

fn ox_hex<T, S>(v: &T, serializer: S) -> Result<S::Ok, S::Error>
Expand Down Expand Up @@ -202,7 +206,10 @@ pub struct RawRegistration {
pub tx_id: TxId,

/// The slot the registration was found in.
pub slot: u64,
pub slot: SlotNo,

/// The block time for the block the registration was found in.
pub block_time: NaiveDateTime,
}

impl RawRegistration {
Expand All @@ -227,6 +234,7 @@ impl RawRegistration {
stake_key_hash: stake_key_hash(&registration.stake_key, network_id),
tx_id: self.tx_id,
slot: self.slot,
block_time: self.block_time,
})
}

Expand Down Expand Up @@ -584,6 +592,12 @@ pub struct SnapshotEntry {
/// Registration transaction id
pub tx_id: TxId,

/// Registration slot number
pub slot: SlotNo,

/// Registration block time
pub block_time: NaiveDateTime,

/// Registration Nonce
pub nonce: u64,
}
Expand Down
4 changes: 3 additions & 1 deletion src/voting-tools-rs/src/db/queries/vote_registrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{db::inner::DbQuery, Db};
use crate::data::{SignedRegistration, SlotNo};
use crate::db::schema::{block, tx, tx_metadata};
use bigdecimal::{BigDecimal, FromPrimitive};
use chrono::NaiveDateTime;
use color_eyre::eyre::{eyre, Result};
use color_eyre::{Help, Report};
use diesel::RunQueryDsl;
Expand Down Expand Up @@ -128,7 +129,8 @@ fn convert_row((tx_id, metadata, signature, _slot_no): Row) -> Result<SignedRegi
signature,
stake_key_hash: vec![0; 29],
tx_id,
slot: 0,
slot: SlotNo(0),
block_time: NaiveDateTime::MIN,
})
}

Expand Down
4 changes: 4 additions & 0 deletions src/voting-tools-rs/src/logic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ fn convert_to_snapshot_entry(
voting_purpose,
},
tx_id,
slot,
block_time,
stake_key_hash,
..
} = registration;
Expand All @@ -128,6 +130,8 @@ fn convert_to_snapshot_entry(
voting_power,
voting_purpose,
tx_id,
slot,
block_time,
nonce: nonce.0,
})
}
4 changes: 3 additions & 1 deletion src/voting-tools-rs/src/testing/test_api/fake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bigdecimal::{BigDecimal, FromPrimitive};
use cardano_serialization_lib::address::Address;
use cardano_serialization_lib::crypto::{Ed25519Signature, PublicKey};
use cardano_serialization_lib::utils::BigNum;
use chrono::NaiveDateTime;
use dashmap::DashMap;
use mainnet_lib::{
InMemoryDbSync, METADATUM_1, METADATUM_2, METADATUM_3, METADATUM_4,
Expand Down Expand Up @@ -96,7 +97,8 @@ impl DataProvider for MockDbProvider {

SignedRegistration {
tx_id: TxId::from(tx_id),
slot: 0,
slot: SlotNo(0),
block_time: NaiveDateTime::MIN,
registration: Registration {
voting_key,
stake_key,
Expand Down
6 changes: 4 additions & 2 deletions src/voting-tools-rs/src/testing/vectors/cip15.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use chrono::NaiveDateTime;
use serde_json::{json, Value};

use crate::{
data::{Nonce, PubKey, Registration, RewardsAddress, SignedRegistration, StakeKeyHex, TxId},
Sig, Signature, VotingKey,
Sig, Signature, SlotNo, VotingKey,
};

/// CIP-15 test vector voting key
Expand Down Expand Up @@ -37,7 +38,8 @@ pub const STAKE_PRIVATE_KEY: &str =
pub fn vector() -> SignedRegistration {
SignedRegistration {
tx_id: TxId(1), // not provided in the test vector
slot: 0,
slot: SlotNo(0),
block_time: NaiveDateTime::MIN,
registration: Registration {
voting_key: VotingKey::direct_from_hex(VOTING_KEY).unwrap(),
stake_key: StakeKeyHex(PubKey::from_hex(STAKE_KEY).unwrap()),
Expand Down
4 changes: 1 addition & 3 deletions src/voting-tools-rs/src/validation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ impl Validate for SignedRegistration {
let SignedRegistration {
registration,
signature,
stake_key_hash: _,
tx_id: _,
slot: _,
..
} = self;

validate_voting_power(&registration.voting_key)?;
Expand Down
13 changes: 8 additions & 5 deletions src/voting-tools-rs/src/verification/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ use nonempty::nonempty;
/// DB columns
const REG_TX_ID: usize = 2;
const REG_SLOT_NO: usize = 4;
const REG_JSON: usize = 6;
const REG_BIN: usize = 7;
const SIG_JSON: usize = 9;
const SIG_BIN: usize = 10;
const REG_BLOCK_TIME: usize = 5;
const REG_JSON: usize = 7;
const REG_BIN: usize = 8;
const SIG_JSON: usize = 10;
const SIG_BIN: usize = 11;

/// Contains the most recent registration for each public stake address
pub type Valids = Vec<SignedRegistration>;
Expand Down Expand Up @@ -56,6 +57,7 @@ pub fn filter_registrations(
meta_table.tx_id as reg_tx_id,
sig_table.tx_id as sig_tx_id,
block.slot_no,
block.time,
meta_table.key as reg_key,
meta_table.json as reg_json,
meta_table.bytes as reg_bytes,
Expand Down Expand Up @@ -95,7 +97,8 @@ pub fn filter_registrations(
bin_reg: row.get(REG_BIN),
bin_sig: row.get(SIG_BIN),
tx_id: TxId(tx_id as u64),
slot: slot as u64,
slot: SlotNo(slot as u64),
block_time: row.get(REG_BLOCK_TIME),
};

// deserialize the raw Binary CBOR.
Expand Down

0 comments on commit 25aa133

Please sign in to comment.