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

Feature/validations config #19

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion napi-pallas/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ hex = "0.4.3"
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2.12.2", default-features = false, features = ["napi4"] }
napi-derive = "2.12.2"
pallas = { git = "https://github.com/alegadea/pallas.git", rev = "54ffc77" , features = ["unstable"]}
pallas = { git = "https://github.com/txpipe/pallas.git", rev = "9f5713c" , features = ["unstable"]}

[build-dependencies]
napi-build = "2.0.1"
Expand Down
45 changes: 44 additions & 1 deletion napi-pallas/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,48 @@ export interface Output {
bytes?: string
address?: AddressDiagnostic
}
export interface ValidationContext {
epoch: number
minFeeA: number
minFeeB: number
maxBlockSize: number
maxTxSize: number
maxBlockHeaderSize: number
keyDeposit: number
poolDeposit: number
eMax: number
nOpt: number
a0Numerator: number
a0Denominator: number
rhoNumerator: number
rhoDenominator: number
tauNumerator: number
tauDenominator: number
decentralisationParamNumerator: number
decentralisationParamDenominator: number
extraEntropyNumerator: number
extraEntropyDenominator: number
protocolMajorVer: number
protocolMinorVer: number
minUtxo: number
minPoolCost: number
priceMemNumerator: number
priceMemDenominator: number
priceStepNumerator: number
priceStepDenominator: number
maxTxExMem: number
maxTxExSteps: number
maxBlockExMem: number
maxBlockExSteps: number
maxValSize: number
collateralPercent: number
maxCollateralInputs: number
coinsPerUtxoSize: number
coinsPerUtxoWord: number
network: string
era: string
blockSlot: number
}
export interface Attribute {
topic?: string
value?: string
Expand All @@ -37,7 +79,7 @@ export interface SectionValidation {
section: Section
validations: Validations
}
export function safeParseTx(raw: string): SectionValidation
export function safeParseTx(raw: string, context: ValidationContext): SectionValidation
export function safeParseBlock(raw: string): Section
export interface Validation {
name: string
Expand All @@ -46,4 +88,5 @@ export interface Validation {
}
export interface Validations {
validations: Array<Validation>
era: string
}
58 changes: 56 additions & 2 deletions napi-pallas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,52 @@ mod block;
mod tx;
mod validations;

#[derive(Default)]
#[napi(object)]
pub struct ValidationContext {
pub epoch: u32,
pub min_fee_a: u32,
pub min_fee_b: u32,
pub max_block_size: u32,
pub max_tx_size: u32,
pub max_block_header_size: u32,
pub key_deposit: i64,
pub pool_deposit: i64,
pub e_max: i64,
pub n_opt: u32,
pub a0_numerator: i64,
pub a0_denominator: i64,
pub rho_numerator: i64,
pub rho_denominator: i64,
pub tau_numerator: i64,
pub tau_denominator: i64,
pub decentralisation_param_numerator: i64,
pub decentralisation_param_denominator: i64,
pub extra_entropy_numerator: u32,
pub extra_entropy_denominator: u32,
pub protocol_major_ver: i64,
pub protocol_minor_ver: i64,
pub min_utxo: i64,
pub min_pool_cost: i64,
pub price_mem_numerator: i64,
pub price_mem_denominator: i64,
pub price_step_numerator: i64,
pub price_step_denominator: i64,
pub max_tx_ex_mem: u32,
pub max_tx_ex_steps: i64,
pub max_block_ex_mem: u32,
pub max_block_ex_steps: i64,
pub max_val_size: u32,
pub collateral_percent: u32,
pub max_collateral_inputs: u32,
pub coins_per_utxo_size: i64,
pub coins_per_utxo_word: i64,

pub network: String,
pub era: String,
pub block_slot: u32,
}

#[derive(Default)]
#[napi(object)]
pub struct Attribute {
Expand Down Expand Up @@ -152,8 +198,8 @@ pub struct SectionValidation {
}

#[napi]
pub fn safe_parse_tx(raw: String) -> SectionValidation {
match tx::parse(raw) {
pub fn safe_parse_tx(raw: String, context: ValidationContext) -> SectionValidation {
match tx::parse(raw, context) {
Ok(x) => {
let (section, validations) = x;
SectionValidation {
Expand Down Expand Up @@ -212,6 +258,7 @@ impl Validation {
#[napi(object)]
pub struct Validations {
pub validations: Vec<Validation>,
pub era: String,
}

impl Validations {
Expand All @@ -224,4 +271,11 @@ impl Validations {

self
}

pub fn with_era(self, era: impl ToString) -> Self {
Self {
era: era.to_string(),
..self
}
}
}
19 changes: 15 additions & 4 deletions napi-pallas/src/tx.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::validations::validate::validate;
use crate::Validations;
use crate::{validations::validate::validate, ValidationContext};

use super::Section;
use pallas::ledger::traverse::Era;
use pallas::{
codec::utils::KeepRaw,
crypto::hash::Hasher,
Expand Down Expand Up @@ -235,13 +236,23 @@ pub fn create_cbor_structure(tx: &MultiEraTx<'_>) -> Section {
out
}

pub fn parse(raw: String) -> Result<(Section, Validations), Section> {
pub fn parse(raw: String, context: ValidationContext) -> Result<(Section, Validations), Section> {
let res_cbor = hex::decode(raw);
let mut era_decode = Era::Babbage;
match context.era.as_str() {
"Alonzo" => era_decode = Era::Alonzo,
"Babbage" => era_decode = Era::Babbage,
"Byron" => era_decode = Era::Byron,
"Conway" => era_decode = Era::Conway,
"Shelley MA" => era_decode = Era::Shelley,
// This case should never happen
_ => {}
}
match res_cbor {
Ok(cbor) => {
let res_mtx = MultiEraTx::decode(&cbor);
let res_mtx = MultiEraTx::decode_for_era(era_decode, &cbor);
match res_mtx {
Ok(mtx) => Ok((create_cbor_structure(&mtx), validate(&mtx))),
Ok(mtx) => Ok((create_cbor_structure(&mtx), validate(&mtx, context))),
Err(e) => {
let mut err = Section::new();
err.error = Some(e.to_string());
Expand Down
Loading
Loading