forked from strangelove-ventures/horcrux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_signer_validator_test.go
127 lines (93 loc) · 3.77 KB
/
single_signer_validator_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package signer
import (
"context"
"path/filepath"
"time"
"os"
"testing"
cometcryptoed25519 "github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cometbft/cometbft/crypto/tmhash"
cometjson "github.com/cometbft/cometbft/libs/json"
cometrand "github.com/cometbft/cometbft/libs/rand"
cometprivval "github.com/cometbft/cometbft/privval"
cometproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/require"
)
func TestSingleSignerValidator(t *testing.T) {
t.Skip("TODO: fix this test when run with 'make test'")
tmpDir := t.TempDir()
stateDir := filepath.Join(tmpDir, "state")
err := os.MkdirAll(stateDir, 0700)
require.NoError(t, err)
runtimeConfig := &RuntimeConfig{
HomeDir: tmpDir,
StateDir: filepath.Join(tmpDir, "state"),
}
privateKey := cometcryptoed25519.GenPrivKey()
marshaled, err := cometjson.Marshal(cometprivval.FilePVKey{
Address: privateKey.PubKey().Address(),
PubKey: privateKey.PubKey(),
PrivKey: privateKey,
})
require.NoError(t, err)
err = os.WriteFile(runtimeConfig.KeyFilePathSingleSigner(testChainID), marshaled, 0600)
require.NoError(t, err)
err = os.WriteFile(runtimeConfig.KeyFilePathSingleSigner("different"), marshaled, 0600)
require.NoError(t, err)
validator := NewSingleSignerValidator(runtimeConfig)
proposal := cometproto.Proposal{
Height: 1,
Round: 20,
Type: cometproto.ProposalType,
}
block := ProposalToBlock(testChainID, &proposal)
ctx := context.Background()
signature, _, _, err := validator.Sign(ctx, testChainID, block)
require.NoError(t, err)
require.True(t, privateKey.PubKey().VerifySignature(block.SignBytes, signature))
proposal.Timestamp = time.Now()
// should be able to sign same proposal with only differing timestamp
_, _, _, err = validator.Sign(ctx, testChainID, ProposalToBlock(testChainID, &proposal))
require.NoError(t, err)
// construct different block ID for proposal at same height as highest signed
randHash := cometrand.Bytes(tmhash.Size)
blockID := cometproto.BlockID{Hash: randHash,
PartSetHeader: cometproto.PartSetHeader{Total: 5, Hash: randHash}}
proposal = cometproto.Proposal{
Height: 1,
Round: 20,
Type: cometproto.ProposalType,
BlockID: blockID,
}
// should not be able to sign same proposal at same height as highest signed with different BlockID
_, _, _, err = validator.Sign(ctx, testChainID, ProposalToBlock(testChainID, &proposal))
require.Error(t, err, "double sign!")
proposal.Round = 19
// should not be able to sign lower than highest signed
_, _, _, err = validator.Sign(ctx, testChainID, ProposalToBlock(testChainID, &proposal))
require.Error(t, err, "double sign!")
// lower LSS should sign for different chain ID
_, _, _, err = validator.Sign(ctx, "different", ProposalToBlock("different", &proposal))
require.NoError(t, err)
// reinitialize validator to make sure new runtime will not allow double sign
validator = NewSingleSignerValidator(runtimeConfig)
_, _, _, err = validator.Sign(ctx, testChainID, ProposalToBlock(testChainID, &proposal))
require.Error(t, err, "double sign!")
proposal.Round = 21
// signing higher block now should succeed
_, _, _, err = validator.Sign(ctx, testChainID, ProposalToBlock(testChainID, &proposal))
require.NoError(t, err)
precommit := cometproto.Vote{
Height: 2,
Round: 0,
Type: cometproto.PrecommitType,
Timestamp: time.Now(),
Extension: []byte("test"),
}
block = VoteToBlock(testChainID, &precommit)
sig, voteExtSig, _, err := validator.Sign(ctx, testChainID, block)
require.NoError(t, err)
require.True(t, privateKey.PubKey().VerifySignature(block.SignBytes, sig), "signature verification failed")
require.True(t, privateKey.PubKey().VerifySignature(block.VoteExtensionSignBytes, voteExtSig),
"vote extension signature verification failed")
}