From 13e0b0e7b9b6cf79e7b14d2adee40a2f52437150 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Mon, 24 Feb 2014 09:53:31 -0600 Subject: [PATCH] Limit generated timestamps to one second precision. This commit changes all cases which generate default timestamps to time.Now to limit the timestamp to one second precision. The code which serializes and deserializes timestamps already does this, but it is useful to make sure defaults don't exceed the precision of the protocol either. With this change there is less chance that developers using defaults will end up with structures that have a higher time precision than what will ultimately be sent across the wire. --- blockheader.go | 4 +++- msgversion.go | 2 +- netaddress.go | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/blockheader.go b/blockheader.go index 9eb643e6b..d04d83b33 100644 --- a/blockheader.go +++ b/blockheader.go @@ -89,11 +89,13 @@ func (h *BlockHeader) Serialize(w io.Writer) error { func NewBlockHeader(prevHash *ShaHash, merkleRootHash *ShaHash, bits uint32, nonce uint32) *BlockHeader { + // Limit the timestamp to one second precision since the protocol + // doesn't support better. return &BlockHeader{ Version: BlockVersion, PrevBlock: *prevHash, MerkleRoot: *merkleRootHash, - Timestamp: time.Now(), + Timestamp: time.Unix(time.Now().Unix(), 0), Bits: bits, Nonce: nonce, } diff --git a/msgversion.go b/msgversion.go index c8ac391b0..6b2157599 100644 --- a/msgversion.go +++ b/msgversion.go @@ -176,7 +176,7 @@ func (msg *MsgVersion) MaxPayloadLength(pver uint32) uint32 { func NewMsgVersion(me *NetAddress, you *NetAddress, nonce uint64, userAgent string, lastBlock int32) *MsgVersion { - // Limit the Timestamp to millisecond precision since the protocol + // Limit the timestamp to one second precision since the protocol // doesn't support better. return &MsgVersion{ ProtocolVersion: int32(ProtocolVersion), diff --git a/netaddress.go b/netaddress.go index 8d060f8ea..f40171112 100644 --- a/netaddress.go +++ b/netaddress.go @@ -75,8 +75,10 @@ func (na *NetAddress) SetAddress(ip net.IP, port uint16) { // NewNetAddressIPPort returns a new NetAddress using the provided IP, port, and // supported services with defaults for the remaining fields. func NewNetAddressIPPort(ip net.IP, port uint16, services ServiceFlag) *NetAddress { + // Limit the timestamp to one second precision since the protocol + // doesn't support better. na := NetAddress{ - Timestamp: time.Now(), + Timestamp: time.Unix(time.Now().Unix(), 0), Services: services, IP: ip, Port: port,