Skip to content

Commit

Permalink
commit miner and difficulty adjustment functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroProofs committed Aug 26, 2023
1 parent 11c1945 commit 7ef297e
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/fortuna.ak
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ pub fn get_new_difficulty(
let new_difficulty = new_padded_difficulty / padding

if new_padded_difficulty / 65536 == 0 {
if current_leading_zeros >= 30 {
(4096, 60)
if current_leading_zeros >= 62 {
(4096, 62)
} else {
(new_padded_difficulty, current_leading_zeros + 1)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/fortuna/parameters.ak
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub const halving_number = 210000

pub const epoch_number = 2016

pub const epoch_target = 1209600
pub const epoch_target = 1209600000

pub const initial_payout = 5000000000

Expand Down
49 changes: 39 additions & 10 deletions miner/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import {
toHex,
} from "https://deno.land/x/lucid@0.10.1/mod.ts";
import {
calculateDifficultyNumber,
calculateInterlink,
getDifficulty,
getDifficultyAdjustement,
incrementU8Array,
readValidator,
} from "./utils.ts";
Expand Down Expand Up @@ -91,7 +93,6 @@ const mine = new Command()
while (true) {
targetHash = sha256(sha256(fromHex(Data.to(targetState))));

console.log(`Trying Target Hash: ${toHex(targetHash)}`);
difficulty = getDifficulty(targetHash);

const { leadingZeros, difficulty_number } = difficulty;
Expand All @@ -114,15 +115,43 @@ const mine = new Command()
const interlink = calculateInterlink(toHex(targetHash), difficulty, {
leadingZeros: state.fields[2] as bigint,
difficulty_number: state.fields[3] as bigint,
});
}, state.fields[7] as string[]);

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

let difficulty_number = state.fields[3] as bigint;
let leading_zeros = state.fields[2] as bigint;

if (
state.fields[0] as bigint % 2016n === 0n && state.fields[0] as bigint > 0
) {
const adjustment = getDifficultyAdjustement(epoch_time, 1209600000n);

epoch_time = 0n;

const new_difficulty = calculateDifficultyNumber(
{
leadingZeros: state.fields[2] as bigint,
difficulty_number: state.fields[3] as bigint,
},
adjustment.numerator,
adjustment.denominator,
);

difficulty_number = new_difficulty.difficulty_number;
leading_zeros = new_difficulty.leadingZeros;
}

// calculateDifficultyNumber();

const postDatum = new Constr(0, [
(state.fields[0] as bigint) + 1n,
toHex(targetHash),
state.fields[2] as bigint,
state.fields[3] as bigint,
BigInt(45000 + realTimeNow) - (state.fields[5] as bigint),
BigInt(45000 + realTimeNow),
leading_zeros,
difficulty_number,
epoch_time,
BigInt(90000 + realTimeNow),
0n,
interlink,
]);
Expand All @@ -140,7 +169,7 @@ const mine = new Command()
.payToAddressWithData(validatorAddress, { inline: outDat }, masterToken)
.mintAssets(mintTokens, Data.to(new Constr(0, [])))
.attachSpendingValidator({ type: "PlutusV2", script: validator })
.validTo(realTimeNow + 90000)
.validTo(realTimeNow + 180000)
.validFrom(realTimeNow)
.complete();

Expand Down Expand Up @@ -203,13 +232,13 @@ const genesis = new Command()
// current_hash: ByteArray
boostrapHash,
// leading_zeros: Int
4n,
2n,
// difficulty_number: Int
65535n,
// epoch_time: Int
0n,
// current_posix_time: Int
BigInt(45000 + timeNow),
BigInt(90000 + timeNow),
// extra: Data
0n,
// interlink: List<Data>
Expand All @@ -225,7 +254,7 @@ const genesis = new Command()
.mintAssets(masterToken, Data.to(new Constr(1, [])))
.attachMintingPolicy(validator)
.validFrom(timeNow)
.validTo(timeNow + 90000)
.validTo(timeNow + 180000)
.complete();

const signed = await tx.sign().complete();
Expand Down
66 changes: 64 additions & 2 deletions miner/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,84 @@ export function halfDifficultyNumber(
}
}

export function getDifficultyAdjustement(
total_epoch_time: bigint,
epoch_target: bigint,
): { numerator: bigint; denominator: bigint } {
if (
epoch_target / total_epoch_time >= 4 && epoch_target % total_epoch_time > 0
) {
return { numerator: 1n, denominator: 4n };
} else if (
total_epoch_time / epoch_target >= 4 && total_epoch_time % epoch_target > 0
) {
return { numerator: 4n, denominator: 1n };
} else {
return { numerator: total_epoch_time, denominator: epoch_target };
}
}

export function calculateDifficultyNumber(
a: { leadingZeros: bigint; difficulty_number: bigint },
numerator: bigint,
denominator: bigint,
): { leadingZeros: bigint; difficulty_number: bigint } {
const new_padded_difficulty = a.difficulty_number * 16n * numerator /
denominator;

const new_difficulty = new_padded_difficulty / 16n;

if (new_padded_difficulty / 65536n == 0n) {
if (a.leadingZeros >= 62n) {
return { difficulty_number: 4096n, leadingZeros: 62n };
} else {
return {
difficulty_number: new_padded_difficulty,
leadingZeros: a.leadingZeros + 1n,
};
}
} else if (new_difficulty / 65536n > 0n) {
if (a.leadingZeros <= 2) {
return { difficulty_number: 65535n, leadingZeros: 2n };
} else {
return {
difficulty_number: new_difficulty / 16n,
leadingZeros: a.leadingZeros - 1n,
};
}
} else {
return {
difficulty_number: new_difficulty,
leadingZeros: a.leadingZeros,
};
}
}

export function calculateInterlink(
currentHash: string,
a: { leadingZeros: bigint; difficulty_number: bigint },
b: { leadingZeros: bigint; difficulty_number: bigint },
currentInterlink: string[],
): string[] {
let b_half = halfDifficultyNumber(b);

let interlink: string[] = [];
const interlink: string[] = currentInterlink;

let currentIndex = 0;

while (
b_half.leadingZeros < a.leadingZeros ||
b_half.leadingZeros == a.leadingZeros &&
b_half.difficulty_number > a.difficulty_number
) {
interlink = [currentHash, ...interlink];
if (currentIndex < interlink.length) {
interlink[currentIndex] = currentHash;
} else {
interlink.push(currentHash);
}

b_half = halfDifficultyNumber(b_half);
currentIndex += 1;
}

return interlink;
Expand Down
2 changes: 1 addition & 1 deletion validators/tuna.ak
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ validator(utxo_ref: OutputReference) {
// Posix time is in milliseconds
// Spend(1) requirement: Time range span is 3 minutes or less and inclusive
expect and {
!upper_is_inclusive?,
(!upper_is_inclusive)?,
lower_is_inclusive?,
(upper_range - lower_range <= 180000)?,
}
Expand Down

0 comments on commit 7ef297e

Please sign in to comment.