Skip to content

Commit eb2358a

Browse files
Add UpgradeTime to p2p handshake (#4468)
Signed-off-by: Stephen Buttolph <stephen@avalabs.org> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 3d3a594 commit eb2358a

File tree

7 files changed

+60
-8
lines changed

7 files changed

+60
-8
lines changed

message/messagemock/outbound_message_builder.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

message/outbound_msg_builder.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type OutboundMsgBuilder interface {
2727
major uint32,
2828
minor uint32,
2929
patch uint32,
30+
upgradeTime uint64,
3031
ipSigningTime uint64,
3132
ipNodeIDSig []byte,
3233
ipBLSSig []byte,
@@ -238,6 +239,7 @@ func (b *outMsgBuilder) Handshake(
238239
major uint32,
239240
minor uint32,
240241
patch uint32,
242+
upgradeTime uint64,
241243
ipSigningTime uint64,
242244
ipNodeIDSig []byte,
243245
ipBLSSig []byte,
@@ -258,6 +260,7 @@ func (b *outMsgBuilder) Handshake(
258260
MyTime: myTime,
259261
IpAddr: ip.Addr().AsSlice(),
260262
IpPort: uint32(ip.Port()),
263+
UpgradeTime: upgradeTime,
261264
IpSigningTime: ipSigningTime,
262265
IpNodeIdSig: ipNodeIDSig,
263266
TrackedSubnets: subnetIDBytes,

network/peer/info.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Info struct {
1717
PublicIP netip.AddrPort `json:"publicIP,omitempty"`
1818
ID ids.NodeID `json:"nodeID"`
1919
Version string `json:"version"`
20+
UpgradeTime uint64 `json:"upgradeTime"`
2021
LastSent time.Time `json:"lastSent"`
2122
LastReceived time.Time `json:"lastReceived"`
2223
ObservedUptime json.Uint32 `json:"observedUptime"`

network/peer/peer.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ type peer struct {
143143
// version is the claimed version the peer is running that we received in
144144
// the Handshake message.
145145
version *version.Application
146+
// upgradeTime is the claimed Unix timestamp (in seconds) of the most
147+
// recently scheduled network upgrade from the Handshake message.
148+
upgradeTime uint64
146149
// trackedSubnets are the subnetIDs the peer sent us in the Handshake
147150
// message. The primary network ID is always included.
148151
trackedSubnets set.Set[ids.ID]
@@ -288,6 +291,7 @@ func (p *peer) Info() Info {
288291
PublicIP: p.ip.AddrPort,
289292
ID: p.id,
290293
Version: p.version.String(),
294+
UpgradeTime: p.upgradeTime,
291295
LastSent: p.LastSent(),
292296
LastReceived: p.LastReceived(),
293297
ObservedUptime: json.Uint32(primaryUptime),
@@ -541,6 +545,7 @@ func (p *peer) writeMessages() {
541545
uint32(myVersion.Major),
542546
uint32(myVersion.Minor),
543547
uint32(myVersion.Patch),
548+
uint64(p.VersionCompatibility.UpgradeTime.Unix()),
544549
mySignedIP.Timestamp,
545550
mySignedIP.TLSSignature,
546551
mySignedIP.BLSSignatureBytes,
@@ -909,6 +914,8 @@ func (p *peer) handleHandshake(msg *p2p.Handshake) {
909914
)
910915
}
911916

917+
p.upgradeTime = msg.UpgradeTime
918+
912919
// handle subnet IDs
913920
if numTrackedSubnets := len(msg.TrackedSubnets); numTrackedSubnets > maxNumTrackedSubnets {
914921
p.Log.Debug(malformedMessageLog,

network/peer/peer_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,34 @@ func TestInvalidBLSKeyDisconnects(t *testing.T) {
356356
require.NoError(peer1.AwaitClosed(t.Context()))
357357
}
358358

359+
// Test that the upgrade time is exchanged and exposed correctly after the
360+
// handshake finishes.
361+
func TestUpgradeTime(t *testing.T) {
362+
require := require.New(t)
363+
364+
sharedConfig0 := newConfig(t)
365+
sharedConfig0.VersionCompatibility.UpgradeTime = time.Unix(123, 0)
366+
367+
sharedConfig1 := newConfig(t)
368+
sharedConfig1.VersionCompatibility.UpgradeTime = time.Unix(1234, 0)
369+
370+
peer0, peer1 := startTestPeers(
371+
newRawTestPeer(t, sharedConfig0),
372+
newRawTestPeer(t, sharedConfig1),
373+
)
374+
require.NoError(peer0.AwaitReady(t.Context()))
375+
require.NoError(peer1.AwaitReady(t.Context()))
376+
377+
require.Equal(uint64(sharedConfig1.VersionCompatibility.UpgradeTime.Unix()), peer0.Info().UpgradeTime)
378+
require.Equal(uint64(sharedConfig0.VersionCompatibility.UpgradeTime.Unix()), peer1.Info().UpgradeTime)
379+
380+
peer0.StartClose()
381+
peer1.StartClose()
382+
383+
require.NoError(peer0.AwaitClosed(t.Context()))
384+
require.NoError(peer1.AwaitClosed(t.Context()))
385+
}
386+
359387
func TestShouldDisconnect(t *testing.T) {
360388
peerID := ids.GenerateTestNodeID()
361389
txID := ids.GenerateTestID()

proto/p2p/p2p.proto

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ message Pong {
8383
//
8484
// Peers should drop connections to peers with incompatible versions.
8585
message Handshake {
86-
reserved 5; // Until E upgrade is activated.
8786
// Network the peer is running on (e.g local, testnet, mainnet)
8887
uint32 network_id = 1;
8988
// Unix timestamp when this Handshake message was created
@@ -92,6 +91,9 @@ message Handshake {
9291
bytes ip_addr = 3;
9392
// IP port of the peer
9493
uint32 ip_port = 4;
94+
// Unix timestamp (in seconds) of the most recently scheduled network upgrade.
95+
// This timestamp may be the past upgrade or a future upgrade.
96+
uint64 upgrade_time = 5;
9597
// Timestamp of the IP
9698
uint64 ip_signing_time = 6;
9799
// Signature of the peer IP port pair at a provided timestamp with the TLS

proto/pb/p2p/p2p.pb.go

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)