github.com/evdatsion/aphelion-dpos-bft@v0.32.1/types/canonical.go (about) 1 package types 2 3 import ( 4 "time" 5 6 cmn "github.com/evdatsion/aphelion-dpos-bft/libs/common" 7 tmtime "github.com/evdatsion/aphelion-dpos-bft/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 type CanonicalBlockID struct { 16 Hash cmn.HexBytes 17 PartsHeader CanonicalPartSetHeader 18 } 19 20 type CanonicalPartSetHeader struct { 21 Hash cmn.HexBytes 22 Total int 23 } 24 25 type CanonicalProposal struct { 26 Type SignedMsgType // type alias for byte 27 Height int64 `binary:"fixed64"` 28 Round int64 `binary:"fixed64"` 29 POLRound int64 `binary:"fixed64"` 30 BlockID CanonicalBlockID 31 Timestamp time.Time 32 ChainID string 33 } 34 35 type CanonicalVote struct { 36 Type SignedMsgType // type alias for byte 37 Height int64 `binary:"fixed64"` 38 Round int64 `binary:"fixed64"` 39 BlockID CanonicalBlockID 40 Timestamp time.Time 41 ChainID string 42 } 43 44 //----------------------------------- 45 // Canonicalize the structs 46 47 func CanonicalizeBlockID(blockID BlockID) CanonicalBlockID { 48 return CanonicalBlockID{ 49 Hash: blockID.Hash, 50 PartsHeader: CanonicalizePartSetHeader(blockID.PartsHeader), 51 } 52 } 53 54 func CanonicalizePartSetHeader(psh PartSetHeader) CanonicalPartSetHeader { 55 return CanonicalPartSetHeader{ 56 psh.Hash, 57 psh.Total, 58 } 59 } 60 61 func CanonicalizeProposal(chainID string, proposal *Proposal) CanonicalProposal { 62 return CanonicalProposal{ 63 Type: ProposalType, 64 Height: proposal.Height, 65 Round: int64(proposal.Round), // cast int->int64 to make amino encode it fixed64 (does not work for int) 66 POLRound: int64(proposal.POLRound), 67 BlockID: CanonicalizeBlockID(proposal.BlockID), 68 Timestamp: proposal.Timestamp, 69 ChainID: chainID, 70 } 71 } 72 73 func CanonicalizeVote(chainID string, vote *Vote) CanonicalVote { 74 return CanonicalVote{ 75 Type: vote.Type, 76 Height: vote.Height, 77 Round: int64(vote.Round), // cast int->int64 to make amino encode it fixed64 (does not work for int) 78 BlockID: CanonicalizeBlockID(vote.BlockID), 79 Timestamp: vote.Timestamp, 80 ChainID: chainID, 81 } 82 } 83 84 // CanonicalTime can be used to stringify time in a canonical way. 85 func CanonicalTime(t time.Time) string { 86 // Note that sending time over amino resets it to 87 // local time, we need to force UTC here, so the 88 // signatures match 89 return tmtime.Canonical(t).Format(TimeFormat) 90 }