Skip to content

Commit 6a8b5d8

Browse files
authored
manager: use validator set from state (#814)
This PR changes the signed header to use validator set from last state. Tests needed to be updated to use at least one validator (because `ValidateBasic` now fails if there's nil validators). Since they need to be able to handle multiple validators anyway, we're passing 2 validators for now. Fixes #812
1 parent d450698 commit 6a8b5d8

5 files changed

Lines changed: 62 additions & 50 deletions

File tree

block/manager.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/libp2p/go-libp2p/core/crypto"
1111
abci "github.com/tendermint/tendermint/abci/types"
1212
tmcrypto "github.com/tendermint/tendermint/crypto"
13-
"github.com/tendermint/tendermint/crypto/ed25519"
1413
"github.com/tendermint/tendermint/crypto/merkle"
1514
"github.com/tendermint/tendermint/proxy"
1615
tmtypes "github.com/tendermint/tendermint/types"
@@ -494,19 +493,7 @@ func (m *Manager) publishBlock(ctx context.Context) error {
494493
// set the commit to current block's signed header
495494
block.SignedHeader.Commit = *commit
496495

497-
// set the validator set using the signer's public key
498-
// TODO(ganesh): need to hook into a module that selects signers
499-
pubKeyRaw, err := m.proposerKey.GetPublic().Raw()
500-
if err != nil {
501-
return err
502-
}
503-
pubKey := ed25519.PubKey(pubKeyRaw)
504-
proposer := &tmtypes.Validator{Address: pubKey.Address(), PubKey: pubKey}
505-
// TODO: read staking query to construct validators
506-
block.SignedHeader.Validators = &tmtypes.ValidatorSet{
507-
Validators: []*tmtypes.Validator{proposer},
508-
Proposer: proposer,
509-
}
496+
block.SignedHeader.Validators = m.lastState.Validators
510497

511498
// SaveBlock commits the DB tx
512499
err = m.store.SaveBlock(block, commit)

node/full_client_test.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/tendermint/tendermint/version"
3030

3131
"github.com/rollkit/rollkit/config"
32+
"github.com/rollkit/rollkit/conv"
3233
abciconv "github.com/rollkit/rollkit/conv/abci"
3334
"github.com/rollkit/rollkit/mocks"
3435
"github.com/rollkit/rollkit/types"
@@ -53,6 +54,21 @@ func getRandomValidatorSet() *tmtypes.ValidatorSet {
5354
}
5455
}
5556

57+
// TODO: use n and return n validators
58+
func getGenesisValidatorSetWithSigner(n int) ([]tmtypes.GenesisValidator, crypto.PrivKey) {
59+
validatorKey := ed25519.GenPrivKey()
60+
nodeKey := &p2p.NodeKey{
61+
PrivKey: validatorKey,
62+
}
63+
signingKey, _ := conv.GetNodeKey(nodeKey)
64+
pubKey := validatorKey.PubKey()
65+
66+
genesisValidators := []tmtypes.GenesisValidator{
67+
{Address: pubKey.Address(), PubKey: pubKey, Power: int64(100), Name: "gen #1"},
68+
}
69+
return genesisValidators, signingKey
70+
}
71+
5672
func TestConnectionGetter(t *testing.T) {
5773
assert := assert.New(t)
5874

@@ -410,15 +426,7 @@ func TestTx(t *testing.T) {
410426
mockApp := &mocks.Application{}
411427
mockApp.On("InitChain", mock.Anything).Return(abci.ResponseInitChain{})
412428
key, _, _ := crypto.GenerateEd25519Key(crand.Reader)
413-
signingKey, _, _ := crypto.GenerateEd25519Key(crand.Reader)
414-
415-
vKeys := make([]tmcrypto.PrivKey, 4)
416-
genesisValidators := make([]tmtypes.GenesisValidator, len(vKeys))
417-
for i := 0; i < len(vKeys); i++ {
418-
vKeys[i] = ed25519.GenPrivKey()
419-
genesisValidators[i] = tmtypes.GenesisValidator{Address: vKeys[i].PubKey().Address(), PubKey: vKeys[i].PubKey(), Power: int64(i + 100), Name: fmt.Sprintf("genesis validator #%d", i)}
420-
}
421-
429+
genesisValidators, signingKey := getGenesisValidatorSetWithSigner(1)
422430
node, err := newFullNode(context.Background(), config.NodeConfig{
423431
DALayer: "mock",
424432
Aggregator: true,
@@ -640,6 +648,9 @@ func TestBlockchainInfo(t *testing.T) {
640648
}
641649

642650
func TestValidatorSetHandling(t *testing.T) {
651+
// handle multiple sequencers
652+
t.Skip()
653+
643654
assert := assert.New(t)
644655
require := require.New(t)
645656
app := &mocks.Application{}
@@ -651,14 +662,18 @@ func TestValidatorSetHandling(t *testing.T) {
651662
app.On("GenerateFraudProof", mock.Anything).Return(abci.ResponseGenerateFraudProof{})
652663

653664
key, _, _ := crypto.GenerateEd25519Key(crand.Reader)
654-
signingKey, _, _ := crypto.GenerateEd25519Key(crand.Reader)
655665

656666
vKeys := make([]tmcrypto.PrivKey, 4)
657667
genesisValidators := make([]tmtypes.GenesisValidator, len(vKeys))
658668
for i := 0; i < len(vKeys); i++ {
659669
vKeys[i] = ed25519.GenPrivKey()
660-
genesisValidators[i] = tmtypes.GenesisValidator{Address: vKeys[i].PubKey().Address(), PubKey: vKeys[i].PubKey(), Power: int64(i + 100), Name: "one"}
670+
genesisValidators[i] = tmtypes.GenesisValidator{Address: vKeys[i].PubKey().Address(), PubKey: vKeys[i].PubKey(), Power: int64(i + 100), Name: fmt.Sprintf("gen #%d", i)}
671+
}
672+
673+
nodeKey := &p2p.NodeKey{
674+
PrivKey: vKeys[0],
661675
}
676+
signingKey, _ := conv.GetNodeKey(nodeKey)
662677

663678
pbValKey, err := encoding.PubKeyToProto(vKeys[0].PubKey())
664679
require.NoError(err)
@@ -1072,7 +1087,7 @@ func TestFutureGenesisTime(t *testing.T) {
10721087
mockApp.On("DeliverTx", mock.Anything).Return(abci.ResponseDeliverTx{})
10731088
mockApp.On("CheckTx", mock.Anything).Return(abci.ResponseCheckTx{})
10741089
key, _, _ := crypto.GenerateEd25519Key(crand.Reader)
1075-
signingKey, _, _ := crypto.GenerateEd25519Key(crand.Reader)
1090+
genesisValidators, signingKey := getGenesisValidatorSetWithSigner(1)
10761091
genesisTime := time.Now().Local().Add(time.Second * time.Duration(1))
10771092
node, err := newFullNode(context.Background(), config.NodeConfig{
10781093
DALayer: "mock",
@@ -1086,6 +1101,7 @@ func TestFutureGenesisTime(t *testing.T) {
10861101
ChainID: "test",
10871102
InitialHeight: 1,
10881103
GenesisTime: genesisTime,
1104+
Validators: genesisValidators,
10891105
},
10901106
log.TestingLogger())
10911107
require.NoError(err)

node/full_node_integration_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,13 @@ func TestAggregatorMode(t *testing.T) {
4545
app.On("GetAppHash", mock.Anything).Return(abci.ResponseGetAppHash{})
4646

4747
key, _, _ := crypto.GenerateEd25519Key(rand.Reader)
48-
signingKey, _, _ := crypto.GenerateEd25519Key(rand.Reader)
4948
anotherKey, _, _ := crypto.GenerateEd25519Key(rand.Reader)
50-
49+
genesisValidators, signingKey := getGenesisValidatorSetWithSigner(1)
5150
blockManagerConfig := config.BlockManagerConfig{
5251
BlockTime: 1 * time.Second,
5352
NamespaceID: types.NamespaceID{1, 2, 3, 4, 5, 6, 7, 8},
5453
}
55-
node, err := newFullNode(context.Background(), config.NodeConfig{DALayer: "mock", Aggregator: true, BlockManagerConfig: blockManagerConfig}, key, signingKey, abcicli.NewLocalClient(nil, app), &tmtypes.GenesisDoc{ChainID: "test"}, log.TestingLogger())
54+
node, err := newFullNode(context.Background(), config.NodeConfig{DALayer: "mock", Aggregator: true, BlockManagerConfig: blockManagerConfig}, key, signingKey, abcicli.NewLocalClient(nil, app), &tmtypes.GenesisDoc{ChainID: "test", Validators: genesisValidators}, log.TestingLogger())
5655
require.NoError(err)
5756
require.NotNil(node)
5857

@@ -147,8 +146,7 @@ func TestLazyAggregator(t *testing.T) {
147146
app.On("GetAppHash", mock.Anything).Return(abci.ResponseGetAppHash{})
148147

149148
key, _, _ := crypto.GenerateEd25519Key(rand.Reader)
150-
signingKey, _, _ := crypto.GenerateEd25519Key(rand.Reader)
151-
149+
genesisValidators, signingKey := getGenesisValidatorSetWithSigner(1)
152150
blockManagerConfig := config.BlockManagerConfig{
153151
BlockTime: 1 * time.Second,
154152
NamespaceID: types.NamespaceID{1, 2, 3, 4, 5, 6, 7, 8},
@@ -159,7 +157,7 @@ func TestLazyAggregator(t *testing.T) {
159157
Aggregator: true,
160158
BlockManagerConfig: blockManagerConfig,
161159
LazyAggregator: true,
162-
}, key, signingKey, abcicli.NewLocalClient(nil, app), &tmtypes.GenesisDoc{ChainID: "test"}, log.TestingLogger())
160+
}, key, signingKey, abcicli.NewLocalClient(nil, app), &tmtypes.GenesisDoc{ChainID: "test", Validators: genesisValidators}, log.TestingLogger())
163161
assert.False(node.IsRunning())
164162
assert.NoError(err)
165163
err = node.Start()
@@ -536,7 +534,7 @@ func createNode(ctx context.Context, n int, isMalicious bool, aggregator bool, d
536534
ctx = context.Background()
537535
}
538536

539-
signingKey, _, _ := crypto.GenerateEd25519Key(rand.Reader)
537+
genesisValidators, signingKey := getGenesisValidatorSetWithSigner(1)
540538
node, err := newFullNode(
541539
ctx,
542540
config.NodeConfig{
@@ -548,7 +546,7 @@ func createNode(ctx context.Context, n int, isMalicious bool, aggregator bool, d
548546
keys[n],
549547
signingKey,
550548
abcicli.NewLocalClient(nil, app),
551-
&tmtypes.GenesisDoc{ChainID: "test"},
549+
&tmtypes.GenesisDoc{ChainID: "test", Validators: genesisValidators},
552550
log.TestingLogger().With("node", n))
553551
require.NoError(err)
554552
require.NotNil(node)

rpc/json/service_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ import (
2121
"github.com/libp2p/go-libp2p/core/crypto"
2222
abciclient "github.com/tendermint/tendermint/abci/client"
2323
abci "github.com/tendermint/tendermint/abci/types"
24+
"github.com/tendermint/tendermint/crypto/ed25519"
2425
"github.com/tendermint/tendermint/libs/log"
26+
"github.com/tendermint/tendermint/p2p"
2527
rpcclient "github.com/tendermint/tendermint/rpc/client"
26-
"github.com/tendermint/tendermint/types"
28+
tmtypes "github.com/tendermint/tendermint/types"
2729

2830
"github.com/rollkit/rollkit/config"
31+
"github.com/rollkit/rollkit/conv"
2932
"github.com/rollkit/rollkit/mocks"
3033
"github.com/rollkit/rollkit/node"
3134
)
@@ -291,8 +294,17 @@ func getRPC(t *testing.T) (*mocks.Application, rpcclient.Client) {
291294
LastBlockAppHash: nil,
292295
})
293296
key, _, _ := crypto.GenerateEd25519Key(rand.Reader)
294-
signingKey, _, _ := crypto.GenerateEd25519Key(rand.Reader)
295-
n, err := node.NewNode(context.Background(), config.NodeConfig{Aggregator: true, DALayer: "mock", BlockManagerConfig: config.BlockManagerConfig{BlockTime: 1 * time.Second}, Light: false}, key, signingKey, abciclient.NewLocalClient(nil, app), &types.GenesisDoc{ChainID: "test"}, log.TestingLogger())
297+
validatorKey := ed25519.GenPrivKey()
298+
nodeKey := &p2p.NodeKey{
299+
PrivKey: validatorKey,
300+
}
301+
signingKey, _ := conv.GetNodeKey(nodeKey)
302+
pubKey := validatorKey.PubKey()
303+
304+
genesisValidators := []tmtypes.GenesisValidator{
305+
{Address: pubKey.Address(), PubKey: pubKey, Power: int64(100), Name: "gen #1"},
306+
}
307+
n, err := node.NewNode(context.Background(), config.NodeConfig{Aggregator: true, DALayer: "mock", BlockManagerConfig: config.BlockManagerConfig{BlockTime: 1 * time.Second}, Light: false}, key, signingKey, abciclient.NewLocalClient(nil, app), &tmtypes.GenesisDoc{ChainID: "test", Validators: genesisValidators}, log.TestingLogger())
296308
require.NoError(err)
297309
require.NotNil(n)
298310

types/validation.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,20 @@ func (h *SignedHeader) ValidateBasic() error {
6161
return err
6262
}
6363

64-
// Make sure there are as many signatures as validators
65-
if len(h.Commit.Signatures) != len(h.Validators.Validators) {
66-
return errors.New("number of signatures and keys don't match")
64+
// Make sure there is exactly one signature
65+
if len(h.Commit.Signatures) != 1 {
66+
return errors.New("expected exactly one signature")
6767
}
6868

69-
for i, val := range h.Validators.Validators {
70-
sig := h.Commit.Signatures[i]
71-
var pubKey ed25519.PubKey = val.PubKey.Bytes()
72-
msg, err := h.Header.MarshalBinary()
73-
if err != nil {
74-
return errors.New("signature verification failed, unable to marshal header")
75-
}
76-
if !pubKey.VerifySignature(msg, sig) {
77-
return errors.New("signature verification failed")
78-
}
69+
signature := h.Commit.Signatures[0]
70+
proposer := h.Validators.GetProposer()
71+
var pubKey ed25519.PubKey = proposer.PubKey.Bytes()
72+
msg, err := h.Header.MarshalBinary()
73+
if err != nil {
74+
return errors.New("signature verification failed, unable to marshal header")
75+
}
76+
if !pubKey.VerifySignature(msg, signature) {
77+
return errors.New("signature verification failed")
7978
}
8079

8180
return nil

0 commit comments

Comments
 (0)