From fe1d59ab7b80c04ddaaa60f93b0f8656c1ed8f4b Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Mon, 22 Oct 2018 17:55:49 -0400 Subject: [PATCH] Set protocol versions in NodeInfo from state (#2686) * use types.NewValidator * node: set p2p.ProtocolVersion from state, not globals --- benchmarks/codec_test.go | 2 +- node/node.go | 6 +++++- p2p/node_info.go | 20 ++++++++++++-------- p2p/peer_test.go | 2 +- p2p/test_util.go | 2 +- state/state.go | 11 +---------- types/protobuf_test.go | 18 +++--------------- 7 files changed, 24 insertions(+), 37 deletions(-) diff --git a/benchmarks/codec_test.go b/benchmarks/codec_test.go index 2be1db156..3e0270286 100644 --- a/benchmarks/codec_test.go +++ b/benchmarks/codec_test.go @@ -14,7 +14,7 @@ import ( func testNodeInfo(id p2p.ID) p2p.DefaultNodeInfo { return p2p.DefaultNodeInfo{ - ProtocolVersion: p2p.InitProtocolVersion, + ProtocolVersion: p2p.ProtocolVersion{1, 2, 3}, ID_: id, Moniker: "SOMENAME", Network: "SOMENAME", diff --git a/node/node.go b/node/node.go index 522f18e91..f62a8b472 100644 --- a/node/node.go +++ b/node/node.go @@ -367,7 +367,11 @@ func NewNode(config *cfg.Config, nodeKey.ID(), txIndexer, genDoc.ChainID, - p2p.ProtocolVersionWithApp(state.Version.Consensus.App), + p2p.NewProtocolVersion( + version.P2PProtocol, // global + state.Version.Consensus.Block, + state.Version.Consensus.App, + ), ) ) diff --git a/p2p/node_info.go b/p2p/node_info.go index 1d408eb68..e46174e07 100644 --- a/p2p/node_info.go +++ b/p2p/node_info.go @@ -49,16 +49,20 @@ type ProtocolVersion struct { App version.Protocol `json:"app"` } -// InitProtocolVersion populates the Block and P2P versions, but not the App. -var InitProtocolVersion = ProtocolVersionWithApp(0) +// defaultProtocolVersion populates the Block and P2P versions using +// the global values, but not the App. +var defaultProtocolVersion = NewProtocolVersion( + version.P2PProtocol, + version.BlockProtocol, + 0, +) -// ProtocolVersionWithApp returns a fully populated ProtocolVersion -// using the provided App version and the Block and P2P versions defined in the `version` package. -func ProtocolVersionWithApp(appVersion version.Protocol) ProtocolVersion { +// NewProtocolVersion returns a fully populated ProtocolVersion. +func NewProtocolVersion(p2p, block, app version.Protocol) ProtocolVersion { return ProtocolVersion{ - P2P: version.P2PProtocol, - Block: version.BlockProtocol, - App: appVersion, + P2P: p2p, + Block: block, + App: app, } } diff --git a/p2p/peer_test.go b/p2p/peer_test.go index 02f1d2c0f..d3d9f0c72 100644 --- a/p2p/peer_test.go +++ b/p2p/peer_test.go @@ -207,7 +207,7 @@ func (rp *remotePeer) accept(l net.Listener) { func (rp *remotePeer) nodeInfo(l net.Listener) NodeInfo { return DefaultNodeInfo{ - ProtocolVersion: InitProtocolVersion, + ProtocolVersion: defaultProtocolVersion, ID_: rp.Addr().ID, ListenAddr: l.Addr().String(), Network: "testing", diff --git a/p2p/test_util.go b/p2p/test_util.go index e1f7b5040..d72c0c760 100644 --- a/p2p/test_util.go +++ b/p2p/test_util.go @@ -249,7 +249,7 @@ func testNodeInfo(id ID, name string) NodeInfo { func testNodeInfoWithNetwork(id ID, name, network string) NodeInfo { return DefaultNodeInfo{ - ProtocolVersion: InitProtocolVersion, + ProtocolVersion: defaultProtocolVersion, ID_: id, ListenAddr: fmt.Sprintf("127.0.0.1:%d", cmn.RandIntn(64512)+1023), Network: network, diff --git a/state/state.go b/state/state.go index 5c1b68a26..d6ec6f0be 100644 --- a/state/state.go +++ b/state/state.go @@ -222,7 +222,6 @@ func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) { return State{}, fmt.Errorf("Error in genesis file: %v", err) } - // Make validators slice var validatorSet, nextValidatorSet *types.ValidatorSet if genDoc.Validators == nil { validatorSet = types.NewValidatorSet(nil) @@ -230,15 +229,7 @@ func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) { } else { validators := make([]*types.Validator, len(genDoc.Validators)) for i, val := range genDoc.Validators { - pubKey := val.PubKey - address := pubKey.Address() - - // Make validator - validators[i] = &types.Validator{ - Address: address, - PubKey: pubKey, - VotingPower: val.Power, - } + validators[i] = types.NewValidator(val.PubKey, val.Power) } validatorSet = types.NewValidatorSet(validators) nextValidatorSet = types.NewValidatorSet(validators).CopyIncrementAccum(1) diff --git a/types/protobuf_test.go b/types/protobuf_test.go index f8682abf8..c940f1b42 100644 --- a/types/protobuf_test.go +++ b/types/protobuf_test.go @@ -30,17 +30,9 @@ func TestABCIValidators(t *testing.T) { pkEd := ed25519.GenPrivKey().PubKey() // correct validator - tmValExpected := &Validator{ - Address: pkEd.Address(), - PubKey: pkEd, - VotingPower: 10, - } + tmValExpected := NewValidator(pkEd, 10) - tmVal := &Validator{ - Address: pkEd.Address(), - PubKey: pkEd, - VotingPower: 10, - } + tmVal := NewValidator(pkEd, 10) abciVal := TM2PB.ValidatorUpdate(tmVal) tmVals, err := PB2TM.ValidatorUpdates([]abci.ValidatorUpdate{abciVal}) @@ -127,11 +119,7 @@ func TestABCIValidatorFromPubKeyAndPower(t *testing.T) { func TestABCIValidatorWithoutPubKey(t *testing.T) { pkEd := ed25519.GenPrivKey().PubKey() - abciVal := TM2PB.Validator(&Validator{ - Address: pkEd.Address(), - PubKey: pkEd, - VotingPower: 10, - }) + abciVal := TM2PB.Validator(NewValidator(pkEd, 10)) // pubkey must be nil tmValExpected := abci.Validator{