Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support milliseconds for time delta in Test.moveTime #3015

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion runtime/stdlib/test-framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type Blockchain interface {

Reset(uint64)

MoveTime(int64)
MoveTime(float64)

CreateSnapshot(string) error

Expand Down
2 changes: 1 addition & 1 deletion runtime/stdlib/test_emulatorbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ func (t *testEmulatorBackendType) newMoveTimeFunction(
if !ok {
panic(errors.NewUnreachableError())
}
blockchain.MoveTime(int64(timeDelta.ToInt(invocation.LocationRange)))
blockchain.MoveTime(float64(timeDelta) / sema.Fix64Factor)
Copy link
Member

@SupunS SupunS Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting an int64 to float64 could lose precision in some cases. We could instead pass int64 (Fix64Value as is) to MoveTime, and change the parameter's metric to accept nano-seconds (it currently accepts seconds). But might need to pad-right if we do so, since Fix64Factor is 10^8 and nano-seconds factor 10^9. But then there's a risk of padding-right could overflow/underflow. So IDK which one is better.

In reality, these extreme cases would not be even used, so maybe we don't need to worry much, and just support only up to milliseconds and properly documenting might be enough?
@turbolent wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, we shouldn't use floating point here

return interpreter.Void
},
)
Expand Down
16 changes: 8 additions & 8 deletions runtime/stdlib/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2366,7 +2366,7 @@ func TestBlockchain(t *testing.T) {
// timeDelta is the representation of 35 days,
// in the form of seconds.
let timeDelta = Fix64(35 * 24 * 60 * 60)
Test.moveTime(by: timeDelta)
Test.moveTime(by: timeDelta + 0.5)
}
`

Expand All @@ -2375,9 +2375,9 @@ func TestBlockchain(t *testing.T) {
testFramework := &mockedTestFramework{
emulatorBackend: func() Blockchain {
return &mockedBlockchain{
moveTime: func(timeDelta int64) {
moveTime: func(timeDelta float64) {
moveTimeInvoked = true
assert.Equal(t, int64(3024000), timeDelta)
assert.Equal(t, 3024000.5, timeDelta)
},
}
},
Expand Down Expand Up @@ -2412,9 +2412,9 @@ func TestBlockchain(t *testing.T) {
testFramework := &mockedTestFramework{
emulatorBackend: func() Blockchain {
return &mockedBlockchain{
moveTime: func(timeDelta int64) {
moveTime: func(timeDelta float64) {
moveTimeInvoked = true
assert.Equal(t, int64(-3024000), timeDelta)
assert.Equal(t, -3024000.0, timeDelta)
},
}
},
Expand Down Expand Up @@ -2446,7 +2446,7 @@ func TestBlockchain(t *testing.T) {
testFramework := &mockedTestFramework{
emulatorBackend: func() Blockchain {
return &mockedBlockchain{
moveTime: func(timeDelta int64) {
moveTime: func(timeDelta float64) {
moveTimeInvoked = true
},
}
Expand Down Expand Up @@ -2876,7 +2876,7 @@ type mockedBlockchain struct {
serviceAccount func() (*Account, error)
events func(inter *interpreter.Interpreter, eventType interpreter.StaticType) interpreter.Value
reset func(uint64)
moveTime func(int64)
moveTime func(float64)
createSnapshot func(string) error
loadSnapshot func(string) error
}
Expand Down Expand Up @@ -2989,7 +2989,7 @@ func (m mockedBlockchain) Reset(height uint64) {
m.reset(height)
}

func (m mockedBlockchain) MoveTime(timeDelta int64) {
func (m mockedBlockchain) MoveTime(timeDelta float64) {
if m.moveTime == nil {
panic("'SetTimestamp' is not implemented")
}
Expand Down
Loading