Skip to content

Commit

Permalink
WIP: revrite deposit validator in aiken
Browse files Browse the repository at this point in the history
Missing to compare the outputs hash to the commit hash in the datum.
  • Loading branch information
v0d1ch committed Oct 21, 2024
1 parent 95cb717 commit ce5c6b4
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 4 deletions.
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 108 additions & 1 deletion hydra-plutus/plutus.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"plutusVersion": "v3",
"compiler": {
"name": "Aiken",
"version": "v1.1.4+c7ae161"
"version": "v1.1.5+a7741ec"
},
"license": "Apache-2.0"
},
Expand Down Expand Up @@ -35,17 +35,75 @@
},
"compiledCode": "5902ae010100323232323232323232322533300332323232325332330093001300b37540042646464a66666602800c2646464a66601e60060022a66602660246ea80245400803854ccc03cc01c00454ccc04cc048dd50048a80100700718081baa0081533300d3001300f37540042646464646464a6660266016602a6ea80344cc00cc01130103d87980003370e6660026eacc064c068c068c068c068c058dd50079bae30053016375400c91010b487964726148656164563100480044c94ccc050c020c058dd50008998021802a6103d87a8000300c333002375660346036602e6ea8c068c05cdd50009bae30063017375400e9110b4879647261486561645631001533015491054c35333b39001632533301800114c103d87a80001300333019301a0014bd701bac30053016375401e44464a66602c601c60306ea8004520001375a603860326ea8004c94ccc058c038c060dd50008a6103d87a8000132330010013756603a60346ea8008894ccc070004530103d87a8000132323232533301c337220100042a66603866e3c0200084c02ccc084dd4000a5eb80530103d87a8000133006006003375a603c0066eb8c070008c080008c078004c8cc004004010894ccc06c0045300103d87a8000132323232533301b337220100042a66603666e3c0200084c028cc080dd3000a5eb80530103d87a80001330060060033756603a0066eb8c06c008c07c008c074004dd2a400044a666024002294454cc04c00852812999808180218091baa001149103433031001491034330320023015301630160013013301037540042a6601c9201054c34333b350016370e900000580580580598089809001180800098061baa002370e90010b1806980700118060009806001180500098031baa00114984d95854cc0092401064c34323b333100165734ae7155ceaab9e5573eae815d0aba257481",
"hash": "ea444d37d226e71eef73ac78d149750da977feb588900135bf9e8221"
},
{
"title": "deposit.deposit.spend",
"datum": {
"title": "datum",
"schema": {
"$ref": "#/definitions/deposit~1Datum"
}
},
"redeemer": {
"title": "redeemer",
"schema": {
"$ref": "#/definitions/deposit~1Redeemer"
}
},
"compiledCode": "5902ce0101003232323232323232322533300332323232325332330093001300b37540042646464a66666602600c2646464a66601e6006002264a66602800201e264a666666030002020020020020264a66602c60300062a00a0226eb8004c054004c048dd50048a9998079803800899299980a000807899299999980c000808008008099299980b180c0018a8028089bad00101030150013012375401201c60206ea802054ccc034c004c03cdd5001099191919191929998099803980a9baa00d13253330143300330044c103d87e80003375e6eb8c014c05cdd50038008998019802260103d87980003322325333017300f30193754002266e24dd6980e180d1baa00100213300630074c103d87a80004a0600e60326ea8c010c064dd50011803180b9baa010375a6004602e6ea801c5281bae30183016375401a264a666028660066008980103d87c80003322325333017300f30193754002266e20008dd6980e180d1baa00113300630074c103d87b80004a0600e60326ea8c01cc064dd50011803180b9baa010375a6004602e6ea801c4c8cc894cc061240119696d706c656d656e74207265636f7665725f6f75747075747300163758600260306ea8020dd61800980c1baa0112301a301b301b00114a06eb4c060c058dd50069180c180c8009129998098008a51153301400214a0464a666024600c0022921034430310015333012300a001149010344303200153330123370e90020008a490344303300153330123370e90030008a490344303400153330123370e90040008a49034430350014910344303600301337540024602a00246028602a602a602a602a602a602a602a002602460206ea800854cc039241054c33373b350016370e900000580580580598081808801180780098061baa002370e90010b1806180680118058009805801180480098031baa00114984d95854cc0092401064c33363b333100165734ae7155ceaab9e5573eae855d12ba41",
"hash": "2aa7041434198e52f43a6e8cf5c35b047e3bd4d6314eb679c88d39c2"
},
{
"title": "deposit.deposit.else",
"redeemer": {
"schema": {}
},
"compiledCode": "5902ce0101003232323232323232322533300332323232325332330093001300b37540042646464a66666602600c2646464a66601e6006002264a66602800201e264a666666030002020020020020264a66602c60300062a00a0226eb8004c054004c048dd50048a9998079803800899299980a000807899299999980c000808008008099299980b180c0018a8028089bad00101030150013012375401201c60206ea802054ccc034c004c03cdd5001099191919191929998099803980a9baa00d13253330143300330044c103d87e80003375e6eb8c014c05cdd50038008998019802260103d87980003322325333017300f30193754002266e24dd6980e180d1baa00100213300630074c103d87a80004a0600e60326ea8c010c064dd50011803180b9baa010375a6004602e6ea801c5281bae30183016375401a264a666028660066008980103d87c80003322325333017300f30193754002266e20008dd6980e180d1baa00113300630074c103d87b80004a0600e60326ea8c01cc064dd50011803180b9baa010375a6004602e6ea801c4c8cc894cc061240119696d706c656d656e74207265636f7665725f6f75747075747300163758600260306ea8020dd61800980c1baa0112301a301b301b00114a06eb4c060c058dd50069180c180c8009129998098008a51153301400214a0464a666024600c0022921034430310015333012300a001149010344303200153330123370e90020008a490344303300153330123370e90030008a490344303400153330123370e90040008a49034430350014910344303600301337540024602a00246028602a602a602a602a602a602a602a002602460206ea800854cc039241054c33373b350016370e900000580580580598081808801180780098061baa002370e90010b1806180680118058009805801180480098031baa00114984d95854cc0092401064c33363b333100165734ae7155ceaab9e5573eae855d12ba41",
"hash": "2aa7041434198e52f43a6e8cf5c35b047e3bd4d6314eb679c88d39c2"
}
],
"definitions": {
"ByteArray": {
"dataType": "bytes"
},
"Data": {
"title": "Data",
"description": "Any Plutus data."
},
"Int": {
"dataType": "integer"
},
"List$cardano/transaction/OutputReference": {
"dataType": "list",
"items": {
"$ref": "#/definitions/cardano~1transaction~1OutputReference"
}
},
"PolicyId": {
"title": "PolicyId",
"dataType": "bytes"
},
"cardano/transaction/OutputReference": {
"title": "OutputReference",
"description": "An `OutputReference` is a unique reference to an output on-chain. The `output_index`\n corresponds to the position in the output list of the transaction (identified by its id)\n that produced that output",
"anyOf": [
{
"title": "OutputReference",
"dataType": "constructor",
"index": 0,
"fields": [
{
"title": "transaction_id",
"$ref": "#/definitions/ByteArray"
},
{
"title": "output_index",
"$ref": "#/definitions/Int"
}
]
}
]
},
"commit/Datum": {
"title": "Datum",
"anyOf": [
Expand Down Expand Up @@ -86,6 +144,55 @@
"fields": []
}
]
},
"deposit/Datum": {
"title": "Datum",
"anyOf": [
{
"title": "Datum",
"dataType": "constructor",
"index": 0,
"fields": [
{
"title": "currencySymbol",
"$ref": "#/definitions/ByteArray"
},
{
"title": "deadline",
"$ref": "#/definitions/Int"
},
{
"title": "commits",
"$ref": "#/definitions/List$cardano~1transaction~1OutputReference"
}
]
}
]
},
"deposit/Redeemer": {
"title": "Redeemer",
"anyOf": [
{
"title": "Claim",
"dataType": "constructor",
"index": 0,
"fields": [
{
"$ref": "#/definitions/PolicyId"
}
]
},
{
"title": "Recover",
"dataType": "constructor",
"index": 1,
"fields": [
{
"$ref": "#/definitions/Int"
}
]
}
]
}
}
}
96 changes: 96 additions & 0 deletions hydra-plutus/validators/deposit.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use cardano/assets.{PolicyId}
use aiken/interval.{Finite, Interval, IntervalBound}
use cardano/transaction.{OutputReference, ValidityRange, Transaction, Output}

pub type Datum {
Datum {currencySymbol: ByteArray, deadline: Int, commits: List<OutputReference>}
}

pub type Redeemer {
Claim (PolicyId)
Recover(Int)
}

type DepositError {
DepositDeadlineSurpassed
DepositNoUpperBoundDefined
DepositNoLowerBoundDefined
DepositDeadlineNotReached
IncorrectDepositHash
WrongHeadIdInDepositDatum
}

fn toErrorCode(err: DepositError) -> String {
when err is {
DepositDeadlineSurpassed -> @"D01"
DepositNoUpperBoundDefined -> @"D02"
DepositNoLowerBoundDefined -> @"D03"
DepositDeadlineNotReached -> @"D04"
IncorrectDepositHash -> @"D05"
WrongHeadIdInDepositDatum -> @"D06"
}
}

validator deposit {
spend(datum: Option<Datum>, redeemer: Redeemer, _utxo: OutputReference, self:Transaction ) -> Bool {
expect Some(datum) = datum
when redeemer is {
Claim(currencySymbol) ->
traceIfFalse(
toErrorCode(WrongHeadIdInDepositDatum),
check_head_id(datum.currencySymbol, currencySymbol)
) &&
traceIfFalse(
toErrorCode(DepositDeadlineSurpassed),
before_deadline(self.validity_range, datum.deadline)
)
Recover(n) -> {
traceIfFalse(
toErrorCode(DepositDeadlineNotReached),
after_deadline(self.validity_range, datum.deadline)
) && recover_outputs(n, datum.commits, self.outputs )
}
}
}
}

// Helpers

fn check_head_id(datumCS, redeemerCS){
datumCS == redeemerCS
}

fn before_deadline(range: ValidityRange, dl){
when range.upper_bound.bound_type is {
Finite(tx_upper_validity) -> tx_upper_validity <= dl
_ ->
traceIfFalse(
toErrorCode(DepositNoUpperBoundDefined),
False,
)
}
}

fn after_deadline(range: ValidityRange, dl){
when range.lower_bound.bound_type is {
Finite(tx_lower_validity) -> tx_lower_validity > dl
_ ->
traceIfFalse(
toErrorCode(DepositNoLowerBoundDefined),
False,
)
}
}

fn recover_outputs(_n: Int, _commits: List<OutputReference>, _outputs: List<Output>){
True
}

fn traceIfFalse(traceLog: String, predicate: Bool) -> Bool {
if predicate {
True
} else {
trace traceLog
False
}
}

0 comments on commit ce5c6b4

Please sign in to comment.