refactor: Re-organize and clean-up pallas-primitives (#523)

* Re-organize and clean-up pallas-primitives

  Namely:

  - Move _common_ (i.e. era-independent) types and structures up to the
    `lib` module; to be shared across all eras. If any of those deviate
    in a subsequent era, it is easy to bring them down and define new
    types from the point of divergence onward. This simplifies the scope
    of each era-specific module and make them slightly easier to
    navigate.

    Note that, each era module still re-export all of the common types
    that's relevant to that particular era. So technically, this
    reorganization doesn't really change anything for callers/users of
    the library.

  - Rename `Scripthash` to `ScriptHash`. Before this commit, both
    actually existed as `ScriptHash` was introduced with the Conway era.
    Yet, they refer to the same thing, so the duplication is simply
    confusing.

  - Rename `One` / `Two` constructors for `NetworkId` to `Testnet` and
    `Mainnet` respectively. Also defined idiomatic `From` & `TryFrom`
    implementation for conversion to and from `u8`. This is a lot let
    confusing!

  - Generalize `PlutusScript` with a constant generic, to avoid
    repetition for each plutus script generated for specific version.
    Note that a distinction is still _necessary_ if we want to provie
    out-of-the-box serialisers for Plutus scripts, which are serialised
    with a tag prefix depending on the language. All else apart, they
    are strictly similar types.

  - Rename `CostMdls` to `CostModels`. Because, common.

  - Rename `plutus_script` to `plutus_v1_script` in the Alonzo's witness
    set, for consistency with other eras.

* Fix ordering of ScriptHash variants.

  This is an odd one. See the note.

* Bump minicbor to v0.25.1

* Add aliases with deprecation warnings to various fields and types.

* revert renaming plutus_script to plutus_v1_script in Alonzo witness

  See https://github.com/txpipe/pallas/pull/523#discussion_r1807329742
This commit is contained in:
Matthias Benkort 2024-10-22 13:57:21 +02:00 committed by GitHub
parent 4871342a8d
commit 969d5612b7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 972 additions and 1071 deletions

View file

@ -1,4 +1,4 @@
use pallas_codec::minicbor::{data::Tag, decode, encode, Decode, Decoder, Encode, Encoder};
use pallas_codec::minicbor::{data::IanaTag, decode, encode, Decode, Decoder, Encode, Encoder};
use super::Message;
@ -29,7 +29,7 @@ impl Encode<()> for Message {
}
Message::Block { body } => {
e.array(2)?.u16(4)?;
e.tag(Tag::Cbor)?;
e.tag(IanaTag::Cbor)?;
e.bytes(body)?;
Ok(())
}

View file

@ -186,10 +186,10 @@ impl Encode<()> for HeaderContent {
return Err(Error::message("header variant 0 but no byron prefix"));
}
e.tag(minicbor::data::Tag::Cbor)?;
e.tag(minicbor::data::IanaTag::Cbor)?;
e.bytes(&self.cbor)?;
} else {
e.tag(minicbor::data::Tag::Cbor)?;
e.tag(minicbor::data::IanaTag::Cbor)?;
e.bytes(&self.cbor)?;
}
@ -211,7 +211,7 @@ impl Encode<()> for BlockContent {
e: &mut Encoder<W>,
_ctx: &mut (),
) -> Result<(), encode::Error<W::Error>> {
e.tag(minicbor::data::Tag::Cbor)?;
e.tag(minicbor::data::IanaTag::Cbor)?;
e.bytes(&self.0)?;
Ok(())

View file

@ -100,7 +100,7 @@ impl Encode<()> for BlockQuery {
if !pools.is_empty() {
e.array(1)?;
e.tag(Tag::Unassigned(258))?;
e.tag(Tag::new(258))?;
}
e.encode(pools)?;
@ -330,7 +330,7 @@ impl<C> minicbor::encode::Encode<C> for RationalNumber {
e: &mut minicbor::Encoder<W>,
ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
e.tag(Tag::Unassigned(30))?;
e.tag(Tag::new(30))?;
e.array(2)?;
e.encode_with(self.numerator, ctx)?;
e.encode_with(self.denominator, ctx)?;

View file

@ -1,4 +1,4 @@
use pallas_codec::minicbor::data::Tag;
use pallas_codec::minicbor::data::IanaTag;
use pallas_codec::minicbor::{decode, encode, Decode, Decoder, Encode, Encoder};
use crate::miniprotocols::localtxsubmission::{EraTx, Message, RejectReason};
@ -78,7 +78,7 @@ impl<'b> Decode<'b, ()> for EraTx {
d.array()?;
let era = d.u16()?;
let tag = d.tag()?;
if tag != Tag::Cbor {
if tag != IanaTag::Cbor.tag() {
return Err(decode::Error::message("Expected encoded CBOR data item"));
}
Ok(EraTx(era, d.bytes()?.to_vec()))
@ -93,7 +93,7 @@ impl Encode<()> for EraTx {
) -> Result<(), encode::Error<W::Error>> {
e.array(2)?;
e.u16(self.0)?;
e.tag(Tag::Cbor)?;
e.tag(IanaTag::Cbor)?;
e.bytes(&self.1)?;
Ok(())
}

View file

@ -1,4 +1,4 @@
use pallas_codec::minicbor::{data::Tag, decode, encode, Decode, Decoder, Encode, Encoder};
use pallas_codec::minicbor::{data::IanaTag, decode, encode, Decode, Decoder, Encode, Encoder};
use super::{
protocol::{Message, TxIdAndSize},
@ -89,7 +89,7 @@ impl<'b> Decode<'b, ()> for EraTxBody {
d.array()?;
let era = d.u16()?;
let tag = d.tag()?;
if tag != Tag::Cbor {
if tag != IanaTag::Cbor.tag() {
return Err(decode::Error::message("Expected encoded CBOR data item"));
}
Ok(EraTxBody(era, d.bytes()?.to_vec()))
@ -104,7 +104,7 @@ impl Encode<()> for EraTxBody {
) -> Result<(), encode::Error<W::Error>> {
e.array(2)?;
e.u16(self.0)?;
e.tag(Tag::Cbor)?;
e.tag(IanaTag::Cbor)?;
e.bytes(&self.1)?;
Ok(())
}