FakeTxBuilder: migrate createFakeBlock()' methods to java.time` API

This commit is contained in:
Andreas Schildbach 2023-03-14 01:49:43 +01:00
parent 168ff75bd1
commit 79477ff04a
3 changed files with 43 additions and 19 deletions

View file

@ -260,18 +260,24 @@ public class FakeTxBuilder {
/** Emulates receiving a valid block that builds on top of the chain. */
public static BlockPair createFakeBlock(BlockStore blockStore, long version,
long timeSeconds, Transaction... transactions) {
return createFakeBlock(blockStore, version, timeSeconds, 0, transactions);
Instant time, Transaction... transactions) {
return createFakeBlock(blockStore, version, time, 0, transactions);
}
/** @deprecated use {@link #createFakeBlock(BlockStore, long, Instant, Transaction...)} */
@Deprecated
public static BlockPair createFakeBlock(BlockStore blockStore, long version,
long timeSecs, Transaction... transactions) {
return createFakeBlock(blockStore, version, Instant.ofEpochSecond(timeSecs), transactions);
}
/** Emulates receiving a valid block */
public static BlockPair createFakeBlock(BlockStore blockStore, StoredBlock previousStoredBlock, long version,
long timeSeconds, int height,
Transaction... transactions) {
Instant time, int height, Transaction... transactions) {
try {
Block previousBlock = previousStoredBlock.getHeader();
Address to = randomAddress(previousBlock.getParams());
Block b = previousBlock.createNextBlock(to, version, Instant.ofEpochSecond(timeSeconds), height);
Block b = previousBlock.createNextBlock(to, version, time, height);
// Coinbase tx was already added.
for (Transaction tx : transactions) {
tx.getConfidence().setSource(TransactionConfidence.Source.NETWORK);
@ -289,28 +295,42 @@ public class FakeTxBuilder {
}
}
/** @deprecated use {@link #createFakeBlock(BlockStore, StoredBlock, long, Instant, int, Transaction...)} */
@Deprecated
public static BlockPair createFakeBlock(BlockStore blockStore, StoredBlock previousStoredBlock, long version,
long timeSecs, int height, Transaction... transactions) {
return createFakeBlock(blockStore, previousStoredBlock, version, Instant.ofEpochSecond(timeSecs), height,
transactions);
}
public static BlockPair createFakeBlock(BlockStore blockStore, StoredBlock previousStoredBlock, int height, Transaction... transactions) {
return createFakeBlock(blockStore, previousStoredBlock, Block.BLOCK_VERSION_BIP66, TimeUtils.currentTimeSeconds(), height, transactions);
return createFakeBlock(blockStore, previousStoredBlock, Block.BLOCK_VERSION_BIP66, TimeUtils.currentTime(), height, transactions);
}
/** Emulates receiving a valid block that builds on top of the chain. */
public static BlockPair createFakeBlock(BlockStore blockStore, long version, long timeSeconds, int height, Transaction... transactions) {
public static BlockPair createFakeBlock(BlockStore blockStore, long version, Instant time, int height, Transaction... transactions) {
try {
return createFakeBlock(blockStore, blockStore.getChainHead(), version, timeSeconds, height, transactions);
return createFakeBlock(blockStore, blockStore.getChainHead(), version, time, height, transactions);
} catch (BlockStoreException e) {
throw new RuntimeException(e); // Cannot happen.
}
}
/** @deprecated use {@link #createFakeBlock(BlockStore, long, Instant, int, Transaction...)} */
@Deprecated
public static BlockPair createFakeBlock(BlockStore blockStore, long version, long timeSecs, int height, Transaction... transactions) {
return createFakeBlock(blockStore, version, Instant.ofEpochSecond(timeSecs), height, transactions);
}
/** Emulates receiving a valid block that builds on top of the chain. */
public static BlockPair createFakeBlock(BlockStore blockStore, int height,
Transaction... transactions) {
return createFakeBlock(blockStore, Block.BLOCK_VERSION_GENESIS, TimeUtils.currentTimeSeconds(), height, transactions);
return createFakeBlock(blockStore, Block.BLOCK_VERSION_GENESIS, TimeUtils.currentTime(), height, transactions);
}
/** Emulates receiving a valid block that builds on top of the chain. */
public static BlockPair createFakeBlock(BlockStore blockStore, Transaction... transactions) {
return createFakeBlock(blockStore, Block.BLOCK_VERSION_GENESIS, TimeUtils.currentTimeSeconds(), 0, transactions);
return createFakeBlock(blockStore, Block.BLOCK_VERSION_GENESIS, TimeUtils.currentTime(), 0, transactions);
}
public static Block makeSolvedTestBlock(BlockStore blockStore, Address coinsTo) throws BlockStoreException {

View file

@ -42,6 +42,7 @@ import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
@ -260,24 +261,24 @@ public class BlockChainTest {
final BlockChain versionChain = new BlockChain(UNITTEST, versionBlockStore);
// Build a historical chain of version 3 blocks
long timeSeconds = 1231006505;
Instant time = Instant.ofEpochSecond(1231006505);
int height = 0;
FakeTxBuilder.BlockPair chainHead = null;
// Put in just enough v2 blocks to be a minority
for (height = 0; height < (UNITTEST.getMajorityWindow() - UNITTEST.getMajorityRejectBlockOutdated()); height++) {
chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, deprecatedVersion, timeSeconds, height);
chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, deprecatedVersion, time, height);
versionChain.add(chainHead.block);
timeSeconds += 60;
time = time.plus(1, ChronoUnit.MINUTES);
}
// Fill the rest of the window with v3 blocks
for (; height < UNITTEST.getMajorityWindow(); height++) {
chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, newVersion, timeSeconds, height);
chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, newVersion, time, height);
versionChain.add(chainHead.block);
timeSeconds += 60;
time = time.plus(1, ChronoUnit.MINUTES);
}
chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, deprecatedVersion, timeSeconds, height);
chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, deprecatedVersion, time, height);
// Trying to add a new v2 block should result in rejection
thrown.expect(VerificationException.BlockVersionOutOfDate.class);
try {

View file

@ -31,6 +31,9 @@ import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@ -108,12 +111,12 @@ public class VersionTallyTest {
final BlockChain chain = new BlockChain(TESTNET, blockStore);
// Build a historical chain of version 2 blocks
long timeSeconds = 1231006505;
Instant time = Instant.ofEpochSecond(1231006505);
StoredBlock chainHead = null;
for (int height = 0; height < TESTNET.getMajorityWindow(); height++) {
chainHead = FakeTxBuilder.createFakeBlock(blockStore, 2, timeSeconds, height).storedBlock;
chainHead = FakeTxBuilder.createFakeBlock(blockStore, 2, time, height).storedBlock;
assertEquals(2, chainHead.getHeader().getVersion());
timeSeconds += 60;
time = time.plus(1, ChronoUnit.MINUTES);
}
VersionTally instance = new VersionTally(TESTNET);