mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2024-11-19 18:00:39 +01:00
VersionMessage: migrate time
field to java.time
API
This commit is contained in:
parent
7788f6cfaf
commit
1835370e04
@ -388,8 +388,7 @@ public class Peer extends PeerSocketHandler {
|
||||
String servicesStr = Strings.emptyToNull(VersionMessage.toStringServices(vPeerVersionMessage.localServices));
|
||||
helper.add("services",
|
||||
vPeerVersionMessage.localServices + (servicesStr != null ? " (" + servicesStr + ")" : ""));
|
||||
long peerTime = vPeerVersionMessage.time * 1000;
|
||||
helper.add("time", String.format(Locale.US, "%tF %tT", peerTime, peerTime));
|
||||
helper.add("time", TimeUtils.dateTimeFormat(vPeerVersionMessage.time));
|
||||
helper.add("height", vPeerVersionMessage.bestHeight);
|
||||
return helper.toString();
|
||||
}
|
||||
|
@ -70,6 +70,7 @@ import java.net.Socket;
|
||||
import java.net.SocketAddress;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@ -1489,7 +1490,7 @@ public class PeerGroup implements TransactionBroadcaster {
|
||||
checkState(lock.isHeldByCurrentThread());
|
||||
VersionMessage ver = getVersionMessage().duplicate();
|
||||
ver.bestHeight = chain == null ? 0 : chain.getBestChainHeight();
|
||||
ver.time = TimeUtils.currentTimeSeconds();
|
||||
ver.time = TimeUtils.currentTime().truncatedTo(ChronoUnit.SECONDS);
|
||||
ver.receivingAddr = address;
|
||||
ver.receivingAddr.setParent(ver);
|
||||
|
||||
|
@ -28,6 +28,8 @@ import java.io.OutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.net.InetAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
@ -71,9 +73,9 @@ public class VersionMessage extends Message {
|
||||
*/
|
||||
public long localServices;
|
||||
/**
|
||||
* What the other side believes the current time to be, in seconds.
|
||||
* What the other side believes the current time to be.
|
||||
*/
|
||||
public long time;
|
||||
public Instant time;
|
||||
/**
|
||||
* The network address of the node receiving this message.
|
||||
*/
|
||||
@ -109,7 +111,7 @@ public class VersionMessage extends Message {
|
||||
super(params);
|
||||
clientVersion = serializer.getProtocolVersion();
|
||||
localServices = 0;
|
||||
time = TimeUtils.currentTimeSeconds();
|
||||
time = TimeUtils.currentTime().truncatedTo(ChronoUnit.SECONDS);
|
||||
// Note that the Bitcoin Core doesn't do anything with these, and finding out your own external IP address
|
||||
// is kind of tricky anyway, so we just put nonsense here for now.
|
||||
InetAddress localhost = InetAddresses.forString("127.0.0.1");
|
||||
@ -127,7 +129,7 @@ public class VersionMessage extends Message {
|
||||
protected void parse() throws ProtocolException {
|
||||
clientVersion = (int) readUint32();
|
||||
localServices = readUint64().longValue();
|
||||
time = readUint64().longValue();
|
||||
time = Instant.ofEpochSecond(readUint64().longValue());
|
||||
receivingAddr = new PeerAddress(params, payload, cursor, this, serializer.withProtocolVersion(0));
|
||||
cursor += receivingAddr.getMessageSize();
|
||||
if (clientVersion >= 106) {
|
||||
@ -162,6 +164,7 @@ public class VersionMessage extends Message {
|
||||
ByteUtils.uint32ToByteStreamLE(clientVersion, buf);
|
||||
ByteUtils.uint32ToByteStreamLE(localServices, buf);
|
||||
ByteUtils.uint32ToByteStreamLE(localServices >> 32, buf);
|
||||
long time = this.time.getEpochSecond();
|
||||
ByteUtils.uint32ToByteStreamLE(time, buf);
|
||||
ByteUtils.uint32ToByteStreamLE(time >> 32, buf);
|
||||
receivingAddr.bitcoinSerializeToStream(buf);
|
||||
@ -190,7 +193,7 @@ public class VersionMessage extends Message {
|
||||
return other.bestHeight == bestHeight &&
|
||||
other.clientVersion == clientVersion &&
|
||||
other.localServices == localServices &&
|
||||
other.time == time &&
|
||||
other.time.equals(time) &&
|
||||
other.subVer.equals(subVer) &&
|
||||
other.receivingAddr.equals(receivingAddr) &&
|
||||
other.fromAddr.equals(fromAddr) &&
|
||||
@ -211,7 +214,7 @@ public class VersionMessage extends Message {
|
||||
if (localServices != 0)
|
||||
builder.append(" (").append(toStringServices(localServices)).append(")");
|
||||
builder.append("\n");
|
||||
builder.append("time: ").append(time).append("\n");
|
||||
builder.append("time: ").append(TimeUtils.dateTimeFormat(time)).append("\n");
|
||||
builder.append("receiving addr: ").append(receivingAddr).append("\n");
|
||||
builder.append("from addr: ").append(fromAddr).append("\n");
|
||||
builder.append("sub version: ").append(subVer).append("\n");
|
||||
|
@ -21,6 +21,7 @@ import org.bitcoinj.params.TestNet3Params;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.time.Instant;
|
||||
|
||||
import org.bitcoinj.base.internal.ByteUtils;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@ -70,7 +71,7 @@ public class VersionMessageTest {
|
||||
@Test
|
||||
public void roundTrip_ipv4() throws Exception {
|
||||
VersionMessage ver = new VersionMessage(TESTNET, 1234);
|
||||
ver.time = 23456;
|
||||
ver.time = Instant.ofEpochSecond(23456);
|
||||
ver.subVer = "/bitcoinj/";
|
||||
ver.clientVersion = NetworkParameters.ProtocolVersion.CURRENT.getBitcoinProtocolVersion();
|
||||
ver.localServices = 1;
|
||||
@ -81,7 +82,7 @@ public class VersionMessageTest {
|
||||
byte[] serialized = ver.bitcoinSerialize();
|
||||
VersionMessage ver2 = new VersionMessage(TESTNET, serialized);
|
||||
assertEquals(1234, ver2.bestHeight);
|
||||
assertEquals(23456, ver2.time);
|
||||
assertEquals(Instant.ofEpochSecond(23456), ver2.time);
|
||||
assertEquals("/bitcoinj/", ver2.subVer);
|
||||
assertEquals(NetworkParameters.ProtocolVersion.CURRENT.getBitcoinProtocolVersion(), ver2.clientVersion);
|
||||
assertEquals(1, ver2.localServices);
|
||||
@ -94,7 +95,7 @@ public class VersionMessageTest {
|
||||
@Test
|
||||
public void roundTrip_ipv6() throws Exception {
|
||||
VersionMessage ver = new VersionMessage(TESTNET, 1234);
|
||||
ver.time = 23456;
|
||||
ver.time = Instant.ofEpochSecond(23456);
|
||||
ver.subVer = "/bitcoinj/";
|
||||
ver.clientVersion = NetworkParameters.ProtocolVersion.CURRENT.getBitcoinProtocolVersion();
|
||||
ver.localServices = 1;
|
||||
@ -105,7 +106,7 @@ public class VersionMessageTest {
|
||||
byte[] serialized = ver.bitcoinSerialize();
|
||||
VersionMessage ver2 = new VersionMessage(TESTNET, serialized);
|
||||
assertEquals(1234, ver2.bestHeight);
|
||||
assertEquals(23456, ver2.time);
|
||||
assertEquals(Instant.ofEpochSecond(23456), ver2.time);
|
||||
assertEquals("/bitcoinj/", ver2.subVer);
|
||||
assertEquals(NetworkParameters.ProtocolVersion.CURRENT.getBitcoinProtocolVersion(), ver2.clientVersion);
|
||||
assertEquals(1, ver2.localServices);
|
||||
|
Loading…
Reference in New Issue
Block a user