Browse Source

types/priv_validator: fixes for latest p2p and cmn

pull/1204/head
Ethan Buchman 6 years ago
parent
commit
8da2a6a147
11 changed files with 80 additions and 30 deletions
  1. +3
    -3
      types/canonical_json.go
  2. +52
    -2
      types/priv_validator.go
  3. +2
    -2
      types/priv_validator/json.go
  4. +3
    -3
      types/priv_validator/priv_validator_test.go
  5. +2
    -2
      types/priv_validator/sign_info.go
  6. +5
    -6
      types/priv_validator/socket.go
  7. +5
    -4
      types/priv_validator/socket_test.go
  8. +3
    -3
      types/priv_validator/unencrypted.go
  9. +3
    -3
      types/priv_validator/upgrade.go
  10. +1
    -1
      types/proposal_test.go
  11. +1
    -1
      types/vote_test.go

+ 3
- 3
types/canonical_json.go View File

@ -9,8 +9,8 @@ import (
// canonical json is go-wire's json for structs with fields in alphabetical order
// timeFormat is used for generating the sigs
const timeFormat = wire.RFC3339Millis
// TimeFormat is used for generating the sigs
const TimeFormat = wire.RFC3339Millis
type CanonicalJSONBlockID struct {
Hash cmn.HexBytes `json:"hash,omitempty"`
@ -117,5 +117,5 @@ func CanonicalTime(t time.Time) string {
// note that sending time over go-wire resets it to
// local time, we need to force UTC here, so the
// signatures match
return t.UTC().Format(timeFormat)
return t.UTC().Format(TimeFormat)
}

+ 52
- 2
types/priv_validator.go View File

@ -34,6 +34,56 @@ func voteToStep(vote *Vote) int8 {
}
}
//--------------------------------------------------------------
// PrivValidator is being upgraded! See types/priv_validator
// ValidatorID contains the identity of the validator.
type ValidatorID struct {
Address cmn.HexBytes `json:"address"`
PubKey crypto.PubKey `json:"pub_key"`
}
// PrivValidator defines the functionality of a local Tendermint validator
// that signs votes, proposals, and heartbeats, and never double signs.
type PrivValidator2 interface {
Address() Address // redundant since .PubKey().Address()
PubKey() crypto.PubKey
SignVote(chainID string, vote *Vote) error
SignProposal(chainID string, proposal *Proposal) error
SignHeartbeat(chainID string, heartbeat *Heartbeat) error
}
type TestSigner interface {
Address() cmn.HexBytes
PubKey() crypto.PubKey
Sign([]byte) (crypto.Signature, error)
}
func GenSigner() TestSigner {
return &DefaultTestSigner{
crypto.GenPrivKeyEd25519().Wrap(),
}
}
type DefaultTestSigner struct {
crypto.PrivKey
}
func (ds *DefaultTestSigner) Address() cmn.HexBytes {
return ds.PubKey().Address()
}
func (ds *DefaultTestSigner) PubKey() crypto.PubKey {
return ds.PrivKey.PubKey()
}
func (ds *DefaultTestSigner) Sign(msg []byte) (crypto.Signature, error) {
return ds.PrivKey.Sign(msg), nil
}
//--------------------------------------------------------------
// PrivValidator defines the functionality of a local Tendermint validator
// that signs votes, proposals, and heartbeats, and never double signs.
type PrivValidator interface {
@ -379,7 +429,7 @@ func checkVotesOnlyDifferByTimestamp(lastSignBytes, newSignBytes []byte) (time.T
panic(fmt.Sprintf("signBytes cannot be unmarshalled into vote: %v", err))
}
lastTime, err := time.Parse(timeFormat, lastVote.Vote.Timestamp)
lastTime, err := time.Parse(TimeFormat, lastVote.Vote.Timestamp)
if err != nil {
panic(err)
}
@ -405,7 +455,7 @@ func checkProposalsOnlyDifferByTimestamp(lastSignBytes, newSignBytes []byte) (ti
panic(fmt.Sprintf("signBytes cannot be unmarshalled into proposal: %v", err))
}
lastTime, err := time.Parse(timeFormat, lastProposal.Proposal.Timestamp)
lastTime, err := time.Parse(TimeFormat, lastProposal.Proposal.Timestamp)
if err != nil {
panic(err)
}


+ 2
- 2
types/priv_validator/json.go View File

@ -13,7 +13,7 @@ import (
)
// PrivValidator aliases types.PrivValidator
type PrivValidator = types.PrivValidator
type PrivValidator = types.PrivValidator2
//-----------------------------------------------------
@ -42,7 +42,7 @@ func (pk *PrivKey) UnmarshalJSON(b []byte) error {
//-----------------------------------------------------
var _ types.PrivValidator = (*PrivValidatorJSON)(nil)
var _ types.PrivValidator2 = (*PrivValidatorJSON)(nil)
// PrivValidatorJSON wraps PrivValidatorUnencrypted
// and persists it to disk after every SignVote and SignProposal.


+ 3
- 3
types/priv_validator/priv_validator_test.go View File

@ -11,9 +11,9 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
crypto "github.com/tendermint/go-crypto"
data "github.com/tendermint/go-wire/data"
"github.com/tendermint/tendermint/types"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tendermint/types"
)
func TestGenLoadValidator(t *testing.T) {
@ -230,7 +230,7 @@ func TestDifferByTimestamp(t *testing.T) {
}
}
func newVote(addr data.Bytes, idx int, height int64, round int, typ byte, blockID types.BlockID) *types.Vote {
func newVote(addr cmn.HexBytes, idx int, height int64, round int, typ byte, blockID types.BlockID) *types.Vote {
return &types.Vote{
ValidatorAddress: addr,
ValidatorIndex: idx,


+ 2
- 2
types/priv_validator/sign_info.go View File

@ -8,8 +8,8 @@ import (
"time"
crypto "github.com/tendermint/go-crypto"
data "github.com/tendermint/go-wire/data"
"github.com/tendermint/tendermint/types"
cmn "github.com/tendermint/tmlibs/common"
)
// TODO: type ?
@ -40,7 +40,7 @@ type LastSignedInfo struct {
Round int `json:"round"`
Step int8 `json:"step"`
Signature crypto.Signature `json:"signature,omitempty"` // so we dont lose signatures
SignBytes data.Bytes `json:"signbytes,omitempty"` // so we dont lose signatures
SignBytes cmn.HexBytes `json:"signbytes,omitempty"` // so we dont lose signatures
}
func NewLastSignedInfo() *LastSignedInfo {


+ 5
- 6
types/priv_validator/socket.go View File

@ -9,18 +9,17 @@ import (
"github.com/pkg/errors"
crypto "github.com/tendermint/go-crypto"
wire "github.com/tendermint/go-wire"
"github.com/tendermint/go-wire/data"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log"
"golang.org/x/net/netutil"
"github.com/tendermint/tendermint/p2p"
p2pconn "github.com/tendermint/tendermint/p2p/conn"
"github.com/tendermint/tendermint/types"
)
//-----------------------------------------------------------------
var _ types.PrivValidator = (*PrivValidatorSocketClient)(nil)
var _ types.PrivValidator2 = (*PrivValidatorSocketClient)(nil)
// PrivValidatorSocketClient implements PrivValidator.
// It uses a socket to request signatures.
@ -81,7 +80,7 @@ RETRY_LOOP:
}
if pvsc.privKey != nil {
conn, err = p2p.MakeSecretConnection(conn, *pvsc.privKey)
conn, err = p2pconn.MakeSecretConnection(conn, pvsc.privKey.Wrap())
if err != nil {
pvsc.Logger.Error(
"OnStart",
@ -106,7 +105,7 @@ func (pvsc *PrivValidatorSocketClient) OnStop() {
}
// Address is an alias for PubKey().Address().
func (pvsc *PrivValidatorSocketClient) Address() data.Bytes {
func (pvsc *PrivValidatorSocketClient) Address() cmn.HexBytes {
return pvsc.PubKey().Address()
}
@ -262,7 +261,7 @@ func (pvss *PrivValidatorSocketServer) acceptConnections() {
}
if pvss.privKey != nil {
conn, err = p2p.MakeSecretConnection(conn, *pvss.privKey)
conn, err = p2pconn.MakeSecretConnection(conn, pvss.privKey.Wrap())
if err != nil {
pvss.Logger.Error(
"acceptConnections",


+ 5
- 4
types/priv_validator/socket_test.go View File

@ -8,9 +8,10 @@ import (
"github.com/stretchr/testify/require"
crypto "github.com/tendermint/go-crypto"
data "github.com/tendermint/go-wire/data"
"github.com/tendermint/tendermint/types"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log"
"github.com/tendermint/tendermint/types"
)
func TestPrivValidatorSocketServer(t *testing.T) {
@ -50,7 +51,7 @@ func TestPrivValidatorSocketServer(t *testing.T) {
assert.True(pvsc.IsRunning())
assert.Equal(pvsc.Address(), data.Bytes(pvss.privVal.PubKey().Address()))
assert.Equal(pvsc.Address(), cmn.HexBytes(pvss.privVal.PubKey().Address()))
assert.Equal(pvsc.PubKey(), pvss.privVal.PubKey())
err = pvsc.SignProposal(chainID, &types.Proposal{
@ -103,7 +104,7 @@ func TestPrivValidatorSocketServerWithoutSecret(t *testing.T) {
assert.True(pvsc.IsRunning())
assert.Equal(pvsc.Address(), data.Bytes(pvss.privVal.PubKey().Address()))
assert.Equal(pvsc.Address(), cmn.HexBytes(pvss.privVal.PubKey().Address()))
assert.Equal(pvsc.PubKey(), pvss.privVal.PubKey())
err = pvsc.SignProposal(chainID, &types.Proposal{


+ 3
- 3
types/priv_validator/unencrypted.go View File

@ -4,13 +4,13 @@ import (
"fmt"
crypto "github.com/tendermint/go-crypto"
data "github.com/tendermint/go-wire/data"
"github.com/tendermint/tendermint/types"
cmn "github.com/tendermint/tmlibs/common"
)
//-----------------------------------------------------------------
var _ types.PrivValidator = (*PrivValidatorUnencrypted)(nil)
var _ types.PrivValidator2 = (*PrivValidatorUnencrypted)(nil)
// PrivValidatorUnencrypted implements PrivValidator.
// It uses an in-memory crypto.PrivKey that is
@ -38,7 +38,7 @@ func (upv *PrivValidatorUnencrypted) String() string {
return fmt.Sprintf("PrivValidator{%v %v}", upv.Address(), upv.LastSignedInfo.String())
}
func (upv *PrivValidatorUnencrypted) Address() data.Bytes {
func (upv *PrivValidatorUnencrypted) Address() cmn.HexBytes {
return upv.PrivKey.PubKey().Address()
}


+ 3
- 3
types/priv_validator/upgrade.go View File

@ -5,18 +5,18 @@ import (
"io/ioutil"
crypto "github.com/tendermint/go-crypto"
data "github.com/tendermint/go-wire/data"
"github.com/tendermint/tendermint/types"
cmn "github.com/tendermint/tmlibs/common"
)
type PrivValidatorV1 struct {
Address data.Bytes `json:"address"`
Address cmn.HexBytes `json:"address"`
PubKey crypto.PubKey `json:"pub_key"`
LastHeight int64 `json:"last_height"`
LastRound int `json:"last_round"`
LastStep int8 `json:"last_step"`
LastSignature crypto.Signature `json:"last_signature,omitempty"` // so we dont lose signatures
LastSignBytes data.Bytes `json:"last_signbytes,omitempty"` // so we dont lose signatures
LastSignBytes cmn.HexBytes `json:"last_signbytes,omitempty"` // so we dont lose signatures
PrivKey crypto.PrivKey `json:"priv_key"`
}


+ 1
- 1
types/proposal_test.go View File

@ -12,7 +12,7 @@ import (
var testProposal *Proposal
func init() {
var stamp, err = time.Parse(timeFormat, "2018-02-11T07:09:22.765Z")
var stamp, err = time.Parse(TimeFormat, "2018-02-11T07:09:22.765Z")
if err != nil {
panic(err)
}


+ 1
- 1
types/vote_test.go View File

@ -18,7 +18,7 @@ func examplePrecommit() *Vote {
}
func exampleVote(t byte) *Vote {
var stamp, err = time.Parse(timeFormat, "2017-12-25T03:00:01.234Z")
var stamp, err = time.Parse(TimeFormat, "2017-12-25T03:00:01.234Z")
if err != nil {
panic(err)
}


Loading…
Cancel
Save