tendermint/types/protobuf.go

101 lines
2.4 KiB
Go
Raw Normal View History

2016-08-22 15:57:20 -04:00
package types
import (
2018-05-31 11:09:42 -04:00
"fmt"
"reflect"
2017-01-12 15:53:32 -05:00
"github.com/tendermint/abci/types"
2018-05-31 11:09:42 -04:00
crypto "github.com/tendermint/go-crypto"
2016-08-22 15:57:20 -04:00
)
2017-09-22 12:00:37 -04:00
// TM2PB is used for converting Tendermint types to protobuf types.
// UNSTABLE
2016-08-22 15:57:20 -04:00
var TM2PB = tm2pb{}
type tm2pb struct{}
2018-05-31 11:54:22 -04:00
func (tm2pb) Header(header *Header) *types.Header {
return &types.Header{
2018-05-31 11:09:42 -04:00
ChainId: header.ChainID,
Height: header.Height,
Time: header.Time.Unix(),
NumTxs: int32(header.NumTxs), // XXX: overflow
LastBlockHash: header.LastBlockID.Hash,
AppHash: header.AppHash,
2016-08-22 15:57:20 -04:00
}
}
2018-05-31 11:54:22 -04:00
func (tm2pb) Validator(val *Validator) *types.Validator {
return &types.Validator{
2018-05-31 11:09:42 -04:00
PubKey: TM2PB.PubKey(val.PubKey),
2017-12-02 01:07:17 -05:00
Power: val.VotingPower,
2016-08-22 15:57:20 -04:00
}
}
2018-05-31 11:09:42 -04:00
func (tm2pb) PubKey(pubKey crypto.PubKey) *types.PubKey {
switch pk := pubKey.(type) {
case crypto.PubKeyEd25519:
return &types.PubKey{
Type: "ed25519",
Data: pk[:],
}
case crypto.PubKeySecp256k1:
return &types.PubKey{
Type: "secp256k1",
Data: pk[:],
}
default:
panic(fmt.Sprintf("unknown pubkey type: %v %v", pubKey, reflect.TypeOf(pubKey)))
}
}
2018-05-31 11:54:22 -04:00
func (tm2pb) Validators(vals *ValidatorSet) []*types.Validator {
validators := make([]*types.Validator, len(vals.Validators))
for i, val := range vals.Validators {
validators[i] = TM2PB.Validator(val)
}
return validators
}
2017-12-21 17:46:25 -05:00
func (tm2pb) ConsensusParams(params *ConsensusParams) *types.ConsensusParams {
return &types.ConsensusParams{
BlockSize: &types.BlockSize{
MaxBytes: int32(params.BlockSize.MaxBytes),
MaxTxs: int32(params.BlockSize.MaxTxs),
MaxGas: params.BlockSize.MaxGas,
},
TxSize: &types.TxSize{
MaxBytes: int32(params.TxSize.MaxBytes),
MaxGas: params.TxSize.MaxGas,
},
BlockGossip: &types.BlockGossip{
BlockPartSizeBytes: int32(params.BlockGossip.BlockPartSizeBytes),
},
}
}
2018-05-31 11:54:22 -04:00
//----------------------------------------------------------------------------
// PB2TM is used for converting protobuf types to Tendermint types.
// UNSTABLE
var PB2TM = pb2tm{}
type pb2tm struct{}
// TODO: validate key lengths ...
func (pb2tm) PubKey(pubKey *types.PubKey) (crypto.PubKey, error) {
switch pubKey.Type {
case "ed25519":
var pk crypto.PubKeyEd25519
copy(pk[:], pubKey.Data)
return pk, nil
case "secp256k1":
var pk crypto.PubKeySecp256k1
copy(pk[:], pubKey.Data)
return pk, nil
default:
return nil, fmt.Errorf("Unknown pubkey type %v", pubKey.Type)
}
}