Skip to content

Commit

Permalink
minimize similar functions that take up space to just using one funct…
Browse files Browse the repository at this point in the history
…ion.

Fix a few governance edge cases
  • Loading branch information
MicroProofs committed Jul 8, 2024
1 parent ffef7ba commit 3d1ee87
Show file tree
Hide file tree
Showing 12 changed files with 582 additions and 177 deletions.
19 changes: 14 additions & 5 deletions V1PreviewHistory.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
[
{
"current_hash": "357dc4014a70c866c01c5c0966b6c0ecebf826c0475877a92491d708d8a2a683"
"current_hash": "8a623238dcfe41cd25356cac234e202b8cd3a7e9fe295e462818c610e4b82a8d"
},
{
"current_hash": "0000077d6412d9d9006bd5abd01191087e7a0b41aa7e4f9b69924eda9b8d564a"
"current_hash": "00000c4708deb49a1076a185b4ab963ea7107ee08fd94bbe12ad7f8072a210c4"
},
{
"current_hash": "00000fcdcb33f425a1fae31d92a847bdc60d75cf90a132608bf9074e9675d23e"
"current_hash": "00000f3390355d958e16eaf7252f70058fe19cebb0b1a7f940e2ce18021d8bfe"
},
{
"current_hash": "000002057bf654592884cdd370317eeec835545c5b4909f15fae751b24b014db"
"current_hash": "000002694ef5552134bb700fe031c6591bd4b1d81c1bb4a117b635662953d039"
},
{
"current_hash": "00000d1c0cc8af18e5e69d1f43196f528af8b2b08d52467376b657b7232172a3"
"current_hash": "00000a252051028bd77ab25139419ed03254ba24a8332479176c4df8005d7336"
},
{
"current_hash": "000002fbe7b31cad029b21e69ee39227029be382c8a6f2338405d0b1bf681eaf"
},
{
"current_hash": "00000769debb1fe2576c1c81037073754dadf18431519bd2fab3a2d8abad1ae7"
},
{
"current_hash": "00000e4ed1ded7f0336ec69e01310a1e4b73eaad1f4a900b7c99d30448c5cfee"
}
]
2 changes: 1 addition & 1 deletion aiken.lock
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ requirements = []
source = "github"

[etags]
"aiken-lang/fuzz@main" = [{ secs_since_epoch = 1720150801, nanos_since_epoch = 219072000 }, "a8294651f1577c671d580c99c9bc5445ef1fd44e4aa3dde550434a4cbc8d50b6"]
"aiken-lang/fuzz@main" = [{ secs_since_epoch = 1720397232, nanos_since_epoch = 581616000 }, "a8294651f1577c671d580c99c9bc5445ef1fd44e4aa3dde550434a4cbc8d50b6"]
2 changes: 1 addition & 1 deletion currentPreviewRoot.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e93fd5647f4c458e3a339c9700e1f86c66b55e133a768c32612c9f50ccdbf3f5
98c75e07ffcfd0f40cae415ba423d3e557cc7f24051c9d8dfbf73f0605f8d2bb
2 changes: 1 addition & 1 deletion genesis/preview.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion genesis/previewV2.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion governance/previewV2.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/fortuna/parameters.ak
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub const padding = 16
// New Params for V2

pub const latest_merkle_root =
#"e93fd5647f4c458e3a339c9700e1f86c66b55e133a768c32612c9f50ccdbf3f5"
#"98e42f28ca4afbe7bd43686163ad059342ab93d293321a03b5a272286695ab51"

// pub const latest_merkle_root =
// #"08f6adda95e55efab85705f7b3313559db6750f5d7385de9b11d932a8db86204"
Expand Down
15 changes: 15 additions & 0 deletions lib/fortuna/types.ak
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,18 @@ pub type TunaAction {
BurnToken
FinalizeNomination(OutputReference, OutputReference, Int, Int)
}

pub type TunaUpgradeProcess {
Nominated {
script_hash: ByteArray,
for_count: Int,
anti_script_hash: ByteArray,
against_count: Int,
deadline: Int,
}
Mining {
script_hash: ByteArray,
miner_support_count: Int,
block_height_deadline: Int,
}
}
73 changes: 34 additions & 39 deletions lib/fortunav2.ak
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@ use aiken/bytearray
use aiken/dict.{Dict}
use aiken/interval.{Finite}
use aiken/list
use aiken/pairs
use aiken/transaction.{InlineDatum,
Input, Output, OutputReference, Transaction} as tx
use aiken/transaction/credential.{
Address, Inline, ScriptCredential, StakeCredential,
}
use aiken/transaction/value.{AssetName, PolicyId}
use aiken/transaction/value.{AssetName, PolicyId, Value}
use fortuna.{master_token_name}
use fortuna/parameters.{latest_merkle_root, voting_days}
use fortuna/types.{State, Statev2}
use fortuna/types.{Nominated, State, Statev2, TunaUpgradeProcess}
use fortuna/utils.{
get_inline_datum, integer_to_bytes, list_at, resolve_output_reference,
}
use hardfork/hftypes.{HardFork, NftForkAction}

pub const big_tuna_prefix = "TUNA"

pub const big_tuna_length = 4

pub const counter_prefix = "COUNTER"

pub const counter_length = 7

pub const nominated_prefix = "NOMA"

pub fn genesis_v2(tx, own_policy, fortuna_v1_hash: Data, fork_script_hash: Data) {
Expand All @@ -34,12 +37,11 @@ pub fn genesis_v2(tx, own_policy, fortuna_v1_hash: Data, fork_script_hash: Data)
expect [Pair(_, 1), Pair(_, 1)] =
mint
|> value.from_minted_value
|> value.tokens(own_policy)
|> dict.to_pairs
|> tokens(own_policy)

let fork_purpose = tx.WithdrawFrom(Inline(ScriptCredential(fork_script_hash)))

expect Some(fork_redeemer) = pairs.get_first(tx.redeemers, fork_purpose)
let fork_redeemer = expect_first(tx.redeemers, fork_purpose)

// By looking at the other contract redeemer action that can only run once,
// We can ensure this action only runs once.
Expand Down Expand Up @@ -102,7 +104,7 @@ pub fn genesis_v2(tx, own_policy, fortuna_v1_hash: Data, fork_script_hash: Data)
}

// Voting and governance functions

// TODO change back to normal before launch
pub fn voting_period() -> Int {
1000 * 60 * 20 * 1 * voting_days
}
Expand All @@ -127,18 +129,6 @@ fn do_flip_hash(hash: ByteArray, new_hash: ByteArray, index: Int) -> ByteArray {
}
}

fn mini_loop(assets: Pairs<AssetName, Int>, script_hash: AssetName) -> Int {
when assets is {
[] -> 0
[Pair(asset, quantity), ..rest] ->
if asset == script_hash {
quantity
} else {
mini_loop(rest, script_hash)
}
}
}

pub fn quantity_of(
value: Pairs<PolicyId, Dict<AssetName, Int>>,
own_hash: PolicyId,
Expand All @@ -155,18 +145,15 @@ pub fn quantity_of(
}
}

pub type TunaUpgradeProcess {
Nominated {
script_hash: ByteArray,
for_count: Int,
anti_script_hash: ByteArray,
against_count: Int,
deadline: Int,
}
Mining {
script_hash: ByteArray,
miner_support_count: Int,
block_height_deadline: Int,
fn mini_loop(assets: Pairs<AssetName, Int>, script_hash: AssetName) -> Int {
when assets is {
[] -> 0
[Pair(asset, quantity), ..rest] ->
if asset == script_hash {
quantity
} else {
mini_loop(rest, script_hash)
}
}
}

Expand Down Expand Up @@ -231,20 +218,20 @@ pub fn vote(
)

and {
(quantity_of(
quantity_of(
in_value |> value.to_dict |> dict.to_pairs,
own_hash,
bytearray.concat(nominated_prefix, script_hash),
) == 1)?,
(in_address == out_address)?,
(upper_range <= deadline)?,
(value.without_lovelace(in_value) == value.without_lovelace(out_value))?,
) == 1,
in_address == out_address,
upper_range <= deadline,
value.without_lovelace(in_value) == value.without_lovelace(out_value),
if for {
votes_in_tx > for_count
} else {
votes_in_tx > against_count
}?,
(expected_datum == out_datum)?,
},
expected_datum == out_datum,
}
}

Expand All @@ -266,7 +253,7 @@ pub fn count_votes(
fortuna.token_name,
) + count_votes(rest, own_hash, script_hash, stake_cred)
} else {
when value |> value.tokens(own_hash) |> dict.to_pairs is {
when value |> tokens(own_hash) is {
[Pair(tuna_name, tuna_quantity), Pair(value_script_hash, 1)] ->
if
tuna_name == fortuna.token_name && value_script_hash == script_hash{
Expand Down Expand Up @@ -299,3 +286,11 @@ pub fn expect_first(self: Pairs<key, value>, key k: key) -> value {
}
}
}

pub fn tokens(value: Value, own_policy: PolicyId) -> Pairs<AssetName, Int> {
value
|> value.to_dict()
|> dict.to_pairs()
|> expect_first(own_policy)
|> dict.to_pairs()
}
Loading

0 comments on commit 3d1ee87

Please sign in to comment.