OSDN Git Service

add query
[bytom/bytom.git] / types / protobuf.go
1 package types
2
3 import (
4         "github.com/tendermint/abci/types"
5 )
6
7 // Convert tendermint types to protobuf types
8 var TM2PB = tm2pb{}
9
10 type tm2pb struct{}
11
12 func (tm2pb) Header(header *Header) *types.Header {
13         return &types.Header{
14                 ChainId:        header.ChainID,
15                 Height:         uint64(header.Height),
16                 Time:           uint64(header.Time.Unix()),
17                 NumTxs:         uint64(header.NumTxs),
18                 LastBlockId:    TM2PB.BlockID(header.LastBlockID),
19                 LastCommitHash: header.LastCommitHash,
20                 DataHash:       header.DataHash,
21                 AppHash:        header.AppHash,
22         }
23 }
24
25 func (tm2pb) BlockID(blockID BlockID) *types.BlockID {
26         return &types.BlockID{
27                 Hash:  blockID.Hash,
28                 Parts: TM2PB.PartSetHeader(blockID.PartsHeader),
29         }
30 }
31
32 func (tm2pb) PartSetHeader(partSetHeader PartSetHeader) *types.PartSetHeader {
33         return &types.PartSetHeader{
34                 Total: uint64(partSetHeader.Total),
35                 Hash:  partSetHeader.Hash,
36         }
37 }
38
39 func (tm2pb) Validator(val *Validator) *types.Validator {
40         return &types.Validator{
41                 PubKey: val.PubKey.Bytes(),
42                 Power:  uint64(val.VotingPower),
43         }
44 }
45
46 func (tm2pb) Validators(vals *ValidatorSet) []*types.Validator {
47         validators := make([]*types.Validator, len(vals.Validators))
48         for i, val := range vals.Validators {
49                 validators[i] = TM2PB.Validator(val)
50         }
51         return validators
52 }