Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercurial committed Nov 5, 2024
1 parent c6ab53f commit a8d2cf3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 120 deletions.
119 changes: 2 additions & 117 deletions src/serve/grpc/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,124 +27,9 @@ use crate::mempool::{Event, Mempool, UpdateFilter};
use crate::serve::grpc::query::QueryServiceImpl;
use crate::serve::GenesisFiles;
use crate::state::LedgerStore;
use crate::uplc::script_context::{ResolvedInput as UPLCResolvedInput, SlotConfig};
use crate::uplc::script_context::{ResolvedInput, SlotConfig};
use crate::uplc::tx::eval_tx;

#[derive(Debug, PartialEq, Clone)]
pub struct ResolvedInput {
pub input: TransactionInput,
pub output: TransactionOutput,
}

#[derive(Debug, PartialEq, Clone)]
pub enum ScriptVersion {
Native(NativeScript),
V1(PlutusV1Script),
V2(PlutusV2Script),
V3(PlutusV3Script),
}

pub struct DataLookupTable {
datum: HashMap<DatumHash, PlutusData>,
scripts: HashMap<ScriptHash, ScriptVersion>,
}

impl DataLookupTable {
pub fn from_transaction(tx: &MintedTx, utxos: &[ResolvedInput]) -> DataLookupTable {
let mut datum = HashMap::new();
let mut scripts = HashMap::new();

// discovery in witness set

let plutus_data_witnesses = tx
.transaction_witness_set
.plutus_data
.clone()
.map(|s| s.to_vec())
.unwrap_or_default();

let scripts_native_witnesses = tx
.transaction_witness_set
.native_script
.clone()
.map(|s| s.to_vec())
.unwrap_or_default();

let scripts_v1_witnesses = tx
.transaction_witness_set
.plutus_v1_script
.clone()
.map(|s| s.to_vec())
.unwrap_or_default();

let scripts_v2_witnesses = tx
.transaction_witness_set
.plutus_v2_script
.clone()
.map(|s| s.to_vec())
.unwrap_or_default();

let scripts_v3_witnesses = tx
.transaction_witness_set
.plutus_v3_script
.clone()
.map(|s| s.to_vec())
.unwrap_or_default();

for plutus_data in plutus_data_witnesses.iter() {
datum.insert(plutus_data.original_hash(), plutus_data.clone().unwrap());
}

for script in scripts_native_witnesses.iter() {
scripts.insert(
script.compute_hash(),
ScriptVersion::Native(script.clone().unwrap()),
);
}

for script in scripts_v1_witnesses.iter() {
scripts.insert(script.compute_hash(), ScriptVersion::V1(script.clone()));
}

for script in scripts_v2_witnesses.iter() {
scripts.insert(script.compute_hash(), ScriptVersion::V2(script.clone()));
}

for script in scripts_v3_witnesses.iter() {
scripts.insert(script.compute_hash(), ScriptVersion::V3(script.clone()));
}

// discovery in utxos (script ref)

for utxo in utxos.iter() {
match &utxo.output {
TransactionOutput::Legacy(_) => {}
TransactionOutput::PostAlonzo(output) => {
if let Some(script) = &output.script_ref {
match &script.0 {
PseudoScript::NativeScript(ns) => {
scripts
.insert(ns.compute_hash(), ScriptVersion::Native(ns.clone()));
}
PseudoScript::PlutusV1Script(v1) => {
scripts.insert(v1.compute_hash(), ScriptVersion::V1(v1.clone()));
}
PseudoScript::PlutusV2Script(v2) => {
scripts.insert(v2.compute_hash(), ScriptVersion::V2(v2.clone()));
}
PseudoScript::PlutusV3Script(v3) => {
scripts.insert(v3.compute_hash(), ScriptVersion::V3(v3.clone()));
}
}
}
}
}
}

DataLookupTable { datum, scripts }
}
}

pub struct SubmitServiceImpl {
mempool: Mempool,
ledger: LedgerStore,
Expand Down Expand Up @@ -362,7 +247,7 @@ impl submit_service_server::SubmitService for SubmitServiceImpl {

let resolved_inputs = utxos
.into_iter()
.map(|(txo_ref, utxo_cbor)| UPLCResolvedInput {
.map(|(txo_ref, utxo_cbor)| ResolvedInput {
input: TransactionInput {
transaction_id: txo_ref.0,
index: txo_ref.1.into(),
Expand Down
6 changes: 3 additions & 3 deletions src/uplc/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ pub fn eval_redeemer(
lookup_table: &DataLookupTable,
slot_config: &SlotConfig,
) -> Result<TxEvalResult, Error> {
let tx_info = TxInfoV3::from_transaction(tx, utxos, slot_config).unwrap();

match find_script(redeemer, tx, utxos, lookup_table)? {
(ScriptVersion::Native(_), _) => Err(Error::NativeScriptPhaseTwo),

Expand All @@ -109,6 +107,7 @@ pub fn eval_redeemer(
(ScriptVersion::V2(script), datum) => todo!(),

(ScriptVersion::V3(script), datum) => {
let tx_info = TxInfoV3::from_transaction(tx, utxos, slot_config).unwrap();
let script_context = tx_info
.into_script_context(redeemer, datum.as_ref())
.unwrap();
Expand All @@ -119,13 +118,14 @@ pub fn eval_redeemer(

let arena = uplc::bumpalo::Bump::with_capacity(1_024_000);

// One-liner to decode the Flat-encoded script
// One-liner to cbor decode the Flat-encoded script (assuming its just a CBOR bytestring)
let mut program = uplc::flat::decode(&arena, &script_bytes[2..])
.into_diagnostic()
.unwrap();

let script_context_term =
map_pallas_data_to_pragma_data(&arena, script_context.to_plutus_data());

let script_context_term = Term::data(&arena, script_context_term);
program = program.apply(&arena, script_context_term);

Expand Down

0 comments on commit a8d2cf3

Please sign in to comment.