tendermint/types/canonical_json.go

116 lines
3.6 KiB
Go
Raw Normal View History

2016-12-02 02:01:47 -08:00
package types
2017-04-27 16:01:18 -07:00
import (
"time"
"github.com/tendermint/go-amino"
2018-02-03 00:42:59 -08:00
cmn "github.com/tendermint/tmlibs/common"
2017-04-27 16:01:18 -07:00
)
// Canonical json is amino's json for structs with fields in alphabetical order
2016-12-02 02:01:47 -08:00
// TimeFormat is used for generating the sigs
const TimeFormat = amino.RFC3339Millis
2016-12-02 02:01:47 -08:00
type CanonicalJSONBlockID struct {
2018-02-03 00:42:59 -08:00
Hash cmn.HexBytes `json:"hash,omitempty"`
2016-12-02 02:01:47 -08:00
PartsHeader CanonicalJSONPartSetHeader `json:"parts,omitempty"`
}
type CanonicalJSONPartSetHeader struct {
Hash cmn.HexBytes `json:"hash,omitempty"`
Total int `json:"total,omitempty"`
2016-12-02 02:01:47 -08:00
}
type CanonicalJSONProposal struct {
ChainID string `json:"@chain_id"`
Type string `json:"@type"`
2016-12-02 02:01:47 -08:00
BlockPartsHeader CanonicalJSONPartSetHeader `json:"block_parts_header"`
Height int64 `json:"height"`
2016-12-02 02:01:47 -08:00
POLBlockID CanonicalJSONBlockID `json:"pol_block_id"`
POLRound int `json:"pol_round"`
Round int `json:"round"`
Timestamp string `json:"timestamp"`
2016-12-02 02:01:47 -08:00
}
type CanonicalJSONVote struct {
ChainID string `json:"@chain_id"`
Type string `json:"@type"`
BlockID CanonicalJSONBlockID `json:"block_id"`
Height int64 `json:"height"`
Round int `json:"round"`
Timestamp string `json:"timestamp"`
VoteType byte `json:"type"`
2016-12-02 02:01:47 -08:00
}
2017-07-29 11:15:10 -07:00
type CanonicalJSONHeartbeat struct {
ChainID string `json:"@chain_id"`
Type string `json:"@type"`
Height int64 `json:"height"`
Round int `json:"round"`
Sequence int `json:"sequence"`
ValidatorAddress Address `json:"validator_address"`
ValidatorIndex int `json:"validator_index"`
2017-07-29 11:15:10 -07:00
}
2016-12-02 02:01:47 -08:00
//-----------------------------------
// Canonicalize the structs
func CanonicalBlockID(blockID BlockID) CanonicalJSONBlockID {
return CanonicalJSONBlockID{
Hash: blockID.Hash,
PartsHeader: CanonicalPartSetHeader(blockID.PartsHeader),
}
}
func CanonicalPartSetHeader(psh PartSetHeader) CanonicalJSONPartSetHeader {
return CanonicalJSONPartSetHeader{
psh.Hash,
psh.Total,
}
}
func CanonicalProposal(chainID string, proposal *Proposal) CanonicalJSONProposal {
2016-12-02 02:01:47 -08:00
return CanonicalJSONProposal{
ChainID: chainID,
Type: "proposal",
2016-12-02 02:01:47 -08:00
BlockPartsHeader: CanonicalPartSetHeader(proposal.BlockPartsHeader),
Height: proposal.Height,
Timestamp: CanonicalTime(proposal.Timestamp),
2016-12-02 02:01:47 -08:00
POLBlockID: CanonicalBlockID(proposal.POLBlockID),
POLRound: proposal.POLRound,
Round: proposal.Round,
}
}
func CanonicalVote(chainID string, vote *Vote) CanonicalJSONVote {
2016-12-02 02:01:47 -08:00
return CanonicalJSONVote{
ChainID: chainID,
Type: "vote",
BlockID: CanonicalBlockID(vote.BlockID),
Height: vote.Height,
Round: vote.Round,
Timestamp: CanonicalTime(vote.Timestamp),
VoteType: vote.Type,
2016-12-02 02:01:47 -08:00
}
}
2017-07-29 11:15:10 -07:00
func CanonicalHeartbeat(chainID string, heartbeat *Heartbeat) CanonicalJSONHeartbeat {
2017-07-29 11:15:10 -07:00
return CanonicalJSONHeartbeat{
ChainID: chainID,
Type: "heartbeat",
Height: heartbeat.Height,
Round: heartbeat.Round,
Sequence: heartbeat.Sequence,
ValidatorAddress: heartbeat.ValidatorAddress,
ValidatorIndex: heartbeat.ValidatorIndex,
2017-07-29 11:15:10 -07:00
}
}
func CanonicalTime(t time.Time) string {
// Note that sending time over amino resets it to
// local time, we need to force UTC here, so the
// signatures match
return t.UTC().Format(TimeFormat)
}