diff --git a/include/block/block.h b/include/block/block.h index 6978a66..47de693 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -40,6 +40,7 @@ bool Block_IsFullyValid(const block_t* block); void Block_ShutdownPowContext(void); void Block_Destroy(block_t* block); void Block_Print(const block_t* block); +void Block_ShortPrint(const block_t* block); // Deep-copy a block (allocates a new `block_t*`). Caller must call `Block_Destroy`. block_t* Block_Copy(const block_t* src); diff --git a/src/block/block.c b/src/block/block.c index b45bfb8..2ea4151 100644 --- a/src/block/block.c +++ b/src/block/block.c @@ -286,6 +286,20 @@ void Block_Print(const block_t* block) { } } +void Block_ShortPrint(const block_t* block) { + if (!block) return; + + printf("Block #%llu: Timestamp %llu, Nonce %llu, DiffTarget 0x%08x, Version %u, PrevHash %02x%02x...%02x%02x, MerkleRoot %02x%02x...%02x%02x, TxCount %zu\n", + (unsigned long long)block->header.blockNumber, + (unsigned long long)block->header.timestamp, + (unsigned long long)block->header.nonce, + block->header.difficultyTarget, + block->header.version, + block->header.prevHash[0], block->header.prevHash[1], block->header.prevHash[30], block->header.prevHash[31], + block->header.merkleRoot[0], block->header.merkleRoot[1], block->header.merkleRoot[30], block->header.merkleRoot[31], + block->transactions ? DynArr_size(block->transactions) : 0); +} + block_t* Block_Copy(const block_t* src) { if (!src) return NULL; block_t* dst = (block_t*)malloc(sizeof(block_t)); diff --git a/src/block/chain.c b/src/block/chain.c index c83e035..627a857 100644 --- a/src/block/chain.c +++ b/src/block/chain.c @@ -230,6 +230,9 @@ bool Chain_AddBlock(blockchain_t* chain, block_t* block) { pthread_mutex_unlock(&balanceSheetLock); pthread_rwlock_unlock(&chainLock); + printf("Added new block to chain:\n"); + Block_ShortPrint(block); + return ok; } diff --git a/src/main.c b/src/main.c index 093f81f..8a7a255 100644 --- a/src/main.c +++ b/src/main.c @@ -761,10 +761,11 @@ int main(int argc, char* argv[]) { uint64_t localHeight = (uint64_t)Chain_Size(chain); - // Determine if this is an initial sync. If so, do not apply penalty. - bool isInitialSync = (localHeight == 0); + // Only penalize small near-tip gaps. Large gaps are treated as normal catch-up, + // because a much taller peer on the same chain is not evidence of a reorg. TODO: Maybe look at this again some other day. + bool isInitialSync = (localHeight == 0) || ((peerHeight > localHeight) && ((peerHeight - localHeight) > INITIAL_SYNC_HEIGHT_DIFF)); - // Compute penalty and adjusted peer height (skip penalty for initial sync) + // Compute penalty and adjusted peer height. uint64_t delay = (peerHeight > localHeight) ? (peerHeight - localHeight) : 0ULL; uint64_t penalty = isInitialSync ? 0ULL : FetchScheduler_ComputeReorgPenaltyBlocks(delay); uint64_t adjustedPeerHeight = (peerHeight > penalty) ? (peerHeight - penalty) : 0ULL;