difficulty calculation, move from randomx to autolykos2
This commit is contained in:
55
include/autolykos2/autolykos2.h
Normal file
55
include/autolykos2/autolykos2.h
Normal file
@@ -0,0 +1,55 @@
|
||||
#ifndef MINICOIN_AUTOLYKOS2_H
|
||||
#define MINICOIN_AUTOLYKOS2_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct Autolykos2Context Autolykos2Context;
|
||||
|
||||
Autolykos2Context* Autolykos2_Create(void);
|
||||
void Autolykos2_Destroy(Autolykos2Context* ctx);
|
||||
|
||||
bool Autolykos2_DagAllocate(Autolykos2Context* ctx, size_t bytes);
|
||||
bool Autolykos2_DagAppend(Autolykos2Context* ctx, const uint8_t* data, size_t len);
|
||||
void Autolykos2_DagClear(Autolykos2Context* ctx);
|
||||
size_t Autolykos2_DagSize(const Autolykos2Context* ctx);
|
||||
|
||||
bool Autolykos2_Hash(
|
||||
Autolykos2Context* ctx,
|
||||
const uint8_t* message,
|
||||
size_t messageLen,
|
||||
uint64_t nonce,
|
||||
uint32_t height,
|
||||
uint8_t outHash[32]
|
||||
);
|
||||
|
||||
bool Autolykos2_CheckTarget(
|
||||
Autolykos2Context* ctx,
|
||||
const uint8_t message32[32],
|
||||
uint64_t nonce,
|
||||
uint32_t height,
|
||||
const uint8_t target32[32],
|
||||
uint8_t outHash[32]
|
||||
);
|
||||
|
||||
bool Autolykos2_FindNonceSingleCore(
|
||||
Autolykos2Context* ctx,
|
||||
const uint8_t message32[32],
|
||||
uint32_t height,
|
||||
const uint8_t target32[32],
|
||||
uint64_t startNonce,
|
||||
uint64_t maxIterations,
|
||||
uint64_t* outNonce,
|
||||
uint8_t outHash[32]
|
||||
);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
14
include/blake2/blake2.h
Normal file
14
include/blake2/blake2.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef MINICOIN_BLAKE2_H
|
||||
#define MINICOIN_BLAKE2_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define MINICOIN_BLAKE2B_OUTBYTES 64
|
||||
#define MINICOIN_BLAKE2S_OUTBYTES 32
|
||||
|
||||
bool Blake2b_Hash(const uint8_t* input, size_t inputLen, uint8_t* out, size_t outLen);
|
||||
bool Blake2s_Hash(const uint8_t* input, size_t inputLen, uint8_t* out, size_t outLen);
|
||||
|
||||
#endif
|
||||
@@ -7,7 +7,6 @@
|
||||
#include <block/transaction.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <randomx/librx_wrapper.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
@@ -29,11 +28,12 @@ typedef struct {
|
||||
block_t* Block_Create();
|
||||
void Block_CalculateHash(const block_t* block, uint8_t* outHash);
|
||||
void Block_CalculateMerkleRoot(const block_t* block, uint8_t* outHash);
|
||||
void Block_CalculateRandomXHash(const block_t* block, uint8_t* outHash);
|
||||
void Block_CalculateAutolykos2Hash(const block_t* block, uint8_t* outHash);
|
||||
void Block_AddTransaction(block_t* block, signed_transaction_t* tx);
|
||||
void Block_RemoveTransaction(block_t* block, uint8_t* txHash);
|
||||
bool Block_HasValidProofOfWork(const block_t* block);
|
||||
bool Block_AllTransactionsValid(const block_t* block);
|
||||
void Block_ShutdownPowContext(void);
|
||||
void Block_Destroy(block_t* block);
|
||||
void Block_Print(const block_t* block);
|
||||
|
||||
|
||||
@@ -25,6 +25,9 @@ void Chain_Wipe(blockchain_t* chain);
|
||||
|
||||
// I/O
|
||||
bool Chain_SaveToFile(blockchain_t* chain, const char* dirpath, uint256_t currentSupply);
|
||||
bool Chain_LoadFromFile(blockchain_t* chain, const char* dirpath, uint256_t* outCurrentSupply);
|
||||
bool Chain_LoadFromFile(blockchain_t* chain, const char* dirpath, uint256_t* outCurrentSupply, uint32_t* outDifficultyTarget);
|
||||
|
||||
// Difficulty
|
||||
uint32_t Chain_ComputeNextTarget(blockchain_t* chain, uint32_t currentTarget);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -8,8 +8,29 @@
|
||||
#define DECIMALS 1000000000000ULL
|
||||
#define DIFFICULTY_ADJUSTMENT_INTERVAL 960 // Every 960 blocks (roughly every 24 hours with a 90 second block time)
|
||||
// Max adjustment per is x2. So if blocks are coming in too fast, the difficulty will at most double every 24 hours, and vice versa if they're coming in too slow.
|
||||
#define RANDOMX_KEY_ROTATION_INTERVAL 6720 // 1 week at 90s block time
|
||||
#define TARGET_BLOCK_TIME 90 // Target block time in seconds
|
||||
#define INITIAL_DIFFICULTY 0x1f0c1422 // Default compact target used by Autolykos2 PoW (This is ridiculously low)
|
||||
//#define INITIAL_DIFFICULTY 0x1d1b7c51 // This takes 90s on my machine with a single thread, good for testing
|
||||
|
||||
// Future Autolykos2 constants:
|
||||
#define EPOCH_LENGTH 350000 // ~1 year at 90s
|
||||
#define BASE_DAG_SIZE (2ULL << 30) // 2 GB
|
||||
#define DAG_BASE_GROWTH (1ULL << 30) // 1 GB, calculated fully later
|
||||
#define DAG_BASE_CAP (8ULL << 30) // 8 GB, adjusted per cycle based off DAG_BASE_GROWTH
|
||||
// Swings - calculated as MIN(percentage, absolute GB) to prevent absurd swings from low hashrate or very large DAG growth
|
||||
#define DAG_MAX_UP_SWING_PERCENTAGE 1.3 // 30%
|
||||
#define DAG_MAX_DOWN_SWING_PERCENTAGE 0.85 // 15%
|
||||
#define DAG_MAX_UP_SWING_GB (4ULL << 30) // 4 GB
|
||||
#define DAG_MAX_DOWN_SWING_GB (1ULL << 30) // 1 GB
|
||||
#define KICKOUT_TARGET_PERCENTAGE 75
|
||||
#define KICKOUT_TARGET_BLOCK 30000 // 1 month at 90s block time
|
||||
|
||||
/**
|
||||
* Each epoch has 2 phases, connected logarithmically:
|
||||
* - Phase 1: Aggressive DAG growth (target is ~75% of the max cap) to kick out any ASICs, 30k blocks (roughly 1 month)
|
||||
* - Phase 2: Stable DAG growth (target is the max cap) to provide a stable environment for GPU miners, 320k blocks (roughly 11 months)
|
||||
**/
|
||||
|
||||
static const uint64_t M_CAP = 18446744073709551615ULL; // Max uint64
|
||||
static const uint64_t TAIL_EMISSION = DECIMALS; // Emission floor is 1.0 coins per block
|
||||
// No max supply. Instead of halving, it'll follow a more gradual, Monero-like emission curve.
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
#ifndef LIBRX_WRAPPER_H
|
||||
#define LIBRX_WRAPPER_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <randomx.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
bool RandomX_Init(const char* key, bool preferFullMemory);
|
||||
void RandomX_Destroy();
|
||||
void RandomX_CalculateHash(const uint8_t* input, size_t inputLen, uint8_t* output);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user