From 6a0217688f93fcd4d14fcc9b563dbca2e8259c50 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 22 Mar 2017 18:16:22 +0100 Subject: [PATCH] Ensure private validator addresses are hex --- consensus/replay_test.go | 8 ++++- glide.lock | 2 +- types/priv_validator.go | 7 +++-- types/priv_validator_test.go | 60 ++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 types/priv_validator_test.go diff --git a/consensus/replay_test.go b/consensus/replay_test.go index 43204ab72..9368df99d 100644 --- a/consensus/replay_test.go +++ b/consensus/replay_test.go @@ -131,7 +131,13 @@ func runReplayTest(t *testing.T, cs *ConsensusState, walFile string, newBlockCh thisCase *testCase, i int) { cs.config.Set("cs_wal_file", walFile) - cs.Start() + started, err := cs.Start() + if err != nil { + t.Fatalf("Cannot start consensus: %v", err) + } + if !started { + t.Error("Consensus did not start") + } // Wait to make a new block. // This is just a signal that we haven't halted; its not something contained in the WAL itself. // Assuming the consensus state is running, replay of any WAL, including the empty one, diff --git a/glide.lock b/glide.lock index fcede6fc9..5bddeea33 100644 --- a/glide.lock +++ b/glide.lock @@ -92,7 +92,7 @@ imports: - name: github.com/tendermint/go-crypto version: 67c7682135e7e9a8c56a7eeae9ac885d342a3315 - name: github.com/tendermint/go-data - version: 9fbf0684fefc4fad580992394a0bcf47c1b3d77e + version: 3a135dc080d4304da75b57806f0f83a2ea9d40f8 - name: github.com/tendermint/go-db version: 9643f60bc2578693844aacf380a7c32e4c029fee - name: github.com/tendermint/go-events diff --git a/types/priv_validator.go b/types/priv_validator.go index 905f3bf15..ed6313b81 100644 --- a/types/priv_validator.go +++ b/types/priv_validator.go @@ -11,6 +11,7 @@ import ( . "github.com/tendermint/go-common" "github.com/tendermint/go-crypto" + data "github.com/tendermint/go-data" ) const ( @@ -33,13 +34,13 @@ func voteToStep(vote *Vote) int8 { } type PrivValidator struct { - Address []byte `json:"address"` + Address data.Bytes `json:"address"` PubKey crypto.PubKey `json:"pub_key"` LastHeight int `json:"last_height"` LastRound int `json:"last_round"` LastStep int8 `json:"last_step"` - LastSignature crypto.Signature `json:"last_signature"` // so we dont lose signatures - LastSignBytes []byte `json:"last_signbytes"` // so we dont lose signatures + LastSignature crypto.Signature `json:"last_signature,omitempty"` // so we dont lose signatures + LastSignBytes data.Bytes `json:"last_signbytes,omitempty"` // so we dont lose signatures // PrivKey should be empty if a Signer other than the default is being used. PrivKey crypto.PrivKey `json:"priv_key"` diff --git a/types/priv_validator_test.go b/types/priv_validator_test.go new file mode 100644 index 000000000..1eb0b57db --- /dev/null +++ b/types/priv_validator_test.go @@ -0,0 +1,60 @@ +package types + +import ( + "encoding/hex" + "encoding/json" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + crypto "github.com/tendermint/go-crypto" +) + +func TestLoadValidator(t *testing.T) { + assert, require := assert.New(t), require.New(t) + + // create some fixed values + addrStr := "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456" + pubStr := "3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8" + privStr := "27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8" + addrBytes, _ := hex.DecodeString(addrStr) + pubBytes, _ := hex.DecodeString(pubStr) + privBytes, _ := hex.DecodeString(privStr) + + // prepend type byte + pubKey, err := crypto.PubKeyFromBytes(append([]byte{1}, pubBytes...)) + require.Nil(err, "%+v", err) + privKey, err := crypto.PrivKeyFromBytes(append([]byte{1}, privBytes...)) + require.Nil(err, "%+v", err) + + serialized := fmt.Sprintf(`{ + "address": "%s", + "pub_key": { + "type": "ed25519", + "data": "%s" + }, + "priv_key": { + "type": "ed25519", + "data": "%s" + }, + "last_height": 0, + "last_round": 0, + "last_step": 0, + "last_signature": null +}`, addrStr, pubStr, privStr) + + val := PrivValidator{} + err = json.Unmarshal([]byte(serialized), &val) + require.Nil(err, "%+v", err) + + // make sure the values match + assert.EqualValues(addrBytes, val.Address) + assert.EqualValues(pubKey, val.PubKey) + assert.EqualValues(privKey, val.PrivKey) + + // export it and make sure it is the same + out, err := json.Marshal(val) + require.Nil(err, "%+v", err) + assert.JSONEq(serialized, string(out)) +}