Skip to content

Commit

Permalink
commiting current working preview code
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroProofs committed Jun 11, 2024
1 parent 69be9d4 commit 823b460
Show file tree
Hide file tree
Showing 30 changed files with 76 additions and 589 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 = 1718035763, nanos_since_epoch = 437230000 }, "a8294651f1577c671d580c99c9bc5445ef1fd44e4aa3dde550434a4cbc8d50b6"]
"aiken-lang/fuzz@main" = [{ secs_since_epoch = 1718053478, nanos_since_epoch = 551673000 }, "a8294651f1577c671d580c99c9bc5445ef1fd44e4aa3dde550434a4cbc8d50b6"]
2 changes: 1 addition & 1 deletion currentPreviewRoot.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f818d6aba5abf8c471f40ff8d4ad4177b85dadbb453a42c430191091a7e50c7b
4fdcf1ae84ccbe49b7b2b9cc8e2ef1ef0ac7b380b8c3a3abe06abb8a30878d32
1 change: 0 additions & 1 deletion currentRoot.txt

This file was deleted.

Binary file removed db/000211.log
Binary file not shown.
Binary file removed db/000213.ldb
Binary file not shown.
Binary file removed db/000214.ldb
Binary file not shown.
Binary file removed db/000215.ldb
Binary file not shown.
Binary file removed db/000216.ldb
Binary file not shown.
Binary file removed db/000217.ldb
Binary file not shown.
Binary file removed db/000218.ldb
Binary file not shown.
1 change: 0 additions & 1 deletion db/CURRENT

This file was deleted.

Empty file removed db/LOCK
Empty file.
536 changes: 0 additions & 536 deletions db/LOG

This file was deleted.

Binary file removed db/MANIFEST-000002
Binary file not shown.
Binary file added dbPreview/000005.ldb
Binary file not shown.
Binary file added dbPreview/000008.ldb
Binary file not shown.
Binary file added dbPreview/000009.log
Binary file not shown.
Binary file removed dbPreview/000037.ldb
Binary file not shown.
Binary file removed dbPreview/000039.ldb
Binary file not shown.
Binary file removed dbPreview/000042.log
Binary file not shown.
2 changes: 1 addition & 1 deletion dbPreview/CURRENT
Original file line number Diff line number Diff line change
@@ -1 +1 @@
MANIFEST-000041
MANIFEST-000007
8 changes: 5 additions & 3 deletions dbPreview/LOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
2024/06/10-12:28:03.838247 1724b3000 Recovering log #40
2024/06/10-12:28:03.839507 1724b3000 Delete type=3 #38
2024/06/10-12:28:03.839611 1724b3000 Delete type=0 #40
2024/06/10-17:40:00.288491 1702f7000 Recovering log #6
2024/06/10-17:40:00.289101 1702f7000 Level-0 table #8: started
2024/06/10-17:40:00.289720 1702f7000 Level-0 table #8: 23577 bytes OK
2024/06/10-17:40:00.290163 1702f7000 Delete type=0 #6
2024/06/10-17:40:00.290281 1702f7000 Delete type=3 #4
10 changes: 5 additions & 5 deletions dbPreview/LOG.old
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
2024/06/10-12:23:38.635886 172d0f000 Recovering log #36
2024/06/10-12:23:38.636406 172d0f000 Level-0 table #39: started
2024/06/10-12:23:38.636955 172d0f000 Level-0 table #39: 4314 bytes OK
2024/06/10-12:23:38.637412 172d0f000 Delete type=3 #34
2024/06/10-12:23:38.637458 172d0f000 Delete type=0 #36
2024/06/10-17:38:19.037904 1736ef000 Recovering log #3
2024/06/10-17:38:19.038484 1736ef000 Level-0 table #5: started
2024/06/10-17:38:19.038981 1736ef000 Level-0 table #5: 1501 bytes OK
2024/06/10-17:38:19.039432 1736ef000 Delete type=0 #3
2024/06/10-17:38:19.039469 1736ef000 Delete type=3 #2
Binary file added dbPreview/MANIFEST-000007
Binary file not shown.
Binary file removed dbPreview/MANIFEST-000041
Binary file not shown.
2 changes: 1 addition & 1 deletion genesis/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 =
#"f818d6aba5abf8c471f40ff8d4ad4177b85dadbb453a42c430191091a7e50c7b"
#"4fdcf1ae84ccbe49b7b2b9cc8e2ef1ef0ac7b380b8c3a3abe06abb8a30878d32"

// pub const latest_merkle_root =
// #"08f6adda95e55efab85705f7b3313559db6750f5d7385de9b11d932a8db86204"
Expand Down
32 changes: 17 additions & 15 deletions miner/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ app
const alwaysLoop = true;

// Construct a new trie with on-disk storage under the file path 'db'.
const trie: Trie = await Trie.load(new Store(preview ? 'dbPreview' : 'db'));
let trie: Trie = await Trie.load(new Store(preview ? 'dbPreview' : 'db'));

const {
tunaV2MintValidator: { validatorHash: tunav2ValidatorHash },
Expand Down Expand Up @@ -227,14 +227,17 @@ app

difficulty = getDifficulty(targetHash);

const { leadingZeros, difficultyNumber: difficultyNumber } = difficulty;
const { leadingZeros, difficultyNumber } = difficulty;

if (
leadingZeros > (state.fields[2] as bigint) ||
(leadingZeros == (state.fields[2] as bigint) &&
difficultyNumber < (state.fields[3] as bigint))
) {
console.log(toHex(targetHash));
console.log(toHex(targetState));
// Found a valid nonce so break out of the loop
nonce = targetState.slice(4, 20);
break;
}

Expand All @@ -245,12 +248,10 @@ app

const realTimeNow = Number((Date.now() / 1000).toFixed(0)) * 1000 - 60000;

await trie.insert(Buffer.from(blake256(targetHash)), Buffer.from(targetHash));
trie = await trie.insert(Buffer.from(blake256(targetHash)), Buffer.from(targetHash));

const newMerkleRoot = trie.hash;

console.log(`New Merkle Root: ${toHex(newMerkleRoot)}`);

let epochTime =
(state.fields[4] as bigint) + BigInt(90000 + realTimeNow) - (state.fields[5] as bigint);

Expand Down Expand Up @@ -279,7 +280,7 @@ app

const postDatum = new Constr(0, [
(state.fields[0] as bigint) + 1n,
toHex(targetHash!),
toHex(targetHash),
leadingZeros,
difficultyNumber,
epochTime,
Expand Down Expand Up @@ -325,7 +326,7 @@ app
console.log(minerRedeemer);

const mintRedeemer = Data.to(
new Constr(2, [
new Constr(1, [
new Constr(0, [new Constr(0, [minerOutput.txHash]), BigInt(minerOutput.outputIndex)]),
state.fields[0] as bigint,
]),
Expand All @@ -334,7 +335,7 @@ app
const txMine = await lucid
.newTx()
.collectFrom([minerOutput], minerRedeemer)
.payToAddressWithData(tunaV2ValidatorAddress, { inline: outDat }, masterTokens)
.payToContract(tunaV2ValidatorAddress, { inline: outDat }, masterTokens)
.mintAssets(mintTokens, mintRedeemer)
.addSigner(await lucid.wallet.address())
.readFrom(readUtxos)
Expand All @@ -353,6 +354,7 @@ app
await delay(5000);
} catch (e) {
console.log(e);
await trie.delete(Buffer.from(blake256(targetHash)));
}
}
});
Expand Down Expand Up @@ -668,10 +670,6 @@ app
const lucid = await Lucid.new(provider, preview ? 'Preview' : 'Mainnet');
lucid.selectWalletFromSeed(fs.readFileSync('seed.txt', { encoding: 'utf-8' }));

const utxos = (await lucid.wallet.getUtxos()).sort((a, b) => {
return a.txHash.localeCompare(b.txHash) || a.outputIndex - b.outputIndex;
});

// const tx_test = await lucid
// .newTx()
// .payToAddress(await lucid.wallet.address(), { lovelace: 800000000n })
Expand All @@ -684,6 +682,10 @@ app

// await lucid.awaitTx(signed_test.toHash());

const utxos = (await lucid.wallet.getUtxos()).sort((a, b) => {
return a.txHash.localeCompare(b.txHash) || a.outputIndex - b.outputIndex;
});

console.log(utxos);

if (utxos.length === 0) {
Expand Down Expand Up @@ -726,7 +728,7 @@ app
.collectFrom([utxos.at(1)!])
.payToAddressWithData(forkValidatorAddress, { scriptRef: forkValidatorApplied }, {})
.payToAddressWithData(forkValidatorAddress, { scriptRef: tunaV2SpendApplied }, {})
.complete({ coinSelection: false });
.complete({ coinSelection: true, change: { address: await lucid.wallet.address() } });

const signed = await tx.sign().complete();

Expand All @@ -745,7 +747,7 @@ app
])
.payToAddressWithData(forkValidatorAddress, { scriptRef: tunaV2MintApplied }, {})
.registerStake(forkValidatorRewardAddress)
.complete({ coinSelection: false });
.complete({ coinSelection: true, change: { address: await lucid.wallet.address() } });

const signed2 = await tx2.sign().complete();

Expand Down Expand Up @@ -791,7 +793,7 @@ app
for (const header of headerHashes) {
const hash = blake256(fromHex(header.current_hash as string));

await trie.insert(Buffer.from(hash), Buffer.from(fromHex(header.current_hash)));
await trie.insert(Buffer.from(hash), Buffer.from(fromHex(header.current_hash as string)));
}

const root = trie.hash.toString('hex');
Expand Down
12 changes: 6 additions & 6 deletions plutus.json

Large diffs are not rendered by default.

55 changes: 38 additions & 17 deletions validators/tunav2.ak
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,13 @@ validator(fortuna_v1_hash: Data, fork_script_hash: Data) {
fail @"Big Tuna not found in input value"
}

let expected_in_token_name = integer_to_bytes(block_number, "")
let expected_in_token_name =
integer_to_bytes(block_number, "")
|> bytearray.concat(fortunav2.counter_prefix, _)

let expected_out_token_name = integer_to_bytes(block_number + 1, "")
let expected_out_token_name =
integer_to_bytes(block_number + 1, "")
|> bytearray.concat(fortunav2.counter_prefix, _)

let halving_exponent = block_number / halving_number

Expand Down Expand Up @@ -152,10 +156,10 @@ validator(fortuna_v1_hash: Data, fork_script_hash: Data) {

and {
// input value check
in_token_name == expected_in_token_name,
(in_token_name == expected_in_token_name)?,
// minted counter check
minted_names == expected_mint,
}?
(minted_names == expected_mint)?,
}
}

NominateUpgrade(script_hash, output_index) -> {
Expand Down Expand Up @@ -673,12 +677,29 @@ validator(tunav2_minting_policy: ByteArray) {
merkle_root: out_merkle,
}: Statev2 = get_inline_datum(out_datum)

let block_number_as_bytes = integer_to_bytes(out_block_number, "")

let expected_output_value =
value.from_asset(
tunav2_minting_policy,
bytearray.concat(fortunav2.big_tuna_prefix, own_script_hash),
1,
)
|> value.add(
tunav2_minting_policy,
bytearray.concat(
fortunav2.counter_prefix,
block_number_as_bytes,
),
1,
)

and {
// Posix time is in milliseconds
// Spend(1) requirement: Time range span is 3 minutes or less and inclusive
!upper_is_inclusive?,
lower_is_inclusive?,
(upper_range - lower_range <= 180000)?,
!upper_is_inclusive,
lower_is_inclusive,
upper_range - lower_range <= 180000,
// In case you are wondering here is what enables pools
// A miner can be a pkh or an nft
// Nfts can come from any input, even validators
Expand All @@ -695,7 +716,7 @@ validator(tunav2_minting_policy: ByteArray) {

quantity == 1
}
}?,
},
// Mining Difficulty Met
// Spend(3) requirement: Found difficulty is less than or equal to the current difficulty
// We do this by checking the leading zeros and the difficulty number
Expand All @@ -705,15 +726,15 @@ validator(tunav2_minting_policy: ByteArray) {
found_leading_zeros == leading_zeros,
found_target_number < target_number,
},
}?,
},
// Spend(4) requirement: Input has master token
(quantity_of(
quantity_of(
in_value |> value.to_dict |> dict.to_pairs,
tunav2_minting_policy,
bytearray.concat(fortunav2.big_tuna_prefix, own_script_hash),
) == 1)?,
) == 1,
// Spend(7) requirement: Output has same tokens as input
(value.without_lovelace(in_value) == value.without_lovelace(out_value))?,
expected_output_value == value.without_lovelace(out_value),
// Spend(9) requirement: Check output has correct difficulty number, leading zeros, and epoch time
if block_number % epoch_number == 0 && block_number > 0 {
// use total epoch time with target epoch time to get difficulty adjustment ratio
Expand Down Expand Up @@ -753,16 +774,16 @@ validator(tunav2_minting_policy: ByteArray) {
// Spend(12) requirement: Output current hash is the found hash
out_current_hash == found_bytearray,
// Spend(13) requirement: Check output merkle is correct
(insert(
insert(
merkle_root |> from_root,
found_bytearray |> blake2b_256,
found_bytearray,
merkle_proof_list,
) == from_root(out_merkle))?,
) == from_root(out_merkle),
// Spend(14) requirement: Data size doesn't exceed 1/4 tx size
(builtin.serialise_data(miner)
builtin.serialise_data(miner)
|> builtin.length_of_bytearray()
|> builtin.less_than_equals_integer(4096))?,
|> builtin.less_than_equals_integer(4096),
}
}

Expand Down

0 comments on commit 823b460

Please sign in to comment.