github.com/Oyster-zx/tendermint@v0.34.24-fork/types/canonical.go (about) 1 package types 2 3 import ( 4 "time" 5 6 tmproto "github.com/tendermint/tendermint/proto/tendermint/types" 7 tmtime "github.com/tendermint/tendermint/types/time" 8 ) 9 10 // Canonical* wraps the structs in types for amino encoding them for use in SignBytes / the Signable interface. 11 12 // TimeFormat is used for generating the sigs 13 const TimeFormat = time.RFC3339Nano 14 15 //----------------------------------- 16 // Canonicalize the structs 17 18 func CanonicalizeBlockID(bid tmproto.BlockID) *tmproto.CanonicalBlockID { 19 rbid, err := BlockIDFromProto(&bid) 20 if err != nil { 21 panic(err) 22 } 23 var cbid *tmproto.CanonicalBlockID 24 if rbid == nil || rbid.IsZero() { 25 cbid = nil 26 } else { 27 cbid = &tmproto.CanonicalBlockID{ 28 Hash: bid.Hash, 29 PartSetHeader: CanonicalizePartSetHeader(bid.PartSetHeader), 30 } 31 } 32 33 return cbid 34 } 35 36 // CanonicalizeVote transforms the given PartSetHeader to a CanonicalPartSetHeader. 37 func CanonicalizePartSetHeader(psh tmproto.PartSetHeader) tmproto.CanonicalPartSetHeader { 38 return tmproto.CanonicalPartSetHeader(psh) 39 } 40 41 // CanonicalizeVote transforms the given Proposal to a CanonicalProposal. 42 func CanonicalizeProposal(chainID string, proposal *tmproto.Proposal) tmproto.CanonicalProposal { 43 return tmproto.CanonicalProposal{ 44 Type: tmproto.ProposalType, 45 Height: proposal.Height, // encoded as sfixed64 46 Round: int64(proposal.Round), // encoded as sfixed64 47 POLRound: int64(proposal.PolRound), 48 BlockID: CanonicalizeBlockID(proposal.BlockID), 49 Timestamp: proposal.Timestamp, 50 ChainID: chainID, 51 } 52 } 53 54 // CanonicalizeVote transforms the given Vote to a CanonicalVote, which does 55 // not contain ValidatorIndex and ValidatorAddress fields. 56 func CanonicalizeVote(chainID string, vote *tmproto.Vote) tmproto.CanonicalVote { 57 return tmproto.CanonicalVote{ 58 Type: vote.Type, 59 Height: vote.Height, // encoded as sfixed64 60 Round: int64(vote.Round), // encoded as sfixed64 61 BlockID: CanonicalizeBlockID(vote.BlockID), 62 Timestamp: vote.Timestamp, 63 ChainID: chainID, 64 } 65 } 66 67 // CanonicalTime can be used to stringify time in a canonical way. 68 func CanonicalTime(t time.Time) string { 69 // Note that sending time over amino resets it to 70 // local time, we need to force UTC here, so the 71 // signatures match 72 return tmtime.Canonical(t).Format(TimeFormat) 73 }