@@ -49,6 +49,27 @@ func (s *KeeperTestSuite) TestAddEpochInfo() {
4949 },
5050 expErr : true ,
5151 },
52+ "start in future" : {
53+ addedEpochInfo : types.EpochInfo {
54+ Identifier : defaultIdentifier ,
55+ StartTime : startBlockTime .Add (time .Hour ),
56+ Duration : defaultDuration ,
57+ CurrentEpoch : 0 ,
58+ CurrentEpochStartHeight : 0 ,
59+ CurrentEpochStartTime : time.Time {},
60+ EpochCountingStarted : false ,
61+ },
62+ expEpochInfo : types.EpochInfo {
63+ Identifier : defaultIdentifier ,
64+ StartTime : startBlockTime .Add (time .Hour ),
65+ Duration : defaultDuration ,
66+ CurrentEpoch : 0 ,
67+ CurrentEpochStartHeight : 0 ,
68+ CurrentEpochStartTime : time.Time {},
69+ EpochCountingStarted : false ,
70+ },
71+ expErr : false ,
72+ },
5273 }
5374 for name , test := range tests {
5475 s .Run (name , func () {
@@ -99,3 +120,89 @@ func (s *KeeperTestSuite) TestEpochLifeCycle() {
99120 s .Require ().Equal (allEpochs [3 ].Identifier , "monthly" )
100121 s .Require ().Equal (allEpochs [4 ].Identifier , "week" )
101122}
123+
124+ func (s * KeeperTestSuite ) TestNumBlocksSinceEpochStart () {
125+ s .SetupTest ()
126+
127+ startBlockHeight := int64 (100 )
128+ startBlockTime := time .Unix (1656907200 , 0 ).UTC ()
129+ duration := time .Hour
130+
131+ s .Ctx = s .Ctx .WithBlockHeight (startBlockHeight ).WithBlockTime (startBlockTime )
132+
133+ tests := map [string ]struct {
134+ setupEpoch types.EpochInfo
135+ advanceBlockDelta int64
136+ advanceTimeDelta time.Duration
137+ expErr bool
138+ expBlocksSince int64
139+ }{
140+ "same block as start" : {
141+ setupEpoch : types.EpochInfo {
142+ Identifier : "epoch_same_block" ,
143+ StartTime : startBlockTime ,
144+ Duration : duration ,
145+ CurrentEpoch : 0 ,
146+ CurrentEpochStartHeight : startBlockHeight ,
147+ CurrentEpochStartTime : startBlockTime ,
148+ EpochCountingStarted : true ,
149+ },
150+ advanceBlockDelta : 0 ,
151+ advanceTimeDelta : 0 ,
152+ expErr : false ,
153+ expBlocksSince : 0 ,
154+ },
155+ "after 5 blocks" : {
156+ setupEpoch : types.EpochInfo {
157+ Identifier : "epoch_after_five" ,
158+ StartTime : startBlockTime ,
159+ Duration : duration ,
160+ CurrentEpoch : 0 ,
161+ CurrentEpochStartHeight : startBlockHeight ,
162+ CurrentEpochStartTime : startBlockTime ,
163+ EpochCountingStarted : true ,
164+ },
165+ advanceBlockDelta : 5 ,
166+ advanceTimeDelta : time .Minute * 5 , // just to simulate realistic advancement
167+ expErr : false ,
168+ expBlocksSince : 5 ,
169+ },
170+ "epoch not started yet" : {
171+ setupEpoch : types.EpochInfo {
172+ Identifier : "epoch_future" ,
173+ StartTime : startBlockTime .Add (time .Hour ),
174+ Duration : duration ,
175+ CurrentEpoch : 0 ,
176+ CurrentEpochStartHeight : 0 ,
177+ CurrentEpochStartTime : time.Time {},
178+ EpochCountingStarted : false ,
179+ },
180+ advanceBlockDelta : 0 ,
181+ advanceTimeDelta : 0 ,
182+ expErr : true ,
183+ expBlocksSince : 0 ,
184+ },
185+ }
186+
187+ for name , tc := range tests {
188+ s .Run (name , func () {
189+ s .SetupTest ()
190+ s .Ctx = s .Ctx .WithBlockHeight (startBlockHeight ).WithBlockTime (startBlockTime )
191+
192+ err := s .EpochsKeeper .AddEpochInfo (s .Ctx , tc .setupEpoch )
193+ s .Require ().NoError (err )
194+
195+ // Advance block height and time if needed
196+ s .Ctx = s .Ctx .WithBlockHeight (startBlockHeight + tc .advanceBlockDelta ).
197+ WithBlockTime (startBlockTime .Add (tc .advanceTimeDelta ))
198+
199+ blocksSince , err := s .EpochsKeeper .NumBlocksSinceEpochStart (s .Ctx , tc .setupEpoch .Identifier )
200+ if tc .expErr {
201+ s .Require ().Error (err )
202+ } else {
203+ s .Require ().NoError (err )
204+ s .Require ().Equal (tc .expBlocksSince , blocksSince )
205+ }
206+ })
207+ }
208+ }
0 commit comments