Skip to content

Commit

Permalink
Add genesis tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Mar 28, 2024
1 parent 62a04a5 commit 2eb3e52
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 43 deletions.
1 change: 0 additions & 1 deletion x/observer/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,5 @@ func TestBeginBlocker(t *testing.T) {
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
145 changes: 104 additions & 41 deletions x/observer/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,108 @@ 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),
}

// 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("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)
})
}

0 comments on commit 2eb3e52

Please sign in to comment.