github.com/okex/exchain@v1.8.0/libs/tendermint/types/canonical.go (about)

     1  package types
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/okex/exchain/libs/tendermint/libs/bytes"
     7  	tmtime "github.com/okex/exchain/libs/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  type CanonicalBlockID struct {
    16  	Hash        bytes.HexBytes
    17  	PartsHeader CanonicalPartSetHeader
    18  }
    19  
    20  type CanonicalPartSetHeader struct {
    21  	Hash  bytes.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  
    55  func CanonicalizePartSetHeader(psh PartSetHeader) CanonicalPartSetHeader {
    56  	return CanonicalPartSetHeader{
    57  		psh.Hash,
    58  		psh.Total,
    59  	}
    60  }
    61  
    62  func CanonicalizeProposal(chainID string, proposal *Proposal) CanonicalProposal {
    63  	return CanonicalProposal{
    64  		Type:      ProposalType,
    65  		Height:    proposal.Height,
    66  		Round:     int64(proposal.Round), // cast int->int64 to make amino encode it fixed64 (does not work for int)
    67  		POLRound:  int64(proposal.POLRound),
    68  		BlockID:   CanonicalizeBlockID(proposal.BlockID),
    69  		Timestamp: proposal.Timestamp,
    70  		ChainID:   chainID,
    71  	}
    72  }
    73  
    74  func CanonicalizeVote(chainID string, vote *Vote) CanonicalVote {
    75  	return CanonicalVote{
    76  		Type:      vote.Type,
    77  		Height:    vote.Height,
    78  		Round:     int64(vote.Round), // cast int->int64 to make amino encode it fixed64 (does not work for int)
    79  		BlockID:   CanonicalizeBlockID(vote.BlockID),
    80  		Timestamp: vote.Timestamp,
    81  		ChainID:   chainID,
    82  	}
    83  }
    84  
    85  // CanonicalTime can be used to stringify time in a canonical way.
    86  func CanonicalTime(t time.Time) string {
    87  	// Note that sending time over amino resets it to
    88  	// local time, we need to force UTC here, so the
    89  	// signatures match
    90  	return tmtime.Canonical(t).Format(TimeFormat)
    91  }