Skip to content

Commit

Permalink
Fix round-trip ScriptRef (#20)
Browse files Browse the repository at this point in the history
* fix round-trip ScriptRef

* fix typo

* fix test
  • Loading branch information
ljttl3q04t authored Jun 15, 2023
1 parent d138f8a commit b8621fd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
6 changes: 5 additions & 1 deletion rust/src/plutus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,10 @@ impl Deserialize for PlutusScript {
}
}

fn plutus_v2_deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<PlutusScript, DeserializeError> {
Ok(PlutusScript::new_v2(raw.bytes()?))
}

impl cbor_event::se::Serialize for Script {
fn serialize<'se, W: Write>(
&self,
Expand Down Expand Up @@ -1418,7 +1422,7 @@ impl Deserialize for Script {
let script_enum = match raw.unsigned_integer()? {
0 => ScriptEnum::NativeScript(NativeScript::deserialize(raw)?),
1 => ScriptEnum::PlutusScriptV1(PlutusScript::deserialize(raw)?),
2 => ScriptEnum::PlutusScriptV2(PlutusScript::deserialize(raw)?),
2 => ScriptEnum::PlutusScriptV2(plutus_v2_deserialize(raw)?),
n => {
return Err(DeserializeFailure::FixedValueMismatch {
found: Key::Uint(n),
Expand Down
37 changes: 25 additions & 12 deletions rust/src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,10 @@ impl Deserialize for ScriptRef {
(|| -> Result<_, DeserializeError> {
match raw.tag()? {
//bytes string tag
// 24 => Ok(ScriptRef(from_bytes(&raw.bytes()?)?)),
24 => Ok(Self(Script::from_bytes(raw.bytes()?).unwrap())),
24 => {
let bytes = &raw.bytes()?;
Ok(ScriptRef(from_bytes(bytes)?))
},
tag => {
return Err(DeserializeFailure::TagMismatch {
found: tag,
Expand All @@ -1024,17 +1026,18 @@ impl Deserialize for ScriptRef {
}
}
})()
.map_err(|e| e.annotate("ScriptRef"))
.map_err(|e| e.annotate("ScriptRef"))
}
}

impl cbor_event::se::Serialize for ScriptRef {
fn serialize<'se, W: Write>(
fn serialize<'a, W: Write + Sized>(
&self,
serializer: &'se mut Serializer<W>,
) -> cbor_event::Result<&'se mut Serializer<W>> {
serializer.write_tag(24u64)?;
serializer.write_bytes(&self.0.to_bytes())
serializer: &'a mut Serializer<W>,
) -> cbor_event::Result<&'a mut Serializer<W>> {
let bytes = to_bytes(&self.0);
serializer.write_tag(24)?.write_bytes(&bytes)?;
Ok(serializer)
}
}

Expand Down Expand Up @@ -4862,6 +4865,7 @@ impl Deserialize for NetworkId {

#[cfg(test)]
mod tests {
use std::ptr::null;
use digest::generic_array::typenum::assert_type;
use super::*;
use crate::fakes::{
Expand Down Expand Up @@ -5401,7 +5405,16 @@ mod tests {
let ref2 = ScriptRef::new(&script);
let ref_bytes = ref2.to_bytes();
let ref3 = ScriptRef::from_bytes(ref_bytes).unwrap();
assert_eq!(ref2.to_hex(), ref3.to_hex());
assert_eq!(ref2, ref3);

let mut o2 = TransactionOutput::new(
&fake_base_address(1),
&fake_value()
);
o2.set_script_ref(&ref2);
let o2_bytes = o2.to_bytes();
let o3 = TransactionOutput::from_bytes(o2_bytes).unwrap();
assert_eq!(o2, o3);
}

#[test]
Expand Down Expand Up @@ -5437,17 +5450,17 @@ mod tests {

let mut o4 = TransactionOutput::new(&fake_base_address(3), &fake_value2(234570));
o4.set_script_ref(&ScriptRef::new_plutus_script(&script_v2));
assert_eq!(TransactionOutput::from_bytes(o4.to_bytes()).unwrap().to_hex(), o4.to_hex());
assert_eq!(TransactionOutput::from_bytes(o4.to_bytes()).unwrap(), o4);

let mut o5 = TransactionOutput::new(&fake_base_address(4), &fake_value2(234571));
o5.set_plutus_data(&PlutusData::new_empty_constr_plutus_data(&to_bignum(43)));
o5.set_script_ref(&ScriptRef::new_plutus_script(&script_v2));
assert_eq!(TransactionOutput::from_bytes(o5.to_bytes()).unwrap().to_hex(), o5.to_hex());
assert_eq!(TransactionOutput::from_bytes(o5.to_bytes()).unwrap(), o5);

let mut o6 = TransactionOutput::new(&fake_base_address(5), &fake_value2(234572));
o6.set_data_hash(&fake_data_hash(222));
o6.set_script_ref(&ScriptRef::new_plutus_script(&script_v2));
assert_eq!(TransactionOutput::from_bytes(o6.to_bytes()).unwrap().to_hex(), o6.to_hex());
assert_eq!(TransactionOutput::from_bytes(o6.to_bytes()).unwrap(), o6);
}

#[test]
Expand Down
32 changes: 32 additions & 0 deletions rust/src/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6003,6 +6003,38 @@ mod tests {
assert_eq!(tx.witness_set.redeemers.unwrap().get(0), redeemer);
}

#[test]
fn test_get_ex_unit_hello_world() {
let transaction = Transaction::from_hex("84aa00838258202d53b05adccb4db639c1c826acca02ec9a4bbdcacbb88bc3f5eab652e9e71d76008258202d53b05adccb4db639c1c826acca02ec9a4bbdcacbb88bc3f5eab652e9e71d7601825820b81e90c259bca3d7252f3c529115e4a37a10d320525ea4285c16c0379a2da99000018282581d60f1edcccaa9054e89134b2850b3e4ea8d9ea28748f42f57809af4491d1a002dc6c082581d60f1edcccaa9054e89134b2850b3e4ea8d9ea28748f42f57809af4491d1a00c5b799021a000791ad031a01da0e410b582089249510f9a597a83513f27a8344edd06cc3490cbdb0103963dc7eb20ceb3dfe0d81825820b81e90c259bca3d7252f3c529115e4a37a10d320525ea4285c16c0379a2da990000e81581cf1edcccaa9054e89134b2850b3e4ea8d9ea28748f42f57809af4491d1082581d60f1edcccaa9054e89134b2850b3e4ea8d9ea28748f42f57809af4491d1a004c4b40111a004c4b4012818258200f6e76c45462e1696b1b5db05de5c7717a2d79020cef05c9fea5e315984f7d6b00a10581840000d8799f4d48656c6c6f2c20576f726c6421ff821a002dc6c01a77359400f5f6").unwrap();
let costModel = Costmdls::from_hex("a10198af1a0003236119032c01011903e819023b00011903e8195e7104011903e818201a0001ca761928eb041959d818641959d818641959d818641959d818641959d818641959d81864186418641959d81864194c5118201a0002acfa182019b551041a000363151901ff00011a00015c3518201a000797751936f404021a0002ff941a0006ea7818dc0001011903e8196ff604021a0003bd081a00034ec5183e011a00102e0f19312a011a00032e801901a5011a0002da781903e819cf06011a00013a34182019a8f118201903e818201a00013aac0119e143041903e80a1a00030219189c011a00030219189c011a0003207c1901d9011a000330001901ff0119ccf3182019fd40182019ffd5182019581e18201940b318201a00012adf18201a0002ff941a0006ea7818dc0001011a00010f92192da7000119eabb18201a0002ff941a0006ea7818dc0001011a0002ff941a0006ea7818dc0001011a0011b22c1a0005fdde00021a000c504e197712041a001d6af61a0001425b041a00040c660004001a00014fab18201a0003236119032c010119a0de18201a00033d7618201979f41820197fb8182019a95d1820197df718201995aa18201a0223accc0a1a0374f693194a1f0a1a02515e841980b30a").unwrap();
let mut utxos = TransactionUnspentOutputs::new();
utxos.add(
&TransactionUnspentOutput::from_hex("828258202d53b05adccb4db639c1c826acca02ec9a4bbdcacbb88bc3f5eab652e9e71d760182581d60f1edcccaa9054e89134b2850b3e4ea8d9ea28748f42f57809af4491d1a0034b2c6").unwrap()
);
utxos.add(
&TransactionUnspentOutput::from_hex("82825820b81e90c259bca3d7252f3c529115e4a37a10d320525ea4285c16c0379a2da9900082581d60f1edcccaa9054e89134b2850b3e4ea8d9ea28748f42f57809af4491d1a00989680").unwrap()
);
utxos.add(
&TransactionUnspentOutput::from_hex("828258202d53b05adccb4db639c1c826acca02ec9a4bbdcacbb88bc3f5eab652e9e71d7600a300581d7040c46ca36d4e8a415d689208735a24f2651e640b46aed5f51cf7199b011a002dc6c0028201d8185822d8799f581cf1edcccaa9054e89134b2850b3e4ea8d9ea28748f42f57809af4491dff").unwrap()
);
utxos.add(
&TransactionUnspentOutput::from_hex("828258200f6e76c45462e1696b1b5db05de5c7717a2d79020cef05c9fea5e315984f7d6b00a300583900016ad31ade77490b9967a9ae358b0a035021d50eb38a7c130ef65b3df9e9edfb460fae49f9754b2126513f4b0dacdc86ba8c90b562764fd3011a0030aa9803d8185901fb82025901f65901f30100003232323232323232323232322223232533300a3232533300c002100114a066646002002444a66602600429404c8c94ccc040cdc78010018a5113330050050010033016003375c60280046eb0cc01cc024cc01cc024011200048040dd71980398048012400066e3cdd7198031804001240009110d48656c6c6f2c20576f726c642100149858cc028c94ccc028cdc3a400000226464a66602260260042930a99807249334c6973742f5475706c652f436f6e73747220636f6e7461696e73206d6f7265206974656d73207468616e2065787065637465640016375c6022002601000a2a660189212b436f6e73747220696e64657820646964206e6f74206d6174636820616e7920747970652076617269616e7400163008004004330093253330093370e900000089919299980818090010a4c2a6601a9201334c6973742f5475706c652f436f6e73747220636f6e7461696e73206d6f7265206974656d73207468616e2065787065637465640016375c6020002600e0062a660169212b436f6e73747220696e64657820646964206e6f74206d6174636820616e7920747970652076617269616e740016300700200233001001480008888cccc01ccdc38008018069199980280299b8000448008c03c0040080088c01cdd5000918029baa0015734ae6d5ce2ab9d5573caae7d5d0aba201").unwrap()
);
let maxExUnits = ExUnits::from_hex("821a00d59f801b00000002540be400").unwrap();
let redeemer = get_ex_units(
&transaction,
&utxos,
&costModel,
&maxExUnits,
BigNum::from_str("1666656000000").unwrap(),
BigNum::from_str("0").unwrap(),
1000
).unwrap();
assert_eq!(redeemer.len(), 1);
let redeemer = redeemer.get(0);
println!("redeemer: {}", redeemer.to_json().unwrap());
}

#[test]
fn test_adding_plutus_script_witnesses() {
let mut tx_builder = create_reallistic_tx_builder();
Expand Down

0 comments on commit b8621fd

Please sign in to comment.