From f64ce82586f9713e3cf3cce674963b146c25ca45 Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Tue, 3 Sep 2024 01:35:20 +0200 Subject: [PATCH] AbstractBlockChain: migrate private getMedianTimestampOfRecentBlocks() to Instant --- .../java/org/bitcoinj/core/AbstractBlockChain.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/core/AbstractBlockChain.java b/core/src/main/java/org/bitcoinj/core/AbstractBlockChain.java index 0c16824f78a..a5f7c69e881 100644 --- a/core/src/main/java/org/bitcoinj/core/AbstractBlockChain.java +++ b/core/src/main/java/org/bitcoinj/core/AbstractBlockChain.java @@ -600,7 +600,7 @@ private void connectBlock(final Block block, StoredBlock storedPrev, boolean exp block.getHashAsString(), filteredTxHashList.size(), filteredTxn.size()); for (Sha256Hash hash : filteredTxHashList) log.debug(" matched tx {}", hash); } - if (expensiveChecks && block.time().getEpochSecond() <= getMedianTimestampOfRecentBlocks(head, blockStore)) + if (expensiveChecks && !block.time().isAfter(getMedianTimestampOfRecentBlocks(head, blockStore))) throw new VerificationException("Block's timestamp is too early"); // BIP 66 & 65: Enforce block version 3/4 once they are a supermajority of blocks @@ -770,13 +770,13 @@ private static void informListenerForNewTransactions(Block block, NewBlockType n /** * Gets the median timestamp of the last 11 blocks */ - private static long getMedianTimestampOfRecentBlocks(StoredBlock storedBlock, + private static Instant getMedianTimestampOfRecentBlocks(StoredBlock storedBlock, BlockStore store) throws BlockStoreException { - long[] timestamps = new long[11]; + Instant[] timestamps = new Instant[11]; int unused = 9; - timestamps[10] = storedBlock.getHeader().time().getEpochSecond(); + timestamps[10] = storedBlock.getHeader().time(); while (unused >= 0 && (storedBlock = storedBlock.getPrev(store)) != null) - timestamps[unused--] = storedBlock.getHeader().time().getEpochSecond(); + timestamps[unused--] = storedBlock.getHeader().time(); Arrays.sort(timestamps, unused+1, 11); return timestamps[unused + (11-unused)/2]; @@ -832,7 +832,7 @@ private void handleNewBestChain(StoredBlock storedPrev, StoredBlock newChainHead for (Iterator it = newBlocks.descendingIterator(); it.hasNext();) { cursor = it.next(); Block cursorBlock = cursor.getHeader(); - if (expensiveChecks && cursorBlock.time().getEpochSecond() <= getMedianTimestampOfRecentBlocks(cursor.getPrev(blockStore), blockStore)) + if (expensiveChecks && !cursorBlock.time().isAfter(getMedianTimestampOfRecentBlocks(cursor.getPrev(blockStore), blockStore))) throw new VerificationException("Block's timestamp is too early during reorg"); TransactionOutputChanges txOutChanges; if (cursor != newChainHead || block == null)