Skip to content

Commit

Permalink
fixed emulator
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrokonrad committed Jan 19, 2025
1 parent 0d63e2f commit 118e355
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
Binary file modified src/core/libs/lucid_core/pkg/lucid_core_bg.wasm
Binary file not shown.
59 changes: 52 additions & 7 deletions src/core/libs/lucid_core/src/emulator_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl EmulatorState {
}
}

pub fn validate(&mut self, tx: String) -> CoreResult<String> {
pub fn validate(&mut self, tx: &str) -> CoreResult<String> {
let decoded_tx = hex::decode(tx).map_err(CoreError::msg)?;
let tx: Tx = MintedTx::decode_fragment(&decoded_tx)
.map_err(CoreError::msg)?
Expand Down Expand Up @@ -298,8 +298,7 @@ impl EmulatorState {
};
match &entry.utxo.datum_hash {
Some(datum_hash) => {
consumed_hashes
.borrow_mut()
consumed_datum_hashes
.insert(datum_hash.parse().map_err(CoreError::msg)?);
}
_ => {}
Expand Down Expand Up @@ -356,9 +355,7 @@ impl EmulatorState {
};
match &entry.utxo.datum_hash {
Some(datum_hash) => {
consumed_hashes
.borrow_mut()
.insert(datum_hash.parse().map_err(CoreError::msg)?);
consumed_datum_hashes.insert(datum_hash.parse().map_err(CoreError::msg)?);
}
_ => {}
}
Expand Down Expand Up @@ -1013,6 +1010,7 @@ mod tests {
instruction_builder::{
Change, Instruction, InstructionBuilder, Instructions, RelevantProtocolParameters,
},
instruction_signer::InstructionSigner,
};

fn setup_builder(selection: Vec<Utxo>, change_address: &str) -> InstructionBuilder {
Expand Down Expand Up @@ -1121,8 +1119,55 @@ mod tests {
.sign_with_key(&private_key)?
.commit();

assert!(state.validate(tx.clone()).is_ok());
assert!(state.validate(&tx).is_ok());

Ok(())
}

#[test]
fn test_spending_plutus_v1_script() -> CoreResult<()> {
let private_key = Crypto::generate_private_key();
let address = Addresses::credential_to_address(
Network::Preprod,
Crypto::private_key_to_details(&private_key)?.credential,
None,
)?;

let start_utxos = vec![
Utxo {
tx_hash: "966e4223a4bf5f0b31e4676668b52b276a0f9af39eed03ff88c654979c0f2622"
.to_string(),
output_index: 1,
assets: Assets::from_lovelace(20000000),
address: address.to_string(),
datum_hash: None,
datum: None,
script_ref: None,
},
Utxo {
tx_hash: "966e4223a4bf5f0b31e4676668b52b276a0f9af39eed03ff88c654979c0f2622"
.to_string(),
output_index: 0,
assets: Assets::from_lovelace(20000000),
address: "addr_test1wz8jmzsx9uh2pgcxj7za36jeln7sprheumhkd3srnytfacg6cgclw"
.to_string(),
datum_hash: Some(
"613baf6bfa3607ffc2d721491eb3e406d46d2168c2268388b31ef6acaace1c41".to_string(),
),
datum: None,
script_ref: None,
},
];

let mut state = EmulatorState::new(0., Some(Utxos(start_utxos.clone())));

let tx = "84A700D9010281825820966E4223A4BF5F0B31E4676668B52B276A0F9AF39EED03FF88C654979C0F2622000181A2005839007A10CEB1B9517F511F392F8C9021CF940F30BCCD7DF2745C272AC13B7BFB7BCDA91F6D2C84BFDB558F63E8F22FF492295847394D295DC319011A0019B42E021A0004D0520B5820E9EB787EB922C2BAAAE3588EAA4A001612FBC14E4DB662AA7573A70D50B1AB760DD9010281825820966E4223A4BF5F0B31E4676668B52B276A0F9AF39EED03FF88C654979C0F26220110A2005839007A10CEB1B9517F511F392F8C9021CF940F30BCCD7DF2745C272AC13B7BFB7BCDA91F6D2C84BFDB558F63E8F22FF492295847394D295DC319011A05CD89C4111A0007387BA303D901028159099A59099701000033233223322323233322232333222323333333322222222323332223233332222323233223233322232333222323233223322323233333222223322332233223322332233222222323253353031333006375A00A6EB4010CCCD5CD19B8735573AA004900011980499191919191919191919191999AB9A3370E6AAE754029200023333333333017335025232323333573466E1CD55CEA8012400046603A60706AE854008C0A8D5D09ABA250022350573530583357389201035054310005949926135573CA00226EA8004D5D0A80519A8128131ABA150093335502C75CA0566AE854020CCD540B1D728159ABA1500733502504135742A00C66A04A66AA0A4094EB4D5D0A8029919191999AB9A3370E6AAE7540092000233501F3232323333573466E1CD55CEA80124000466A04E66A080EB4D5D0A80118229ABA135744A00446A0B66A60B866AE712401035054310005D49926135573CA00226EA8004D5D0A8011919191999AB9A3370E6AAE7540092000233502533504075A6AE854008C114D5D09ABA2500223505B35305C3357389201035054310005D49926135573CA00226EA8004D5D09ABA250022350573530583357389201035054310005949926135573CA00226EA8004D5D0A80219A812BAE35742A00666A04A66AA0A4EB88004D5D0A801181B9ABA135744A00446A0A66A60A866AE71241035054310005549926135744A00226AE8940044D5D1280089ABA25001135744A00226AE8940044D5D1280089ABA25001135573CA00226EA8004D5D0A8011919191999AB9A3370EA00290031180E181C9ABA135573CA00646666AE68CDC3A801240084603660866AE84D55CF280211999AB9A3370EA00690011180D98171ABA135573CA00A46666AE68CDC3A802240004603C6EB8D5D09AAB9E500623504E35304F3357389201035054310005049926499264984D55CEA80089BAA001357426AE8940088D411CD4C120CD5CE2490350543100049499261048135046353047335738920103505435000484984D55CF280089BAA0012212330010030022001222222222212333333333300100B00A00900800700600500400300220012212330010030022001122123300100300212001122123300100300212001122123300100300212001212222300400521222230030052122223002005212222300100520011232230023758002640026AA068446666AAE7C004940388CD4034C010D5D080118019ABA200203323232323333573466E1CD55CEA801A4000466600E6464646666AE68CDC39AAB9D5002480008CC034C0C4D5D0A80119A8098169ABA135744A00446A06C6A606E66AE712401035054310003849926135573CA00226EA8004D5D0A801999AA805BAE500A35742A00466A01EEB8D5D09ABA25002235032353033335738921035054310003449926135744A00226AAE7940044DD50009110919980080200180110009109198008018011000899AA800BAE75A224464460046EAC004C8004D540B888C8CCCD55CF80112804919A80419AA81898031AAB9D5002300535573CA00460086AE8800C0B84D5D08008891001091091198008020018900089119191999AB9A3370EA002900011A80418029ABA135573CA00646666AE68CDC3A801240044A01046A0526A605466AE712401035054310002B499264984D55CEA80089BAA001121223002003112200112001232323333573466E1CD55CEA8012400046600C600E6AE854008DD69ABA135744A00446A0466A604866AE71241035054310002549926135573CA00226EA80048848CC00400C00880048C8CCCD5CD19B8735573AA002900011BAE357426AAE7940088D407CD4C080CD5CE24810350543100021499261375400224464646666AE68CDC3A800A40084A00E46666AE68CDC3A8012400446A014600C6AE84D55CF280211999AB9A3370EA00690001280511A8111A981199AB9C490103505431000244992649926135573AA00226EA8004484888C00C0104488800844888004480048C8CCCD5CD19B8750014800880188CCCD5CD19B8750024800080188D4068D4C06CCD5CE249035054310001C499264984D55CE9BAA0011220021220012001232323232323333573466E1D4005200C200B23333573466E1D4009200A200D23333573466E1D400D200823300B375C6AE854014DD69ABA135744A00A46666AE68CDC3A8022400C46601A6EB8D5D0A8039BAE357426AE89401C8CCCD5CD19B875005480108CC048C050D5D0A8049BAE357426AE8940248CCCD5CD19B875006480088C050C054D5D09AAB9E500B23333573466E1D401D2000230133016357426AAE7940308D407CD4C080CD5CE2481035054310002149926499264992649926135573AA00826AAE79400C4D55CF280109AAB9E500113754002424444444600E01044244444446600C012010424444444600A010244444440082444444400644244444446600401201044244444446600201201040024646464646666AE68CDC3A800A400446660106EB4D5D0A8021BAD35742A0066EB4D5D09ABA2500323333573466E1D400920002300A300B357426AAE7940188D4040D4C044CD5CE2490350543100012499264984D55CEA80189ABA25001135573CA00226EA80048488C00800C888488CCC00401401000C80048C8C8CCCD5CD19B875001480088C018DD71ABA135573CA00646666AE68CDC3A80124000460106EB8D5D09AAB9E500423500A35300B3357389201035054310000C499264984D55CEA80089BAA001212230020032122300100320011122232323333573466E1CD55CEA80124000466AA016600C6AE854008C014D5D09ABA25002235007353008335738921035054310000949926135573CA00226EA8004498480048004448848CC00400C008448004448C8C00400488CC00CC008008004CCC888CCC888CCCCCCCC88888888CC88CCCCC88888CCCC8888CCC888CC88CC88CC88CCC888CC88CC88CCC888CC88CC88CC88CC88888CCD5CD19B8700300201E01D2212330010030022001222222222212333333333300100B00A0090080070060050040030022001221233001003002200122212333001004003002200111220021221223300100400312001112212330010030021120012212330010030022001121223002003112200112001122123300100300212001122123300100300212001122123300100300212001122002122001200112122230030041122200211222001120012122223004005212222300300521222230020052122223001005200122123300100300220012122222223007008221222222233006009008212222222300500812222222004122222220032212222222330020090082212222222330010090082001212230020032221223330010050040032001212230020032122300100320010104D9010281182105A1820000821821821A00067EC21A07AF27B2F5F6";

let mut tx = InstructionSigner::from_tx(tx, Some(Utxos(start_utxos)))?;

let signed_tx = tx.sign_with_key(&private_key)?.commit();

state.validate(&signed_tx).unwrap();
Ok(())
}
}
6 changes: 3 additions & 3 deletions tests/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ Deno.test("Construct plutus data", () => {
new Constr(1, [BigInt(1), "abcd", "deff", new Constr(0, [])]),
);

assertEquals(data, "d87a840142abcd42deffd87980");
assertEquals(data, "d87a9f0142abcd42deffd87980ff");
// == 122([1, h'ABCD', h'DEFF', 121([])])
});

Deno.test("Deserialize plutus data", () => {
const data = "d87a840141ab41ded87980";
const data = "d87a9f0141ab41ded87980ff";
const desData = Data.from(data);
assertEquals(data, Data.to(desData));
});
Expand Down Expand Up @@ -214,7 +214,7 @@ Deno.test("json datum to cbor datum", () => {
constructor: 0,
};
const cborDatum =
"d87981d87985581cbdd5402a608267d8d47e021a61e5c1ae6aa62a1f770579aa38b88143a140a1401a02ebae4082d87982581cbdd5402a608267d8d47e021a61e5c1ae6aa62a1f770579aa38b88143a140a1401a02ebae40d87982581c6900ecb87083dbbe74a65b1036186bc9c12df2878842d936902f0b51a140a1401a000b37181b00000180f1db168b581cf4a4a183be0b0da6e7a7548d1b26f2191b1ab7b2d20ac1c7d97b681c";
"d8799fd8799f581cbdd5402a608267d8d47e021a61e5c1ae6aa62a1f770579aa38b88143a140a1401a02ebae409fd8799f581cbdd5402a608267d8d47e021a61e5c1ae6aa62a1f770579aa38b88143a140a1401a02ebae40ffd8799f581c6900ecb87083dbbe74a65b1036186bc9c12df2878842d936902f0b51a140a1401a000b3718ffff1b00000180f1db168b581cf4a4a183be0b0da6e7a7548d1b26f2191b1ab7b2d20ac1c7d97b681cffff";
assertEquals(cborDatum, Codec.encodeData(jsonDatum as DataJson));
});

Expand Down

0 comments on commit 118e355

Please sign in to comment.