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

     1  package types
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  
     7  	"github.com/okex/exchain/libs/tendermint/version"
     8  
     9  	ce "github.com/okex/exchain/libs/tendermint/crypto/encoding"
    10  	"github.com/okex/exchain/libs/tendermint/libs/bits"
    11  	tmbytes "github.com/okex/exchain/libs/tendermint/libs/bytes"
    12  	tmproto "github.com/okex/exchain/libs/tendermint/proto/types"
    13  	tmversion "github.com/okex/exchain/libs/tendermint/proto/version"
    14  )
    15  
    16  type CM40Block struct {
    17  	mtx sync.Mutex
    18  
    19  	IBCHeader  `json:"header"`
    20  	Data       `json:"data"`
    21  	Evidence   EvidenceData `json:"evidence"`
    22  	LastCommit *IBCCommit   `json:"last_commit"`
    23  }
    24  
    25  // SignedHeader is a header along with the commits that prove it.
    26  type IBCSignedHeader struct {
    27  	*IBCHeader `json:"header"`
    28  
    29  	Commit *IBCCommit `json:"commit"`
    30  }
    31  
    32  type IBCPartSetHeader struct {
    33  	Total uint32           `json:"total"`
    34  	Hash  tmbytes.HexBytes `json:"hash"`
    35  }
    36  
    37  type IBCBlockID struct {
    38  	Hash          tmbytes.HexBytes `json:"hash"`
    39  	PartSetHeader IBCPartSetHeader `json:"parts"`
    40  }
    41  
    42  func (b IBCBlockID) ToBlockID() BlockID {
    43  	return BlockID{
    44  		Hash: b.Hash,
    45  		PartsHeader: PartSetHeader{
    46  			Total: int(b.PartSetHeader.Total),
    47  			Hash:  b.PartSetHeader.Hash,
    48  		},
    49  	}
    50  }
    51  
    52  type IBCHeader struct {
    53  	// basic block info
    54  	Version tmversion.Consensus `json:"version"`
    55  	ChainID string              `json:"chain_id"`
    56  	Height  int64               `json:"height"`
    57  	Time    time.Time           `json:"time"`
    58  
    59  	// prev block info
    60  	LastBlockID IBCBlockID `json:"last_block_id"`
    61  
    62  	// hashes of block data
    63  	LastCommitHash tmbytes.HexBytes `json:"last_commit_hash"` // commit from validators from the last block
    64  	DataHash       tmbytes.HexBytes `json:"data_hash"`        // transactions
    65  
    66  	// hashes from the app output from the prev block
    67  	ValidatorsHash     tmbytes.HexBytes `json:"validators_hash"`      // validators for the current block
    68  	NextValidatorsHash tmbytes.HexBytes `json:"next_validators_hash"` // validators for the next block
    69  	ConsensusHash      tmbytes.HexBytes `json:"consensus_hash"`       // consensus params for current block
    70  	AppHash            tmbytes.HexBytes `json:"app_hash"`             // state after txs from the previous block
    71  	// root hash of all results from the txs from the previous block
    72  	// see `deterministicResponseDeliverTx` to understand which parts of a tx is hashed into here
    73  	LastResultsHash tmbytes.HexBytes `json:"last_results_hash"`
    74  
    75  	// consensus info
    76  	EvidenceHash    tmbytes.HexBytes `json:"evidence_hash"`    // evidence included in the block
    77  	ProposerAddress Address          `json:"proposer_address"` // original proposer of the block
    78  }
    79  
    80  func (h IBCHeader) ToCM39Header() Header {
    81  	return Header{
    82  		Version: version.Consensus{
    83  			Block: version.Protocol(h.Version.Block),
    84  			App:   version.Protocol(h.Version.App),
    85  		},
    86  		ChainID:            h.ChainID,
    87  		Height:             h.Height,
    88  		Time:               h.Time,
    89  		LastBlockID:        h.LastBlockID.ToBlockID(),
    90  		LastCommitHash:     h.LastCommitHash,
    91  		DataHash:           h.DataHash,
    92  		ValidatorsHash:     h.ValidatorsHash,
    93  		NextValidatorsHash: h.NextValidatorsHash,
    94  		ConsensusHash:      h.ConsensusHash,
    95  		AppHash:            h.AppHash,
    96  		LastResultsHash:    h.LastResultsHash,
    97  		EvidenceHash:       h.EvidenceHash,
    98  		ProposerAddress:    h.ProposerAddress,
    99  	}
   100  }
   101  
   102  type IBCCommit struct {
   103  	// NOTE: The signatures are in order of address to preserve the bonded
   104  	// ValidatorSet order.
   105  	// Any peer with a block can gossip signatures by index with a peer without
   106  	// recalculating the active ValidatorSet.
   107  	Height     int64       `json:"height"`
   108  	Round      int32       `json:"round"`
   109  	BlockID    IBCBlockID  `json:"block_id"`
   110  	Signatures []CommitSig `json:"signatures"`
   111  
   112  	// Memoized in first call to corresponding method.
   113  	// NOTE: can't memoize in constructor because constructor isn't used for
   114  	// unmarshaling.
   115  	hash     tmbytes.HexBytes
   116  	bitArray *bits.BitArray
   117  }
   118  
   119  func (c *IBCCommit) ToCommit() *Commit {
   120  	return &Commit{
   121  		Height:     c.Height,
   122  		Round:      int(c.Round),
   123  		BlockID:    c.BlockID.ToBlockID(),
   124  		Signatures: c.Signatures,
   125  		hash:       c.hash,
   126  		bitArray:   c.bitArray,
   127  	}
   128  }
   129  
   130  func (v *Validator) HeightBytes(h int64) []byte {
   131  	if HigherThanVenus1(h) {
   132  		return v.IBCHeightBytes()
   133  	}
   134  	return v.OriginBytes()
   135  }
   136  
   137  func (v *Validator) IBCHeightBytes() []byte {
   138  	pk, err := ce.PubKeyToProto(v.PubKey)
   139  	if err != nil {
   140  		panic(err)
   141  	}
   142  
   143  	pbv := tmproto.SimpleValidator{
   144  		PubKey:      &pk,
   145  		VotingPower: v.VotingPower,
   146  	}
   147  
   148  	bz, err := pbv.Marshal()
   149  	if err != nil {
   150  		panic(err)
   151  	}
   152  	return bz
   153  }