Skip to content

Commit

Permalink
AbstractBlockChain: migrate private getMedianTimestampOfRecentBlocks(…
Browse files Browse the repository at this point in the history
…) to Instant
  • Loading branch information
schildbach committed Sep 3, 2024
1 parent c912ee3 commit f64ce82
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions core/src/main/java/org/bitcoinj/core/AbstractBlockChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -832,7 +832,7 @@ private void handleNewBestChain(StoredBlock storedPrev, StoredBlock newChainHead
for (Iterator<StoredBlock> 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)
Expand Down

0 comments on commit f64ce82

Please sign in to comment.