#ifndef CHAIN_H #define CHAIN_H #include #include #include #include #include #include #include #include #include typedef struct { DynArr* blocks; size_t size; } blockchain_t; blockchain_t* Chain_Create(); void Chain_Destroy(blockchain_t* chain); bool Chain_AddBlock(blockchain_t* chain, block_t* block); block_t* Chain_GetBlock(blockchain_t* chain, size_t index); size_t Chain_Size(blockchain_t* chain); bool Chain_IsValid(blockchain_t* chain); void Chain_Wipe(blockchain_t* chain); // Roll back the chain to `height` (exclusive): after this call, Chain_Size(chain) == height // Returns true on success. bool Chain_RollbackToHeight(blockchain_t* chain, size_t height); // Recompute `currentSupply` and `currentReward` from the in-memory chain blocks. // Returns true on success and updates runtime state globals. bool Chain_RecomputeRuntimeState(blockchain_t* chain); // Retrieve a deep copy of the block at `index`. Caller must free with `Block_Destroy`. bool Chain_GetBlockCopy(blockchain_t* chain, size_t index, block_t** outCopy); // I/O bool Chain_SaveToFile(blockchain_t* chain, const char* dirpath, uint256_t currentSupply, uint64_t currentReward); bool Chain_LoadFromFile(blockchain_t* chain, const char* dirpath, uint256_t* outCurrentSupply, uint32_t* outDifficultyTarget, uint64_t* outCurrentReward, uint8_t* outLastSavedHash, bool loadTransactions); bool Chain_LoadBlockFromFile(const char* dirpath, uint64_t blockNumber, bool loadTransactions, block_t** outBlock, size_t* outTxCount); // Difficulty uint32_t Chain_ComputeNextTarget(blockchain_t* chain, uint32_t currentTarget); #endif