Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement phase one validation #426

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use crate::{
use futures_util::StreamExt;
use itertools::Itertools;
use pallas::{
crypto::hash::Hash,
ledger::traverse::{MultiEraBlock, MultiEraTx},
applying::{utils::AccountState, validate_tx, CertState, Environment, UTxOs},
crypto::hash::Hash, ledger::{primitives::TransactionInput,
traverse::{wellknown::GenesisValues, MultiEraBlock, MultiEraInput, MultiEraOutput, MultiEraTx}}
};
use std::{
collections::{HashMap, HashSet},
sync::{Arc, RwLock},
borrow::Cow,
collections::{HashMap, HashSet},
sync::{Arc, RwLock}
};
use thiserror::Error;
use tokio::sync::broadcast;
Expand All @@ -28,6 +30,9 @@ pub enum MempoolError {
#[error("decode error: {0}")]
DecodeError(#[from] pallas::codec::minicbor::decode::Error),

#[error("tx validation failed: {0}")]
ValidationError(#[from] pallas::applying::utils::ValidationError),

#[cfg(feature = "phase2")]
#[error("tx evaluation failed")]
EvaluationError(#[from] crate::uplc::error::Error),
Expand Down Expand Up @@ -119,6 +124,51 @@ impl Mempool {
);
}

pub fn validate(&self, tx: &MultiEraTx) -> Result<(), MempoolError> {
let tip = self.ledger.cursor()?;

let updates: Vec<_> = self
.ledger
.get_pparams(tip.as_ref().map(|p| p.0).unwrap_or_default())?;

let updates: Vec<_> = updates.into_iter().map(TryInto::try_into).try_collect()?;

let (pparams, _) = crate::ledger::pparams::fold(&self.genesis, &updates);

let network_magic = self.genesis.shelley.network_magic.unwrap();

let genesis_values = GenesisValues::from_magic(network_magic.into()).unwrap();

let env = Environment {
prot_params: pparams,
prot_magic: self.genesis.shelley.network_magic.unwrap(),
block_slot: tip.unwrap().0,
network_id: genesis_values.network_id as u8,
acnt: Some(AccountState::default()),

};

let input_refs = tx.requires().iter().map(From::from).collect();

let utxos = self.ledger.get_utxos(input_refs)?;

let mut pallas_utxos = UTxOs::new();

for (txoref, eracbor) in utxos.iter() {
let tx_in = TransactionInput {
transaction_id: txoref.0,
index: txoref.1.into(),
};
let input = MultiEraInput::AlonzoCompatible(<Box<Cow<'_,TransactionInput>>>::from(Cow::Owned(tx_in)));
let output = MultiEraOutput::try_from(eracbor)?;
pallas_utxos.insert(input, output);
}

validate_tx(tx, 0, &env, &pallas_utxos, &mut CertState::default())?;

Ok(())
}

#[cfg(feature = "phase2")]
pub fn evaluate(&self, tx: &MultiEraTx) -> Result<EvalReport, MempoolError> {
let tip = self.ledger.cursor()?;
Expand Down Expand Up @@ -161,6 +211,8 @@ impl Mempool {
pub fn receive_raw(&self, cbor: &[u8]) -> Result<TxHash, MempoolError> {
let tx = MultiEraTx::decode(cbor)?;

self.validate(&tx)?;

#[cfg(feature = "phase2")]
self.evaluate(&tx)?;

Expand Down
Loading