upgrade_test.gno
2.49 Kb · 67 lines
1package tendermint
2
3import (
4 "testing"
5 "time"
6
7 "gno.land/p/aib/ibc/types"
8 "gno.land/p/nt/uassert/v0"
9 "gno.land/p/nt/urequire/v0"
10)
11
12func TestCalculateNewTrustingPeriod(t *testing.T) {
13 hour := time.Hour
14 day := 24 * time.Hour
15 week := 7 * day
16
17 t.Run("scales proportionally when unbonding shrinks", func(t *testing.T) {
18 // trusting=2 weeks, current unbonding=3 weeks, new unbonding=2 weeks
19 // → new trusting = 2w * 2w / 3w = ~9.33d, truncated to seconds.
20 got := calculateNewTrustingPeriod(2*week, 3*week, 2*week)
21 want := time.Duration((2 * week / time.Second) * (2 * week / time.Second) / (3 * week / time.Second)) * time.Second
22 uassert.Equal(t, int64(want), int64(got))
23 // Sanity: result is strictly less than original trusting period.
24 uassert.True(t, got < 2*week)
25 })
26
27 t.Run("equal unbonding leaves trusting unchanged (modulo second truncation)", func(t *testing.T) {
28 got := calculateNewTrustingPeriod(2*week, 3*week, 3*week)
29 uassert.Equal(t, int64(2*week), int64(got))
30 })
31
32 t.Run("zero current unbonding returns zero rather than panicking", func(t *testing.T) {
33 got := calculateNewTrustingPeriod(hour, 0, hour)
34 uassert.Equal(t, int64(0), int64(got))
35 })
36
37 t.Run("sub-second trusting truncates to zero", func(t *testing.T) {
38 // Documented loss of precision: math is performed in seconds.
39 got := calculateNewTrustingPeriod(500*time.Millisecond, week, week)
40 uassert.Equal(t, int64(0), int64(got))
41 })
42}
43
44func TestBuildUpgradeMerklePath(t *testing.T) {
45 height := types.NewHeight(1, 100)
46
47 t.Run("canonical two-element path keeps elements separate", func(t *testing.T) {
48 mp := buildUpgradeMerklePath([]string{"upgrade", "upgradedIBCState"}, "upgradedClient", height)
49 urequire.Equal(t, 2, len(mp.KeyPath))
50 uassert.Equal(t, "upgrade", string(mp.KeyPath[0]))
51 uassert.Equal(t, "upgradedIBCState/100/upgradedClient", string(mp.KeyPath[1]))
52 })
53
54 t.Run("single-element path produces a single key with the suffix", func(t *testing.T) {
55 mp := buildUpgradeMerklePath([]string{"upgrade"}, "upgradedConsState", height)
56 urequire.Equal(t, 1, len(mp.KeyPath))
57 uassert.Equal(t, "upgrade/100/upgradedConsState", string(mp.KeyPath[0]))
58 })
59
60 t.Run("three-element path keeps each prefix segment separate", func(t *testing.T) {
61 mp := buildUpgradeMerklePath([]string{"a", "b", "c"}, "upgradedClient", height)
62 urequire.Equal(t, 3, len(mp.KeyPath))
63 uassert.Equal(t, "a", string(mp.KeyPath[0]))
64 uassert.Equal(t, "b", string(mp.KeyPath[1]))
65 uassert.Equal(t, "c/100/upgradedClient", string(mp.KeyPath[2]))
66 })
67}