Block: rename time() method from getTimeInstant()

This commit is contained in:
Andreas Schildbach 2023-03-18 10:21:20 +01:00
parent 31123bea3d
commit e6e4745e1a
14 changed files with 32 additions and 32 deletions

View file

@ -1039,7 +1039,7 @@ public abstract class AbstractBlockChain {
public Instant estimateBlockTimeInstant(int height) {
synchronized (chainHeadLock) {
long offset = height - chainHead.getHeight();
Instant headTime = chainHead.getHeader().getTimeInstant();
Instant headTime = chainHead.getHeader().time();
return headTime.plus(10 * offset, ChronoUnit.MINUTES);
}
}

View file

@ -904,14 +904,14 @@ public class Block extends Message {
/**
* Returns the time at which the block was solved and broadcast, according to the clock of the solving node.
*/
public Instant getTimeInstant() {
public Instant time() {
return time;
}
/**
* Returns the time at which the block was solved and broadcast, according to the clock of the solving node. This
* is measured in seconds since the UNIX epoch (midnight Jan 1st 1970).
* @deprecated use {@link #getTimeInstant()}
* @deprecated use {@link #time()}
*/
@Deprecated
public long getTimeSeconds() {
@ -920,11 +920,11 @@ public class Block extends Message {
/**
* Returns the time at which the block was solved and broadcast, according to the clock of the solving node.
* @deprecated use {@link #getTimeInstant()}
* @deprecated use {@link #time()}
*/
@Deprecated
public Date getTime() {
return Date.from(getTimeInstant());
return Date.from(time());
}
@VisibleForTesting
@ -1059,8 +1059,8 @@ public class Block extends Message {
b.setPrevBlockHash(getHash());
// Don't let timestamp go backwards
Instant bitcoinTime = time.truncatedTo(ChronoUnit.SECONDS);
if (getTimeInstant().compareTo(bitcoinTime) >= 0)
b.setTime(getTimeInstant().plusSeconds(1));
if (time().compareTo(bitcoinTime) >= 0)
b.setTime(time().plusSeconds(1));
else
b.setTime(bitcoinTime);
b.solve();
@ -1086,12 +1086,12 @@ public class Block extends Message {
@VisibleForTesting
public Block createNextBlock(@Nullable Address to, TransactionOutPoint prevOut) {
return createNextBlock(to, BLOCK_VERSION_GENESIS, prevOut, getTimeInstant().plusSeconds(5), pubkeyForTesting, FIFTY_COINS, BLOCK_HEIGHT_UNKNOWN);
return createNextBlock(to, BLOCK_VERSION_GENESIS, prevOut, time().plusSeconds(5), pubkeyForTesting, FIFTY_COINS, BLOCK_HEIGHT_UNKNOWN);
}
@VisibleForTesting
public Block createNextBlock(@Nullable Address to, Coin value) {
return createNextBlock(to, BLOCK_VERSION_GENESIS, null, getTimeInstant().plusSeconds(5), pubkeyForTesting, value, BLOCK_HEIGHT_UNKNOWN);
return createNextBlock(to, BLOCK_VERSION_GENESIS, null, time().plusSeconds(5), pubkeyForTesting, value, BLOCK_HEIGHT_UNKNOWN);
}
@VisibleForTesting

View file

@ -146,7 +146,7 @@ public class CheckpointManager {
throw new IOException("Incomplete read whilst loading checkpoints.");
StoredBlock block = StoredBlock.deserializeCompact(params, buffer);
((Buffer) buffer).position(0);
checkpoints.put(block.getHeader().getTimeInstant(), block);
checkpoints.put(block.getHeader().time(), block);
}
Sha256Hash dataHash = Sha256Hash.wrap(digest.digest());
log.info("Read {} checkpoints up to time {}, hash is {}", checkpoints.size(),
@ -183,7 +183,7 @@ public class CheckpointManager {
buffer.put(bytes);
((Buffer) buffer).position(0);
StoredBlock block = StoredBlock.deserializeCompact(params, buffer);
checkpoints.put(block.getHeader().getTimeInstant(), block);
checkpoints.put(block.getHeader().time(), block);
}
HashCode hash = hasher.hash();
log.info("Read {} checkpoints up to time {}, hash is {}", checkpoints.size(),
@ -198,7 +198,7 @@ public class CheckpointManager {
*/
public StoredBlock getCheckpointBefore(Instant time) {
try {
checkArgument(time.isAfter(params.getGenesisBlock().getTimeInstant()));
checkArgument(time.isAfter(params.getGenesisBlock().time()));
// This is thread safe because the map never changes after creation.
Map.Entry<Instant, StoredBlock> entry = checkpoints.floorEntry(time);
if (entry != null) return entry.getValue();

View file

@ -238,7 +238,7 @@ public class Peer extends PeerSocketHandler {
this.vDownloadData = chain != null;
this.getDataFutures = new ConcurrentLinkedQueue<>();
this.getAddrFutures = new LinkedList<>();
this.fastCatchupTime = params.getGenesisBlock().getTimeInstant();
this.fastCatchupTime = params.getGenesisBlock().time();
this.pendingPings = new CopyOnWriteArrayList<>();
this.vMinProtocolVersion = params.getProtocolVersionNum(NetworkParameters.ProtocolVersion.PONG);
this.wallets = new CopyOnWriteArrayList<>();
@ -637,7 +637,7 @@ public class Peer extends PeerSocketHandler {
// Process headers until we pass the fast catchup time, or are about to catch up with the head
// of the chain - always process the last block as a full/filtered block to kick us out of the
// fast catchup mode (in which we ignore new blocks).
boolean passedTime = header.getTimeInstant().compareTo(fastCatchupTime) >= 0;
boolean passedTime = header.time().compareTo(fastCatchupTime) >= 0;
boolean reachedTop = blockChain.getBestChainHeight() >= vPeerVersionMessage.bestHeight;
if (!passedTime && !reachedTop) {
if (!vDownloadData) {
@ -1320,7 +1320,7 @@ public class Peer extends PeerSocketHandler {
this.fastCatchupTime = fastCatchupTime;
// If the given time is before the current chains head block time, then this has no effect (we already
// downloaded everything we need).
if (blockChain != null && this.fastCatchupTime.isAfter(blockChain.getChainHead().getHeader().getTimeInstant()))
if (blockChain != null && this.fastCatchupTime.isAfter(blockChain.getChainHead().getHeader().time()))
downloadBlockBodies = false;
this.useFilteredBlocks = useFilteredBlocks;
} finally {
@ -1335,7 +1335,7 @@ public class Peer extends PeerSocketHandler {
public void setDownloadParameters(boolean useFilteredBlocks) {
lock.lock();
try {
this.fastCatchupTime = params.getGenesisBlock().getTimeInstant();
this.fastCatchupTime = params.getGenesisBlock().time();
downloadBlockBodies = true;
this.useFilteredBlocks = useFilteredBlocks;
} finally {

View file

@ -425,7 +425,7 @@ public class PeerGroup implements TransactionBroadcaster {
Context.getOrCreate(); // create a context for convenience
this.params = params;
this.chain = chain;
fastCatchupTime = params.getGenesisBlock().getTimeInstant();
fastCatchupTime = params.getGenesisBlock().time();
wallets = new CopyOnWriteArrayList<>();
peerFilterProviders = new CopyOnWriteArrayList<>();

View file

@ -420,7 +420,7 @@ public class Transaction extends ChildMessage {
* @param relativityOffset A number that disambiguates the order of transactions within a block.
*/
public void setBlockAppearance(StoredBlock block, boolean bestChain, int relativityOffset) {
Instant blockTime = block.getHeader().getTimeInstant();
Instant blockTime = block.getHeader().time();
if (bestChain && (updateTime == null || updateTime.equals(Instant.EPOCH) || updateTime.isAfter(blockTime))) {
updateTime = blockTime;
}

View file

@ -69,7 +69,7 @@ public class DownloadProgressTracker implements BlockchainDownloadEventListener
caughtUp = true;
if (lastPercent != 100) {
lastPercent = 100;
progress(lastPercent, blocksLeft, block.getTimeInstant());
progress(lastPercent, blocksLeft, block.time());
}
doneDownload();
future.complete(peer.getBestHeight());
@ -81,7 +81,7 @@ public class DownloadProgressTracker implements BlockchainDownloadEventListener
double pct = 100.0 - (100.0 * (blocksLeft / (double) originalBlocksLeft));
if ((int) pct != lastPercent) {
progress(pct, blocksLeft, block.getTimeInstant());
progress(pct, blocksLeft, block.time());
lastPercent = (int) pct;
}
}

View file

@ -104,7 +104,7 @@ public class TestNet3Params extends BitcoinNetworkParams {
@Override
public void checkDifficultyTransitions(final StoredBlock storedPrev, final Block nextBlock,
final BlockStore blockStore) throws VerificationException, BlockStoreException {
if (!isDifficultyTransitionPoint(storedPrev.getHeight()) && nextBlock.getTimeInstant().isAfter(testnetDiffDate)) {
if (!isDifficultyTransitionPoint(storedPrev.getHeight()) && nextBlock.time().isAfter(testnetDiffDate)) {
Block prev = storedPrev.getHeader();
// After 15th February 2012 the rules on the testnet change to avoid people running up the difficulty

View file

@ -2455,7 +2455,7 @@ public class Wallet extends BaseTaggableObject
// Store the new block hash.
setLastBlockSeenHash(newBlockHash);
setLastBlockSeenHeight(block.getHeight());
setLastBlockSeenTime(block.getHeader().getTimeInstant());
setLastBlockSeenTime(block.getHeader().time());
// Notify all the BUILDING transactions of the new block.
// This is so that they can update their depth.
Set<Transaction> transactions = getTransactions(true);

View file

@ -85,7 +85,7 @@ public class BlockTest {
@Test
public void testDate() {
assertEquals("2016-02-13T22:59:39Z", TimeUtils.dateTimeFormat(block700000.getTimeInstant()));
assertEquals("2016-02-13T22:59:39Z", TimeUtils.dateTimeFormat(block700000.time()));
}
private static class TweakableTestNet3Params extends TestNet3Params {

View file

@ -916,7 +916,7 @@ public class FullBlockTestGenerator {
b44.addTransaction(t);
b44.setPrevBlockHash(b43.getHash());
b44.setTime(b43.block.getTimeInstant().plusSeconds(1));
b44.setTime(b43.block.time().plusSeconds(1));
}
b44.solve();
blocks.add(new BlockAndValidity(b44, true, false, b44.getHash(), chainHeadHeight + 15, "b44"));
@ -944,7 +944,7 @@ public class FullBlockTestGenerator {
b45.addTransaction(t, false);
b45.setPrevBlockHash(b44.getHash());
b45.setTime(b44.getTimeInstant().plusSeconds(1));
b45.setTime(b44.time().plusSeconds(1));
}
b45.solve();
blocks.add(new BlockAndValidity(b45, false, true, b44.getHash(), chainHeadHeight + 15, "b45"));
@ -957,7 +957,7 @@ public class FullBlockTestGenerator {
b46.setMerkleRoot(Sha256Hash.ZERO_HASH);
b46.setPrevBlockHash(b44.getHash());
b46.setTime(b44.getTimeInstant().plusSeconds(1));
b46.setTime(b44.time().plusSeconds(1));
}
b46.solve();
blocks.add(new BlockAndValidity(b46, false, true, b44.getHash(), chainHeadHeight + 15, "b46"));
@ -1039,13 +1039,13 @@ public class FullBlockTestGenerator {
// Block with invalid timestamp
NewBlock b54 = createNextBlock(b53, chainHeadHeight + 16, out15, null);
b54.block.setTime(b35.block.getTimeInstant().minusSeconds(1));
b54.block.setTime(b35.block.time().minusSeconds(1));
b54.solve();
blocks.add(new BlockAndValidity(b54, false, true, b44.getHash(), chainHeadHeight + 15, "b54"));
// Block with valid timestamp
NewBlock b55 = createNextBlock(b53, chainHeadHeight + 16, out15, null);
b55.block.setTime(b35.block.getTimeInstant());
b55.block.setTime(b35.block.time());
b55.solve();
blocks.add(new BlockAndValidity(b55, true, false, b55.getHash(), chainHeadHeight + 16, "b55"));
spendableOutputs.offer(b55.getCoinbaseOutput());

View file

@ -1538,7 +1538,7 @@ public class WalletTest extends TestWithWallet {
// Receive a block on the best chain - this should set the last block seen hash.
chain.add(b10);
assertEquals(b10.getHash(), wallet.getLastBlockSeenHash());
assertEquals(b10.getTimeInstant(), wallet.getLastBlockSeenTimeInstant().get());
assertEquals(b10.time(), wallet.getLastBlockSeenTimeInstant().get());
assertEquals(1, wallet.getLastBlockSeenHeight());
// Receive a block on the side chain - this should not change the last block seen hash.
chain.add(b11);

View file

@ -146,10 +146,10 @@ public class BuildCheckpoints implements Callable<Integer> {
chain.addNewBestBlockListener(Threading.SAME_THREAD, block -> {
int height = block.getHeight();
if (height % params.getInterval() == 0 && timeAgo.isAfter(block.getHeader().getTimeInstant())) {
if (height % params.getInterval() == 0 && timeAgo.isAfter(block.getHeader().time())) {
System.out.println(String.format("Checkpointing block %s at height %d, time %s",
block.getHeader().getHash(), block.getHeight(),
TimeUtils.dateTimeFormat(block.getHeader().getTimeInstant())));
TimeUtils.dateTimeFormat(block.getHeader().time())));
checkpoints.put(height, block);
}
});

View file

@ -994,7 +994,7 @@ public class WalletTool implements Callable<Integer> {
wallet.getEarliestKeyCreationTimeInstant());
StoredBlock head = store.getChainHead();
System.out.println("Skipped to checkpoint " + head.getHeight() + " at "
+ TimeUtils.dateTimeFormat(head.getHeader().getTimeInstant()));
+ TimeUtils.dateTimeFormat(head.getHeader().time()));
} catch (IOException x) {
System.out.println("Could not load checkpoints: " + x.getMessage());
}