Skip to content

Commit

Permalink
Rename modules, move generic functions to upstream dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Sep 20, 2024
1 parent 3d5d65e commit 5b7c49d
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 240 deletions.
17 changes: 17 additions & 0 deletions lib/aiken/collection/list/extra.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// This Source Code Form is subject to the terms of the Mozilla Public
//// License, v. 2.0. If a copy of the MPL was not distributed with this
//// file, You can obtain one at http://mozilla.org/MPL/2.0/.

/// Insert an element in a list at the given position.
pub fn insert(self: List<a>, ix: Int, elem: a) -> List<a> {
when self is {
[] ->
[elem]
[head, ..tail] ->
if ix == 0 {
[elem, head, ..tail]
} else {
[head, ..insert(tail, ix - 1, elem)]
}
}
}
15 changes: 15 additions & 0 deletions lib/aiken/collection/list/extra.test.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// This Source Code Form is subject to the terms of the Mozilla Public
//// License, v. 2.0. If a copy of the MPL was not distributed with this
//// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use aiken/collection/list/extra.{insert}

test insert_examples() {
and {
(insert([], 0, elem: 0) == [0])?,
(insert([], 2, elem: 0) == [0])?,
(insert([1, 2], 0, elem: 0) == [0, 1, 2])?,
(insert([1, 2], 2, elem: 0) == [1, 2, 0])?,
(insert([1, 2], -1, elem: 0) == [1, 2, 0])?,
}
}
20 changes: 10 additions & 10 deletions lib/aiken/fuzz/scenario.ak
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//// This Source Code Form is subject to the terms of the Mozilla Public
//// License, v. 2.0. If a copy of the MPL was not distributed with this
//// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use aiken/cbor.{serialise}
use aiken/collection/dict
Expand Down Expand Up @@ -255,22 +255,22 @@ fn publish_handlers(
True,
fn(ix, certificate, st) {
when certificate is {
UnregisterCredential { credential, .. } |
DelegateCredential { credential, .. } |
RegisterAndDelegateCredential { credential, .. } |
UnregisterCredential { credential, .. } |
DelegateCredential { credential, .. } |
RegisterAndDelegateCredential { credential, .. } |
RegisterDelegateRepresentative {
delegate_representative: credential,
..
} |
UpdateDelegateRepresentative { delegate_representative: credential } |
} |
UpdateDelegateRepresentative { delegate_representative: credential } |
UnregisterDelegateRepresentative {
delegate_representative: credential,
..
} |
} |
AuthorizeConstitutionalCommitteeProxy {
constitutional_committee_member: credential,
..
} |
} |
RetireFromConstitutionalCommittee {
constitutional_committee_member: credential,
} ->
Expand Down
185 changes: 4 additions & 181 deletions lib/cardano/generator.ak
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//// This Source Code Form is subject to the terms of the Mozilla Public
//// License, v. 2.0. If a copy of the MPL was not distributed with this
//// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use aiken/collection/list
use aiken/crypto.{ScriptHash}
use aiken/fuzz.{
and_then, byte, bytearray_between, constant, int, int_at_least, int_between,
and_then, bytearray_between, constant, int, int_at_least, int_between,
list_between, map, option,
}
use cardano/address.{Address, Inline, Script, VerificationKey}
Expand All @@ -14,183 +14,6 @@ use cardano/transaction.{
Datum, DatumHash, InlineDatum, Input, NoDatum, Output, OutputReference,
}

// ---------------------------------------------- TODO: Move to the fuzz library

pub fn pick(xs: List<a>) -> Fuzzer<(Int, a)> {
let ix <- map(int_between(0, list.length(xs) - 1))
expect Some(x) = list.at(xs, ix)
(ix, x)
}

pub fn either3(
a: Fuzzer<result>,
b: Fuzzer<result>,
c: Fuzzer<result>,
) -> Fuzzer<result> {
let ix <- and_then(byte())
if ix < 85 {
a
} else if ix < 170 {
b
} else {
c
}
}

pub fn either4(
a: Fuzzer<result>,
b: Fuzzer<result>,
c: Fuzzer<result>,
d: Fuzzer<result>,
) -> Fuzzer<result> {
let ix <- and_then(byte())
if ix < 128 {
if ix < 64 {
a
} else {
b
}
} else {
if ix < 192 {
c
} else {
d
}
}
}

pub fn either5(
a: Fuzzer<result>,
b: Fuzzer<result>,
c: Fuzzer<result>,
d: Fuzzer<result>,
e: Fuzzer<result>,
) -> Fuzzer<result> {
let ix <- and_then(byte())
if ix < 102 {
if ix < 51 {
a
} else {
b
}
} else if ix < 204 {
if ix < 153 {
c
} else {
d
}
} else {
e
}
}

pub fn either6(
a: Fuzzer<result>,
b: Fuzzer<result>,
c: Fuzzer<result>,
d: Fuzzer<result>,
e: Fuzzer<result>,
f: Fuzzer<result>,
) -> Fuzzer<result> {
let ix <- and_then(byte())
if ix < 127 {
if ix < 42 {
a
} else if ix < 85 {
b
} else {
c
}
} else {
if ix < 170 {
d
} else if ix < 212 {
e
} else {
f
}
}
}

pub fn either7(
a: Fuzzer<result>,
b: Fuzzer<result>,
c: Fuzzer<result>,
d: Fuzzer<result>,
e: Fuzzer<result>,
f: Fuzzer<result>,
g: Fuzzer<result>,
) -> Fuzzer<result> {
let ix <- and_then(byte())
if ix < 109 {
if ix < 36 {
a
} else if ix < 72 {
b
} else {
c
}
} else {
if ix < 182 {
if ix < 145 {
d
} else {
e
}
} else {
if ix < 218 {
f
} else {
g
}
}
}
}

pub fn either8(
a: Fuzzer<result>,
b: Fuzzer<result>,
c: Fuzzer<result>,
d: Fuzzer<result>,
e: Fuzzer<result>,
f: Fuzzer<result>,
g: Fuzzer<result>,
h: Fuzzer<result>,
) -> Fuzzer<result> {
let ix <- and_then(byte())
if ix < 128 {
if ix < 64 {
if ix < 32 {
a
} else {
b
}
} else {
if ix < 96 {
c
} else {
d
}
}
} else {
if ix < 192 {
if ix < 160 {
e
} else {
f
}
} else {
if ix < 224 {
g
} else {
h
}
}
}
}

// -----------------------------------------------------------------------------

pub fn any_asset_name() -> Fuzzer<AssetName> {
bytearray_between(0, 32)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/cardano/credential/proxy.ak → lib/zhuli/predicate.ak
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use cardano/assets.{Lovelace, PolicyId, Value, ada_policy_id}
use cardano/certificate.{
RegisterDelegateRepresentative, UnregisterDelegateRepresentative,
}
use cardano/credential/proxy/state.{DelegateKind}
use cardano/transaction.{Input, NoDatum, Output, Transaction}
use sundae/multisig.{MultisigScript}
use zhuli/state.{DelegateKind}

pub type Update {
Register
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use aiken/crypto.{ScriptHash}
use aiken/fuzz.{both}
use cardano/address.{Address}
use cardano/assets.{ada_policy_id}
use cardano/credential/proxy.{must_forward_strict_assets}
use cardano/generator.{any_ada_only_value, any_value_with}
use sundae/multisig.{MultisigScript}
use zhuli/predicate.{must_forward_strict_assets}

// NOTE: Needs not to be an actual policy id, as we never re-hash anything. So
// we might as well make it something we can easily recognize.
Expand Down
File renamed without changes.
74 changes: 74 additions & 0 deletions lib/zhuli/state.test.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//// This Source Code Form is subject to the terms of the Mozilla Public
//// License, v. 2.0. If a copy of the MPL was not distributed with this
//// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use aiken/collection/list
use aiken/fuzz.{
and_then, both, bytearray_between, constant, either, int, int_between,
label_when, list_between, map,
}
use sundae/multisig.{
After, AllOf, AnyOf, AtLeast, Before, MultisigScript, Script, Signature,
}
use zhuli/state.{
BlockProduction, DelegateKind, Governance, into_asset_name, match_prefix,
}

test prop_prefix(
(kind, script) via both(any_delegate_kind(), any_multisig_script()),
) {
label_when(kind == Governance, @"Governance", @"BlockProduction")
match_prefix(kind, into_asset_name(kind, script))
}

fn any_delegate_kind() -> Fuzzer<DelegateKind> {
either(constant(BlockProduction), constant(Governance))
}

fn any_multisig_script() -> Fuzzer<MultisigScript> {
any_multisig_script_at_depth(3)
}

fn any_multisig_script_at_depth(depth: Int) -> Fuzzer<MultisigScript> {
if depth <= 0 {
map(bytearray_between(28, 28), Signature)
} else {
let variant <- and_then(int_between(0, 7))
if variant < 4 {
if variant < 2 {
if variant < 1 {
map(bytearray_between(28, 28), Signature)
} else {
map(
list_between(any_multisig_script_at_depth(depth - 1), 0, depth),
AllOf,
)
}
} else {
if variant == 2 {
map(
list_between(any_multisig_script_at_depth(depth - 1), 0, depth),
AnyOf,
)
} else {
let scripts <-
and_then(
list_between(any_multisig_script_at_depth(depth - 1), 0, depth),
)
let required <- map(int_between(0, list.length(scripts)))
AtLeast { required, scripts }
}
}
} else {
if variant < 6 {
if variant < 5 {
map(int(), Before)
} else {
map(int(), After)
}
} else {
map(bytearray_between(28, 28), Script)
}
}
}
}
Loading

0 comments on commit 5b7c49d

Please sign in to comment.