forked from omni-network/omni
-
Notifications
You must be signed in to change notification settings - Fork 1
/
privval_internal_test.go
112 lines (95 loc) · 2.62 KB
/
privval_internal_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package app
import (
"os"
"path/filepath"
"testing"
halocfg "github.com/omni-network/omni/halo/config"
"github.com/omni-network/omni/lib/k1util"
cmtconfig "github.com/cometbft/cometbft/config"
"github.com/cometbft/cometbft/privval"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
)
func TestLoadPrivVal(t *testing.T) {
t.Parallel()
tests := []struct {
name string
cmtPrivval bool
cmtPrivState bool
err bool
}{
{
name: "comet privval and state",
cmtPrivval: true,
cmtPrivState: true,
},
{
name: "no files",
cmtPrivval: false,
cmtPrivState: false,
err: true,
},
{
name: "comet privval only",
cmtPrivval: true,
cmtPrivState: false,
err: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
homeDir := t.TempDir()
// Define the file paths
cmtPrivvalFile := filepath.Join(homeDir, "config", "priv_validator_key.json")
cmtPrivStateFile := filepath.Join(homeDir, "data", "priv_validator_state.json")
// Ensure the config and data directories exist
require.NoError(t, os.Mkdir(filepath.Dir(cmtPrivvalFile), 0755))
require.NoError(t, os.Mkdir(filepath.Dir(cmtPrivStateFile), 0755))
// Generate the expected private key
privKey, err := crypto.GenerateKey()
require.NoError(t, err)
// Convert the private key to a comet private key
cmtPrivKey, err := k1util.StdPrivKeyToComet(privKey)
require.NoError(t, err)
// Write the comet privval file (with non-zero state)
key := privval.NewFilePV(cmtPrivKey, cmtPrivvalFile, cmtPrivStateFile)
err = key.SignVote("chain", &cmtproto.Vote{
Type: cmtproto.PrecommitType,
})
require.NoError(t, err)
key.Save()
// Remove the files if they are not needed
if !tt.cmtPrivval {
require.NoError(t, os.Remove(cmtPrivvalFile))
}
if !tt.cmtPrivState {
require.NoError(t, os.Remove(cmtPrivStateFile))
}
// Setup the config
cfg := Config{
Config: halocfg.Config{
HomeDir: homeDir,
},
Comet: cmtconfig.Config{
BaseConfig: cmtconfig.BaseConfig{
RootDir: homeDir,
PrivValidatorKey: "config/priv_validator_key.json",
PrivValidatorState: "data/priv_validator_state.json",
},
},
}
// Run the test
pv, err := loadPrivVal(cfg)
// Assert the results
if tt.err {
require.Error(t, err)
} else {
require.NoError(t, err)
require.NotNil(t, pv)
require.True(t, pv.Key.PrivKey.Equals(cmtPrivKey))
}
})
}
}