forked from lumtis/poa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenesis_test.go
84 lines (66 loc) · 2.49 KB
/
genesis_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package poa_test
import (
"testing"
"github.com/google/go-cmp/cmp"
tmtypes "github.com/tendermint/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ltacker/poa"
"github.com/ltacker/poa/types"
)
func TestValidateGenesis(t *testing.T) {
validator, _ := poa.MockValidator()
// Valid genesis
validGenesis := types.NewGenesisState(types.DefaultParams(), []types.Validator{validator})
if types.ValidateGenesis(validGenesis) != nil {
t.Errorf("The genesis state %v should be valid", validGenesis)
}
// A genesis with two validators with the same consensus pukey is invalid
invalidGenesis := types.NewGenesisState(types.DefaultParams(), []types.Validator{validator, validator})
if types.ValidateGenesis(invalidGenesis) == nil {
t.Errorf("The genesis state %v should not be valid", invalidGenesis)
}
// Default genesis state
if types.ValidateGenesis(types.DefaultGenesisState()) != nil {
t.Errorf("The default genesis state should be valid")
}
}
func TestInitGenesis(t *testing.T) {
ctx, poaKeeper := poa.MockContext()
validator, consPubKey := poa.MockValidator()
// Test genesis data
testGenesis := types.NewGenesisState(types.DefaultParams(), []types.Validator{validator})
// InitGenesis
validatorUpdates := poa.InitGenesis(ctx, poaKeeper, testGenesis)
// Only one update
if len(validatorUpdates) != 1 {
t.Errorf("Should get exactly one validator update")
}
// No weight
power := validatorUpdates[0].Power
if power != 1 {
t.Errorf("power should be 1, got %v", power)
}
// Correct public key
pubKey, err := tmtypes.PB2TM.PubKey(validatorUpdates[0].PubKey)
if err != nil {
t.Errorf("Incorrect public key: %v", err)
}
pubKeyString := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, pubKey)
if pubKeyString != consPubKey {
t.Errorf("validator PubKey should be %v, got %v", consPubKey, pubKeyString)
}
}
func TestExportGenesis(t *testing.T) {
ctx, poaKeeper := poa.MockContext()
validator, _ := poa.MockValidator()
// Manually set values in keeper
poaKeeper.SetValidator(ctx, validator)
poaKeeper.SetParams(ctx, types.DefaultParams())
exportedGenesis := poa.ExportGenesis(ctx, poaKeeper)
if !cmp.Equal(exportedGenesis.Params, types.DefaultParams()) {
t.Errorf("Exported genesis param shoud be: %v, not %v", types.DefaultParams(), exportedGenesis.Params)
}
if !cmp.Equal(exportedGenesis.Validators, []types.Validator{validator}) {
t.Errorf("Exported genesis validators shoud be: %v, not %v", []types.Validator{validator}, exportedGenesis.Validators)
}
}