Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Jan 10, 2024
1 parent d1feda8 commit 5676f2c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 78 deletions.
76 changes: 0 additions & 76 deletions x/observer/migrations/v4/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ import (
"testing"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/common"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/testutil/sample"
v4 "github.com/zeta-chain/zetacore/x/observer/migrations/v4"
"github.com/zeta-chain/zetacore/x/observer/types"
)
Expand All @@ -33,75 +29,3 @@ func TestMigrateCrosschainFlags(t *testing.T) {
assert.True(t, flags.BlockHeaderVerificationFlags.IsBtcTypeChainEnabled)
assert.True(t, flags.BlockHeaderVerificationFlags.IsEthTypeChainEnabled)
}

func TestMigrateObserverParams(t *testing.T) {
k, ctx := keepertest.ObserverKeeper(t)

// set chain params
previousChainParamsList := types.ChainParamsList{
ChainParams: []*types.ChainParams{
sample.ChainParams(1),
sample.ChainParams(2),
sample.ChainParams(3),
sample.ChainParams(4),
},
}
k.SetChainParamsList(ctx, previousChainParamsList)

// set observer params
dec42, err := sdk.NewDecFromStr("0.42")
require.NoError(t, err)
dec43, err := sdk.NewDecFromStr("0.43")
require.NoError(t, err)
dec1000, err := sdk.NewDecFromStr("1000.0")
require.NoError(t, err)
dec1001, err := sdk.NewDecFromStr("1001.0")
require.NoError(t, err)
params := types.Params{
ObserverParams: []*types.ObserverParams{
{
Chain: &common.Chain{ChainId: 2},
BallotThreshold: dec42,
MinObserverDelegation: dec1000,
IsSupported: true,
},
{
Chain: &common.Chain{ChainId: 3},
BallotThreshold: dec43,
MinObserverDelegation: dec1001,
IsSupported: true,
},
},
}
k.SetParams(ctx, params)

// perform migration
err = v4.MigrateObserverParams(ctx, *k)
require.NoError(t, err)

// check chain params
newChainParamsList, found := k.GetChainParamsList(ctx)
require.True(t, found)

// unchanged values
require.EqualValues(t, previousChainParamsList.ChainParams[0], newChainParamsList.ChainParams[0])
require.EqualValues(t, previousChainParamsList.ChainParams[3], newChainParamsList.ChainParams[3])

// changed values
require.EqualValues(t, dec42, newChainParamsList.ChainParams[1].BallotThreshold)
require.EqualValues(t, dec1000, newChainParamsList.ChainParams[1].MinObserverDelegation)
require.EqualValues(t, dec43, newChainParamsList.ChainParams[2].BallotThreshold)
require.EqualValues(t, dec1001, newChainParamsList.ChainParams[2].MinObserverDelegation)
require.True(t, newChainParamsList.ChainParams[1].IsSupported)
require.True(t, newChainParamsList.ChainParams[2].IsSupported)

// check remaining values are unchanged
previousChainParamsList.ChainParams[1].BallotThreshold = dec42
previousChainParamsList.ChainParams[2].BallotThreshold = dec43
previousChainParamsList.ChainParams[1].MinObserverDelegation = dec1000
previousChainParamsList.ChainParams[2].MinObserverDelegation = dec1001
previousChainParamsList.ChainParams[1].IsSupported = true
previousChainParamsList.ChainParams[2].IsSupported = true
require.EqualValues(t, previousChainParamsList.ChainParams[1], newChainParamsList.ChainParams[1])
require.EqualValues(t, previousChainParamsList.ChainParams[2], newChainParamsList.ChainParams[2])
}
78 changes: 76 additions & 2 deletions x/observer/migrations/v5/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/common"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/testutil/sample"
v5 "github.com/zeta-chain/zetacore/x/observer/migrations/v5"
"github.com/zeta-chain/zetacore/x/observer/types"
)

func TestMigrateStore(t *testing.T) {
func TestMigrateObserverMapper(t *testing.T) {
t.Run("TestMigrateStore", func(t *testing.T) {
k, ctx := keepertest.ObserverKeeper(t)
legacyObserverMapperStore := prefix.NewStore(ctx.KVStore(k.StoreKey()), types.KeyPrefix(types.ObserverMapperKey))
legacyObserverMapperList := sample.LegacyObserverMapperList(t, 12, "sample")
for _, legacyObserverMapper := range legacyObserverMapperList {
legacyObserverMapperStore.Set(types.KeyPrefix(legacyObserverMapper.Index), k.Codec().MustMarshal(legacyObserverMapper))
}
err := v5.MigrateStore(ctx, k.StoreKey(), k.Codec())
err := v5.MigrateObserverMapper(ctx, k.StoreKey(), k.Codec())
assert.NoError(t, err)
observerSet, found := k.GetObserverSet(ctx)
assert.True(t, found)
Expand All @@ -40,3 +42,75 @@ func TestMigrateStore(t *testing.T) {
assert.Equal(t, 0, len(observerMappers))
})
}

func TestMigrateObserverParams(t *testing.T) {
k, ctx := keepertest.ObserverKeeper(t)

// set chain params
previousChainParamsList := types.ChainParamsList{
ChainParams: []*types.ChainParams{
sample.ChainParams(1),
sample.ChainParams(2),
sample.ChainParams(3),
sample.ChainParams(4),
},
}
k.SetChainParamsList(ctx, previousChainParamsList)

// set observer params
dec42, err := sdk.NewDecFromStr("0.42")
require.NoError(t, err)
dec43, err := sdk.NewDecFromStr("0.43")
require.NoError(t, err)
dec1000, err := sdk.NewDecFromStr("1000.0")
require.NoError(t, err)
dec1001, err := sdk.NewDecFromStr("1001.0")
require.NoError(t, err)
params := types.Params{
ObserverParams: []*types.ObserverParams{
{
Chain: &common.Chain{ChainId: 2},
BallotThreshold: dec42,
MinObserverDelegation: dec1000,
IsSupported: true,
},
{
Chain: &common.Chain{ChainId: 3},
BallotThreshold: dec43,
MinObserverDelegation: dec1001,
IsSupported: true,
},
},
}
k.SetParams(ctx, params)

// perform migration
err = v5.MigrateObserverParams(ctx, *k)
require.NoError(t, err)

// check chain params
newChainParamsList, found := k.GetChainParamsList(ctx)
require.True(t, found)

// unchanged values
require.EqualValues(t, previousChainParamsList.ChainParams[0], newChainParamsList.ChainParams[0])
require.EqualValues(t, previousChainParamsList.ChainParams[3], newChainParamsList.ChainParams[3])

// changed values
require.EqualValues(t, dec42, newChainParamsList.ChainParams[1].BallotThreshold)
require.EqualValues(t, dec1000, newChainParamsList.ChainParams[1].MinObserverDelegation)
require.EqualValues(t, dec43, newChainParamsList.ChainParams[2].BallotThreshold)
require.EqualValues(t, dec1001, newChainParamsList.ChainParams[2].MinObserverDelegation)
require.True(t, newChainParamsList.ChainParams[1].IsSupported)
require.True(t, newChainParamsList.ChainParams[2].IsSupported)

// check remaining values are unchanged
previousChainParamsList.ChainParams[1].BallotThreshold = dec42
previousChainParamsList.ChainParams[2].BallotThreshold = dec43
previousChainParamsList.ChainParams[1].MinObserverDelegation = dec1000
previousChainParamsList.ChainParams[2].MinObserverDelegation = dec1001
previousChainParamsList.ChainParams[1].IsSupported = true
previousChainParamsList.ChainParams[2].IsSupported = true
require.EqualValues(t, previousChainParamsList.ChainParams[1], newChainParamsList.ChainParams[1])
require.EqualValues(t, previousChainParamsList.ChainParams[2], newChainParamsList.ChainParams[2])
}

0 comments on commit 5676f2c

Please sign in to comment.