github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/consensus/ipbft/types/canonical_json.go (about)

     1  package types
     2  
     3  // canonical json is go-wire's json for structs with fields in alphabetical order
     4  import (
     5  	crypto "github.com/intfoundation/go-crypto"
     6  )
     7  
     8  type CanonicalJSONBlockID struct {
     9  	Hash        []byte                     `json:"hash,omitempty"`
    10  	PartsHeader CanonicalJSONPartSetHeader `json:"parts,omitempty"`
    11  }
    12  
    13  type CanonicalJSONPartSetHeader struct {
    14  	Hash  []byte `json:"hash"`
    15  	Total uint64 `json:"total"`
    16  }
    17  
    18  type CanonicalJSONProposal struct {
    19  	BlockPartsHeader CanonicalJSONPartSetHeader `json:"block_parts_header"`
    20  	Height           uint64                     `json:"height"`
    21  	POLBlockID       CanonicalJSONBlockID       `json:"pol_block_id"`
    22  	POLRound         int                        `json:"pol_round"`
    23  	Round            int                        `json:"round"`
    24  	Hash             []byte                     `json:"hash"`
    25  }
    26  
    27  type CanonicalJSONVote struct {
    28  	BlockID CanonicalJSONBlockID `json:"block_id"`
    29  	Height  uint64               `json:"height"`
    30  	Round   uint64               `json:"round"`
    31  	Type    byte                 `json:"type"`
    32  }
    33  
    34  type CanonicalJSONSignAggr struct {
    35  	Height        uint64               `json:"height"`
    36  	Round         int                  `json:"round"`
    37  	Type          byte                 `json:"type"`
    38  	NumValidators int                  `json:"NumValidators"`
    39  	BlockID       CanonicalJSONBlockID `json:"block_id"`
    40  	Maj23         CanonicalJSONBlockID `json:"maj23"`
    41  	Sum           int64                `json:"sum"`
    42  }
    43  
    44  //------------------------------------
    45  // Messages including a "chain id" can only be applied to one chain, hence "Once"
    46  
    47  type CanonicalJSONOnceProposal struct {
    48  	ChainID  string                `json:"chain_id"`
    49  	Proposal CanonicalJSONProposal `json:"proposal"`
    50  }
    51  
    52  type CanonicalJSONOnceVote struct {
    53  	ChainID string            `json:"chain_id"`
    54  	Vote    CanonicalJSONVote `json:"vote"`
    55  }
    56  
    57  type CanonicalJSONOnceSignAggr struct {
    58  	ChainID  string                `json:"chain_id"`
    59  	SignAggr CanonicalJSONSignAggr `json:"sign_aggr"`
    60  }
    61  
    62  //-----------------------------
    63  //author@liaoyd
    64  type CanonicalJSONOnceValidatorMsg struct {
    65  	ChainID      string                    `json:"chain_id"`
    66  	ValidatorMsg CanonicalJSONValidatorMsg `json:"validator_msg"`
    67  }
    68  
    69  type CanonicalJSONValidatorMsg struct {
    70  	From           string        `json:"from"`
    71  	Epoch          int           `json:"epoch"`
    72  	ValidatorIndex int           `json:"validator_index"`
    73  	Key            string        `json:"key"`
    74  	PubKey         crypto.PubKey `json:"pub_key"`
    75  	Power          uint64        `json:"power"`
    76  	Action         string        `json:"action"`
    77  	Target         string        `json:"target"`
    78  }
    79  
    80  //-----------------------------------
    81  // Canonicalize the structs
    82  
    83  func CanonicalBlockID(blockID BlockID) CanonicalJSONBlockID {
    84  	return CanonicalJSONBlockID{
    85  		Hash:        blockID.Hash,
    86  		PartsHeader: CanonicalPartSetHeader(blockID.PartsHeader),
    87  	}
    88  }
    89  
    90  func CanonicalPartSetHeader(psh PartSetHeader) CanonicalJSONPartSetHeader {
    91  	return CanonicalJSONPartSetHeader{
    92  		psh.Hash,
    93  		psh.Total,
    94  	}
    95  }
    96  
    97  func CanonicalProposal(proposal *Proposal) CanonicalJSONProposal {
    98  	return CanonicalJSONProposal{
    99  		BlockPartsHeader: CanonicalPartSetHeader(proposal.BlockPartsHeader),
   100  		Height:           proposal.Height,
   101  		POLBlockID:       CanonicalBlockID(proposal.POLBlockID),
   102  		POLRound:         proposal.POLRound,
   103  		Round:            proposal.Round,
   104  	}
   105  }
   106  
   107  func CanonicalVote(vote *Vote) CanonicalJSONVote {
   108  	return CanonicalJSONVote{
   109  		CanonicalBlockID(vote.BlockID),
   110  		vote.Height,
   111  		vote.Round,
   112  		vote.Type,
   113  	}
   114  }
   115  
   116  func CanonicalSignAggr(signAggr *SignAggr) CanonicalJSONSignAggr {
   117  	return CanonicalJSONSignAggr{
   118  		signAggr.Height,
   119  		signAggr.Round,
   120  		signAggr.Type,
   121  		signAggr.NumValidators,
   122  		CanonicalBlockID(signAggr.BlockID),
   123  		CanonicalBlockID(signAggr.Maj23),
   124  		signAggr.Sum,
   125  	}
   126  }