diff --git a/docs/reference/stdlibs/std/testing.md b/docs/reference/stdlibs/std/testing.md index 8c9146c81a1..fd42ddac9b5 100644 --- a/docs/reference/stdlibs/std/testing.md +++ b/docs/reference/stdlibs/std/testing.md @@ -31,6 +31,8 @@ func TestSkipHeights(count int64) ``` Modifies the block height variable by skipping **count** blocks. +It also increases block timestamp by 5 seconds for every single count + #### Usage ```go std.TestSkipHeights(100) diff --git a/examples/gno.land/p/demo/rand/rand0_filetest.gno b/examples/gno.land/p/demo/rand/rand0_filetest.gno index c5c138924fc..446e04b696d 100644 --- a/examples/gno.land/p/demo/rand/rand0_filetest.gno +++ b/examples/gno.land/p/demo/rand/rand0_filetest.gno @@ -49,8 +49,8 @@ func main() { // 177 // 802 // --- -// 450 -// 78 -// 777 -// 15 -// 339 +// 269 +// 233 +// 591 +// 936 +// 908 diff --git a/examples/gno.land/r/x/skip_height_to_skip_time/skiptime.gno b/examples/gno.land/r/x/skip_height_to_skip_time/skiptime.gno new file mode 100644 index 00000000000..524e6f58ad9 --- /dev/null +++ b/examples/gno.land/r/x/skip_height_to_skip_time/skiptime.gno @@ -0,0 +1 @@ +package skiptime diff --git a/examples/gno.land/r/x/skip_height_to_skip_time/skiptime_test.gno b/examples/gno.land/r/x/skip_height_to_skip_time/skiptime_test.gno new file mode 100644 index 00000000000..52670a5626b --- /dev/null +++ b/examples/gno.land/r/x/skip_height_to_skip_time/skiptime_test.gno @@ -0,0 +1,27 @@ +package skiptime + +import ( + "std" + "testing" + "time" +) + +func TestSkipHeights(t *testing.T) { + oldHeight := std.GetHeight() + shouldEQ(t, oldHeight, 123) + + oldNow := time.Now().Unix() + shouldEQ(t, oldNow, 1234567890) + + // skip 3 blocks == 15 seconds + std.TestSkipHeights(3) + + shouldEQ(t, std.GetHeight()-oldHeight, 3) + shouldEQ(t, time.Now().Unix()-oldNow, 15) +} + +func shouldEQ(t *testing.T, got, expected int64) { + if got != expected { + t.Fatalf("expected %d, got %d.", expected, got) + } +} diff --git a/gnovm/tests/stdlibs/std/std.go b/gnovm/tests/stdlibs/std/std.go index f7bc001043e..a39d87acef4 100644 --- a/gnovm/tests/stdlibs/std/std.go +++ b/gnovm/tests/stdlibs/std/std.go @@ -46,6 +46,7 @@ func TestCurrentRealm(m *gno.Machine) string { func TestSkipHeights(m *gno.Machine, count int64) { ctx := m.Context.(std.ExecContext) ctx.Height += count + ctx.Timestamp += (count * 5) m.Context = ctx }