diff --git a/types/canonical_json.go b/types/canonical_json.go index 45d12b45f..879bb5c52 100644 --- a/types/canonical_json.go +++ b/types/canonical_json.go @@ -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) } diff --git a/types/priv_validator.go b/types/priv_validator.go index 062fe09d2..4038b1adf 100644 --- a/types/priv_validator.go +++ b/types/priv_validator.go @@ -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) } diff --git a/types/priv_validator/json.go b/types/priv_validator/json.go index 446587765..8fe7a3749 100644 --- a/types/priv_validator/json.go +++ b/types/priv_validator/json.go @@ -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. diff --git a/types/priv_validator/priv_validator_test.go b/types/priv_validator/priv_validator_test.go index 33f8338d4..6f68256da 100644 --- a/types/priv_validator/priv_validator_test.go +++ b/types/priv_validator/priv_validator_test.go @@ -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, diff --git a/types/priv_validator/sign_info.go b/types/priv_validator/sign_info.go index 1a3dae442..60113c570 100644 --- a/types/priv_validator/sign_info.go +++ b/types/priv_validator/sign_info.go @@ -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 { diff --git a/types/priv_validator/socket.go b/types/priv_validator/socket.go index 1fd45ed65..4093ce9ca 100644 --- a/types/priv_validator/socket.go +++ b/types/priv_validator/socket.go @@ -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", diff --git a/types/priv_validator/socket_test.go b/types/priv_validator/socket_test.go index 725d5af65..627d4fb18 100644 --- a/types/priv_validator/socket_test.go +++ b/types/priv_validator/socket_test.go @@ -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{ diff --git a/types/priv_validator/unencrypted.go b/types/priv_validator/unencrypted.go index e9b0bb880..483f57c11 100644 --- a/types/priv_validator/unencrypted.go +++ b/types/priv_validator/unencrypted.go @@ -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() } diff --git a/types/priv_validator/upgrade.go b/types/priv_validator/upgrade.go index 6d5c472ab..063655421 100644 --- a/types/priv_validator/upgrade.go +++ b/types/priv_validator/upgrade.go @@ -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"` } diff --git a/types/proposal_test.go b/types/proposal_test.go index 0d2af71e2..6fbfbba05 100644 --- a/types/proposal_test.go +++ b/types/proposal_test.go @@ -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) } diff --git a/types/vote_test.go b/types/vote_test.go index 51eca12de..5e2d5c0df 100644 --- a/types/vote_test.go +++ b/types/vote_test.go @@ -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) }