Skip to content

Commit

Permalink
feat: finish cleanup transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
yHSJ committed Nov 11, 2024
1 parent 95ac52b commit b84e275
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 33 deletions.
7 changes: 1 addition & 6 deletions example-k8s-resource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ metadata:
name: a00000
namespace: hydra-doom
spec:
blockfrostKey: abc
commitInputs:
- 8484848484848484848484848484848484848484848484848484848484848484#0
# initialUtxoAddress: addr_test1vru2drx33ev6dt8gfq245r5k0tmy7ngqe79va69de9dxkrg09c7d3
initialUtxoAddress: addr_test1vpgcjapuwl7gfnzhzg6svtj0ph3gxu8kyuadudmf0kzsksqrfugfc
- 8484848484848484848484848484848484848484848484848484848484848484#0
networkId: 1
offline: true
participant: foo
party: foo
seedInput: 8484848484848484848484848484848484848484848484848484848484848484#0
2 changes: 1 addition & 1 deletion src/model/game/contract/_referee.cbor
Original file line number Diff line number Diff line change
@@ -1 +1 @@
590967010000332323232323232323232323223225333007323232323253323300d3001300f37540042646464a66666603000c2a66466022600260266ea801c4c94ccc05800403c4c94cccccc06c0040400400404c8c94ccc0640040484c94cccccc07800454ccc068c0740084c8c8c94ccc064c02400454ccc074c070dd50020a80100b0a99980c98068008a99980e980e1baa004150020161533301930030011533301d301c37540082a00402c02c60346ea800c54ccc05cc01cc064dd50040991919191919191919192999810980898119baa00113232323232323232323232323232323232323232532333036302600413253330373375e024980103d87980001533303733712664600200244a66607a00229000099b8048008cc008008c10000402c0c84c94ccc0e0cc0040548cc01403400454ccc0e0cc00400894ccc0e4c0a4c0ecdd500089980300f1bae303f303c37540022a660749212565787065637420566572696669636174696f6e4b6579286372656429203d20706c617965720016153330383375e0220102a66607066ebc04c02854ccc0e0c00c0384cdd78080038a5014a029405280a5022323300100100322533303f00114a2264a666078600860840042660060060022940c1080045280a5033323001001222533303d0011002133300300332323300100100422533304000114bd7009919299981f19baf00200510011330430023300400400130440023042001303f001304000101300a15333036302a00415333036302630383754018264a66606e66ebc04930103d8798000153330373375e6e9c050dd38058a99981b98010068a99981b9980200e0008a99981b9815981c9baa0071533303730273039375400c264a66607066ebc029300103d87a8000153330383375e0100222660080180022940528181e981d1baa006153303849012265787065637420536f6d65286368656174657229203d206e65775f63686561746572001613253330383375e014980103d87b8000153330383375e00e0202660080180022940528181e981d1baa00714a029405280a50375c607860726ea80300d054ccc0d8c098c0e0dd5006099299981ba99981b99baf0124c103d87b800014a2266ebc04930103d87a8000153330373375e0240122a66606e66ebcdd380a1ba700b153330373375e02000e2a66606e600401a2a66606e66ebc03c0184cc0100700045280a5014a029405280a50375c607860726ea80300d0dd7981d80a11191980080080191299981e0008a5013253330393375e607e00400829444cc00c00c004c0fc00488c8cc00400400c894ccc0ec004528099299981c19b8f375c607c00400829444cc00c00c004c0f8004c0d0dd500a181b981c001181b000981b001181a000981a0011bac303200130320023030001302c37546464646464a66605c603c60606ea80044c8c94ccc0c0cdd7981b0010020a999818180d18191baa001132533333303900115333031302130333754002264a66606c002062264a666666076002264a666070002066264a66666607a00206806826464a66607600206c264a666666080002264a66607a002070264a666666084002264a66607e002074264a6666660880022a66608060860042666026014264660020020104464a66608600426464a66608260620022a66608a60886ea80285400810054ccc104c0d400454ccc114c110dd50050a8010200a99982098158008a99982298221baa00a1500204004030423754012266602a00c266602a008201e07c07c264a666666090002266602e00226004608e00607e07e07e07e07e608a004600400407607607607607607660820026082004072072072072607e002607e00406e06e06e06e607a002607a0066eb00040d00d0c0e8004c0e80080c80c80c80c8c0e0004c0d0dd5000818018018018018181b18199baa00115330314912065787065637420496e6c696e65446174756d286461746129203d20646174756d0016153303149124657870656374206e65775f61646472657373203d3d207363726970745f61646472657373001630353036303600130313754606860626ea800454cc0bd2412c65787065637420536f6d65286e65775f7574786f29203d206c6973742e6174286f7574707574732c206978290016333002002013010300d302f375460646066605e6ea8c0c8c0bcdd500611129998190010a6103d87a8000132325333030302000313374a90001981a80125eb804ccc014014004cdc0001a4002606c006606800444464a66605c603c002264a666066002006264a6666660700022a666068606e004266600e0022a00a008008008008008008606a00260626ea801054ccc0b8c08800454ccc0c8c0c4dd50020a80180100118179baa00322232533302d301d001132533303200100313253333330370010040040040041325333034303700315006005375c002606800260606ea801054ccc0b4c0840044c94ccc0c800400c4c94cccccc0dc0040100100100104c94ccc0d0c0dc00c54018014dd7000981a00098181baa004002302e3754006605c605e004605a002605a004605600260560046eb0c0a4004c0a4004c090dd50050a998112493f65787065637420536f6d65287370656e745f696e70757429203d207472616e73616374696f6e2e66696e645f696e70757428696e707574732c207574786f29001632330010013758604e01244a66604c0022980103d87a80001323253330243375e600a604e6ea80080584cdd2a40006605200497ae0133004004001302a00230280012302600130243025002375a6046002603e6ea8048dd618109811181118111811181118110011bac302000130203020001301b3754020603a60346ea802054cc06124012465787065637420536f6d65286f6c645f737461746529203d206d617962655f646174756d0016370e9002009809809809809980d800980d8019bad00101030180013014375400e6e1d200000d00d00d00d00d301530160023014001301037540046e1d2002153300d490113756e737570706f7274656420707572706f736500163011301200230100013010002300e001300a37540022930a998042491856616c696461746f722072657475726e65642066616c73650013656375a0022a660089211272656465656d65723a2052656465656d6572001615330034912a65787065637420566572696669636174696f6e4b6579286372656429203d206e65775f7265666572656500161533002491216578706563742067616d655f646174613a2047616d6544617461203d206461746100165734ae7155ceaab9e5573eae815d0aba2574898101040001
5905e101010033232323232323223225333004323232323253323300a3001300b37540042646464a6646601c6002601e6ea801c4c8c8c8c94ccc054c0600084c8c8c94ccc054c02000454ccc060c05cdd50020a8010b0a99980a98060008a99980c180b9baa004150021615333015300300115333018301737540082a0042c2c602a6ea800c54ccc04cc018c050dd5003899191919191919191919299980e9808180f1baa00113232323232323232323232323232323232533302e3021007132323232323232323253330373375e02898103d87980001533303733712664600200244a66607800229000099b8048008cc008008c0fc0040180d454ccc0dcc8cc00400405c894ccc0f0004528899299981d19806804981f8010998018018008a50303f001153330373375e0240042a66606e66ebc05001054ccc0dccdd780c004099baf01100114a029405280a5014a02940c0ecc0f0008c0e8004c0e8008c0e0004c0e0008dd6181b000981b001181a00098181baa333002015018011132533302f302600813232323232323232325333038302b30393754010264a66607266ebc05930103d8798000153330393375e6e9c060dd38038a99981c99baf01a009153330393300b022001153330393030303a37540062a666072605860746ea80084c94ccc0e8cdd780326103d87a80001533303a3375e00802a26601a0100022940528181f181d9baa00216132533303a3375e00c98103d87b80001533303a3375e00602826601a0100022940528181f181d9baa00314a029405280a50375c607a60746ea802058c0f0c0f4008c0ec004c0ec008c0e4004c0e4008dd6181b800981b801181a80098189baa3330030160190121533302f302230303754020264a666060a66606066ebc03530103d87b800014a2266ebc03530103d87a800013300201900114a06eb8c0d0c0c4dd50080b11191980080080191299981a8008a5013253330333371e6eb8c0e0008010528899801801800981c00091191980080080191299981a0008a5013253330323375e606e00400829444cc00c00c004c0dc004888c8c94ccc0c4c090c0c8dd500089919299981999baf303800200415333033302130343754002264a666068604e606a6ea80044c8c8c8c8c8c8c8c8c8c94ccc104c1100084cc0580244c8cc00400402088c94ccc1100084c8c94ccc10cc0d800454ccc118c114dd50050a8010b0a999821981d0008a99982318229baa00a150021615333043303100115333046304537540142a0042c2c60866ea80244cc0600184cc06001040384cc064c1180084c004c11c008c00800858c108004c108008c100004c100008c0f8004c0f8008dd6181e000981e001181d000981b1baa001163038303537540022c2c606e6070607000260666ea8c0d8c0ccdd50008b19802801802180a18189baa0013001001222533303000214c103d87a800013232533302f302200313374a90001981980125eb804ccc014014004cdc0001a4002606800660640044464a666058603e00226464a666062606800426600c0022a0082c6064002605c6ea800c54ccc0b0c08c00454ccc0bcc0b8dd50018a8010b0b18161baa0022232533302b301e00113232533303030330021500416375c6062002605a6ea800c54ccc0acc0880044c8c94ccc0c0c0cc0085401058dd7181880098169baa00316302b375400460506ea8034c0acc0b0008c0a8004c0a8008c0a0004c0a0008dd618130009813001181200098101baa00b30223023301f37546044603e6ea800458c8cc004004dd618110049129998108008a60103d87a80001323253330203375e600a60446ea80080544cdd2a40006604800497ae01330040040013025002302300123021001301f3020002375a603c00260346ea8044dd6180e180e980e980e980e980e980e8011bac301b001301b301b0013016375401e6030602a6ea801c58dc3a40082c602c002602c0046eb4c050004c040dd50039b874800058c044c048008c040004c030dd50011b874800858c034c038008c030004c030008c028004c018dd50008a4c26cac6eb40055cd2ab9d5573caae7d5d02ba1574498101040001
35 changes: 26 additions & 9 deletions src/model/game/contract/game_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ use pallas::ledger::{

use crate::model::game::player::Player;

#[derive(Debug)]
pub struct PaymentCredential([u8; 28]);

#[derive(Debug)]
pub enum State {
RUNNING,
CHEATED,
FINISHED,
}
#[derive(Debug)]
pub struct GameState {
referee: PaymentCredential,
pub players: Vec<PaymentCredential>,
Expand Down Expand Up @@ -145,14 +148,21 @@ impl TryFrom<PlutusData> for GameState {
let winner: Option<PaymentCredential> = match constr.fields[3].clone() {
PlutusData::Constr(constr) => {
if Some(0) == constr.any_constructor {
Some(
PaymentCredential::try_from(PlutusData::Constr(constr))
.context("winner")?,
)
if constr.fields.len() != 1 {
bail!("invalid length for Just type");
}

match constr.fields[0].clone() {
PlutusData::Constr(constr) => Some(
PaymentCredential::try_from(PlutusData::Constr(constr))
.context("failed to get PaymentCredential for winner")?,
),
_ => bail!("invalid inner type for Just<PaymentCredential>"),
}
} else if Some(1) == constr.any_constructor {
None
} else {
bail!("Invalid constructor tag for winner");
bail!("Invalid constructor for winner");
}
}
_ => bail!("Invalid data type for winner"),
Expand All @@ -161,10 +171,17 @@ impl TryFrom<PlutusData> for GameState {
let cheater: Option<PaymentCredential> = match constr.fields[4].clone() {
PlutusData::Constr(constr) => {
if Some(0) == constr.any_constructor {
Some(
PaymentCredential::try_from(PlutusData::Constr(constr))
.context("cheater")?,
)
if constr.fields.len() != 1 {
bail!("invalid length for Just type");
}

match constr.fields[0].clone() {
PlutusData::Constr(constr) => Some(
PaymentCredential::try_from(PlutusData::Constr(constr))
.context("failed to get PaymentCredential for cheater")?,
),
_ => bail!("invalid inner type for Just<PaymentCredential>"),
}
} else if Some(1) == constr.any_constructor {
None
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/model/game/contract/redeemer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ impl From<Redeemer> for PlutusData {
impl From<SpendAction> for PlutusData {
fn from(value: SpendAction) -> Self {
PlutusData::Constr(Constr {
tag: 121,
any_constructor: match value {
SpendAction::AddPlayer => Some(0),
SpendAction::EndGame => Some(1),
SpendAction::Collect => Some(2),
tag: match value {
SpendAction::AddPlayer => 121,
SpendAction::EndGame => 122,
SpendAction::Collect => 123,
},
any_constructor: None,
fields: MaybeIndefArray::Def(vec![]),
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/model/game/contract/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Validator {
}

pub fn address(network: Network) -> Address {
let mut hash = Self::to_plutus().compute_hash().to_vec();
let mut hash = Self::compute_hash();
hash.insert(
0,
0b01110000
Expand All @@ -31,7 +31,7 @@ impl Validator {
}

pub fn to_plutus() -> PlutusScript<3> {
PlutusScript(
PlutusScript::<3>(
hex::decode(Self::cbor())
.expect("invalid script cbor hex string")
.into(),
Expand Down
46 changes: 36 additions & 10 deletions src/model/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl TxBuilder {
let input_utxo = admin_utxos.first().unwrap();

let script_address = Validator::address(network);
println!("Script Address: {:?}", script_address.to_bech32());
let player_inbound_address = player
.inbound_address(self.admin_pkh, network)
.context("failed to build player multisig inbound address")?;
Expand Down Expand Up @@ -104,7 +105,9 @@ impl TxBuilder {

let game_state: PlutusData = match game_state_utxo.datum.clone() {
Datum::Hash(_) => bail!("Unexpected datum hash in game utxo"),
Datum::Inline(data) => GameState::try_from(data)?,
Datum::Inline(data) => {
GameState::try_from(data).context("failed to decode plutus data for game state")?
}
Datum::None => bail!("No datum in game utxo"),
}
.add_player(player.signing_key.into())
Expand Down Expand Up @@ -260,6 +263,7 @@ impl TxBuilder {
4, 1293828, 28716, 63, 0, 1, 1006041, 43623, 251, 0, 1,
],
)
.disclosed_signer(self.admin_pkh)
.fee(0);

let tx = tx_builder.build_conway_raw()?;
Expand All @@ -280,24 +284,33 @@ impl TxBuilder {

let game_state: GameState = match game_state_utxo.datum.clone() {
Datum::Hash(_) => bail!("Unexpected datum hash in game utxo"),
Datum::Inline(data) => data.try_into()?,
Datum::Inline(data) => data
.try_into()
.context("failed to convert data to GameState")?,
Datum::None => bail!("No datum in game utxo"),
};

let collateral_utxos = self.find_admin_utxos(utxos.clone());

let collateral_utxo = collateral_utxos
.get(0)
.ok_or_else(|| anyhow!("No collateral utxo found"))?;

let redeemer: PlutusData = Redeemer::new(0, SpendAction::AddPlayer).into();
let redeemer: PlutusData = Redeemer::new(0, SpendAction::Collect).into();
let mut redeemer_bytes = Vec::new();
encode(&redeemer, &mut redeemer_bytes)?;

let mut tx_builder = Some(
StagingTransaction::new()
.input(game_state_utxo.clone().into())
.input(collateral_utxo.clone().into())
.collateral_input(collateral_utxo.clone().into())
.output(Output::new(collateral_utxo.address.clone(), 0))
.output(
collateral_utxo
.clone()
.try_into()
.context("failed to build target output from utxo object")?,
)
.add_spend_redeemer(
game_state_utxo.into(),
redeemer_bytes,
Expand Down Expand Up @@ -337,14 +350,20 @@ impl TxBuilder {

for player in game_state.players {
let player: Player = player.into();
let inbound_address = player.inbound_address(self.admin_pkh, network)?;
let outbound_address = player.outbound_address(self.admin_pkh, network)?;
let inbound_address = player
.inbound_address(self.admin_pkh, network)
.context("failed to get player inbound address")?;
let outbound_address = player
.outbound_address(self.admin_pkh, network)
.context("failed to get player outbound address")?;
let inbound_script = player.inbound_script(self.admin_pkh);
let mut inbound_bytes = Vec::new();
encode(&inbound_script, &mut inbound_bytes)?;
encode(&inbound_script, &mut inbound_bytes)
.context("Failed to cbor encode outbound script")?;
let outbound_script = player.outbound_script(self.admin_pkh);
let mut outbound_bytes = Vec::new();
encode(&outbound_script, &mut outbound_bytes)?;
encode(&outbound_script, &mut outbound_bytes)
.context("Failed to cbor encode outbound script")?;
let player_utxos: Vec<_> = utxos
.clone()
.into_iter()
Expand All @@ -366,7 +385,12 @@ impl TxBuilder {

let tx = tx_builder
.ok_or(anyhow!("fatal error: no tx builder"))
.and_then(|builder| builder.build_conway_raw().map_err(|e| anyhow!("{}", e)))
.and_then(|builder| {
builder
.disclosed_signer(self.admin_pkh)
.build_conway_raw()
.map_err(|e| anyhow!("{}", e))
})
.map_err(|e| anyhow!("Failed to build tx: {}", e))?;

let signed_tx = tx
Expand Down Expand Up @@ -395,6 +419,8 @@ impl TxBuilder {

#[cfg(test)]
mod tests {
use tracing::debug;

use super::*;
use crate::model::cluster::KeyEnvelope;
use std::{collections::HashMap, fs::File};
Expand Down Expand Up @@ -437,6 +463,6 @@ mod tests {
.build_new_game(player.into(), utxos, Network::Testnet)
.expect("Failed to build tx");

println!("{}", hex::encode(tx.tx_bytes));
debug!("{}", hex::encode(tx.tx_bytes));
}
}

0 comments on commit b84e275

Please sign in to comment.