Skip to content

Commit 7032af8

Browse files
committed
feat: reference script persistence in utxo state
Signed-off-by: William Hankins <[email protected]>
1 parent 37aad40 commit 7032af8

File tree

7 files changed

+56
-2
lines changed

7 files changed

+56
-2
lines changed

codec/src/map_parameters.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use acropolis_common::{
1818
rational_number::RationalNumber,
1919
*,
2020
};
21+
use pallas_primitives::conway::PseudoScript;
2122
use std::collections::{HashMap, HashSet};
2223

2324
/// Map Pallas Network to our NetworkId
@@ -1100,3 +1101,21 @@ pub fn map_datum(datum: &Option<conway::MintedDatumOption>) -> Option<Datum> {
11001101
None => None,
11011102
}
11021103
}
1104+
1105+
pub fn map_reference_script(script: &Option<conway::MintedScriptRef>) -> Option<ReferenceScript> {
1106+
match script {
1107+
Some(PseudoScript::NativeScript(script)) => {
1108+
Some(ReferenceScript::Native(script.raw_cbor().to_vec()))
1109+
}
1110+
Some(PseudoScript::PlutusV1Script(script)) => {
1111+
Some(ReferenceScript::PlutusV1(script.as_ref().to_vec()))
1112+
}
1113+
Some(PseudoScript::PlutusV2Script(script)) => {
1114+
Some(ReferenceScript::PlutusV2(script.as_ref().to_vec()))
1115+
}
1116+
Some(PseudoScript::PlutusV3Script(script)) => {
1117+
Some(ReferenceScript::PlutusV3(script.as_ref().to_vec()))
1118+
}
1119+
None => None,
1120+
}
1121+
}

common/src/types.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,15 @@ pub enum Datum {
324324
Inline(Vec<u8>),
325325
}
326326

327+
// The full CBOR bytes of a reference script
328+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
329+
pub enum ReferenceScript {
330+
Native(Vec<u8>),
331+
PlutusV1(Vec<u8>),
332+
PlutusV2(Vec<u8>),
333+
PlutusV3(Vec<u8>),
334+
}
335+
327336
/// Value (lovelace + multiasset)
328337
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
329338
pub struct Value {
@@ -448,6 +457,9 @@ pub struct TxOutput {
448457

449458
/// Datum (Inline or Hash)
450459
pub datum: Option<Datum>,
460+
461+
/// Reference script
462+
pub reference_script: Option<ReferenceScript>,
451463
}
452464

453465
/// Transaction input (UTXO reference)

modules/assets_state/src/state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ mod tests {
769769
)],
770770
},
771771
datum: datum.map(Datum::Inline),
772+
reference_script: None,
772773
}
773774
}
774775

modules/genesis_bootstrapper/src/genesis_bootstrapper.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ impl GenesisBootstrapper {
152152
}),
153153
value: Value::new(*amount, Vec::new()),
154154
datum: None,
155+
reference_script: None,
155156
};
156157

157158
utxo_deltas_message.deltas.push(UTXODelta::Output(tx_output));

modules/tx_unpacker/src/tx_unpacker.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ impl TxUnpacker {
230230
address,
231231
value: map_parameters::map_value(&output.value()),
232232
datum: map_parameters::map_datum(&output.datum()),
233+
reference_script: map_parameters::map_reference_script(&output.script_ref())
233234
}));
234235

235236
// catch all output lovelaces

modules/utxo_state/src/fake_immutable_utxo_store.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ impl ImmutableUTXOStore for FakeImmutableUTXOStore {
5353
address: Address::None,
5454
value: Value::new(42, Vec::new()),
5555
datum: None,
56+
reference_script: None,
5657
}))
5758
}
5859

modules/utxo_state/src/state.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use acropolis_common::{
44
messages::UTXODeltasMessage, params::SECURITY_PARAMETER_K, Address, BlockInfo, BlockStatus,
55
Datum, TxInput, TxOutput, UTXODelta,
66
};
7-
use acropolis_common::{AddressDelta, UTxOIdentifier, Value, ValueDelta};
7+
use acropolis_common::{AddressDelta, ReferenceScript, UTxOIdentifier, Value, ValueDelta};
88
use anyhow::Result;
99
use async_trait::async_trait;
1010
use std::collections::HashMap;
@@ -22,6 +22,9 @@ pub struct UTXOValue {
2222

2323
/// Datum
2424
pub datum: Option<Datum>,
25+
26+
/// Reference script
27+
pub reference_script: Option<ReferenceScript>,
2528
}
2629

2730
/// Address delta observer
@@ -268,6 +271,7 @@ impl State {
268271
address: output.address.clone(),
269272
value: output.value.clone(),
270273
datum: output.datum.clone(),
274+
reference_script: output.reference_script.clone(),
271275
};
272276

273277
// Add to volatile or immutable maps
@@ -406,7 +410,9 @@ impl State {
406410
mod tests {
407411
use super::*;
408412
use crate::InMemoryImmutableUTXOStore;
409-
use acropolis_common::{AssetName, BlockHash, ByronAddress, Era, NativeAsset, Value};
413+
use acropolis_common::{
414+
AssetName, BlockHash, ByronAddress, Era, NativeAsset, ReferenceScript, Value,
415+
};
410416
use config::Config;
411417
use tokio::sync::Mutex;
412418

@@ -450,6 +456,7 @@ mod tests {
450456
async fn observe_output_adds_to_immutable_utxos() {
451457
let mut state = new_state();
452458
let datum_data = vec![1, 2, 3, 4, 5];
459+
let reference_script_bytes = vec![0xde, 0xad, 0xbe, 0xef];
453460

454461
let output = TxOutput {
455462
utxo_identifier: UTxOIdentifier::new(0, 0, 0),
@@ -471,6 +478,7 @@ mod tests {
471478
)],
472479
),
473480
datum: Some(Datum::Inline(datum_data.clone())),
481+
reference_script: Some(ReferenceScript::PlutusV1(reference_script_bytes.clone())),
474482
};
475483

476484
let block = create_block(BlockStatus::Immutable, 1, 1);
@@ -503,6 +511,9 @@ mod tests {
503511
value.datum,
504512
Some(Datum::Inline(ref data)) if data == &datum_data
505513
));
514+
assert!(matches!(
515+
value.reference_script,
516+
Some(ReferenceScript::PlutusV1(ref bytes)) if bytes == &reference_script_bytes));
506517
}
507518

508519
_ => panic!("UTXO not found"),
@@ -532,6 +543,7 @@ mod tests {
532543
)],
533544
),
534545
datum: None,
546+
reference_script: None,
535547
};
536548

537549
let block1 = create_block(BlockStatus::Immutable, 1, 1);
@@ -576,6 +588,7 @@ mod tests {
576588
)],
577589
),
578590
datum: None,
591+
reference_script: None,
579592
};
580593

581594
let block10 = create_block(BlockStatus::Volatile, 10, 10);
@@ -616,6 +629,7 @@ mod tests {
616629
)],
617630
),
618631
datum: None,
632+
reference_script: None,
619633
};
620634

621635
let block10 = create_block(BlockStatus::Volatile, 10, 10);
@@ -671,6 +685,7 @@ mod tests {
671685
)],
672686
),
673687
datum: None,
688+
reference_script: None,
674689
};
675690

676691
let block1 = create_block(BlockStatus::Volatile, 1, 1);
@@ -718,6 +733,7 @@ mod tests {
718733
)],
719734
),
720735
datum: None,
736+
reference_script: None,
721737
};
722738

723739
let block1 = create_block(BlockStatus::Volatile, 1, 1);
@@ -826,6 +842,7 @@ mod tests {
826842
)],
827843
),
828844
datum: None,
845+
reference_script: None,
829846
};
830847

831848
let block1 = create_block(BlockStatus::Immutable, 1, 1);
@@ -884,6 +901,7 @@ mod tests {
884901
)],
885902
),
886903
datum: None,
904+
reference_script: None,
887905
};
888906

889907
let block10 = create_block(BlockStatus::Volatile, 10, 10);
@@ -938,6 +956,7 @@ mod tests {
938956
)],
939957
),
940958
datum: None,
959+
reference_script: None,
941960
};
942961

943962
let block10 = create_block(BlockStatus::Volatile, 10, 10);

0 commit comments

Comments
 (0)