Skip to content

Commit

Permalink
feat(interop): map gov proposals for u5c (#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega authored Jan 22, 2025
1 parent 305c5c5 commit f53ecb3
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 11 deletions.
62 changes: 62 additions & 0 deletions pallas-traverse/src/governance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::{borrow::Cow, ops::Deref};

use pallas_primitives::conway::{self, GovActionId};

use crate::{MultiEraGovAction, MultiEraProposal};

impl<'b> MultiEraProposal<'b> {
pub fn from_conway(x: &'b conway::ProposalProcedure) -> Self {
Self::Conway(Box::new(Cow::Borrowed(x)))
}

pub fn as_conway(&self) -> Option<&conway::ProposalProcedure> {
match self {
MultiEraProposal::Conway(x) => Some(x.deref()),
}
}

pub fn deposit(&self) -> u64 {
match self {
MultiEraProposal::Conway(x) => x.deposit,
}
}

pub fn reward_account(&self) -> &[u8] {
match self {
MultiEraProposal::Conway(x) => x.reward_account.as_ref(),
}
}

pub fn gov_action(&self) -> MultiEraGovAction {
match self {
MultiEraProposal::Conway(x) => {
MultiEraGovAction::Conway(Box::new(Cow::Borrowed(&x.gov_action)))
}
}
}

pub fn anchor(&self) -> &conway::Anchor {
match self {
MultiEraProposal::Conway(x) => &x.anchor,
}
}
}

impl<'b> MultiEraGovAction<'b> {
pub fn from_conway(x: &'b conway::GovAction) -> Self {
Self::Conway(Box::new(Cow::Borrowed(x)))
}

pub fn id(&self) -> Option<GovActionId> {
match self {
MultiEraGovAction::Conway(x) => match x.deref().deref().clone() {
conway::GovAction::ParameterChange(id, ..) => id.into(),
conway::GovAction::HardForkInitiation(id, ..) => id.into(),
conway::GovAction::NoConfidence(id) => id.into(),
conway::GovAction::UpdateCommittee(id, ..) => id.into(),
conway::GovAction::NewConstitution(id, ..) => id.into(),
_ => None,
},
}
}
}
13 changes: 13 additions & 0 deletions pallas-traverse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod block;
pub mod cert;
pub mod era;
pub mod fees;
pub mod governance;
pub mod hashes;
pub mod header;
pub mod input;
Expand Down Expand Up @@ -189,6 +190,18 @@ pub enum MultiEraUpdate<'b> {
Conway(Box<Cow<'b, conway::Update>>),
}

#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum MultiEraProposal<'b> {
Conway(Box<Cow<'b, conway::ProposalProcedure>>),
}

#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum MultiEraGovAction<'b> {
Conway(Box<Cow<'b, conway::GovAction>>),
}

#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum MultiEraSigners<'b> {
Expand Down
16 changes: 15 additions & 1 deletion pallas-traverse/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use pallas_primitives::{

use crate::{
Era, Error, MultiEraCert, MultiEraInput, MultiEraMeta, MultiEraOutput, MultiEraPolicyAssets,
MultiEraSigners, MultiEraTx, MultiEraUpdate, MultiEraWithdrawals, OriginalHash,
MultiEraProposal, MultiEraSigners, MultiEraTx, MultiEraUpdate, MultiEraWithdrawals,
OriginalHash,
};

impl<'b> MultiEraTx<'b> {
Expand Down Expand Up @@ -386,6 +387,19 @@ impl<'b> MultiEraTx<'b> {
}
}

pub fn gov_proposals(&self) -> Vec<MultiEraProposal> {
match self {
MultiEraTx::Conway(x) => x
.transaction_body
.proposal_procedures
.iter()
.flatten()
.map(|x| MultiEraProposal::from_conway(x))
.collect(),
_ => vec![],
}
}

/// Returns the list of inputs consumed by the Tx
///
/// Helper method to abstract the logic of which inputs are consumed
Expand Down
4 changes: 4 additions & 0 deletions pallas-traverse/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ impl<'b> MultiEraUpdate<'b> {
Self::Babbage(Box::new(Cow::Borrowed(update)))
}

pub fn from_conway(update: &'b conway::Update) -> Self {
Self::Conway(Box::new(Cow::Borrowed(update)))
}

pub fn as_byron(&self) -> Option<&byron::UpProp> {
match self {
Self::Byron(_, x) => Some(x),
Expand Down
2 changes: 1 addition & 1 deletion pallas-utxorpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ authors = ["Santiago Carmuega <[email protected]>"]

[dependencies]
# utxorpc-spec = { path = "../../../utxorpc/spec/gen/rust" }
utxorpc-spec = { version = "0.15.0" }
utxorpc-spec = { version = "0.16.0" }

pallas-traverse = { version = "=0.32.0", path = "../pallas-traverse" }
pallas-primitives = { version = "=0.32.0", path = "../pallas-primitives" }
Expand Down
141 changes: 140 additions & 1 deletion pallas-utxorpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{collections::HashMap, ops::Deref};
use pallas_codec::utils::KeyValuePairs;
use pallas_crypto::hash::Hash;
use pallas_primitives::{alonzo, babbage, conway};
use pallas_traverse as trv;
use pallas_traverse::{self as trv};

use prost_types::FieldMask;
use trv::OriginalHash;
Expand All @@ -23,6 +23,13 @@ pub type EraCbor = (trv::Era, Cbor);
pub type UtxoMap = HashMap<TxoRef, EraCbor>;
pub type DatumMap = HashMap<Hash<32>, alonzo::PlutusData>;

fn rational_number_to_u5c(value: pallas_primitives::RationalNumber) -> u5c::RationalNumber {
u5c::RationalNumber {
numerator: value.numerator as i32,
denominator: value.denominator as u32,
}
}

pub trait LedgerContext: Clone {
fn get_utxos(&self, refs: &[TxoRef]) -> Option<UtxoMap>;
}
Expand Down Expand Up @@ -408,6 +415,133 @@ impl<C: LedgerContext> Mapper<C> {
}
}

pub fn map_gov_action_id(
&self,
x: &conway::Nullable<conway::GovActionId>,
) -> Option<u5c::GovernanceActionId> {
match x {
conway::Nullable::Some(x) => Some(u5c::GovernanceActionId {
transaction_id: x.transaction_id.to_vec().into(),
governance_action_index: x.action_index,
}),
_ => None,
}
}

pub fn map_conway_gov_action(&self, x: &conway::GovAction) -> u5c::GovernanceAction {
let inner =
match x {
conway::GovAction::ParameterChange(gov_id, params, script) => {
u5c::governance_action::GovernanceAction::ParameterChangeAction(
u5c::ParameterChangeAction {
gov_action_id: self.map_gov_action_id(gov_id),
protocol_param_update: Some(self.map_conway_pparams_update(&params)),
policy_hash: match script {
conway::Nullable::Some(x) => x.to_vec().into(),
_ => Default::default(),
},
},
)
}
conway::GovAction::HardForkInitiation(gov_id, version) => {
u5c::governance_action::GovernanceAction::HardForkInitiationAction(
u5c::HardForkInitiationAction {
gov_action_id: self.map_gov_action_id(gov_id),
protocol_version: Some(u5c::ProtocolVersion {
major: version.0 as u32,
minor: version.1 as u32,
}),
},
)
}
conway::GovAction::TreasuryWithdrawals(withdrawals, script) => {
u5c::governance_action::GovernanceAction::TreasuryWithdrawalsAction(
u5c::TreasuryWithdrawalsAction {
withdrawals: withdrawals
.iter()
.map(|(k, v)| u5c::WithdrawalAmount {
reward_account: k.to_vec().into(),
coin: *v,
})
.collect(),
policy_hash: match script {
conway::Nullable::Some(x) => x.to_vec().into(),
_ => Default::default(),
},
},
)
}
conway::GovAction::NoConfidence(gov_id) => {
u5c::governance_action::GovernanceAction::NoConfidenceAction(
u5c::NoConfidenceAction {
gov_action_id: self.map_gov_action_id(gov_id),
},
)
}
conway::GovAction::UpdateCommittee(gov_id, remove, add, threshold) => {
u5c::governance_action::GovernanceAction::UpdateCommitteeAction(
u5c::UpdateCommitteeAction {
gov_action_id: self.map_gov_action_id(gov_id),
remove_committee_credentials: remove
.iter()
.map(|x| self.map_stake_credential(x))
.collect(),
new_committee_credentials: add
.iter()
.map(|(cred, epoch)| u5c::NewCommitteeCredentials {
committee_cold_credential: Some(
self.map_stake_credential(cred),
),
expires_epoch: *epoch as u32,
})
.collect(),
new_committee_threshold: Some(rational_number_to_u5c(
threshold.clone(),
)),
},
)
}
conway::GovAction::NewConstitution(gov_id, constitution) => {
u5c::governance_action::GovernanceAction::NewConstitutionAction(
u5c::NewConstitutionAction {
gov_action_id: self.map_gov_action_id(gov_id),
constitution: Some(u5c::Constitution {
anchor: Some(u5c::Anchor {
url: constitution.anchor.url.clone(),
content_hash: constitution.anchor.content_hash.to_vec().into(),
}),
hash: match constitution.guardrail_script {
conway::Nullable::Some(x) => x.to_vec().into(),
_ => Default::default(),
},
}),
},
)
}
conway::GovAction::Information => {
u5c::governance_action::GovernanceAction::InfoAction(6) // The 6 is just a placeholder, we don't need to use it
}
};

u5c::GovernanceAction {
governance_action: Some(inner),
}
}

pub fn map_gov_proposal(&self, x: &trv::MultiEraProposal) -> u5c::GovernanceActionProposal {
u5c::GovernanceActionProposal {
deposit: x.deposit(),
reward_account: x.reward_account().to_vec().into(),
gov_action: x
.as_conway()
.map(|x| self.map_conway_gov_action(&x.gov_action)),
anchor: Some(u5c::Anchor {
url: x.anchor().url.clone(),
content_hash: x.anchor().content_hash.to_vec().into(),
}),
}
}

pub fn map_metadatum(x: &alonzo::Metadatum) -> u5c::Metadatum {
let inner = match x {
babbage::Metadatum::Int(x) => u5c::metadatum::Metadatum::Int(i128::from(x.0) as i64),
Expand Down Expand Up @@ -507,6 +641,11 @@ impl<C: LedgerContext> Mapper<C> {
.enumerate()
.filter_map(|(order, x)| self.map_cert(x, tx, order as u32))
.collect(),
proposals: tx
.gov_proposals()
.iter()
.map(|x| self.map_gov_proposal(x))
.collect(),
withdrawals: tx
.withdrawals_sorted_set()
.iter()
Expand Down
Loading

0 comments on commit f53ecb3

Please sign in to comment.