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

@ -5,7 +5,7 @@ use pallas_primitives::alonzo;
use crate::MultiEraTx;
impl<'b> MultiEraTx<'b> {
pub fn aux_plutus_v1_scripts(&self) -> &[alonzo::PlutusScript] {
pub fn aux_plutus_v1_scripts(&self) -> &[alonzo::PlutusScript<1>] {
if let Some(aux_data) = self.aux_data() {
if let alonzo::AuxiliaryData::PostAlonzo(x) = aux_data.deref() {
if let Some(plutus) = &x.plutus_scripts {

View file

@ -82,9 +82,9 @@ impl OriginalHash<28> for KeepRaw<'_, alonzo::NativeScript> {
}
}
impl ComputeHash<28> for alonzo::PlutusScript {
impl<const VERSION: usize> ComputeHash<28> for alonzo::PlutusScript<VERSION> {
fn compute_hash(&self) -> Hash<28> {
Hasher::<224>::hash_tagged(&self.0, 1)
Hasher::<224>::hash_tagged(&self.0, VERSION as u8)
}
}
@ -124,12 +124,6 @@ impl OriginalHash<32> for KeepRaw<'_, babbage::Header> {
}
}
impl ComputeHash<28> for babbage::PlutusV2Script {
fn compute_hash(&self) -> Hash<28> {
Hasher::<224>::hash_tagged(&self.0, 2)
}
}
impl ComputeHash<32> for babbage::TransactionBody {
fn compute_hash(&self) -> Hash<32> {
Hasher::<256>::hash_cbor(self)
@ -159,12 +153,6 @@ impl ComputeHash<32> for babbage::DatumOption {
// conway
impl ComputeHash<28> for conway::PlutusV3Script {
fn compute_hash(&self) -> Hash<28> {
Hasher::<224>::hash_tagged(&self.0, 3)
}
}
impl ComputeHash<32> for conway::TransactionBody {
fn compute_hash(&self) -> Hash<32> {
Hasher::<256>::hash_cbor(self)
@ -334,7 +322,7 @@ mod tests {
fn plutus_v1_script_hashes_as_cardano_cli() {
let bytecode_hex = include_str!("../../test_data/jpgstore.plutus");
let bytecode = hex::decode(bytecode_hex).unwrap();
let script = alonzo::PlutusScript(Bytes::from(bytecode));
let script = alonzo::PlutusScript::<1>(Bytes::from(bytecode));
let generated = script.compute_hash().to_string();
@ -350,7 +338,7 @@ mod tests {
fn plutus_v2_script_hashes_as_cardano_cli() {
let bytecode_hex = include_str!("../../test_data/v2script.plutus");
let bytecode = hex::decode(bytecode_hex).unwrap();
let script = babbage::PlutusV2Script(Bytes::from(bytecode));
let script = babbage::PlutusScript::<2>(Bytes::from(bytecode));
let generated = script.compute_hash().to_string();

View file

@ -1,6 +1,6 @@
//! Lightweight inspection of block data without full CBOR decoding
use pallas_codec::minicbor::decode::{Token, Tokenizer};
use pallas_codec::minicbor::{data::Token, decode::Tokenizer};
use crate::Era;

View file

@ -47,9 +47,9 @@ pub type UnitInterval = alonzo::UnitInterval;
pub type Nonce = alonzo::Nonce;
pub type ExUnitPrices = alonzo::ExUnitPrices;
pub type ExUnits = alonzo::ExUnits;
pub type AlonzoCostMdls = alonzo::CostMdls;
pub type BabbageCostMdls = babbage::CostMdls;
pub type ConwayCostMdls = conway::CostMdls;
pub type AlonzoCostModels = alonzo::CostModels;
pub type BabbageCostModels = babbage::CostModels;
pub type ConwayCostModels = conway::CostModels;
pub type ProtocolVersion = alonzo::ProtocolVersion;
pub type PoolVotingThresholds = conway::PoolVotingThresholds;
pub type DRepVotingThresholds = conway::DRepVotingThresholds;
@ -157,7 +157,9 @@ impl<'b> MultiEraUpdate<'b> {
}
}
pub fn alonzo_first_proposed_cost_models_for_script_languages(&self) -> Option<AlonzoCostMdls> {
pub fn alonzo_first_proposed_cost_models_for_script_languages(
&self,
) -> Option<AlonzoCostModels> {
match self {
MultiEraUpdate::AlonzoCompatible(x) => x
.proposed_protocol_parameter_updates
@ -169,7 +171,7 @@ impl<'b> MultiEraUpdate<'b> {
pub fn babbage_first_proposed_cost_models_for_script_languages(
&self,
) -> Option<BabbageCostMdls> {
) -> Option<BabbageCostModels> {
match self {
MultiEraUpdate::Babbage(x) => x
.proposed_protocol_parameter_updates
@ -179,7 +181,9 @@ impl<'b> MultiEraUpdate<'b> {
}
}
pub fn conway_first_proposed_cost_models_for_script_languages(&self) -> Option<ConwayCostMdls> {
pub fn conway_first_proposed_cost_models_for_script_languages(
&self,
) -> Option<ConwayCostModels> {
match self {
MultiEraUpdate::Conway(x) => x
.proposed_protocol_parameter_updates

View file

@ -1,8 +1,7 @@
use pallas_codec::utils::KeepRaw;
use pallas_primitives::{
alonzo::{self, BootstrapWitness, NativeScript, PlutusData, VKeyWitness},
babbage::PlutusV2Script,
conway::PlutusV3Script,
alonzo::{self, BootstrapWitness, NativeScript, VKeyWitness},
PlutusData, PlutusScript,
};
use crate::{MultiEraRedeemer, MultiEraTx};
@ -80,7 +79,7 @@ impl<'b> MultiEraTx<'b> {
}
}
pub fn plutus_v1_scripts(&self) -> &[alonzo::PlutusScript] {
pub fn plutus_v1_scripts(&self) -> &[alonzo::PlutusScript<1>] {
match self {
Self::Byron(_) => &[],
Self::AlonzoCompatible(x, _) => x
@ -181,7 +180,7 @@ impl<'b> MultiEraTx<'b> {
})
}
pub fn plutus_v2_scripts(&self) -> &[PlutusV2Script] {
pub fn plutus_v2_scripts(&self) -> &[PlutusScript<2>] {
match self {
Self::Byron(_) => &[],
Self::AlonzoCompatible(_, _) => &[],
@ -200,7 +199,7 @@ impl<'b> MultiEraTx<'b> {
}
}
pub fn plutus_v3_scripts(&self) -> &[PlutusV3Script] {
pub fn plutus_v3_scripts(&self) -> &[PlutusScript<3>] {
match self {
Self::Byron(_) => &[],
Self::AlonzoCompatible(_, _) => &[],