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

test: improve observer module coverage #1961

Merged
merged 33 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
62a04a5
Add abci tests
skosito Mar 28, 2024
2eb3e52
Add genesis tests
skosito Mar 28, 2024
363af6b
Extend genesis tests
skosito Mar 28, 2024
6e6edf7
Add ballot types test and remove redundant test data file
skosito Mar 28, 2024
b47ac82
Add chain params tests and fix failing test
skosito Mar 28, 2024
b6f6db6
Add more types tests
skosito Mar 28, 2024
19b4d90
Add some grpc query keeper tests
skosito Mar 28, 2024
e557164
More grpc query tests
skosito Mar 29, 2024
d182989
Add more tests
skosito Mar 29, 2024
a3b926d
Fix test
skosito Mar 29, 2024
896c0ef
Add tss tests
skosito Mar 29, 2024
ede13b2
Ignore couple more files
skosito Mar 29, 2024
36081e6
Add more tests
skosito Mar 29, 2024
a2582a8
Add msg server blame vote tests
skosito Mar 29, 2024
4299774
Add msg server add observer tests
skosito Mar 29, 2024
e7c6d9f
Add update key gen tests
skosito Mar 29, 2024
de88a7d
Fix tests
skosito Mar 29, 2024
e99b43b
Add more tests
skosito Mar 29, 2024
7721694
Add utils test
skosito Mar 29, 2024
6ef3710
Add hooks tests
skosito Mar 29, 2024
e70f026
Merge branch 'develop' into test-observer-module
skosito Mar 29, 2024
204c68c
Changelog
skosito Mar 29, 2024
60fb2b1
Fix lint
skosito Mar 29, 2024
9aae130
Remove redundant todos
skosito Apr 1, 2024
7a10802
Merge branch 'develop' into test-observer-module
skosito Apr 2, 2024
c3ee9a1
Remove todo
skosito Apr 2, 2024
a90b256
Merge branch 'develop' into test-observer-module
skosito Apr 2, 2024
c7bf3d8
Fix PR comments
skosito Apr 3, 2024
dc1a85e
add issues
kingpinXD Apr 3, 2024
3033978
Add missing test
skosito Apr 3, 2024
68a0b60
Merge branch 'develop' into test-observer-module
skosito Apr 3, 2024
936bde8
Make generate
skosito Apr 3, 2024
f502001
Fix lint
skosito Apr 3, 2024
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
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
* [1879](https://github.com/zeta-chain/node/pull/1879) - full coverage for messages in types packages
* [1899](https://github.com/zeta-chain/node/pull/1899) - add empty test files so packages are included in coverage
* [1903](https://github.com/zeta-chain/node/pull/1903) - common package tests
* [1961](https://github.com/zeta-chain/node/pull/1961) - improve observer module coverage

### Fixes

Expand Down
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ ignore:
- "x/**/types/types.go"
- "x/**/types/expected_keepers.go"
- "x/**/module.go"
- "x/**/events.go"
- "x/**/migrator.go"
- "x/**/module_simulation.go"
- "x/**/simulation/"
- "**/*.proto"
Expand Down
5 changes: 5 additions & 0 deletions testutil/keeper/mocks/observer/staking.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions x/observer/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
return
}
totalObserverCountCurrentBlock := allObservers.LenUint()
if totalObserverCountCurrentBlock < 0 {
ctx.Logger().Error("TotalObserverCount is negative at height", ctx.BlockHeight())
return
}
// #nosec G701 always in range
if totalObserverCountCurrentBlock == lastBlockObserverCount.Count {
return
Expand Down
98 changes: 98 additions & 0 deletions x/observer/abci_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package observer_test

import (
"math"
"testing"

"github.com/stretchr/testify/require"
keepertest "github.com/zeta-chain/zetacore/testutil/keeper"
"github.com/zeta-chain/zetacore/testutil/sample"
"github.com/zeta-chain/zetacore/x/observer"
"github.com/zeta-chain/zetacore/x/observer/types"
)

func TestBeginBlocker(t *testing.T) {
t.Run("should not update LastObserverCount if not found", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

observer.BeginBlocker(ctx, *k)

_, found := k.GetLastObserverCount(ctx)
require.False(t, found)

_, found = k.GetKeygen(ctx)
require.False(t, found)
})

t.Run("should not update LastObserverCount if observer set not found", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

count := 1
k.SetLastObserverCount(ctx, &types.LastObserverCount{
Count: uint64(count),
})

observer.BeginBlocker(ctx, *k)

lastObserverCount, found := k.GetLastObserverCount(ctx)
require.True(t, found)
require.Equal(t, uint64(count), lastObserverCount.Count)
require.Equal(t, int64(0), lastObserverCount.LastChangeHeight)

_, found = k.GetKeygen(ctx)
require.False(t, found)
})

t.Run("should not update LastObserverCount if observer set count equal last observed count", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

count := 1
os := sample.ObserverSet(count)
k.SetObserverSet(ctx, os)
k.SetLastObserverCount(ctx, &types.LastObserverCount{
Count: uint64(count),
})

observer.BeginBlocker(ctx, *k)

lastObserverCount, found := k.GetLastObserverCount(ctx)
require.True(t, found)
require.Equal(t, uint64(count), lastObserverCount.Count)
require.Equal(t, int64(0), lastObserverCount.LastChangeHeight)

_, found = k.GetKeygen(ctx)
require.False(t, found)
})

t.Run("should update LastObserverCount", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

observeSetLen := 10
count := 1
os := sample.ObserverSet(observeSetLen)
k.SetObserverSet(ctx, os)
k.SetLastObserverCount(ctx, &types.LastObserverCount{
Count: uint64(count),
})

keygen, found := k.GetKeygen(ctx)
require.False(t, found)
require.Equal(t, types.Keygen{}, keygen)

observer.BeginBlocker(ctx, *k)

keygen, found = k.GetKeygen(ctx)
require.True(t, found)
require.Empty(t, keygen.GranteePubkeys)
require.Equal(t, types.KeygenStatus_PendingKeygen, keygen.Status)
require.Equal(t, int64(math.MaxInt64), keygen.BlockNumber)

inboundEnabled := k.IsInboundEnabled(ctx)
require.False(t, inboundEnabled)

lastObserverCount, found := k.GetLastObserverCount(ctx)
require.True(t, found)
require.Equal(t, uint64(observeSetLen), lastObserverCount.Count)
require.Equal(t, ctx.BlockHeight(), lastObserverCount.LastChangeHeight)
})
}
2 changes: 1 addition & 1 deletion x/observer/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
observerCount = uint64(len(genState.Observers.ObserverList))
}

// if chian params are defined set them
// if chain params are defined set them
if len(genState.ChainParamsList.ChainParams) > 0 {
k.SetChainParamsList(ctx, genState.ChainParamsList)
} else {
Expand Down
188 changes: 147 additions & 41 deletions x/observer/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,151 @@ import (
)

func TestGenesis(t *testing.T) {
params := types.DefaultParams()
tss := sample.Tss()
genesisState := types.GenesisState{
Params: &params,
Tss: &tss,
BlameList: sample.BlameRecordsList(t, 10),
Ballots: []*types.Ballot{
sample.Ballot(t, "0"),
sample.Ballot(t, "1"),
sample.Ballot(t, "2"),
},
Observers: sample.ObserverSet(3),
NodeAccountList: []*types.NodeAccount{
sample.NodeAccount(),
sample.NodeAccount(),
sample.NodeAccount(),
},
CrosschainFlags: types.DefaultCrosschainFlags(),
Keygen: sample.Keygen(t),
ChainParamsList: sample.ChainParamsList(),
LastObserverCount: sample.LastObserverCount(10),
TssFundMigrators: []types.TssFundMigratorInfo{sample.TssFundsMigrator(1), sample.TssFundsMigrator(2)},
ChainNonces: []types.ChainNonces{
sample.ChainNonces(t, "0"),
sample.ChainNonces(t, "1"),
sample.ChainNonces(t, "2"),
},
PendingNonces: sample.PendingNoncesList(t, "sample", 20),
NonceToCctx: sample.NonceToCctxList(t, "sample", 20),
}

// Init and export
k, ctx, _, _ := keepertest.ObserverKeeper(t)
observer.InitGenesis(ctx, *k, genesisState)
got := observer.ExportGenesis(ctx, *k)
require.NotNil(t, got)

// Compare genesis after init and export
nullify.Fill(&genesisState)
nullify.Fill(got)
require.Equal(t, genesisState, *got)
t.Run("genState fields defined", func(t *testing.T) {
params := types.DefaultParams()
tss := sample.Tss()
genesisState := types.GenesisState{
Params: &params,
Tss: &tss,
BlameList: sample.BlameRecordsList(t, 10),
Ballots: []*types.Ballot{
sample.Ballot(t, "0"),
sample.Ballot(t, "1"),
sample.Ballot(t, "2"),
},
Observers: sample.ObserverSet(3),
NodeAccountList: []*types.NodeAccount{
sample.NodeAccount(),
sample.NodeAccount(),
sample.NodeAccount(),
},
CrosschainFlags: types.DefaultCrosschainFlags(),
Keygen: sample.Keygen(t),
ChainParamsList: sample.ChainParamsList(),
LastObserverCount: sample.LastObserverCount(10),
TssFundMigrators: []types.TssFundMigratorInfo{sample.TssFundsMigrator(1), sample.TssFundsMigrator(2)},
ChainNonces: []types.ChainNonces{
sample.ChainNonces(t, "0"),
sample.ChainNonces(t, "1"),
sample.ChainNonces(t, "2"),
},
PendingNonces: sample.PendingNoncesList(t, "sample", 20),
NonceToCctx: sample.NonceToCctxList(t, "sample", 20),
TssHistory: []types.TSS{sample.Tss()},
}

// Init and export
k, ctx, _, _ := keepertest.ObserverKeeper(t)
observer.InitGenesis(ctx, *k, genesisState)
got := observer.ExportGenesis(ctx, *k)
require.NotNil(t, got)

// Compare genesis after init and export
nullify.Fill(&genesisState)
nullify.Fill(got)
require.Equal(t, genesisState, *got)
})

t.Run("genState fields not defined", func(t *testing.T) {
genesisState := types.GenesisState{}

k, ctx, _, _ := keepertest.ObserverKeeper(t)
observer.InitGenesis(ctx, *k, genesisState)
got := observer.ExportGenesis(ctx, *k)
require.NotNil(t, got)

defaultParams := types.DefaultParams()
btcChainParams := types.GetDefaultBtcRegtestChainParams()
btcChainParams.IsSupported = true
goerliChainParams := types.GetDefaultGoerliLocalnetChainParams()
goerliChainParams.IsSupported = true
zetaPrivnetChainParams := types.GetDefaultZetaPrivnetChainParams()
zetaPrivnetChainParams.IsSupported = true
localnetChainParams := types.ChainParamsList{
ChainParams: []*types.ChainParams{
btcChainParams,
goerliChainParams,
zetaPrivnetChainParams,
},
}
expectedGenesisState := types.GenesisState{
Params: &defaultParams,
CrosschainFlags: types.DefaultCrosschainFlags(),
ChainParamsList: localnetChainParams,
Tss: &types.TSS{},
Keygen: &types.Keygen{},
LastObserverCount: &types.LastObserverCount{},
NodeAccountList: []*types.NodeAccount{},
}

require.Equal(t, expectedGenesisState, *got)
})

t.Run("genState fields not defined except tss", func(t *testing.T) {
tss := sample.Tss()
genesisState := types.GenesisState{
Tss: &tss,
}

k, ctx, _, _ := keepertest.ObserverKeeper(t)
observer.InitGenesis(ctx, *k, genesisState)
got := observer.ExportGenesis(ctx, *k)
require.NotNil(t, got)

defaultParams := types.DefaultParams()
btcChainParams := types.GetDefaultBtcRegtestChainParams()
btcChainParams.IsSupported = true
goerliChainParams := types.GetDefaultGoerliLocalnetChainParams()
goerliChainParams.IsSupported = true
zetaPrivnetChainParams := types.GetDefaultZetaPrivnetChainParams()
zetaPrivnetChainParams.IsSupported = true
localnetChainParams := types.ChainParamsList{
ChainParams: []*types.ChainParams{
btcChainParams,
goerliChainParams,
zetaPrivnetChainParams,
},
}
pendingNonces, err := k.GetAllPendingNonces(ctx)
require.NoError(t, err)
require.NotEmpty(t, pendingNonces)
expectedGenesisState := types.GenesisState{
Params: &defaultParams,
CrosschainFlags: types.DefaultCrosschainFlags(),
ChainParamsList: localnetChainParams,
Tss: &tss,
Keygen: &types.Keygen{},
LastObserverCount: &types.LastObserverCount{},
NodeAccountList: []*types.NodeAccount{},
PendingNonces: pendingNonces,
}

require.Equal(t, expectedGenesisState, *got)
})

t.Run("export without init", func(t *testing.T) {
k, ctx, _, _ := keepertest.ObserverKeeper(t)

got := observer.ExportGenesis(ctx, *k)
require.NotNil(t, got)

params := k.GetParamsIfExists(ctx)
expectedGenesisState := types.GenesisState{
Params: &params,
CrosschainFlags: types.DefaultCrosschainFlags(),
ChainParamsList: types.ChainParamsList{},
Tss: &types.TSS{},
Keygen: &types.Keygen{},
LastObserverCount: &types.LastObserverCount{},
NodeAccountList: []*types.NodeAccount{},
Ballots: k.GetAllBallots(ctx),
TssHistory: k.GetAllTSS(ctx),
TssFundMigrators: k.GetAllTssFundMigrators(ctx),
BlameList: k.GetAllBlame(ctx),
ChainNonces: k.GetAllChainNonces(ctx),
NonceToCctx: k.GetAllNonceToCctx(ctx),
}

require.Equal(t, expectedGenesisState, *got)
})
}
Loading
Loading