Skip to content

Commit

Permalink
use lucid instead of translucent due to wasm error
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroProofs committed Jun 9, 2024
1 parent 01ca40e commit 3b8ebec
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 451 deletions.
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 = 1717698560, nanos_since_epoch = 351748000 }, "98cf81aa68f9ccf68bc5aba9be06d06cb1db6e8eff60b668ed5e8ddf3588206b"]
"aiken-lang/fuzz@main" = [{ secs_since_epoch = 1717904342, nanos_since_epoch = 572523000 }, "a8294651f1577c671d580c99c9bc5445ef1fd44e4aa3dde550434a4cbc8d50b6"]
194 changes: 118 additions & 76 deletions lib/fortunav2.ak
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use aiken/builtin
use aiken/bytearray
use aiken/dict
use aiken/dict.{Dict}
use aiken/interval.{Finite}
use aiken/list
use aiken/pairs
use aiken/transaction.{Input, Output, Transaction} as tx
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 fortuna.{master_token_name}
use fortuna/parameters.{latest_merkle_root, voting_days}
use fortuna/types.{State, Statev2}
use fortuna/utils.{get_inline_datum, integer_to_bytes, list_at}
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"
Expand Down Expand Up @@ -46,7 +50,9 @@ pub fn genesis_v2(tx, own_policy, fortuna_v1_hash: Data, fork_script_hash: Data)
expect Some(v1_miner_ref) = {
let input <- list.find(reference_inputs)
input.output.value
|> value.quantity_of(fortuna_v1_hash, master_token_name)
|> value.to_dict
|> dict.to_pairs
|> quantity_of(fortuna_v1_hash, master_token_name)
|> builtin.equals_integer(1)
}

Expand Down Expand Up @@ -133,22 +139,111 @@ fn mini_loop(assets: Pairs<AssetName, Int>, script_hash: AssetName) -> Int {
}
}

fn quantity_of(
value: Pairs<PolicyId, Pairs<AssetName, Int>>,
pub fn quantity_of(
value: Pairs<PolicyId, Dict<AssetName, Int>>,
own_hash: PolicyId,
script_hash: AssetName,
) -> Int {
when value is {
[] -> 0
[Pair(policy, assets), ..rest] ->
if policy == own_hash {
mini_loop(assets, script_hash)
mini_loop(assets |> dict.to_pairs, script_hash)
} else {
quantity_of(rest, own_hash, script_hash)
}
}
}

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,
}
}

pub fn vote(
for: Bool,
own_ref: OutputReference,
tx: Transaction,
dat: TunaUpgradeProcess,
) {
let Transaction { inputs, outputs, reference_inputs, validity_range, .. } = tx

expect Nominated {
script_hash,
deadline,
for_count,
anti_script_hash,
against_count,
} = dat

let check_hash =
if for {
script_hash
} else {
anti_script_hash
}

let Output { address: in_address, value: in_value, .. } =
resolve_output_reference(inputs, own_ref)

expect ScriptCredential(own_hash) = in_address.payment_credential

expect Finite(upper_range) = validity_range.upper_bound.bound_type

let Output { address: out_address, value: out_value, datum: out_datum, .. } =
utils.list_at(outputs, 0)

let votes_in_tx =
count_votes(
reference_inputs,
own_hash,
check_hash,
Some(Inline(ScriptCredential(check_hash))),
)

let expected_datum =
InlineDatum(
Nominated {
script_hash,
for_count: if for {
votes_in_tx
} else {
for_count
},
deadline,
anti_script_hash,
against_count: if for {
against_count
} else {
votes_in_tx
},
},
)

and {
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),
votes_in_tx > for_count,
expected_datum == out_datum,
}
}

pub fn count_votes(
reference_inputs: List<Input>,
own_hash: PolicyId,
Expand All @@ -161,12 +256,11 @@ pub fn count_votes(
let Output { address, value, .. } = input.output

if stake_cred == address.stake_credential {
value.quantity_of(value, own_hash, fortuna.token_name) + count_votes(
rest,
quantity_of(
value |> value.to_dict |> dict.to_pairs,
own_hash,
script_hash,
stake_cred,
)
fortuna.token_name,
) + count_votes(rest, own_hash, script_hash, stake_cred)
} else {
when value |> value.tokens(own_hash) |> dict.to_pairs is {
[Pair(tuna_name, tuna_quantity), Pair(value_script_hash, 1)] ->
Expand All @@ -189,67 +283,15 @@ pub fn count_votes(
}
}
}
// A little experiment to see how the current optimizer handles the_counter function

// pub fn the_counter() {
// super_optimize()
// }

// pub fn super_optimize() {
// repeated_count_votes(10)
// }

// pub type FakeOutput {
// address: Address,
// value: Pairs<PolicyId, Pairs<AssetName, Int>>,
// datum: Datum,
// thing: Data,
// }

// pub type FakeInput {
// thing: OutputReference,
// output: FakeOutput,
// }

// pub fn repeated_count_votes(a: Int) {
// if a == 0 {
// fn(
// reference_inputs: List<FakeInput>,
// own_hash: PolicyId,
// script_hash: AssetName,
// stake_cred: Option<StakeCredential>,
// ) {
// count_votes(reference_inputs, own_hash, script_hash, stake_cred)
// }
// } else {
// fn(
// reference_inputs: List<FakeInput>,
// own_hash: PolicyId,
// script_hash: AssetName,
// stake_cred: Option<StakeCredential>,
// ) {
// when reference_inputs is {
// [] -> 0
// [input, ..rest] -> {
// let FakeOutput { address, value, .. } = input.output

// let x =
// if stake_cred == address.stake_credential {
// quantity_of(value, own_hash, script_hash)
// } else if quantity_of(value, own_hash, script_hash) == 1 {
// quantity_of(value, own_hash, script_hash)
// } else {
// 0
// }

// x + repeated_count_votes(a - 1)(
// rest,
// own_hash,
// script_hash,
// stake_cred,
// )
// }
// }
// }
// }
// }

pub fn expect_first(self: Pairs<key, value>, key k: key) -> value {
when self is {
[] -> fail
[Pair(k2, v), ..rest] ->
if k == k2 {
v
} else {
expect_first(rest, k)
}
}
}
36 changes: 16 additions & 20 deletions miner/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import {
fromHex,
fromText,
generateSeedPhrase,
KupmiosV5 as Kupmios,
Translucent,
Kupmios,
Lucid,
type Script,
toHex,
UTxO,
applyDoubleCborEncoding,
} from 'translucent-cardano/index';
} from 'lucid-cardano';
import fs from 'fs';
import crypto from 'crypto';
import { WebSocket } from 'ws';
Expand Down Expand Up @@ -105,7 +104,7 @@ app
);

const provider = new Kupmios(kupoUrl, ogmiosUrl);
const lucid = await Translucent.new(provider, preview ? 'Preview' : 'Mainnet');
const lucid = await Lucid.new(provider, preview ? 'Preview' : 'Mainnet');
lucid.selectWalletFromSeed(fs.readFileSync('seed.txt', { encoding: 'utf8' }));

const userPkh = lucid.utils.getAddressDetails(await lucid.wallet.address()).paymentCredential!;
Expand Down Expand Up @@ -348,7 +347,7 @@ app
const unAppliedValidator = readValidator();

const provider = new Kupmios(kupoUrl, ogmiosUrl);
const lucid = await Translucent.new(provider, preview ? 'Preview' : 'Mainnet');
const lucid = await Lucid.new(provider, preview ? 'Preview' : 'Mainnet');
lucid.selectWalletFromSeed(fs.readFileSync('seed.txt', { encoding: 'utf-8' }));

const utxos = await lucid.wallet.getUtxos();
Expand Down Expand Up @@ -458,9 +457,8 @@ app
encoding: 'utf-8',
});

console.log(forkMerkleRoot);
const provider = new Kupmios(kupoUrl, ogmiosUrl);
const lucid = await Translucent.new(provider, preview ? 'Preview' : 'Mainnet');
const lucid = await Lucid.new(provider, preview ? 'Preview' : 'Mainnet');
lucid.selectWalletFromSeed(fs.readFileSync('seed.txt', { encoding: 'utf-8' }));

const utxos = await lucid.wallet.getUtxos();
Expand All @@ -481,9 +479,7 @@ app

const forkValidatorApplied: Script = {
type: 'PlutusV2',
script: applyDoubleCborEncoding(
applyParamsToScript(forkValidator.script, [initOutputRef, fortunaV1Hash]),
),
script: applyParamsToScript(forkValidator.script, [initOutputRef, fortunaV1Hash]),
};

console.log('here22211111');
Expand All @@ -496,9 +492,7 @@ app

const tunaV2MintApplied: Script = {
type: 'PlutusV2',
script: applyDoubleCborEncoding(
applyParamsToScript(fortunaV2Mint.script, [fortunaV1Hash, forkValidatorHash]),
),
script: applyParamsToScript(fortunaV2Mint.script, [fortunaV1Hash, forkValidatorHash]),
};

console.log('here222333');
Expand All @@ -507,9 +501,7 @@ app

const tunaV2SpendApplied: Script = {
type: 'PlutusV2',
script: applyDoubleCborEncoding(
applyParamsToScript(fortunaV2Spend.script, [tunaV2MintAppliedHash]),
),
script: applyParamsToScript(fortunaV2Spend.script, [tunaV2MintAppliedHash]),
};

const tunaV2SpendAppliedHash = lucid.utils.validatorToScriptHash(tunaV2SpendApplied);
Expand Down Expand Up @@ -537,9 +529,14 @@ app

const blockNumber = bn as bigint;

const blockNumberHex =
blockNumber.toString(16).length % 2 === 0
? blockNumber.toString(16)
: `0${blockNumber.toString(16)}`;

const masterTokensV2 = {
[tunaV2MintAppliedHash + fromText('TUNA') + tunaV2SpendAppliedHash]: 1n,
[tunaV2MintAppliedHash + fromText('COUNTER') + blockNumber.toString(16)]: 1n,
[tunaV2MintAppliedHash + fromText('COUNTER') + blockNumberHex]: 1n,
};

const forkLockToken = {
Expand Down Expand Up @@ -584,7 +581,6 @@ app
.attachSpendingValidator(tunaV2SpendApplied)
.attachMintingPolicy(tunaV2MintApplied)
.attachMintingPolicy(forkValidatorApplied)
.attachWithdrawalValidator(forkValidatorApplied)
.readFrom([lastestV1Block])
.registerStake(forkValidatorRewardAddress)
.withdraw(forkValidatorRewardAddress, 0n, forkRedeemer)
Expand Down Expand Up @@ -687,7 +683,7 @@ app
.addOption(previewOption)
.action(async ({ preview, ogmiosUrl, kupoUrl }) => {
const provider = new Kupmios(kupoUrl, ogmiosUrl);
const lucid = await Translucent.new(provider, preview ? 'Preview' : 'Mainnet');
const lucid = await Lucid.new(provider, preview ? 'Preview' : 'Mainnet');

lucid.selectWalletFromSeed(fs.readFileSync('seed.txt', { encoding: 'utf-8' }));

Expand Down

0 comments on commit 3b8ebec

Please sign in to comment.