Skip to content

Commit

Permalink
costmodel should not be hardcoded
Browse files Browse the repository at this point in the history
  • Loading branch information
kalomaaan authored and scarmuega committed Oct 20, 2024
1 parent 65b6c39 commit ae4e800
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pallas-txbuilder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod transaction;

pub use babbage::BuildBabbage;
pub use transaction::model::{
BuiltTransaction, ExUnits, Input, Output, ScriptKind, StagingTransaction, PlutusVersion,
BuiltTransaction, ExUnits, Input, Output, ScriptKind, StagingTransaction,
};

#[derive(Debug, Clone, PartialEq, thiserror::Error)]
Expand Down
39 changes: 17 additions & 22 deletions pallas-txbuilder/src/transaction/model.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use pallas_addresses::Address as PallasAddress;
use pallas_codec::minicbor;
use pallas_crypto::{
hash::{Hash, Hasher},
key::ed25519,
};
use pallas_primitives::{babbage, Fragment, alonzo::{Redeemer, PlutusData}};
use pallas_primitives::{alonzo::{PlutusData, Redeemer}, babbage, conway::CostMdls, Fragment};
use pallas_wallet::PrivateKey;

use std::{collections::HashMap, ops::Deref};
Expand Down Expand Up @@ -340,11 +341,8 @@ impl StagingTransaction {
self
}

pub fn script_data_hash(mut self, redeemers: Vec<Redeemer>,datums: Option<Vec<PlutusData>>, version: PlutusVersion) -> Self{
let plutus_v1_costmodel: Vec<u8> = vec![161, 65, 0, 89, 1, 166, 159, 26, 0, 1, 137, 180, 25, 1, 164, 1, 1, 25, 3, 232, 24, 173, 0, 1, 25, 3, 232, 25, 234, 53, 4, 1, 25, 43, 175, 24, 32, 26, 0, 3, 18, 89, 25, 32, 164, 4, 25, 62, 128, 24, 100, 25, 62, 128, 24, 100, 25, 62, 128, 24, 100, 25, 62, 128, 24, 100, 25, 62, 128, 24, 100, 25, 62, 128, 24, 100, 24, 100, 24, 100, 25, 62, 128, 24, 100, 26, 0, 1, 112, 167, 24, 32, 26, 0, 2, 7, 130, 24, 32, 25, 240, 22, 4, 26, 0, 1, 25, 74, 24, 178, 0, 1, 25, 86, 135, 24, 32, 26, 0, 1, 100, 53, 25, 3, 1, 4, 2, 26, 0, 1, 79, 88, 26, 0, 3, 124, 113, 24, 122, 0, 1, 1, 25, 3, 232, 25, 167, 169, 4, 2, 25, 95, 228, 25, 115, 58, 24, 38, 1, 26, 0, 13, 180, 100, 25, 106, 143, 1, 25, 202, 63, 25, 2, 46, 1, 25, 153, 16, 25, 3, 232, 25, 236, 178, 1, 26, 0, 2, 42, 71, 24, 32, 26, 0, 1, 68, 206, 24, 32, 25, 59, 195, 24, 32, 26, 0, 1, 41, 17, 1, 25, 51, 113, 4, 25, 86, 84, 10, 25, 113, 71, 24, 74, 1, 25, 113, 71, 24, 74, 1, 25, 169, 21, 25, 2, 40, 1, 25, 174, 205, 25, 2, 29, 1, 25, 132, 60, 24, 32, 26, 0, 1, 10, 150, 24, 32, 26, 0, 1, 26, 170, 24, 32, 25, 28, 75, 24, 32, 25, 28, 223, 24, 32, 25, 45, 26, 24, 32, 26, 0, 1, 79, 88, 26, 0, 3, 124, 113, 24, 122, 0, 1, 1, 26, 0, 1, 97, 66, 25, 2, 7, 0, 1, 26, 0, 1, 34, 193, 24, 32, 26, 0, 1, 79, 88, 26, 0, 3, 124, 113, 24, 122, 0, 1, 1, 26, 0, 1, 79, 88, 26, 0, 3, 124, 113, 24, 122, 0, 1, 1, 26, 0, 4, 33, 60, 25, 88, 60, 4, 26, 0, 22, 60, 173, 25, 252, 54, 4, 25, 79, 243, 1, 4, 0, 26, 0, 2, 42, 168, 24, 32, 26, 0, 1, 137, 180, 25, 1, 164, 1, 1, 26, 0, 1, 62, 255, 24, 32, 25, 232, 106, 24, 32, 25, 78, 174, 24, 32, 25, 96, 12, 24, 32, 25, 81, 8, 24, 32, 25, 101, 77, 24, 32, 25, 96, 47, 24, 32, 26, 3, 46, 147, 175, 25, 55, 253, 10, 255];
let plutus_v2_costmodel: Vec<u8> = vec![161, 1, 152, 175, 26, 0, 1, 137, 180, 25, 1, 164, 1, 1, 25, 3, 232, 24, 173, 0, 1, 25, 3, 232, 25, 234, 53, 4, 1, 25, 43, 175, 24, 32, 26, 0, 3, 18, 89, 25, 32, 164, 4, 25, 62, 128, 24, 100, 25, 62, 128, 24, 100, 25, 62, 128, 24, 100, 25, 62, 128, 24, 100, 25, 62, 128, 24, 100, 25, 62, 128, 24, 100, 24, 100, 24, 100, 25, 62, 128, 24, 100, 26, 0, 1, 112, 167, 24, 32, 26, 0, 2, 7, 130, 24, 32, 25, 240, 22, 4, 26, 0, 1, 25, 74, 24, 178, 0, 1, 25, 86, 135, 24, 32, 26, 0, 1, 100, 53, 25, 3, 1, 4, 2, 26, 0, 1, 79, 88, 26, 0, 3, 124, 113, 24, 122, 0, 1, 1, 25, 3, 232, 25, 167, 169, 4, 2, 25, 95, 228, 25, 115, 58, 24, 38, 1, 26, 0, 13, 180, 100, 25, 106, 143, 1, 25, 202, 63, 25, 2, 46, 1, 25, 153, 16, 25, 3, 232, 25, 236, 178, 1, 26, 0, 2, 42, 71, 24, 32, 26, 0, 1, 68, 206, 24, 32, 25, 59, 195, 24, 32, 26, 0, 1, 41, 17, 1, 25, 51, 113, 4, 25, 86, 84, 10, 25, 113, 71, 24, 74, 1, 25, 113, 71, 24, 74, 1, 25, 169, 21, 25, 2, 40, 1, 25, 174, 205, 25, 2, 29, 1, 25, 132, 60, 24, 32, 26, 0, 1, 10, 150, 24, 32, 26, 0, 1, 26, 170, 24, 32, 25, 28, 75, 24, 32, 25, 28, 223, 24, 32, 25, 45, 26, 24, 32, 26, 0, 1, 79, 88, 26, 0, 3, 124, 113, 24, 122, 0, 1, 1, 26, 0, 1, 97, 66, 25, 2, 7, 0, 1, 26, 0, 1, 34, 193, 24, 32, 26, 0, 1, 79, 88, 26, 0, 3, 124, 113, 24, 122, 0, 1, 1, 26, 0, 1, 79, 88, 26, 0, 3, 124, 113, 24, 122, 0, 1, 1, 26, 0, 14, 148, 114, 26, 0, 3, 65, 64, 0, 2, 26, 0, 4, 33, 60, 25, 88, 60, 4, 26, 0, 22, 60, 173, 25, 252, 54, 4, 25, 79, 243, 1, 4, 0, 26, 0, 2, 42, 168, 24, 32, 26, 0, 1, 137, 180, 25, 1, 164, 1, 1, 26, 0, 1, 62, 255, 24, 32, 25, 232, 106, 24, 32, 25, 78, 174, 24, 32, 25, 96, 12, 24, 32, 25, 81, 8, 24, 32, 25, 101, 77, 24, 32, 25, 96, 47, 24, 32, 26, 2, 144, 241, 231, 10, 26, 3, 46, 147, 175, 25, 55, 253, 10, 26, 2, 152, 228, 11, 25, 102, 196, 10];
let plutus_v3_costmodel: Vec<u8> = vec![161, 1, 152, 251, 26, 0, 1, 137, 180, 25, 1, 164, 1, 1, 25, 3, 232, 24, 173, 0, 1, 25, 3, 232, 25, 234, 53, 4, 1, 25, 43, 175, 24, 32, 26, 0, 3, 18, 89, 25, 32, 164, 4, 25, 62, 128, 24, 100, 25, 62, 128, 24, 100, 25, 62, 128, 24, 100, 25, 62, 128, 24, 100, 25, 62, 128, 24, 100, 25, 62, 128, 24, 100, 24, 100, 24, 100, 25, 62, 128, 24, 100, 26, 0, 1, 112, 167, 24, 32, 26, 0, 2, 7, 130, 24, 32, 25, 240, 22, 4, 26, 0, 1, 25, 74, 24, 178, 0, 1, 25, 86, 135, 24, 32, 26, 0, 1, 100, 53, 25, 3, 1, 4, 2, 26, 0, 1, 79, 88, 26, 0, 1, 225, 67, 25, 28, 137, 57, 3, 131, 25, 6, 180, 25, 2, 37, 24, 57, 26, 0, 1, 79, 88, 0, 1, 1, 25, 3, 232, 25, 167, 169, 4, 2, 25, 95, 228, 25, 115, 58, 24, 38, 1, 26, 0, 13, 180, 100, 25, 106, 143, 1, 25, 202, 63, 25, 2, 46, 1, 25, 153, 16, 25, 3, 232, 25, 236, 178, 1, 26, 0, 2, 42, 71, 24, 32, 26, 0, 1, 68, 206, 24, 32, 25, 59, 195, 24, 32, 26, 0, 1, 41, 17, 1, 25, 51, 113, 4, 25, 86, 84, 10, 25, 113, 71, 24, 74, 1, 25, 113, 71, 24, 74, 1, 25, 169, 21, 25, 2, 40, 1, 25, 174, 205, 25, 2, 29, 1, 25, 132, 60, 24, 32, 26, 0, 1, 10, 150, 24, 32, 26, 0, 1, 26, 170, 24, 32, 25, 28, 75, 24, 32, 25, 28, 223, 24, 32, 25, 45, 26, 24, 32, 26, 0, 1, 79, 88, 26, 0, 1, 225, 67, 25, 28, 137, 57, 3, 131, 25, 6, 180, 25, 2, 37, 24, 57, 26, 0, 1, 79, 88, 0, 1, 26, 0, 1, 97, 66, 25, 2, 7, 0, 1, 26, 0, 1, 34, 193, 24, 32, 26, 0, 1, 79, 88, 26, 0, 1, 225, 67, 25, 28, 137, 57, 3, 131, 25, 6, 180, 25, 2, 37, 24, 57, 26, 0, 1, 79, 88, 0, 1, 1, 26, 0, 1, 79, 88, 26, 0, 1, 225, 67, 25, 28, 137, 57, 3, 131, 25, 6, 180, 25, 2, 37, 24, 57, 26, 0, 1, 79, 88, 0, 1, 26, 0, 14, 148, 114, 26, 0, 3, 65, 64, 0, 2, 26, 0, 4, 33, 60, 25, 88, 60, 4, 26, 0, 22, 60, 173, 25, 252, 54, 4, 25, 79, 243, 1, 4, 0, 26, 0, 2, 42, 168, 24, 32, 26, 0, 1, 137, 180, 25, 1, 164, 1, 1, 26, 0, 1, 62, 255, 24, 32, 25, 232, 106, 24, 32, 25, 78, 174, 24, 32, 25, 96, 12, 24, 32, 25, 81, 8, 24, 32, 25, 101, 77, 24, 32, 25, 96, 47, 24, 32, 26, 2, 144, 241, 231, 10, 26, 3, 46, 147, 175, 25, 55, 253, 10, 26, 2, 152, 228, 11, 25, 102, 196, 10, 25, 62, 128, 24, 100, 25, 62, 128, 24, 100, 26, 0, 14, 175, 31, 18, 26, 0, 42, 110, 6, 6, 26, 0, 6, 190, 152, 1, 26, 3, 33, 170, 199, 25, 14, 172, 18, 26, 0, 4, 22, 153, 18, 26, 4, 142, 70, 110, 25, 34, 164, 18, 26, 3, 39, 236, 154, 18, 26, 0, 30, 116, 60, 24, 36, 26, 0, 49, 65, 15, 12, 26, 0, 13, 191, 158, 1, 26, 9, 242, 246, 211, 25, 16, 211, 24, 36, 26, 0, 4, 87, 130, 24, 36, 26, 9, 110, 68, 2, 25, 103, 181, 24, 36, 26, 4, 115, 206, 232, 24, 36, 26, 19, 230, 36, 114, 1, 26, 15, 35, 212, 1, 24, 72, 26, 0, 33, 44, 86, 24, 72, 26, 0, 34, 129, 70, 25, 252, 59, 4, 26, 0, 3, 43, 0, 25, 32, 118, 4, 26, 0, 19, 190, 4, 25, 112, 44, 24, 63, 0, 1, 26, 0, 15, 89, 217, 25, 170, 103, 24, 251, 0, 1];

pub fn script_data_hash(mut self, redeemers: Vec<Redeemer>,datums: Option<Vec<PlutusData>>, costmodel: CostMdls) -> Self{

let mut buf = Vec::new();
if redeemers.len() == 0 && datums.is_some() {
buf.push(0xA0);
Expand All @@ -362,16 +360,19 @@ impl StagingTransaction {
buf.extend(datum_bytes[0].clone());
}

match version {
PlutusVersion::PlutusV1 => {
buf.extend(plutus_v1_costmodel);
}
PlutusVersion::PlutusV2 => {
buf.extend(plutus_v2_costmodel);
}
PlutusVersion::Plutusv3 => {
buf.extend(plutus_v3_costmodel);
}
let mut language_view = vec![];
if let Some(plutus_v1_model) = &costmodel.plutus_v1 {
let plutusv1 = [(0, plutus_v1_model.to_vec())];
minicbor::encode(plutusv1, &mut language_view).unwrap();
buf.extend(language_view);
} else if let Some(plutus_v2_model) = &costmodel.plutus_v2 {
let plutusv2 = [(1, plutus_v2_model.to_vec())];
minicbor::encode(plutusv2, &mut language_view).unwrap();
buf.extend(language_view);
} else if let Some(plutus_v3_model) = &costmodel.plutus_v3 {
let plutusv3 = [(2, plutus_v3_model.to_vec())];
minicbor::encode(plutusv3, &mut language_view).unwrap();
buf.extend(language_view);
}
}
let hash = Hasher::<256>::hash(buf.as_ref());
Expand Down Expand Up @@ -549,12 +550,6 @@ pub enum ScriptKind {
PlutusV2,
}

pub enum PlutusVersion {
PlutusV1,
PlutusV2,
Plutusv3,
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub struct Script {
pub kind: ScriptKind,
Expand Down

0 comments on commit ae4e800

Please sign in to comment.