2017-08-03 13:58:17 -04:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/tendermint/go-crypto"
|
2018-02-15 14:26:49 -05:00
|
|
|
"github.com/tendermint/tendermint/wire"
|
2017-08-03 13:58:17 -04:00
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
|
|
|
)
|
|
|
|
|
2017-10-05 16:34:14 -06:00
|
|
|
// Heartbeat is a simple vote-like structure so validators can
|
|
|
|
// alert others that they are alive and waiting for transactions.
|
|
|
|
// Note: We aren't adding ",omitempty" to Heartbeat's
|
|
|
|
// json field tags because we always want the JSON
|
|
|
|
// representation to be in its canonical form.
|
2017-08-03 13:58:17 -04:00
|
|
|
type Heartbeat struct {
|
2018-03-19 18:39:37 +02:00
|
|
|
ValidatorAddress Address `json:"validator_address"`
|
2017-08-03 13:58:17 -04:00
|
|
|
ValidatorIndex int `json:"validator_index"`
|
2017-12-01 19:04:53 -06:00
|
|
|
Height int64 `json:"height"`
|
2017-08-03 13:58:17 -04:00
|
|
|
Round int `json:"round"`
|
|
|
|
Sequence int `json:"sequence"`
|
|
|
|
Signature crypto.Signature `json:"signature"`
|
|
|
|
}
|
|
|
|
|
2018-01-14 19:05:22 -05:00
|
|
|
// SignBytes returns the Heartbeat bytes for signing.
|
2017-10-05 16:34:14 -06:00
|
|
|
// It panics if the Heartbeat is nil.
|
2018-01-14 19:05:22 -05:00
|
|
|
func (heartbeat *Heartbeat) SignBytes(chainID string) []byte {
|
|
|
|
bz, err := wire.MarshalJSON(CanonicalJSONOnceHeartbeat{
|
2017-08-03 13:58:17 -04:00
|
|
|
chainID,
|
|
|
|
CanonicalHeartbeat(heartbeat),
|
2018-01-14 19:05:22 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return bz
|
2017-08-03 13:58:17 -04:00
|
|
|
}
|
|
|
|
|
2017-09-22 12:00:37 -04:00
|
|
|
// Copy makes a copy of the Heartbeat.
|
2017-08-03 13:58:17 -04:00
|
|
|
func (heartbeat *Heartbeat) Copy() *Heartbeat {
|
2017-10-05 16:34:14 -06:00
|
|
|
if heartbeat == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-03 13:58:17 -04:00
|
|
|
heartbeatCopy := *heartbeat
|
|
|
|
return &heartbeatCopy
|
|
|
|
}
|
|
|
|
|
2017-09-22 12:00:37 -04:00
|
|
|
// String returns a string representation of the Heartbeat.
|
2017-08-03 13:58:17 -04:00
|
|
|
func (heartbeat *Heartbeat) String() string {
|
|
|
|
if heartbeat == nil {
|
|
|
|
return "nil-heartbeat"
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("Heartbeat{%v:%X %v/%02d (%v) %v}",
|
|
|
|
heartbeat.ValidatorIndex, cmn.Fingerprint(heartbeat.ValidatorAddress),
|
|
|
|
heartbeat.Height, heartbeat.Round, heartbeat.Sequence, heartbeat.Signature)
|
|
|
|
}
|