Skip to content
Open
Show file tree
Hide file tree
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
31 changes: 22 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions crates/trp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ jsonrpsee = { version = "0.24.9", features = ["server"] }
opentelemetry = "0.30.0"
opentelemetry_sdk = "0.30.0"

tx3-lang = "0.11.4"
tx3-cardano = "0.11.4"
tx3-resolver = "0.11.4"
# tx3-lang = "0.11.4"
# tx3-cardano = "0.11.4"
# tx3-resolver = "0.11.4"

# tx3-lang = { path = "../../../../tx3-lang/tx3/crates/tx3-lang" }
# tx3-cardano = { path = "../../../../tx3-lang/tx3/crates/tx3-cardano" }
# tx3-resolver = { path = "../../../../tx3-lang/tx3/crates/tx3-resolver" }

# tx3-lang = { git = "https://github.com/tx3-lang/tx3.git" }
# tx3-cardano = { git = "https://github.com/tx3-lang/tx3.git" }
# tx3-resolver = { git = "https://github.com/tx3-lang/tx3.git" }
tx3-lang = { git = "https://github.com/tx3-lang/tx3.git" }
tx3-cardano = { git = "https://github.com/tx3-lang/tx3.git" }
tx3-resolver = { git = "https://github.com/tx3-lang/tx3.git" }

tx3-sdk = { version = "^0" }
# tx3-sdk = { path = "../../../../tx3-lang/rust-sdk/sdk" }
Expand Down
33 changes: 13 additions & 20 deletions crates/trp/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use dolos_core::{Domain, Genesis};

use crate::{Config, Error};
use crate::{Facade, Config, Error};

pub fn network_id_from_genesis(genesis: &Genesis) -> Option<tx3_cardano::Network> {
match genesis.shelley.network_id.as_ref() {
Expand All @@ -29,36 +29,29 @@
HashMap::from_iter(present)
}

fn build_pparams<D: Domain>(domain: &D) -> Result<tx3_cardano::PParams, Error> {
pub fn load_compiler<D: Domain>(
domain: &Facade<D>,
config: &Config,
) -> Result<tx3_cardano::Compiler, Error> {
let network = network_id_from_genesis(&domain.genesis()).unwrap();

let pparams = dolos_cardano::load_effective_pparams::<D>(domain.state())
.map_err(|_| Error::PParamsNotAvailable)?;
let pparams = domain.get_pparams()?;

let costs = pparams.cost_models_for_script_languages();

let out = tx3_cardano::PParams {
network,
cost_models: map_cost_models(costs),
min_fee_coefficient: pparams.min_fee_a_or_default() as u64,
min_fee_constant: pparams.min_fee_b_or_default() as u64,
coins_per_utxo_byte: pparams.ada_per_utxo_byte_or_default() as u64,
};

Ok(out)
}
let chain_tip = domain.get_chain_tip()?;

pub fn load_compiler<D: Domain>(
domain: &D,
config: &Config,
) -> Result<tx3_cardano::Compiler, Error> {
let pparams = build_pparams::<D>(domain)?;
let slot_config = domain.get_slot_config()?;

let compiler = tx3_cardano::Compiler::new(

Check failure on line 46 in crates/trp/src/compiler.rs

View workflow job for this annotation

GitHub Actions / Check Build

this function takes 3 arguments but 6 arguments were supplied
pparams,
tx3_cardano::Config {
extra_fees: config.extra_fees,
},
network,
chain_tip,
pparams,
map_cost_models(costs),
slot_config,
);

Ok(compiler)
Expand Down
10 changes: 10 additions & 0 deletions crates/trp/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ pub enum Error {

#[error("tx script returned failure")]
TxScriptFailure(Vec<String>),

#[error("failed to resolve tip slot/hash")]
TipNotResolved,

#[error("failed to resolve chain summary: {0}")]
ChainSummaryNotResolved(String),
}

trait IntoErrorData {
Expand Down Expand Up @@ -128,6 +134,8 @@ impl Error {
pub const CODE_MISSING_TX_ARG: i32 = -32001;
pub const CODE_INPUT_NOT_RESOLVED: i32 = -32002;
pub const CODE_TX_SCRIPT_FAILURE: i32 = -32003;
pub const CODE_TIP_NOT_RESOLVED: i32 = -32004;
pub const CODE_CHAIN_SUMMARY_NOT_RESOLVED: i32 = -32005;

pub fn code(&self) -> i32 {
match self {
Expand All @@ -149,6 +157,8 @@ impl Error {
Error::MissingTxArg { .. } => Self::CODE_MISSING_TX_ARG,
Error::InputNotResolved(_, _, _) => Self::CODE_INPUT_NOT_RESOLVED,
Error::TxScriptFailure(_) => Self::CODE_TX_SCRIPT_FAILURE,
Error::TipNotResolved => Self::CODE_TIP_NOT_RESOLVED,
Error::ChainSummaryNotResolved(_) => Self::CODE_CHAIN_SUMMARY_NOT_RESOLVED,
}
}

Expand Down
59 changes: 57 additions & 2 deletions crates/trp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
use jsonrpsee::server::{RpcModule, Server};
use serde::{Deserialize, Serialize};
use std::{net::SocketAddr, sync::Arc};
use std::{
net::SocketAddr,
sync::Arc,
ops::Deref,
};
use tokio::select;
use tower::ServiceBuilder;
use tower_http::cors::CorsLayer;
use tracing::info;

use dolos_core::{CancelToken, Domain, ServeError};
use tx3_cardano::ChainPoint;
use dolos_core::{Domain, StateStore, CancelToken, ServeError};
use dolos_cardano::{ChainSummary, PParamsSet};
use pallas::ledger::validate::phase2::script_context::SlotConfig;

mod compiler;
mod error;
Expand Down Expand Up @@ -107,3 +114,51 @@ impl<D: Domain, C: CancelToken> dolos_core::Driver<D, C> for Driver {
}
}
}

#[derive(Clone)]
pub struct Facade<D: Domain> {
pub inner: D,
}

impl<D: Domain> Deref for Facade<D> {
type Target = D;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl<D: Domain> Facade<D> {
pub fn get_chain_summary(&self) -> Result<ChainSummary, error::Error> {
let summary = dolos_cardano::eras::load_era_summary::<D>(&self.inner.state())
.map_err(|e| error::Error::ChainSummaryNotResolved(e.to_string()))?;

Ok(summary)
}

pub fn get_slot_config(&self) -> Result<SlotConfig, error::Error> {
let chain = self.get_chain_summary()?;

Ok(SlotConfig {
slot_length: chain.edge().slot_length,
zero_slot: chain.edge().start.slot,
zero_time: chain.edge().start.timestamp,
})
}

pub fn get_chain_tip(&self) -> Result<ChainPoint, error::Error> {
let cursor = self.inner.state().read_cursor()?.ok_or(Error::TipNotResolved)?;
let slot = cursor.slot();
let hash = cursor.hash().map(|h| h.to_vec()).unwrap_or_default();
let timestamp = (slot * 1000) as u128; // TODO: check if this is the correct way to get timestamp

Ok(ChainPoint { slot, hash, timestamp })
}

pub fn get_pparams(&self) -> Result<PParamsSet, error::Error> {
let pparams = dolos_cardano::load_effective_pparams::<D>(&self.inner.state())
.map_err(|_| Error::PParamsNotAvailable)?;

Ok(pparams)
}
}
6 changes: 4 additions & 2 deletions crates/trp/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use dolos_core::{Domain, MempoolStore as _, StateStore as _};

use crate::{compiler::load_compiler, utxos::UtxoStoreAdapter};
use crate::{Facade, compiler::load_compiler, utxos::UtxoStoreAdapter};

use super::{Context, Error};

Expand Down Expand Up @@ -61,9 +61,9 @@
return Err(Error::MissingTxArg { key, ty });
};

let arg = tx3_sdk::trp::args::from_json(arg.clone(), &ty)?;

Check failure on line 64 in crates/trp/src/methods.rs

View workflow job for this annotation

GitHub Actions / Check Build

mismatched types

tx.set_arg(&key, arg);

Check failure on line 66 in crates/trp/src/methods.rs

View workflow job for this annotation

GitHub Actions / Check Build

mismatched types
}

Ok(tx)
Expand All @@ -77,7 +77,9 @@

let tx = load_tx(params)?;

let mut compiler = load_compiler::<D>(&context.domain, &context.config)?;
let facade = Facade { inner: context.domain.clone() };

let mut compiler = load_compiler::<D>(&facade, &context.config)?;

let utxos = UtxoStoreAdapter::<D>::new(context.domain.state().clone());

Expand Down
28 changes: 27 additions & 1 deletion crates/trp/src/utxos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
UtxoRef, UtxoSet,
};

use dolos_core::{Domain, StateStore as _, TxoRef};
use dolos_core::{Domain, StateStore as _, TxoRef, EraCbor};
use pallas::ledger::validate::utils::UtxoMap;

use crate::{
mapping::{from_tx3_utxoref, into_tx3_utxo, into_tx3_utxoref},
Expand Down Expand Up @@ -75,4 +76,29 @@

Ok(utxos)
}

async fn fetch_utxos_deps(

Check failure on line 80 in crates/trp/src/utxos.rs

View workflow job for this annotation

GitHub Actions / Check Build

method `fetch_utxos_deps` is not a member of trait `UtxoStore`
&self,
refs: HashSet<UtxoRef>
) -> Result<UtxoMap, tx3_lang::backend::Error> {
let refs: Vec<_> = refs.into_iter().map(from_tx3_utxoref).collect();

let utxos = self.state().get_utxos(refs).map_err(Error::from)?;

let utxos = utxos
.into_iter()
.map(|(txoref, eracbor)| {
let TxoRef(a, b) = txoref;
let EraCbor(c, d) = eracbor.as_ref();
let era = pallas::ledger::traverse::Era::try_from(*c).expect("era out of range");

(
pallas::ledger::validate::utils::TxoRef::from((a, b)),
pallas::ledger::validate::utils::EraCbor::from((era, d.clone())),
)
})
.collect();

Ok(utxos)
}
}
Loading