forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(challenger,dispute-mon): refactor out min game timestamp into (et…
…hereum-optimism#9890) op-service.
- Loading branch information
Showing
6 changed files
with
94 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package clock | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type RWClock interface { | ||
Now() time.Time | ||
} | ||
|
||
// MinCheckedTimestamp returns the minimum checked unix timestamp. | ||
// If the duration is 0, the returned minimum timestamp is 0. | ||
// Otherwise, the minimum timestamp is the current unix time minus the duration. | ||
// The subtraction operation is checked and returns 0 on underflow. | ||
func MinCheckedTimestamp(clock RWClock, duration time.Duration) uint64 { | ||
if duration.Seconds() == 0 { | ||
return 0 | ||
} | ||
// To compute t-d for a duration d, use t.Add(-d). | ||
// See https://pkg.go.dev/time#Time.Sub | ||
if clock.Now().Unix() > int64(duration.Seconds()) { | ||
return uint64(clock.Now().Add(-duration).Unix()) | ||
} | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package clock | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMinCheckedTimestamp(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
now time.Time | ||
duration time.Duration | ||
want uint64 | ||
}{ | ||
{ | ||
name: "ZeroDurationZeroClock", | ||
now: time.Unix(0, 0), | ||
duration: 0, | ||
want: 0, | ||
}, | ||
{ | ||
name: "ZeroDurationPositiveClock", | ||
now: time.Unix(1, 0), | ||
duration: 0, | ||
want: 0, | ||
}, | ||
{ | ||
name: "UnderflowZeroClock", | ||
now: time.Unix(0, 0), | ||
duration: time.Second, | ||
want: 0, | ||
}, | ||
{ | ||
name: "UnderflowPositiveClock", | ||
now: time.Unix(1, 0), | ||
duration: time.Second * 2, | ||
want: 0, | ||
}, | ||
{ | ||
name: "CorrectArithmetic", | ||
now: time.Unix(100, 0), | ||
duration: time.Second * 10, | ||
want: 90, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
test := test | ||
t.Run(test.name, func(t *testing.T) { | ||
clock := &mockClock{now: test.now} | ||
require.Equal(t, test.want, MinCheckedTimestamp(clock, test.duration)) | ||
}) | ||
} | ||
} | ||
|
||
type mockClock struct { | ||
now time.Time | ||
} | ||
|
||
func (m *mockClock) Now() time.Time { | ||
return m.now | ||
} |