Browse Source

Ensure private validator addresses are hex

pull/455/head
Ethan Frey 8 years ago
committed by Ethan Buchman
parent
commit
6a0217688f
4 changed files with 72 additions and 5 deletions
  1. +7
    -1
      consensus/replay_test.go
  2. +1
    -1
      glide.lock
  3. +4
    -3
      types/priv_validator.go
  4. +60
    -0
      types/priv_validator_test.go

+ 7
- 1
consensus/replay_test.go View File

@ -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,


+ 1
- 1
glide.lock View File

@ -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


+ 4
- 3
types/priv_validator.go View File

@ -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"`


+ 60
- 0
types/priv_validator_test.go View File

@ -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))
}

Loading…
Cancel
Save