3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 15:22:16 +00:00

refactor s/Utils.now().getTime()/Utils.currentTimeMillis()/g

This commit is contained in:
gubatron 2014-01-04 16:44:47 -05:00 committed by Mike Hearn
parent 7324798242
commit 0c2f362795
19 changed files with 37 additions and 37 deletions

View File

@ -659,7 +659,7 @@ public class Block extends Message {
private void checkTimestamp() throws VerificationException {
maybeParseHeader();
// Allow injection of a fake clock to allow unit testing.
long currentTime = Utils.now().getTime()/1000;
long currentTime = Utils.currentTimeMillis()/1000;
if (time > currentTime + ALLOWED_TIME_DRIFT)
throw new VerificationException("Block too far in future");
}
@ -1029,12 +1029,12 @@ public class Block extends Message {
@VisibleForTesting
public Block createNextBlock(@Nullable Address to, TransactionOutPoint prevOut) {
return createNextBlock(to, prevOut, Utils.now().getTime() / 1000, EMPTY_BYTES, Utils.toNanoCoins(50, 0));
return createNextBlock(to, prevOut, Utils.currentTimeMillis() / 1000, EMPTY_BYTES, Utils.toNanoCoins(50, 0));
}
@VisibleForTesting
public Block createNextBlock(@Nullable Address to, BigInteger value) {
return createNextBlock(to, null, Utils.now().getTime() / 1000, EMPTY_BYTES, value);
return createNextBlock(to, null, Utils.currentTimeMillis() / 1000, EMPTY_BYTES, value);
}
@VisibleForTesting
@ -1044,7 +1044,7 @@ public class Block extends Message {
@VisibleForTesting
public Block createNextBlockWithCoinbase(byte[] pubKey, BigInteger coinbaseValue) {
return createNextBlock(null, null, Utils.now().getTime() / 1000, pubKey, coinbaseValue);
return createNextBlock(null, null, Utils.currentTimeMillis() / 1000, pubKey, coinbaseValue);
}
/**
@ -1053,7 +1053,7 @@ public class Block extends Message {
*/
@VisibleForTesting
Block createNextBlockWithCoinbase(byte[] pubKey) {
return createNextBlock(null, null, Utils.now().getTime() / 1000, pubKey, Utils.toNanoCoins(50, 0));
return createNextBlock(null, null, Utils.currentTimeMillis() / 1000, pubKey, Utils.toNanoCoins(50, 0));
}
@VisibleForTesting

View File

@ -131,7 +131,7 @@ public class ECKey implements Serializable {
ECPoint compressed = compressPoint(uncompressed);
pub = compressed.getEncoded();
creationTimeSeconds = Utils.now().getTime() / 1000;
creationTimeSeconds = Utils.currentTimeMillis() / 1000;
}
private static ECPoint compressPoint(ECPoint uncompressed) {

View File

@ -1268,12 +1268,12 @@ public class Peer extends PeerSocketHandler {
public PendingPing(long nonce) {
future = SettableFuture.create();
this.nonce = nonce;
startTimeMsec = Utils.now().getTime();
startTimeMsec = Utils.currentTimeMillis();
}
public void complete() {
checkNotNull(future, "Already completed");
Long elapsed = Utils.now().getTime() - startTimeMsec;
Long elapsed = Utils.currentTimeMillis() - startTimeMsec;
Peer.this.addPingTimeData(elapsed);
log.debug("{}: ping time is {} msec", Peer.this.toString(), elapsed);
future.set(elapsed);

View File

@ -113,7 +113,7 @@ public class PeerAddress extends ChildMessage {
//TODO this appears to be dynamic because the client only ever sends out it's own address
//so assumes itself to be up. For a fuller implementation this needs to be dynamic only if
//the address refers to this client.
int secs = (int) (Utils.now().getTime() / 1000);
int secs = (int) (Utils.currentTimeMillis() / 1000);
uint32ToByteStreamLE(secs, stream);
}
uint64ToByteStreamLE(services, stream); // nServices.

View File

@ -775,7 +775,7 @@ public class PeerGroup extends AbstractExecutionThreadService implements Transac
protected Peer connectTo(PeerAddress address, boolean incrementMaxConnections) {
VersionMessage ver = getVersionMessage().duplicate();
ver.bestHeight = chain == null ? 0 : chain.getBestChainHeight();
ver.time = Utils.now().getTime() / 1000;
ver.time = Utils.currentTimeMillis() / 1000;
Peer peer = new Peer(params, ver, address, chain, memoryPool);
peer.addEventListener(startupListener, Threading.SAME_THREAD);

View File

@ -2039,7 +2039,7 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi
* Same as {@link #addWatchedAddress(Address, long)} with the current time as the creation time.
*/
public boolean addWatchedAddress(final Address address) {
long now = Utils.now().getTime() / 1000;
long now = Utils.currentTimeMillis() / 1000;
return addWatchedAddresses(Lists.newArrayList(address), now) == 1;
}
@ -2577,7 +2577,7 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi
for (Script script : watchedScripts)
earliestTime = Math.min(script.getCreationTimeSeconds(), earliestTime);
if (earliestTime == Long.MAX_VALUE)
return Utils.now().getTime() / 1000;
return Utils.currentTimeMillis() / 1000;
return earliestTime;
} finally {
lock.unlock();

View File

@ -134,7 +134,7 @@ public class PaymentChannelClient implements IPaymentChannelClient {
checkState(initiate.getExpireTimeSecs() > 0 && initiate.getMinAcceptedChannelSize() >= 0);
final long MAX_EXPIRY_TIME = Utils.now().getTime() / 1000 + MAX_TIME_WINDOW;
final long MAX_EXPIRY_TIME = Utils.currentTimeMillis() / 1000 + MAX_TIME_WINDOW;
if (initiate.getExpireTimeSecs() > MAX_EXPIRY_TIME) {
log.error("Server expiry time was out of our allowed bounds: {} vs {}", initiate.getExpireTimeSecs(),
MAX_EXPIRY_TIME);

View File

@ -364,7 +364,7 @@ public class PaymentChannelClientState {
* storage and throwing an {@link IllegalStateException} if it is.
*/
public synchronized void checkNotExpired() {
if (Utils.now().getTime()/1000 > expiryTime) {
if (Utils.currentTimeMillis()/1000 > expiryTime) {
state = State.EXPIRED;
disconnectFromChannel();
throw new IllegalStateException("Channel expired");

View File

@ -219,7 +219,7 @@ public class PaymentChannelServer {
myKey = new ECKey();
wallet.addKey(myKey);
expireTime = Utils.now().getTime() / 1000 + timeWindow;
expireTime = Utils.currentTimeMillis() / 1000 + timeWindow;
step = InitStep.WAITING_ON_UNSIGNED_REFUND;
Protos.Initiate.Builder initiateBuilder = Protos.Initiate.newBuilder()

View File

@ -94,7 +94,7 @@ public class StoredPaymentChannelClientStates implements WalletExtension {
lock.lock();
try {
final Set<StoredClientChannel> setChannels = mapChannels.get(id);
final long nowSeconds = Utils.now().getTime() / 1000;
final long nowSeconds = Utils.currentTimeMillis() / 1000;
int earliestTime = Integer.MAX_VALUE;
for (StoredClientChannel channel : setChannels) {
synchronized (channel) {
@ -177,7 +177,7 @@ public class StoredPaymentChannelClientStates implements WalletExtension {
announcePeerGroup.broadcastTransaction(channel.refund);
}
// Add the difference between real time and Utils.now() so that test-cases can use a mock clock.
}, new Date(channel.expiryTimeSeconds() * 1000 + (System.currentTimeMillis() - Utils.now().getTime())));
}, new Date(channel.expiryTimeSeconds() * 1000 + (System.currentTimeMillis() - Utils.currentTimeMillis())));
} finally {
lock.unlock();
}

View File

@ -119,7 +119,7 @@ public class StoredPaymentChannelServerStates implements WalletExtension {
checkArgument(mapChannels.put(channel.contract.getHash(), checkNotNull(channel)) == null);
// Add the difference between real time and Utils.now() so that test-cases can use a mock clock.
Date autocloseTime = new Date((channel.refundTransactionUnlockTimeSecs + CHANNEL_EXPIRE_OFFSET) * 1000L
+ (System.currentTimeMillis() - Utils.now().getTime()));
+ (System.currentTimeMillis() - Utils.currentTimeMillis()));
log.info("Scheduling channel for automatic closure at {}: {}", autocloseTime, channel);
channelTimeoutHandler.schedule(new TimerTask() {
@Override

View File

@ -72,7 +72,7 @@ public class Script {
// Used from ScriptBuilder.
Script(List<ScriptChunk> chunks) {
this.chunks = Collections.unmodifiableList(new ArrayList<ScriptChunk>(chunks));
creationTimeSeconds = Utils.now().getTime() / 1000;
creationTimeSeconds = Utils.currentTimeMillis() / 1000;
}
/**
@ -83,7 +83,7 @@ public class Script {
public Script(byte[] programBytes) throws ScriptException {
program = programBytes;
parse(programBytes);
creationTimeSeconds = Utils.now().getTime() / 1000;
creationTimeSeconds = Utils.currentTimeMillis() / 1000;
}
public Script(byte[] programBytes, long creationTimeSeconds) throws ScriptException {

View File

@ -178,7 +178,7 @@ public class TestUtils {
}
public static BlockPair createFakeBlock(BlockStore blockStore, Transaction... transactions) {
return createFakeBlock(blockStore, Utils.now().getTime() / 1000, transactions);
return createFakeBlock(blockStore, Utils.currentTimeMillis() / 1000, transactions);
}
public static Block makeSolvedTestBlock(BlockStore blockStore, Address coinsTo) throws BlockStoreException {

View File

@ -181,7 +181,7 @@ public class BlockChainTest {
Block prev = unitTestParams.getGenesisBlock();
Utils.setMockClock(System.currentTimeMillis()/1000);
for (int i = 0; i < unitTestParams.getInterval() - 1; i++) {
Block newBlock = prev.createNextBlock(coinbaseTo, Utils.now().getTime()/1000);
Block newBlock = prev.createNextBlock(coinbaseTo, Utils.currentTimeMillis()/1000);
assertTrue(chain.add(newBlock));
prev = newBlock;
// The fake chain should seem to be "fast" for the purposes of difficulty calculations.
@ -189,13 +189,13 @@ public class BlockChainTest {
}
// Now add another block that has no difficulty adjustment, it should be rejected.
try {
chain.add(prev.createNextBlock(coinbaseTo, Utils.now().getTime()/1000));
chain.add(prev.createNextBlock(coinbaseTo, Utils.currentTimeMillis()/1000));
fail();
} catch (VerificationException e) {
}
// Create a new block with the right difficulty target given our blistering speed relative to the huge amount
// of time it's supposed to take (set in the unit test network parameters).
Block b = prev.createNextBlock(coinbaseTo, Utils.now().getTime()/1000);
Block b = prev.createNextBlock(coinbaseTo, Utils.currentTimeMillis()/1000);
b.setDifficultyTarget(0x201fFFFFL);
b.solve();
assertTrue(chain.add(b));

View File

@ -964,7 +964,7 @@ public class FullBlockTestGenerator {
// Block with timestamp > 2h in the future
Block b48 = createNextBlock(b44, chainHeadHeight + 16, out15, null);
b48.setTime(Utils.now().getTime() / 1000 + 60*60*3);
b48.setTime(Utils.currentTimeMillis() / 1000 + 60*60*3);
b48.solve();
blocks.add(new BlockAndValidity(blockToHeightMap, b48, false, true, b44.getHash(), chainHeadHeight + 15, "b48"));

View File

@ -338,7 +338,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
// Check the fast catchup time was initialized to something around the current runtime minus a week.
// The wallet was already added to the peer in setup.
final int WEEK = 86400 * 7;
final long now = Utils.now().getTime() / 1000;
final long now = Utils.currentTimeMillis() / 1000;
assertTrue(peerGroup.getFastCatchupTimeSecs() > now - WEEK - 10000);
Wallet w2 = new Wallet(params);
ECKey key1 = new ECKey();

View File

@ -449,7 +449,7 @@ public class PeerTest extends TestWithNetworkConnections {
Block b4 = makeSolvedTestBlock(b3);
// Request headers until the last 2 blocks.
peer.setDownloadParameters((Utils.now().getTime() / 1000) - (600*2) + 1, false);
peer.setDownloadParameters((Utils.currentTimeMillis() / 1000) - (600*2) + 1, false);
peer.startBlockChainDownload();
GetHeadersMessage getheaders = (GetHeadersMessage) outbound(writeTarget);
List<Sha256Hash> expectedLocator = new ArrayList<Sha256Hash>();

View File

@ -479,7 +479,7 @@ public class ChannelConnectionTest extends TestWithWallet {
server.receiveMessage(pair.clientRecorder.checkNextMsg(MessageType.CLIENT_VERSION));
client.receiveMessage(pair.serverRecorder.checkNextMsg(MessageType.SERVER_VERSION));
client.receiveMessage(Protos.TwoWayChannelMessage.newBuilder()
.setInitiate(Protos.Initiate.newBuilder().setExpireTimeSecs(Utils.now().getTime() / 1000 + 60 * 60 * 48)
.setInitiate(Protos.Initiate.newBuilder().setExpireTimeSecs(Utils.currentTimeMillis() / 1000 + 60 * 60 * 48)
.setMinAcceptedChannelSize(100)
.setMultisigKey(ByteString.copyFrom(new ECKey().getPubKey()))
.setMinPayment(Transaction.MIN_NONDUST_OUTPUT.longValue()))
@ -504,7 +504,7 @@ public class ChannelConnectionTest extends TestWithWallet {
server.receiveMessage(pair.clientRecorder.checkNextMsg(MessageType.CLIENT_VERSION));
client.receiveMessage(pair.serverRecorder.checkNextMsg(MessageType.SERVER_VERSION));
client.receiveMessage(Protos.TwoWayChannelMessage.newBuilder()
.setInitiate(Protos.Initiate.newBuilder().setExpireTimeSecs(Utils.now().getTime() / 1000)
.setInitiate(Protos.Initiate.newBuilder().setExpireTimeSecs(Utils.currentTimeMillis() / 1000)
.setMinAcceptedChannelSize(Utils.COIN.add(BigInteger.ONE).longValue())
.setMultisigKey(ByteString.copyFrom(new ECKey().getPubKey()))
.setMinPayment(Transaction.MIN_NONDUST_OUTPUT.longValue()))
@ -530,7 +530,7 @@ public class ChannelConnectionTest extends TestWithWallet {
server.receiveMessage(pair.clientRecorder.checkNextMsg(MessageType.CLIENT_VERSION));
client.receiveMessage(pair.serverRecorder.checkNextMsg(MessageType.SERVER_VERSION));
client.receiveMessage(Protos.TwoWayChannelMessage.newBuilder()
.setInitiate(Protos.Initiate.newBuilder().setExpireTimeSecs(Utils.now().getTime() / 1000)
.setInitiate(Protos.Initiate.newBuilder().setExpireTimeSecs(Utils.currentTimeMillis() / 1000)
.setMinAcceptedChannelSize(Utils.COIN.add(BigInteger.ONE).longValue())
.setMultisigKey(ByteString.copyFrom(new ECKey().getPubKey()))
.setMinPayment(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.longValue()))
@ -553,7 +553,7 @@ public class ChannelConnectionTest extends TestWithWallet {
client.receiveMessage(pair.serverRecorder.checkNextMsg(MessageType.SERVER_VERSION));
try {
client.receiveMessage(Protos.TwoWayChannelMessage.newBuilder()
.setInitiate(Protos.Initiate.newBuilder().setExpireTimeSecs(Utils.now().getTime() / 1000)
.setInitiate(Protos.Initiate.newBuilder().setExpireTimeSecs(Utils.currentTimeMillis() / 1000)
.setMinAcceptedChannelSize(Utils.CENT.longValue())
.setMultisigKey(ByteString.copyFrom(new ECKey().getPubKey()))
.setMinPayment(Transaction.REFERENCE_DEFAULT_MIN_TX_FEE.longValue()))

View File

@ -115,7 +115,7 @@ public class PaymentChannelStateTest extends TestWithWallet {
// Check it all works when things are normal (no attacks, no problems).
Utils.rollMockClock(0); // Use mock clock
final long EXPIRE_TIME = Utils.now().getTime()/1000 + 60*60*24;
final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24;
serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME);
assertEquals(PaymentChannelServerState.State.WAITING_FOR_REFUND_TRANSACTION, serverState.getState());
@ -229,7 +229,7 @@ public class PaymentChannelStateTest extends TestWithWallet {
wallet.addOrUpdateExtension(stateStorage);
Utils.rollMockClock(0); // Use mock clock
final long EXPIRE_TIME = Utils.now().getTime()/1000 + 60*60*24;
final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24;
serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME);
assertEquals(PaymentChannelServerState.State.WAITING_FOR_REFUND_TRANSACTION, serverState.getState());
@ -331,7 +331,7 @@ public class PaymentChannelStateTest extends TestWithWallet {
// We'll broadcast only one tx: multisig contract
Utils.rollMockClock(0); // Use mock clock
final long EXPIRE_TIME = Utils.now().getTime()/1000 + 60*60*24;
final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24;
serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME);
assertEquals(PaymentChannelServerState.State.WAITING_FOR_REFUND_TRANSACTION, serverState.getState());
@ -542,7 +542,7 @@ public class PaymentChannelStateTest extends TestWithWallet {
assertEquals(Utils.CENT, wallet.getBalance());
Utils.rollMockClock(0); // Use mock clock
final long EXPIRE_TIME = Utils.now().getTime()/1000 + 60*60*24;
final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24;
serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME);
assertEquals(PaymentChannelServerState.State.WAITING_FOR_REFUND_TRANSACTION, serverState.getState());
@ -645,7 +645,7 @@ public class PaymentChannelStateTest extends TestWithWallet {
// Test that the server properly adds the necessary fee at the end (or just drops the payment if its not worth it)
Utils.rollMockClock(0); // Use mock clock
final long EXPIRE_TIME = Utils.now().getTime()/1000 + 60*60*24;
final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24;
serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME);
assertEquals(PaymentChannelServerState.State.WAITING_FOR_REFUND_TRANSACTION, serverState.getState());
@ -731,7 +731,7 @@ public class PaymentChannelStateTest extends TestWithWallet {
// Start with a copy of basic()....
Utils.rollMockClock(0); // Use mock clock
final long EXPIRE_TIME = Utils.now().getTime()/1000 + 60*60*24;
final long EXPIRE_TIME = Utils.currentTimeMillis()/1000 + 60*60*24;
serverState = new PaymentChannelServerState(mockBroadcaster, serverWallet, serverKey, EXPIRE_TIME);
assertEquals(PaymentChannelServerState.State.WAITING_FOR_REFUND_TRANSACTION, serverState.getState());