@@ -13,6 +13,7 @@ import (
1313 "github.com/ava-labs/avalanchego/snow/engine/enginetest"
1414 "github.com/ava-labs/avalanchego/snow/validators/validatorstest"
1515 "github.com/ava-labs/avalanchego/upgrade/upgradetest"
16+ "github.com/ava-labs/avalanchego/vms/evm/uptimetracker"
1617 "github.com/stretchr/testify/require"
1718
1819 commonEng "github.com/ava-labs/avalanchego/snow/engine/common"
@@ -22,7 +23,8 @@ import (
2223func TestUptimeTracker (t * testing.T ) {
2324 testNodeID := ids .GenerateTestNodeID ()
2425 testValidationID := ids .GenerateTestID ()
25- startTime := uint64 (time .Now ().Unix ())
26+ baseTime := time .Unix (0 , 0 )
27+ startTime := uint64 (baseTime .Unix ())
2628
2729 // TODO(JonathanOppenheimer): see func NewTestValidatorState() -- this should be examined
2830 // when we address the issue of that function.
@@ -31,11 +33,13 @@ func TestUptimeTracker(t *testing.T) {
3133 GetCurrentValidatorSetF : func (context.Context , ids.ID ) (map [ids.ID ]* avagovalidators.GetCurrentValidatorOutput , uint64 , error ) {
3234 return map [ids.ID ]* avagovalidators.GetCurrentValidatorOutput {
3335 testValidationID : {
34- NodeID : testNodeID ,
35- PublicKey : nil ,
36- Weight : 1 ,
37- StartTime : startTime ,
38- IsActive : true ,
36+ ValidationID : testValidationID ,
37+ NodeID : testNodeID ,
38+ PublicKey : nil ,
39+ Weight : 1 ,
40+ StartTime : startTime ,
41+ IsActive : true ,
42+ IsL1Validator : true ,
3943 },
4044 }, 0 , nil
4145 },
@@ -60,18 +64,56 @@ func TestUptimeTracker(t *testing.T) {
6064 ))
6165 defer vm .Shutdown (context .Background ())
6266
67+ // Mock the clock to control time progression
68+ clock := vm .clock
69+ clock .Set (baseTime )
70+
6371 require .NoError (vm .SetState (context .Background (), snow .Bootstrapping ))
6472
6573 // After bootstrapping but before NormalOp, uptimeTracker hasn't started syncing yet
66- _ , _ , found , err := vm . uptimeTracker . GetUptime ( testValidationID )
67- require . NoError ( err )
68- require .False ( found , "uptime should not be tracked yet" )
74+ // so GetUptime should not be able to find the validation ID
75+ _ , _ , err := vm . uptimeTracker . GetUptime ( testValidationID )
76+ require .ErrorIs ( err , uptimetracker . ErrValidationIDNotFound )
6977
7078 require .NoError (vm .SetState (context .Background (), snow .NormalOp ))
7179
7280 // After transitioning to NormalOp, Sync() is called automatically to populate uptime
73- // from validator state
74- _ , _ , found , err = vm .uptimeTracker .GetUptime (testValidationID )
81+ // from validator state, so GetUptime should work now
82+ // The validator starts with 0 uptime since it has not connected yet
83+ initialUptime , initialLastUpdated , err := vm .uptimeTracker .GetUptime (testValidationID )
84+ require .NoError (err )
85+ require .Equal (time .Duration (0 ), initialUptime , "Initial uptime should be 0" )
86+ require .Equal (baseTime , initialLastUpdated , "Initial lastUpdated should be baseTime" )
87+
88+ // connect, time passes
89+ require .NoError (vm .uptimeTracker .Connect (testNodeID ))
90+ clock .Set (baseTime .Add (1 * time .Hour ))
91+
92+ // get uptime after 1 hour of being connected - uptime should have increased by 1 hour
93+ upDuration , lastUpdated , err := vm .uptimeTracker .GetUptime (testValidationID )
94+ require .NoError (err )
95+ require .Equal (1 * time .Hour , upDuration , "Uptime should be 1 hour" )
96+ require .Equal (baseTime .Add (1 * time .Hour ), lastUpdated , "lastUpdated should reflect new clock time" )
97+
98+ // disconnect, time passes another 2 hours
99+ require .NoError (vm .uptimeTracker .Disconnect (testNodeID ))
100+ clock .Set (baseTime .Add (2 * time .Hour ))
101+
102+ // get uptime - should not have increased since we were disconnected
103+ upDuration , lastUpdated , err = vm .uptimeTracker .GetUptime (testValidationID )
104+ require .NoError (err )
105+ require .Equal (1 * time .Hour , upDuration , "Uptime should not increase while disconnected" )
106+ require .Equal (baseTime .Add (2 * time .Hour ), lastUpdated , "lastUpdated should still advance" )
107+
108+ // reconnect, time passes another 30 minutes
109+ require .NoError (vm .uptimeTracker .Connect (testNodeID ))
110+ clock .Set (baseTime .Add (2 * time .Hour + 30 * time .Minute ))
111+
112+ // get uptime - total uptime should be 1h30m
113+ upDuration , lastUpdated , err = vm .uptimeTracker .GetUptime (testValidationID )
75114 require .NoError (err )
76- require .True (found , "uptime should be tracked after transitioning to NormalOp" )
115+ require .Equal (1 * time .Hour + 30 * time .Minute , upDuration ,
116+ "Uptime should be 1h30m (1h from first connection + 30m from second)" )
117+ require .Equal (baseTime .Add (2 * time .Hour + 30 * time .Minute ), lastUpdated ,
118+ "lastUpdated should reflect final clock time" )
77119}
0 commit comments