Skip to content

Commit

Permalink
- moved some mining-related functions to Ixian-Core
Browse files Browse the repository at this point in the history
- code cleanup
  • Loading branch information
firestorm40 committed Dec 27, 2021
1 parent b26e643 commit e226633
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 210 deletions.
2 changes: 1 addition & 1 deletion IxianDLT/API/APIServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private Dictionary<string, string> blockToJsonDictionary(Block block)
blockData.Add("PoW field", Crypto.hashToString(block.powField));
blockData.Add("Timestamp", block.timestamp.ToString());
blockData.Add("Difficulty", block.difficulty.ToString());
blockData.Add("Hashrate", (Miner.getTargetHashcountPerBlock(block.difficulty) / 60).ToString());
blockData.Add("Hashrate", (MiningUtils.getTargetHashcountPerBlock(block.difficulty) / 60).ToString());
blockData.Add("Compacted Sigs", block.compactedSigs.ToString());
blockData.Add("Signature count", block.signatures.Count.ToString());
blockData.Add("Transaction count", block.transactions.Count.ToString());
Expand Down
18 changes: 9 additions & 9 deletions IxianDLT/Block/BlockProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2992,13 +2992,13 @@ public static ulong calculateDifficulty_v1()
window_size = Node.blockChain.getLastBlockNum();
}
//
BigInteger target_hashes_per_block = Miner.getTargetHashcountPerBlock(current_difficulty);
BigInteger target_hashes_per_block = MiningUtils.getTargetHashcountPerBlock(current_difficulty);
BigInteger actual_hashes_per_block = target_hashes_per_block * solved_blocks / (window_size / 2);
ulong target_difficulty = 0;
if (actual_hashes_per_block != 0)
{
// find an appropriate difficulty for actual hashes:
target_difficulty = Miner.calculateTargetDifficulty(actual_hashes_per_block);
target_difficulty = MiningUtils.calculateTargetDifficulty(actual_hashes_per_block);
}
// we jump hafway to the target difficulty each time
ulong next_difficulty = 0;
Expand Down Expand Up @@ -3045,13 +3045,13 @@ public static ulong calculateDifficulty_v2()
window_size = Node.blockChain.getLastBlockNum();
}
//
BigInteger target_hashes_per_block = Miner.getTargetHashcountPerBlock(current_difficulty);
BigInteger target_hashes_per_block = MiningUtils.getTargetHashcountPerBlock(current_difficulty);
BigInteger actual_hashes_per_block = target_hashes_per_block * solved_blocks / (window_size / 2);
ulong target_difficulty = 0;
if (actual_hashes_per_block != 0)
{
// find an appropriate difficulty for actual hashes:
target_difficulty = Miner.calculateTargetDifficulty(actual_hashes_per_block);
target_difficulty = MiningUtils.calculateTargetDifficulty(actual_hashes_per_block);
}
else
{
Expand Down Expand Up @@ -3119,7 +3119,7 @@ private static BigInteger calculateEstimatedHashRate()
{
continue;
}
hash_rate += Miner.getTargetHashcountPerBlock(pow_b.difficulty);
hash_rate += MiningUtils.getTargetHashcountPerBlock(pow_b.difficulty);
}
}
hash_rate = hash_rate / (i / 2); // i / 2 since every second block has to be full
Expand Down Expand Up @@ -3170,7 +3170,7 @@ public static ulong calculateDifficulty_v3()

ulong next_difficulty = min_difficulty;
BigInteger current_hashes_per_block = 0;
BigInteger previous_hashes_per_block = Miner.getTargetHashcountPerBlock(current_difficulty);
BigInteger previous_hashes_per_block = MiningUtils.getTargetHashcountPerBlock(current_difficulty);

// if there are more than 3/4 of solved blocks, max out the difficulty
if (solved_blocks > window_size * 0.75f)
Expand All @@ -3187,13 +3187,13 @@ public static ulong calculateDifficulty_v3()
{
// if there are between 25% and 48% of solved blocks, ideally use estimated hashrate * 0.7 for difficulty
current_hashes_per_block = calculateEstimatedHashRate() * 7 / 10; // * 0.7f
next_difficulty = Miner.calculateTargetDifficulty(current_hashes_per_block);
next_difficulty = MiningUtils.calculateTargetDifficulty(current_hashes_per_block);
}
else if (solved_blocks < window_size * 0.53f)
{
// if there are between 48% and 53% of solved blocks, ideally use estimated hashrate * 1.5 for difficulty
current_hashes_per_block = calculateEstimatedHashRate() * 15 / 10; // * 1.5f
next_difficulty = Miner.calculateTargetDifficulty(current_hashes_per_block);
next_difficulty = MiningUtils.calculateTargetDifficulty(current_hashes_per_block);
}
else
{
Expand All @@ -3210,7 +3210,7 @@ public static ulong calculateDifficulty_v3()
previous_n = (long)solved_blocks - solutions_in_previous_block - (long)(window_size * 0.50f);
}
BigInteger estimated_hash_rate = previous_hashes_per_block / (10 + (previous_n / 10));
next_difficulty = Miner.calculateTargetDifficulty(estimated_hash_rate * (10 + (n / 10)));
next_difficulty = MiningUtils.calculateTargetDifficulty(estimated_hash_rate * (10 + (n / 10)));
}

}
Expand Down
Loading

0 comments on commit e226633

Please sign in to comment.