chore: Merge Byron / Alonzo into single crate (#43)

This commit is contained in:
Santiago Carmuega 2022-02-09 07:19:17 -03:00 committed by GitHub
parent e41a99883c
commit fec96ffd99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
61 changed files with 120 additions and 335 deletions

View file

@ -4,8 +4,7 @@ members = [
"pallas-multiplexer",
"pallas-miniprotocols",
"pallas-crypto",
"pallas-alonzo",
"pallas-byron",
"pallas-primitives",
"pallas",
]

View file

@ -39,12 +39,12 @@ As already explained, _Pallas_ aims at being an expanding set of components. The
### Ouroboros Ledger
| Crates | Description |
| ------------------------------- | ----------------------------------------------------------------------- |
| [pallas-alonzo](/pallas-alonzo) | Ledger primitives and cbor codec for the Alonzo era |
| pallas-ticking | Time passage implementation for consensus algorithm |
| pallas-applying | Logic for validating and applying new blocks and txs to the chain state |
| pallas-forecasting | Ledger forecasting algorithm to be used by the consensus layer |
| Crates | Description |
| --------------------------------------- | ----------------------------------------------------------------------- |
| [pallas-primitives](/pallas-primitives) | Ledger primitives and cbor codec for the different Cardano eras |
| pallas-ticking | Time passage implementation for consensus algorithm |
| pallas-applying | Logic for validating and applying new blocks and txs to the chain state |
| pallas-forecasting | Ledger forecasting algorithm to be used by the consensus layer |
## Etymology

View file

@ -1,14 +1,18 @@
use net2::TcpStreamExt;
use pallas::ledger::alonzo::*;
use pallas::ouroboros::network::blockfetch::{BatchClient, Observer};
use pallas::ouroboros::network::handshake::{
n2n::{Client, VersionTable},
MAINNET_MAGIC,
use pallas::network::{
miniprotocols::{
handshake::n2n::{Client, VersionTable},
run_agent, Point, MAINNET_MAGIC,
},
multiplexer::Multiplexer,
};
use pallas::ouroboros::network::machines::primitives::Point;
use pallas::ouroboros::network::machines::run_agent;
use pallas::ouroboros::network::multiplexer::Multiplexer;
use pallas::{
ledger::primitives::{alonzo::*, Fragment},
network::miniprotocols::blockfetch::{BatchClient, Observer},
};
use std::net::TcpStream;
#[derive(Debug)]

View file

@ -1,14 +1,17 @@
use net2::TcpStreamExt;
use pallas::ledger::byron::*;
use pallas::ouroboros::network::blockfetch::{BatchClient, Observer};
use pallas::ouroboros::network::handshake::{
n2n::{Client, VersionTable},
MAINNET_MAGIC,
use pallas::{
ledger::primitives::{byron::Block, Fragment},
network::{
miniprotocols::{
blockfetch::{BatchClient, Observer},
handshake::n2n::{Client, VersionTable},
run_agent, Point, MAINNET_MAGIC,
},
multiplexer::Multiplexer,
},
};
use pallas::ouroboros::network::machines::primitives::Point;
use pallas::ouroboros::network::machines::run_agent;
use pallas::ouroboros::network::multiplexer::Multiplexer;
use std::net::TcpStream;
#[derive(Debug)]

View file

@ -1,20 +0,0 @@
[package]
name = "pallas-alonzo"
description = "Ledger primitives and cbor codec for the Alonzo era"
version = "0.4.0"
edition = "2021"
repository = "https://github.com/txpipe/pallas"
homepage = "https://github.com/txpipe/pallas"
documentation = "https://docs.rs/pallas-alonzo"
license = "Apache-2.0"
readme = "README.md"
authors = [
"Santiago Carmuega <santiago@carmuega.me>"
]
[dependencies]
minicbor = { version = "0.13", features = ["std"] }
minicbor-derive = "0.8.0"
hex = "0.4.3"
log = "0.4.14"
pallas-crypto = { version = "0.4.0", path = "../pallas-crypto" }

View file

@ -1,2 +0,0 @@
# Pallas Alonzo

View file

@ -1,10 +0,0 @@
//! Ledger primitives and cbor codec for the Alonzo era
mod framework;
mod model;
mod utils;
pub use framework::*;
pub use model::*;
pub mod crypto;

View file

@ -1,146 +0,0 @@
use std::ops::Deref;
use minicbor::{Decode, Encode};
/// Custom collection to ensure ordered pairs of values
///
/// Since the ordering of the entries requires a particular order to maintain
/// canonicalization for isomorphic decoding / encoding operators, we use a Vec
/// as the underlaying struct for storage of the items (as opposed to a BTreeMap
/// or HashMap).
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum KeyValuePairs<K, V> {
Def(Vec<(K, V)>),
Indef(Vec<(K, V)>),
}
impl<K, V> Deref for KeyValuePairs<K, V> {
type Target = Vec<(K, V)>;
fn deref(&self) -> &Self::Target {
match self {
KeyValuePairs::Def(x) => x,
KeyValuePairs::Indef(x) => x,
}
}
}
impl<'b, K, V> minicbor::decode::Decode<'b> for KeyValuePairs<K, V>
where
K: Encode + Decode<'b>,
V: Encode + Decode<'b>,
{
fn decode(d: &mut minicbor::Decoder<'b>) -> Result<Self, minicbor::decode::Error> {
let datatype = d.datatype()?;
let items: Result<Vec<_>, _> = d.map_iter::<K, V>()?.collect();
let items = items?;
match datatype {
minicbor::data::Type::Map => Ok(KeyValuePairs::Def(items)),
minicbor::data::Type::MapIndef => Ok(KeyValuePairs::Indef(items)),
_ => Err(minicbor::decode::Error::Message(
"invalid data type for keyvaluepairs",
)),
}
}
}
impl<K, V> minicbor::encode::Encode for KeyValuePairs<K, V>
where
K: Encode,
V: Encode,
{
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut minicbor::Encoder<W>,
) -> Result<(), minicbor::encode::Error<W::Error>> {
match self {
KeyValuePairs::Def(x) => {
e.map(x.len() as u64)?;
for (k, v) in x.iter() {
k.encode(e)?;
v.encode(e)?;
}
}
KeyValuePairs::Indef(x) => {
e.begin_map()?;
for (k, v) in x.iter() {
k.encode(e)?;
v.encode(e)?;
}
e.end()?;
}
}
Ok(())
}
}
/// A struct that maintains a reference to whether a cbor array was indef or not
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum MaybeIndefArray<A> {
Def(Vec<A>),
Indef(Vec<A>),
}
impl<A> Deref for MaybeIndefArray<A> {
type Target = Vec<A>;
fn deref(&self) -> &Self::Target {
match self {
MaybeIndefArray::Def(x) => x,
MaybeIndefArray::Indef(x) => x,
}
}
}
impl<'b, A> minicbor::decode::Decode<'b> for MaybeIndefArray<A>
where
A: minicbor::decode::Decode<'b>,
{
fn decode(d: &mut minicbor::Decoder<'b>) -> Result<Self, minicbor::decode::Error> {
let datatype = d.datatype()?;
match datatype {
minicbor::data::Type::Array => Ok(Self::Def(d.decode()?)),
minicbor::data::Type::ArrayIndef => Ok(Self::Indef(d.decode()?)),
_ => Err(minicbor::decode::Error::Message(
"unknown data type of maybe indef array",
)),
}
}
}
impl<A> minicbor::encode::Encode for MaybeIndefArray<A>
where
A: minicbor::encode::Encode,
{
fn encode<W: minicbor::encode::Write>(
&self,
e: &mut minicbor::Encoder<W>,
) -> Result<(), minicbor::encode::Error<W::Error>> {
match self {
MaybeIndefArray::Def(x) => {
e.encode(x)?;
}
MaybeIndefArray::Indef(x) if x.is_empty() => {
e.encode(x)?;
}
MaybeIndefArray::Indef(x) => {
e.begin_array()?;
for v in x.iter() {
e.encode(v)?;
}
e.end()?;
}
};
Ok(())
}
}

View file

@ -1,2 +0,0 @@
# Pallas Byron

View file

@ -1,22 +0,0 @@
pub type Error = Box<dyn std::error::Error>;
pub trait Fragment<'a>
where
Self: Sized,
{
fn encode_fragment(&self) -> Result<Vec<u8>, Error>;
fn decode_fragment(bytes: &'a [u8]) -> Result<Self, Error>;
}
impl<'a, T> Fragment<'a> for T
where
T: minicbor::Encode + minicbor::Decode<'a> + Sized,
{
fn encode_fragment(&self) -> Result<Vec<u8>, Error> {
minicbor::to_vec(self).map_err(|e| e.into())
}
fn decode_fragment(bytes: &'a [u8]) -> Result<Self, Error> {
minicbor::decode(bytes).map_err(|e| e.into())
}
}

View file

@ -22,4 +22,4 @@ net2 = "0.2.37"
[dev-dependencies]
env_logger = "0.9.0"
pallas-alonzo = { version = "0.4.0", path = "../pallas-alonzo/" }
pallas-primitives = { version = "0.4.0", path = "../pallas-primitives/" }

View file

@ -1,13 +1,9 @@
use net2::TcpStreamExt;
use pallas_miniprotocols::Point;
use std::net::TcpStream;
use pallas_miniprotocols::blockfetch::{BatchClient, NoopObserver};
use pallas_miniprotocols::handshake::{
n2n::{Client, VersionTable},
MAINNET_MAGIC,
};
use pallas_miniprotocols::run_agent;
use pallas_miniprotocols::handshake::n2n::{Client, VersionTable};
use pallas_miniprotocols::{run_agent, Point, MAINNET_MAGIC};
use pallas_multiplexer::Multiplexer;
fn main() {

View file

@ -1,10 +1,9 @@
use pallas_alonzo::{crypto, Block, BlockWrapper, Fragment};
use pallas_primitives::alonzo::{crypto, Block, BlockWrapper};
use pallas_primitives::Fragment;
use pallas_miniprotocols::chainsync::{BlockLike, Consumer, NoopObserver};
use pallas_miniprotocols::handshake::n2c::{Client, VersionTable};
use pallas_miniprotocols::handshake::MAINNET_MAGIC;
use pallas_miniprotocols::run_agent;
use pallas_miniprotocols::Point;
use pallas_miniprotocols::{run_agent, Point, MAINNET_MAGIC};
use pallas_miniprotocols::{DecodePayload, EncodePayload, PayloadDecoder, PayloadEncoder};
use pallas_multiplexer::Multiplexer;
use std::os::unix::net::UnixStream;

View file

@ -1,14 +1,15 @@
use minicbor::data::Tag;
use net2::TcpStreamExt;
use pallas_alonzo::{crypto, Fragment, Header};
use pallas_primitives::alonzo::{crypto, Header};
use pallas_primitives::Fragment;
use pallas_miniprotocols::Point;
use std::net::TcpStream;
use pallas_miniprotocols::chainsync::{BlockLike, Consumer, NoopObserver};
use pallas_miniprotocols::handshake::n2n::{Client, VersionTable};
use pallas_miniprotocols::handshake::MAINNET_MAGIC;
use pallas_miniprotocols::{
run_agent, DecodePayload, EncodePayload, PayloadDecoder, PayloadEncoder,
run_agent, DecodePayload, EncodePayload, PayloadDecoder, PayloadEncoder, MAINNET_MAGIC,
};
use pallas_multiplexer::Multiplexer;

View file

@ -2,8 +2,7 @@ use net2::TcpStreamExt;
use std::net::TcpStream;
use pallas_miniprotocols::handshake::n2c::{Client, VersionTable};
use pallas_miniprotocols::handshake::MAINNET_MAGIC;
use pallas_miniprotocols::run_agent;
use pallas_miniprotocols::{run_agent, MAINNET_MAGIC};
use pallas_multiplexer::Multiplexer;
fn main() {

View file

@ -2,8 +2,7 @@ use net2::TcpStreamExt;
use std::net::TcpStream;
use pallas_miniprotocols::handshake::n2n::{Client, VersionTable};
use pallas_miniprotocols::handshake::MAINNET_MAGIC;
use pallas_miniprotocols::run_agent;
use pallas_miniprotocols::{run_agent, MAINNET_MAGIC};
use pallas_multiplexer::Multiplexer;
fn main() {

View file

@ -1,8 +1,10 @@
use pallas_miniprotocols::handshake::n2c::{Client, VersionTable};
use pallas_miniprotocols::handshake::MAINNET_MAGIC;
use pallas_miniprotocols::localstate::queries::RequestV10;
use pallas_miniprotocols::localstate::{queries::QueryV10, OneShotClient};
use pallas_miniprotocols::localstate::{
queries::{QueryV10, RequestV10},
OneShotClient,
};
use pallas_miniprotocols::run_agent;
use pallas_miniprotocols::MAINNET_MAGIC;
use pallas_multiplexer::Multiplexer;
use std::os::unix::net::UnixStream;

View file

@ -2,9 +2,8 @@ use net2::TcpStreamExt;
use std::net::TcpStream;
use pallas_miniprotocols::handshake::n2c::{Client, VersionTable};
use pallas_miniprotocols::handshake::MAINNET_MAGIC;
use pallas_miniprotocols::run_agent;
use pallas_miniprotocols::txsubmission::NaiveProvider;
use pallas_miniprotocols::{run_agent, MAINNET_MAGIC};
use pallas_multiplexer::Multiplexer;
fn main() {

View file

@ -5,7 +5,7 @@ use crate::machines::{
Transition,
};
use crate::primitives::Point;
use crate::common::Point;
use log::debug;

View file

@ -6,8 +6,8 @@ use crate::machines::{
Agent, DecodePayload, EncodePayload, MachineError, MachineOutput, Transition,
};
use crate::common::Point;
use crate::payloads::{PayloadDecoder, PayloadEncoder};
use crate::primitives::Point;
use super::{Message, State, Tip};

View file

@ -1,5 +1,5 @@
use crate::common::Point;
use crate::machines::{CodecError, DecodePayload, EncodePayload, PayloadDecoder, PayloadEncoder};
use crate::primitives::Point;
use super::{Message, Tip};

View file

@ -1,7 +1,7 @@
use std::fmt::Debug;
use crate::common::Point;
use crate::machines::{DecodePayload, EncodePayload};
use crate::primitives::Point;
#[derive(Debug)]
pub struct Tip(pub Point, pub u64);

View file

@ -1,5 +1,5 @@
use super::common::*;
use super::payloads::*;
use super::primitives::*;
impl EncodePayload for Point {
fn encode_payload(&self, e: &mut PayloadEncoder) -> Result<(), Box<dyn std::error::Error>> {

View file

@ -1,5 +1,11 @@
use std::fmt::Debug;
/// Well-known magic for testnet
pub const TESTNET_MAGIC: u64 = 1097911063;
/// Well-known magic for mainnet
pub const MAINNET_MAGIC: u64 = 764824073;
/// A point within a chain
#[derive(Clone)]
pub struct Point(pub u64, pub Vec<u8>);

View file

@ -5,9 +5,6 @@ use crate::{
use itertools::Itertools;
use std::{collections::HashMap, fmt::Debug};
pub const TESTNET_MAGIC: u64 = 1097911063;
pub const MAINNET_MAGIC: u64 = 764824073;
#[derive(Debug, Clone)]
pub struct VersionTable<T>
where

View file

@ -2,5 +2,3 @@ mod common;
pub mod n2c;
pub mod n2n;
pub use common::{MAINNET_MAGIC, TESTNET_MAGIC};

View file

@ -1,7 +1,7 @@
mod codec;
mod common;
mod machines;
mod payloads;
mod primitives;
pub mod blockfetch;
pub mod chainsync;
@ -10,6 +10,6 @@ pub mod localstate;
pub mod txsubmission;
pub use codec::*;
pub use common::*;
pub use machines::*;
pub use payloads::*;
pub use primitives::*;

View file

@ -9,7 +9,7 @@ use crate::machines::{
Agent, DecodePayload, EncodePayload, MachineError, MachineOutput, Transition,
};
use crate::primitives::Point;
use crate::common::Point;
#[derive(Debug, PartialEq, Clone)]
pub enum State {

View file

@ -1,6 +1,6 @@
use crate::common::Point;
use crate::machines::{DecodePayload, EncodePayload, PayloadDecoder};
use crate::payloads::PayloadEncoder;
use crate::primitives::Point;
use minicbor::{data::Cbor, Decoder};
use super::Query;

View file

@ -1,6 +1,6 @@
[package]
name = "pallas-byron"
description = "Ledger primitives and cbor codec for the Byron era"
name = "pallas-primitives"
description = "Ledger primitives and cbor codec for the different Cardano eras"
version = "0.4.0"
edition = "2021"
repository = "https://github.com/txpipe/pallas"
@ -11,7 +11,6 @@ readme = "README.md"
authors = [
"Santiago Carmuega <santiago@carmuega.me>",
"Lucas Rosa <x@rvcas.dev>",
]
[dependencies]

View file

@ -0,0 +1,2 @@
# Pallas Primitives

View file

@ -1,4 +1,4 @@
use crate::{AuxiliaryData, Header, PlutusData, TransactionBody};
use super::{AuxiliaryData, Header, PlutusData, TransactionBody};
use pallas_crypto::hash::{Hash, Hasher};
pub fn hash_block_header(data: &Header) -> Hash<32> {
@ -19,7 +19,8 @@ pub fn hash_plutus_data(data: &PlutusData) -> Hash<32> {
#[cfg(test)]
mod tests {
use crate::{BlockWrapper, Fragment};
use crate::alonzo::BlockWrapper;
use crate::Fragment;
use super::hash_transaction;

View file

@ -0,0 +1,5 @@
mod model;
pub mod crypto;
pub use model::*;

View file

@ -2,7 +2,6 @@
//!
//! Handcrafted, idiomatic rust artifacts based on based on the [Alonzo CDDL](https://github.com/input-output-hk/cardano-ledger/blob/master/eras/alonzo/test-suite/cddl-files/alonzo.cddl) file in IOHK repo.
use log::warn;
use minicbor::{bytes::ByteVec, data::Tag};
use minicbor_derive::{Decode, Encode};
use pallas_crypto::hash::Hash;
@ -10,31 +9,6 @@ use std::{collections::BTreeMap, ops::Deref};
use crate::utils::{KeyValuePairs, MaybeIndefArray};
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord)]
pub struct SkipCbor<const N: usize> {}
impl<'b, const N: usize> minicbor::Decode<'b> for SkipCbor<N> {
fn decode(d: &mut minicbor::Decoder<'b>) -> Result<Self, minicbor::decode::Error> {
{
let probe = d.probe();
warn!("skipped cbor value {}: {:?}", N, probe.datatype()?);
println!("skipped cbor value {}: {:?}", N, probe.datatype()?);
}
d.skip()?;
Ok(SkipCbor {})
}
}
impl<const N: usize> minicbor::Encode for SkipCbor<N> {
fn encode<W: minicbor::encode::Write>(
&self,
_e: &mut minicbor::Encoder<W>,
) -> Result<(), minicbor::encode::Error<W::Error>> {
todo!()
}
}
#[derive(Encode, Decode, Debug, PartialEq, Clone)]
pub struct VrfCert(#[n(0)] pub ByteVec, #[n(1)] pub ByteVec);
@ -1418,7 +1392,8 @@ pub struct BlockWrapper(#[n(0)] pub u16, #[n(1)] pub Block);
#[cfg(test)]
mod tests {
use crate::{BlockWrapper, Fragment};
use super::BlockWrapper;
use crate::Fragment;
use minicbor::{self, to_vec};
#[test]

View file

@ -1,10 +1,7 @@
//! Ledger primitives and cbor codec for the Byron era
mod framework;
mod model;
mod utils;
pub use framework::*;
pub use model::*;
// pub mod crypto;

View file

@ -2,7 +2,6 @@
//!
//! Handcrafted, idiomatic rust artifacts based on based on the [Byron CDDL](https://github.com/input-output-hk/cardano-ledger/blob/master/eras/byron/cddl-spec/byron.cddl) file in IOHK repo.
use log::warn;
use minicbor::bytes::ByteVec;
use minicbor_derive::{Decode, Encode};
use pallas_crypto::hash::Hash;
@ -11,31 +10,6 @@ use crate::utils::{
CborWrap, EmptyMap, KeyValuePairs, MaybeIndefArray, OrderPreservingProperties, TagWrap,
};
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord)]
pub struct SkipCbor<const N: usize> {}
impl<'b, const N: usize> minicbor::Decode<'b> for SkipCbor<N> {
fn decode(d: &mut minicbor::Decoder<'b>) -> Result<Self, minicbor::decode::Error> {
{
let probe = d.probe();
warn!("skipped cbor value {}: {:?}", N, probe.datatype()?);
println!("skipped cbor value {}: {:?}", N, probe.datatype()?);
}
d.skip()?;
Ok(SkipCbor {})
}
}
impl<const N: usize> minicbor::Encode for SkipCbor<N> {
fn encode<W: minicbor::encode::Write>(
&self,
_e: &mut minicbor::Encoder<W>,
) -> Result<(), minicbor::encode::Error<W::Error>> {
todo!()
}
}
// Basic Cardano Types
pub type Blake2b256 = Hash<32>;
@ -926,7 +900,9 @@ impl minicbor::Encode for Block {
#[cfg(test)]
mod tests {
use crate::{Block, Fragment};
use crate::byron::Block;
use crate::Fragment;
use minicbor::{self, to_vec};
#[test]

View file

@ -0,0 +1,9 @@
//! Ledger primitives and cbor codec for the Cardano eras
mod framework;
pub mod alonzo;
pub mod byron;
pub mod utils;
pub use framework::*;

View file

@ -2,6 +2,32 @@ use std::ops::Deref;
use minicbor::{data::Tag, Decode, Encode};
/// Utility for skipping parts of the CBOR payload, use only for debugging
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord)]
pub struct SkipCbor<const N: usize> {}
impl<'b, const N: usize> minicbor::Decode<'b> for SkipCbor<N> {
fn decode(d: &mut minicbor::Decoder<'b>) -> Result<Self, minicbor::decode::Error> {
{
let probe = d.probe();
log::warn!("skipped cbor value {}: {:?}", N, probe.datatype()?);
println!("skipped cbor value {}: {:?}", N, probe.datatype()?);
}
d.skip()?;
Ok(SkipCbor {})
}
}
impl<const N: usize> minicbor::Encode for SkipCbor<N> {
fn encode<W: minicbor::encode::Write>(
&self,
_e: &mut minicbor::Encoder<W>,
) -> Result<(), minicbor::encode::Error<W::Error>> {
todo!()
}
}
/// Custom collection to ensure ordered pairs of values
///
/// Since the ordering of the entries requires a particular order to maintain

View file

@ -15,6 +15,5 @@ authors = [
[dependencies]
pallas-multiplexer = { version = "0.4.0", path = "../pallas-multiplexer/" }
pallas-miniprotocols = { version = "0.4.0", path = "../pallas-miniprotocols/" }
pallas-byron = { version = "0.4.0", path = "../pallas-byron/" }
pallas-alonzo = { version = "0.4.0", path = "../pallas-alonzo/" }
pallas-primitives = { version = "0.4.0", path = "../pallas-primitives/" }
pallas-crypto = { version = "0.4.0", path = "../pallas-crypto/" }

View file

@ -1,7 +1,4 @@
//! Ledger primitives and cbor codecs for different Cardano eras
#[doc(inline)]
pub use pallas_byron as byron;
#[doc(inline)]
pub use pallas_alonzo as alonzo;
pub use pallas_primitives as primitives;

View file

@ -9,7 +9,9 @@
#![warn(missing_docs)]
#![warn(missing_doc_code_examples)]
pub mod ouroboros;
pub mod network;
pub mod ledger;
#[doc(inline)]
pub use pallas_crypto as crypto;

View file

@ -1,3 +0,0 @@
//! Ouroboros protocol artifacts
pub mod network;