Skip to content

Commit f598b57

Browse files
authored
Merge pull request #138 from input-output-hk/sg/chain-store
ChainStore module
2 parents 449b0de + 0e54ebd commit f598b57

File tree

40 files changed

+2569
-119
lines changed

40 files changed

+2569
-119
lines changed

Cargo.lock

Lines changed: 280 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[workspace]
44
members = [
55
# Global message and common definitions
6+
"codec",
67
"common",
78

89
# Modules
@@ -21,11 +22,12 @@ members = [
2122
"modules/epochs_state", # Tracks fees and blocks minted and epochs history
2223
"modules/accounts_state", # Tracks stake and reward accounts
2324
"modules/assets_state", # Tracks native asset mints and burns
25+
"modules/chain_store", # Tracks historical information about blocks and TXs
2426

2527
# Process builds
2628
"processes/omnibus", # All-inclusive omnibus process
2729
"processes/replayer", # All-inclusive process to replay messages
28-
"processes/golden_tests", #All-inclusive golden tests process
30+
"processes/golden_tests", # All-inclusive golden tests process
2931
]
3032
resolver = "2"
3133

@@ -42,9 +44,11 @@ config = "0.15.11"
4244
dashmap = "6.1.0"
4345
hex = "0.4"
4446
imbl = { version = "5.0.0", features = ["serde"] }
45-
pallas = "0.32.1"
46-
pallas-addresses = "0.32.0"
47-
pallas-crypto = "0.32.0"
47+
pallas = "0.33.0"
48+
pallas-addresses = "0.33.0"
49+
pallas-crypto = "0.33.0"
50+
pallas-primitives = "0.33.0"
51+
pallas-traverse = "0.33.0"
4852
serde = { version = "1.0.214", features = ["derive"] }
4953
serde_json = "1.0.132"
5054
serde_with = { version = "3.12.0", features = ["hex"] }

codec/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "acropolis_codec"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
acropolis_common = { path = "../common" }
8+
9+
anyhow = { workspace = true }
10+
pallas = { workspace = true }
11+
pallas-primitives = { workspace = true }
12+
pallas-traverse = { workspace = true }
13+
tracing = { workspace = true }

codec/src/block.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use acropolis_common::{
2+
GenesisDelegate, HeavyDelegate, crypto::keyhash_224, queries::blocks::BlockIssuer,
3+
};
4+
use pallas_primitives::byron::BlockSig::DlgSig;
5+
use pallas_traverse::MultiEraHeader;
6+
use std::collections::HashMap;
7+
8+
pub fn map_to_block_issuer(
9+
header: &MultiEraHeader,
10+
byron_heavy_delegates: &HashMap<Vec<u8>, HeavyDelegate>,
11+
shelley_genesis_delegates: &HashMap<Vec<u8>, GenesisDelegate>,
12+
) -> Option<BlockIssuer> {
13+
match header.issuer_vkey() {
14+
Some(vkey) => match header {
15+
MultiEraHeader::ShelleyCompatible(_) => {
16+
let digest = keyhash_224(vkey);
17+
if let Some(issuer) = shelley_genesis_delegates
18+
.values()
19+
.find(|v| v.delegate == digest)
20+
.map(|i| BlockIssuer::GenesisDelegate(i.clone()))
21+
{
22+
Some(issuer)
23+
} else {
24+
Some(BlockIssuer::SPO(vkey.to_vec()))
25+
}
26+
}
27+
_ => Some(BlockIssuer::SPO(vkey.to_vec())),
28+
},
29+
None => match header {
30+
MultiEraHeader::Byron(_) => match header.as_byron() {
31+
Some(block_head) => match &block_head.consensus_data.3 {
32+
DlgSig(sig) => byron_heavy_delegates
33+
.values()
34+
.find(|v| v.issuer_pk == *sig.0.issuer)
35+
.map(|i| BlockIssuer::HeavyDelegate(i.clone())),
36+
_ => None,
37+
},
38+
None => None,
39+
},
40+
_ => None,
41+
},
42+
}
43+
}

codec/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod block;
2+
pub mod map_parameters;

modules/tx_unpacker/src/map_parameters.rs renamed to codec/src/map_parameters.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! Acropolis transaction unpacker module for Caryatid
22
//! Performs conversion from Pallas library data to Acropolis
33
4-
use anyhow::{anyhow, bail, Result};
4+
use anyhow::{Result, anyhow, bail};
55
use pallas::ledger::{
66
primitives::{
7-
alonzo, babbage, conway, ExUnitPrices as PallasExUnitPrices, Nullable,
8-
ProtocolVersion as PallasProtocolVersion, Relay as PallasRelay, ScriptHash,
9-
StakeCredential as PallasStakeCredential,
7+
ExUnitPrices as PallasExUnitPrices, Nullable, ProtocolVersion as PallasProtocolVersion,
8+
Relay as PallasRelay, ScriptHash, StakeCredential as PallasStakeCredential, alonzo,
9+
babbage, conway,
1010
},
1111
traverse::{MultiEraCert, MultiEraPolicyAssets, MultiEraValue},
1212
*,
@@ -145,7 +145,7 @@ pub fn map_gov_action_id(pallas_action_id: &conway::GovActionId) -> Result<GovAc
145145
};
146146

147147
Ok(GovActionId {
148-
transaction_id: *pallas_action_id.transaction_id,
148+
transaction_id: TxHash(*pallas_action_id.transaction_id),
149149
action_index: act_idx_u8,
150150
})
151151
}

common/src/address.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use anyhow::{anyhow, bail, Result};
77
use crc::{Crc, CRC_32_ISO_HDLC};
88
use minicbor::data::IanaTag;
99
use serde_with::{hex::Hex, serde_as};
10+
use std::cmp::Ordering;
1011

1112
/// a Byron-era address
1213
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
@@ -517,6 +518,22 @@ impl Address {
517518
}
518519
}
519520

521+
/// Used for ordering addresses by their bech representation
522+
#[derive(Eq, PartialEq)]
523+
pub struct BechOrdAddress(pub Address);
524+
525+
impl Ord for BechOrdAddress {
526+
fn cmp(&self, other: &Self) -> Ordering {
527+
self.0.to_string().into_iter().cmp(other.0.to_string())
528+
}
529+
}
530+
531+
impl PartialOrd for BechOrdAddress {
532+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
533+
Some(self.cmp(other))
534+
}
535+
}
536+
520537
// -- Tests --
521538
#[cfg(test)]
522539
mod tests {

common/src/byte_array.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use crate::serialization::{Bech32Conversion, Bech32WithHrp};
2+
use anyhow::Error;
3+
use hex::{FromHex, FromHexError};
4+
use serde_with::{hex::Hex, serde_as};
5+
use std::ops::Deref;
6+
7+
macro_rules! declare_byte_array_type {
8+
($name:ident, $size:expr) => {
9+
/// $name
10+
#[serde_as]
11+
#[derive(
12+
Default, Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize,
13+
)]
14+
pub struct $name(#[serde_as(as = "Hex")] pub [u8; $size]);
15+
16+
impl From<[u8; $size]> for $name {
17+
fn from(bytes: [u8; $size]) -> Self {
18+
Self(bytes)
19+
}
20+
}
21+
22+
impl FromHex for $name {
23+
type Error = FromHexError;
24+
25+
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
26+
Ok(match Self::try_from(Vec::<u8>::from_hex(hex)?) {
27+
Ok(b) => Ok(b),
28+
Err(_) => Err(FromHexError::InvalidStringLength),
29+
}?)
30+
}
31+
}
32+
33+
impl TryFrom<Vec<u8>> for $name {
34+
type Error = Vec<u8>;
35+
fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error> {
36+
Ok($name(vec.try_into()?))
37+
}
38+
}
39+
40+
impl TryFrom<&[u8]> for $name {
41+
type Error = std::array::TryFromSliceError;
42+
fn try_from(arr: &[u8]) -> Result<Self, Self::Error> {
43+
Ok($name(arr.try_into()?))
44+
}
45+
}
46+
47+
impl AsRef<[u8]> for $name {
48+
fn as_ref(&self) -> &[u8] {
49+
&self.0
50+
}
51+
}
52+
53+
impl Deref for $name {
54+
type Target = [u8; $size];
55+
fn deref(&self) -> &Self::Target {
56+
&self.0
57+
}
58+
}
59+
};
60+
}
61+
62+
macro_rules! declare_byte_array_type_with_bech32 {
63+
($name:ident, $size:expr, $hrp:expr) => {
64+
declare_byte_array_type!($name, $size);
65+
impl Bech32Conversion for $name {
66+
fn to_bech32(&self) -> Result<String, anyhow::Error> {
67+
self.0.to_vec().to_bech32_with_hrp($hrp)
68+
}
69+
fn from_bech32(s: &str) -> Result<Self, anyhow::Error> {
70+
match Vec::<u8>::from_bech32_with_hrp(s, $hrp) {
71+
Ok(v) => match Self::try_from(v) {
72+
Ok(s) => Ok(s),
73+
Err(_) => Err(Error::msg(format!(
74+
"Bad vector input to {}",
75+
stringify!($name)
76+
))),
77+
},
78+
Err(e) => Err(e),
79+
}
80+
}
81+
}
82+
};
83+
}
84+
85+
declare_byte_array_type!(BlockHash, 32);
86+
87+
declare_byte_array_type!(TxHash, 32);
88+
89+
declare_byte_array_type_with_bech32!(VRFKey, 32, "vrf_vk");

common/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Acropolis common library - main library exports
22

33
pub mod address;
4+
pub mod byte_array;
45
pub mod calculations;
56
pub mod cip19;
67
pub mod crypto;
@@ -23,4 +24,5 @@ pub mod types;
2324

2425
// Flattened re-exports
2526
pub use self::address::*;
27+
pub use self::byte_array::*;
2628
pub use self::types::*;

common/src/messages.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use crate::queries::{
2525
transactions::{TransactionsStateQuery, TransactionsStateQueryResponse},
2626
};
2727

28+
use crate::byte_array::*;
2829
use crate::types::*;
2930

3031
// Caryatid core messages which we re-export

0 commit comments

Comments
 (0)