feat(applying): implement Alonzo phase-1 validations (#380)

This commit is contained in:
Maico Leberle 2024-01-19 22:19:46 -03:00 committed by GitHub
parent 0b1e5f0231
commit da3e636759
33 changed files with 4900 additions and 704 deletions

View file

@ -1,9 +1,10 @@
use std::borrow::Cow;
pub mod common;
use common::*;
use pallas_addresses::{Address, Network, ShelleyAddress};
use pallas_applying::{
types::{
Environment, FeePolicy, MultiEraProtParams, ShelleyMAError::*, ShelleyProtParams,
utils::{
Environment, FeePolicy, MultiEraProtParams, ShelleyMAError, ShelleyProtParams,
ValidationError::*,
},
validate, UTxOs,
@ -13,51 +14,17 @@ use pallas_codec::{
decode::{Decode, Decoder},
encode,
},
utils::Bytes,
utils::{Bytes, Nullable},
};
use pallas_crypto::hash::Hash;
use pallas_primitives::alonzo::{
MintedTx, MintedWitnessSet, TransactionBody, TransactionInput, TransactionOutput, VKeyWitness,
Value,
MintedTx, MintedWitnessSet, TransactionBody, TransactionOutput, VKeyWitness, Value,
};
use pallas_traverse::{Era, MultiEraInput, MultiEraOutput, MultiEraTx};
use pallas_traverse::{Era, MultiEraTx};
#[cfg(test)]
mod shelley_tests {
mod shelley_ma_tests {
use super::*;
fn cbor_to_bytes(input: &str) -> Vec<u8> {
hex::decode(input).unwrap()
}
fn minted_tx_from_cbor<'a>(tx_cbor: &'a Vec<u8>) -> MintedTx<'a> {
pallas_codec::minicbor::decode::<MintedTx>(&tx_cbor[..]).unwrap()
}
// Careful: this function assumes tx_body has exactly one input.
fn mk_utxo_for_single_input_tx<'a>(
tx_body: &TransactionBody,
address: String,
amount: Value,
datum_hash: Option<Hash<32>>,
) -> UTxOs<'a> {
let tx_ins: &Vec<TransactionInput> = &tx_body.inputs;
assert_eq!(tx_ins.len(), 1, "Unexpected number of inputs");
let tx_in: TransactionInput = tx_ins.first().unwrap().clone();
let address_bytes: Bytes = match hex::decode(address) {
Ok(bytes_vec) => Bytes::from(bytes_vec),
_ => panic!("Unable to decode input address"),
};
let tx_out: TransactionOutput = TransactionOutput {
address: address_bytes,
amount,
datum_hash,
};
let mut utxos: UTxOs = UTxOs::new();
add_to_utxo(&mut utxos, tx_in, tx_out);
utxos
}
#[test]
// Transaction hash:
// 50eba65e73c8c5f7b09f4ea28cf15dce169f3d1c322ca3deff03725f51518bb2
@ -65,11 +32,13 @@ mod shelley_tests {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/shelley1.tx"));
let mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
let metx: MultiEraTx = MultiEraTx::from_alonzo_compatible(&mtx, Era::Shelley);
let utxos: UTxOs = mk_utxo_for_single_input_tx(
let utxos: UTxOs = mk_utxo_for_alonzo_compatible_tx(
&mtx.transaction_body,
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
&[(
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
)],
);
let env: Environment = Environment {
prot_params: MultiEraProtParams::Shelley(ShelleyProtParams {
@ -97,11 +66,13 @@ mod shelley_tests {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/shelley2.tx"));
let mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
let metx: MultiEraTx = MultiEraTx::from_alonzo_compatible(&mtx, Era::Shelley);
let utxos: UTxOs = mk_utxo_for_single_input_tx(
let utxos: UTxOs = mk_utxo_for_alonzo_compatible_tx(
&mtx.transaction_body,
String::from(include_str!("../../test_data/shelley2.address")),
Value::Coin(2000000),
None,
&[(
String::from(include_str!("../../test_data/shelley2.address")),
Value::Coin(2000000),
None,
)],
);
let env: Environment = Environment {
prot_params: MultiEraProtParams::Shelley(ShelleyProtParams {
@ -129,11 +100,13 @@ mod shelley_tests {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/shelley3.tx"));
let mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
let metx: MultiEraTx = MultiEraTx::from_alonzo_compatible(&mtx, Era::Shelley);
let utxos: UTxOs = mk_utxo_for_single_input_tx(
let utxos: UTxOs = mk_utxo_for_alonzo_compatible_tx(
&mtx.transaction_body,
String::from(include_str!("../../test_data/shelley3.address")),
Value::Coin(10000000),
None,
&[(
String::from(include_str!("../../test_data/shelley3.address")),
Value::Coin(10000000),
None,
)],
);
let env: Environment = Environment {
prot_params: MultiEraProtParams::Shelley(ShelleyProtParams {
@ -161,11 +134,13 @@ mod shelley_tests {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/mary1.tx"));
let mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
let metx: MultiEraTx = MultiEraTx::from_alonzo_compatible(&mtx, Era::Mary);
let utxos: UTxOs = mk_utxo_for_single_input_tx(
let utxos: UTxOs = mk_utxo_for_alonzo_compatible_tx(
&mtx.transaction_body,
String::from(include_str!("../../test_data/mary1.address")),
Value::Coin(3500000),
None,
&[(
String::from(include_str!("../../test_data/mary1.address")),
Value::Coin(3500000),
None,
)],
);
let env: Environment = Environment {
prot_params: MultiEraProtParams::Shelley(ShelleyProtParams {
@ -191,14 +166,16 @@ mod shelley_tests {
fn empty_ins() {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/shelley1.tx"));
let mut mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
let utxos: UTxOs = mk_utxo_for_single_input_tx(
let utxos: UTxOs = mk_utxo_for_alonzo_compatible_tx(
&mtx.transaction_body,
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
&[(
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
)],
);
// Clear the set of inputs in the transaction.
let mut tx_body: TransactionBody = (*mtx.transaction_body).clone();
let mut tx_body: TransactionBody = mtx.transaction_body.unwrap().clone();
tx_body.inputs = Vec::new();
let mut tx_buf: Vec<u8> = Vec::new();
match encode(tx_body, &mut tx_buf) {
@ -224,7 +201,7 @@ mod shelley_tests {
match validate(&metx, &utxos, &env) {
Ok(()) => assert!(false, "Inputs set should not be empty"),
Err(err) => match err {
Shelley(TxInsEmpty) => (),
ShelleyMA(ShelleyMAError::TxInsEmpty) => (),
_ => assert!(false, "Unexpected error ({:?})", err),
},
}
@ -253,7 +230,7 @@ mod shelley_tests {
match validate(&metx, &utxos, &env) {
Ok(()) => assert!(false, "All inputs must be within the UTxO set"),
Err(err) => match err {
Shelley(InputNotInUTxO) => (),
ShelleyMA(ShelleyMAError::InputNotInUTxO) => (),
_ => assert!(false, "Unexpected error ({:?})", err),
},
}
@ -264,13 +241,15 @@ mod shelley_tests {
fn missing_ttl() {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/shelley1.tx"));
let mut mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
let utxos: UTxOs = mk_utxo_for_single_input_tx(
let utxos: UTxOs = mk_utxo_for_alonzo_compatible_tx(
&mtx.transaction_body,
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
&[(
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
)],
);
let mut tx_body: TransactionBody = (*mtx.transaction_body).clone();
let mut tx_body: TransactionBody = mtx.transaction_body.unwrap().clone();
tx_body.ttl = None;
let mut tx_buf: Vec<u8> = Vec::new();
match encode(tx_body, &mut tx_buf) {
@ -296,7 +275,7 @@ mod shelley_tests {
match validate(&metx, &utxos, &env) {
Ok(()) => assert!(false, "TTL must always be present in Shelley transactions"),
Err(err) => match err {
Shelley(AlonzoCompNotShelley) => (),
ShelleyMA(ShelleyMAError::AlonzoCompNotShelley) => (),
_ => assert!(false, "Unexpected error ({:?})", err),
},
}
@ -308,11 +287,13 @@ mod shelley_tests {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/shelley1.tx"));
let mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
let metx: MultiEraTx = MultiEraTx::from_alonzo_compatible(&mtx, Era::Shelley);
let utxos: UTxOs = mk_utxo_for_single_input_tx(
let utxos: UTxOs = mk_utxo_for_alonzo_compatible_tx(
&mtx.transaction_body,
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
&[(
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
)],
);
let env: Environment = Environment {
prot_params: MultiEraProtParams::Shelley(ShelleyProtParams {
@ -330,7 +311,7 @@ mod shelley_tests {
match validate(&metx, &utxos, &env) {
Ok(()) => assert!(false, "TTL cannot be exceeded"),
Err(err) => match err {
Shelley(TTLExceeded) => (),
ShelleyMA(ShelleyMAError::TTLExceeded) => (),
_ => assert!(false, "Unexpected error ({:?})", err),
},
}
@ -342,11 +323,13 @@ mod shelley_tests {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/shelley1.tx"));
let mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
let metx: MultiEraTx = MultiEraTx::from_alonzo_compatible(&mtx, Era::Shelley);
let utxos: UTxOs = mk_utxo_for_single_input_tx(
let utxos: UTxOs = mk_utxo_for_alonzo_compatible_tx(
&mtx.transaction_body,
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
&[(
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
)],
);
let env: Environment = Environment {
prot_params: MultiEraProtParams::Shelley(ShelleyProtParams {
@ -364,7 +347,7 @@ mod shelley_tests {
match validate(&metx, &utxos, &env) {
Ok(()) => assert!(false, "Tx size exceeds max limit"),
Err(err) => match err {
Shelley(MaxTxSizeExceeded) => (),
ShelleyMA(ShelleyMAError::MaxTxSizeExceeded) => (),
_ => assert!(false, "Unexpected error ({:?})", err),
},
}
@ -377,11 +360,13 @@ mod shelley_tests {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/shelley1.tx"));
let mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
let metx: MultiEraTx = MultiEraTx::from_alonzo_compatible(&mtx, Era::Shelley);
let utxos: UTxOs = mk_utxo_for_single_input_tx(
let utxos: UTxOs = mk_utxo_for_alonzo_compatible_tx(
&mtx.transaction_body,
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
&[(
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
)],
);
let env: Environment = Environment {
prot_params: MultiEraProtParams::Shelley(ShelleyProtParams {
@ -399,7 +384,7 @@ mod shelley_tests {
match validate(&metx, &utxos, &env) {
Ok(()) => assert!(false, "Output amount must be above min lovelace value"),
Err(err) => match err {
Shelley(MinLovelaceUnreached) => (),
ShelleyMA(ShelleyMAError::MinLovelaceUnreached) => (),
_ => assert!(false, "Unexpected error ({:?})", err),
},
}
@ -411,7 +396,7 @@ mod shelley_tests {
fn preservation_of_value() {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/shelley1.tx"));
let mut mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
let mut tx_body: TransactionBody = (*mtx.transaction_body).clone();
let mut tx_body: TransactionBody = mtx.transaction_body.unwrap().clone();
tx_body.fee = tx_body.fee - 1;
let mut tx_buf: Vec<u8> = Vec::new();
match encode(tx_body, &mut tx_buf) {
@ -421,11 +406,13 @@ mod shelley_tests {
mtx.transaction_body =
Decode::decode(&mut Decoder::new(&tx_buf.as_slice()), &mut ()).unwrap();
let metx: MultiEraTx = MultiEraTx::from_alonzo_compatible(&mtx, Era::Shelley);
let utxos: UTxOs = mk_utxo_for_single_input_tx(
let utxos: UTxOs = mk_utxo_for_alonzo_compatible_tx(
&mtx.transaction_body,
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
&[(
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
)],
);
let env: Environment = Environment {
prot_params: MultiEraProtParams::Shelley(ShelleyProtParams {
@ -443,7 +430,7 @@ mod shelley_tests {
match validate(&metx, &utxos, &env) {
Ok(()) => assert!(false, "Preservation of value property doesn't hold"),
Err(err) => match err {
Shelley(PreservationOfValue) => (),
ShelleyMA(ShelleyMAError::PreservationOfValue) => (),
_ => assert!(false, "Unexpected error ({:?})", err),
},
}
@ -455,11 +442,13 @@ mod shelley_tests {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/shelley1.tx"));
let mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
let metx: MultiEraTx = MultiEraTx::from_alonzo_compatible(&mtx, Era::Shelley);
let utxos: UTxOs = mk_utxo_for_single_input_tx(
let utxos: UTxOs = mk_utxo_for_alonzo_compatible_tx(
&mtx.transaction_body,
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
&[(
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
)],
);
let env: Environment = Environment {
prot_params: MultiEraProtParams::Shelley(ShelleyProtParams {
@ -477,7 +466,7 @@ mod shelley_tests {
match validate(&metx, &utxos, &env) {
Ok(()) => assert!(false, "Fee should not be below minimum"),
Err(err) => match err {
Shelley(FeesBelowMin) => (),
ShelleyMA(ShelleyMAError::FeesBelowMin) => (),
_ => assert!(false, "Unexpected error ({:?})", err),
},
}
@ -490,7 +479,7 @@ mod shelley_tests {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/shelley1.tx"));
let mut mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
// Modify the first output address.
let mut tx_body: TransactionBody = (*mtx.transaction_body).clone();
let mut tx_body: TransactionBody = mtx.transaction_body.unwrap().clone();
let (first_output, rest): (&TransactionOutput, &[TransactionOutput]) =
(&tx_body.outputs).split_first().unwrap();
let addr: ShelleyAddress =
@ -533,33 +522,39 @@ mod shelley_tests {
block_slot: 5281340,
network_id: 1,
};
let utxos: UTxOs = mk_utxo_for_single_input_tx(
let utxos: UTxOs = mk_utxo_for_alonzo_compatible_tx(
&mtx.transaction_body,
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
&[(
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
)],
);
match validate(&metx, &utxos, &env) {
Ok(()) => assert!(false, "Output with wrong network ID should be rejected"),
Err(err) => match err {
Shelley(WrongNetworkID) => (),
ShelleyMA(ShelleyMAError::WrongNetworkID) => (),
_ => assert!(false, "Unexpected error ({:?})", err),
},
}
}
#[test]
// Like successful_mainnet_shelley_tx_with_metadata (hash:
// c220e20cc480df9ce7cd871df491d7390c6a004b9252cf20f45fc3c968535b4a)
// Same as successful_mainnet_shelley_tx_with_metadata (hash:
// c220e20cc480df9ce7cd871df491d7390c6a004b9252cf20f45fc3c968535b4a), except
// that the AuxiliaryData is removed.
fn auxiliary_data_removed() {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/shelley3.tx"));
let mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
let mut mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
mtx.auxiliary_data = Nullable::Null;
let metx: MultiEraTx = MultiEraTx::from_alonzo_compatible(&mtx, Era::Shelley);
let utxos: UTxOs = mk_utxo_for_single_input_tx(
let utxos: UTxOs = mk_utxo_for_alonzo_compatible_tx(
&mtx.transaction_body,
String::from(include_str!("../../test_data/shelley3.address")),
Value::Coin(10000000),
None,
&[(
String::from(include_str!("../../test_data/shelley3.address")),
Value::Coin(10000000),
None,
)],
);
let env: Environment = Environment {
prot_params: MultiEraProtParams::Shelley(ShelleyProtParams {
@ -575,8 +570,11 @@ mod shelley_tests {
network_id: 1,
};
match validate(&metx, &utxos, &env) {
Ok(()) => (),
Err(err) => assert!(false, "Unexpected error ({:?})", err),
Ok(()) => assert!(false, "Output with wrong network ID should be rejected"),
Err(err) => match err {
ShelleyMA(ShelleyMAError::MetadataHash) => (),
_ => assert!(false, "Unexpected error ({:?})", err),
},
}
}
@ -588,7 +586,7 @@ mod shelley_tests {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/shelley1.tx"));
let mut mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
// Modify the first output address.
let mut tx_wits: MintedWitnessSet = (*mtx.transaction_witness_set).clone();
let mut tx_wits: MintedWitnessSet = mtx.transaction_witness_set.unwrap().clone();
tx_wits.vkeywitness = Some(Vec::new());
let mut tx_buf: Vec<u8> = Vec::new();
match encode(tx_wits, &mut tx_buf) {
@ -611,16 +609,18 @@ mod shelley_tests {
block_slot: 5281340,
network_id: 1,
};
let utxos: UTxOs = mk_utxo_for_single_input_tx(
let utxos: UTxOs = mk_utxo_for_alonzo_compatible_tx(
&mtx.transaction_body,
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
&[(
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
)],
);
match validate(&metx, &utxos, &env) {
Ok(()) => assert!(false, "Missing verification key witness"),
Err(err) => match err {
Shelley(MissingVKWitness) => (),
ShelleyMA(ShelleyMAError::MissingVKWitness) => (),
_ => assert!(false, "Unexpected error ({:?})", err),
},
}
@ -634,7 +634,7 @@ mod shelley_tests {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/shelley1.tx"));
let mut mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
// Modify the first output address.
let mut tx_wits: MintedWitnessSet = (*mtx.transaction_witness_set).clone();
let mut tx_wits: MintedWitnessSet = mtx.transaction_witness_set.unwrap().clone();
let mut wit: VKeyWitness = tx_wits.vkeywitness.clone().unwrap().pop().unwrap();
let mut sig_as_vec: Vec<u8> = wit.signature.to_vec();
sig_as_vec.pop();
@ -662,16 +662,18 @@ mod shelley_tests {
block_slot: 5281340,
network_id: 1,
};
let utxos: UTxOs = mk_utxo_for_single_input_tx(
let utxos: UTxOs = mk_utxo_for_alonzo_compatible_tx(
&mtx.transaction_body,
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
&[(
String::from(include_str!("../../test_data/shelley1.address")),
Value::Coin(2332267427205),
None,
)],
);
match validate(&metx, &utxos, &env) {
Ok(()) => assert!(false, "Missing verification key witness"),
Err(err) => match err {
Shelley(WrongSignature) => (),
ShelleyMA(ShelleyMAError::WrongSignature) => (),
_ => assert!(false, "Unexpected error ({:?})", err),
},
}
@ -685,7 +687,7 @@ mod shelley_tests {
let cbor_bytes: Vec<u8> = cbor_to_bytes(include_str!("../../test_data/shelley2.tx"));
let mut mtx: MintedTx = minted_tx_from_cbor(&cbor_bytes);
// Modify the first output address.
let mut tx_wits: MintedWitnessSet = (*mtx.transaction_witness_set).clone();
let mut tx_wits: MintedWitnessSet = mtx.transaction_witness_set.unwrap().clone();
tx_wits.native_script = Some(Vec::new());
let mut tx_buf: Vec<u8> = Vec::new();
match encode(tx_wits, &mut tx_buf) {
@ -708,26 +710,20 @@ mod shelley_tests {
block_slot: 5281340,
network_id: 1,
};
let utxos: UTxOs = mk_utxo_for_single_input_tx(
let utxos: UTxOs = mk_utxo_for_alonzo_compatible_tx(
&mtx.transaction_body,
String::from(include_str!("../../test_data/shelley2.address")),
Value::Coin(2000000),
None,
&[(
String::from(include_str!("../../test_data/shelley2.address")),
Value::Coin(2000000),
None,
)],
);
match validate(&metx, &utxos, &env) {
Ok(()) => assert!(false, "Missing native script witness"),
Err(err) => match err {
Shelley(MissingScriptWitness) => (),
ShelleyMA(ShelleyMAError::MissingScriptWitness) => (),
_ => assert!(false, "Unexpected error ({:?})", err),
},
}
}
}
// Helper functions.
fn add_to_utxo<'a>(utxos: &mut UTxOs<'a>, tx_in: TransactionInput, tx_out: TransactionOutput) {
let multi_era_in: MultiEraInput = MultiEraInput::AlonzoCompatible(Box::new(Cow::Owned(tx_in)));
let multi_era_out: MultiEraOutput =
MultiEraOutput::AlonzoCompatible(Box::new(Cow::Owned(tx_out)));
utxos.insert(multi_era_in, multi_era_out);
}