-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Anastasia-Labs/feature/merkelized-validators
Implement Merkelized Validators Pattern
- Loading branch information
Showing
10 changed files
with
122 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use aiken/dict.{Dict} | ||
use aiken/hash.{Blake2b_224, Hash} | ||
use aiken/transaction.{Redeemer, ScriptContext, ScriptPurpose, WithdrawFrom} | ||
use aiken/transaction/credential.{Inline, Script, ScriptCredential} | ||
|
||
pub type WithdrawRedeemer<a, b> { | ||
input_args: List<a>, | ||
results: List<b>, | ||
} | ||
|
||
pub fn spend( | ||
staking_validator: Hash<Blake2b_224, Script>, | ||
function_args: List<Data>, | ||
redeemers: Dict<ScriptPurpose, Redeemer>, | ||
) -> List<Data> { | ||
expect Some(rdmr) = | ||
redeemers | ||
|> dict.get(WithdrawFrom(Inline(ScriptCredential(staking_validator)))) | ||
expect WithdrawRedeemer { input_args, results }: WithdrawRedeemer<Data, Data> = | ||
rdmr | ||
|
||
// Given input arguments must be identical to the ones provided to the | ||
// withdrawal validator. | ||
expect (input_args == function_args)? | ||
results | ||
} | ||
|
||
pub fn withdraw( | ||
function: fn(List<a>) -> List<b>, | ||
redeemer: WithdrawRedeemer<a, b>, | ||
ctx: ScriptContext, | ||
) -> Bool { | ||
expect ScriptContext { purpose: WithdrawFrom(_), .. } = ctx | ||
let WithdrawRedeemer { input_args, results } = redeemer | ||
let computed_results = function(input_args) | ||
results == computed_results | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use aiken/hash.{Blake2b_224, Hash} | ||
use aiken/transaction.{ScriptContext, Transaction} | ||
use aiken/transaction/credential.{Script} | ||
use aiken_design_patterns/merkelized_validator.{WithdrawRedeemer} as merkelized_validator | ||
use aiken_design_patterns/utils.{sum_of_squares} | ||
|
||
validator(stake_validator: Hash<Blake2b_224, Script>) { | ||
fn spend(x: Int, y: Int, ctx: ScriptContext) { | ||
let ScriptContext { transaction: tx, .. } = ctx | ||
let xData: Data = x | ||
let yData: Data = y | ||
expect [sumData] = | ||
merkelized_validator.spend(stake_validator, [xData, yData], tx.redeemers) | ||
expect sum: Int = sumData | ||
sum < 42 | ||
} | ||
} | ||
|
||
validator { | ||
fn withdraw(redeemer: WithdrawRedeemer<Int, Int>, ctx: ScriptContext) { | ||
merkelized_validator.withdraw(sum_of_squares, redeemer, ctx) | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.