github.com/Finschia/ostracon@v1.1.5/types/protobuf.go (about) 1 package types 2 3 import ( 4 abci "github.com/tendermint/tendermint/abci/types" 5 tmproto "github.com/tendermint/tendermint/proto/tendermint/types" 6 7 "github.com/Finschia/ostracon/crypto" 8 "github.com/Finschia/ostracon/crypto/ed25519" 9 cryptoenc "github.com/Finschia/ostracon/crypto/encoding" 10 "github.com/Finschia/ostracon/crypto/secp256k1" 11 ) 12 13 //------------------------------------------------------- 14 // Use strings to distinguish types in ABCI messages 15 16 const ( 17 ABCIPubKeyTypeEd25519 = ed25519.KeyType 18 ABCIPubKeyTypeSecp256k1 = secp256k1.KeyType 19 ) 20 21 // TODO: Make non-global by allowing for registration of more pubkey types 22 23 var ABCIPubKeyTypesToNames = map[string]string{ 24 ABCIPubKeyTypeEd25519: ed25519.PubKeyName, 25 ABCIPubKeyTypeSecp256k1: secp256k1.PubKeyName, 26 } 27 28 //------------------------------------------------------- 29 30 // OC2PB is used for converting Ostracon ABCI to protobuf ABCI. 31 // UNSTABLE 32 var OC2PB = oc2pb{} 33 34 type oc2pb struct{} 35 36 func (oc2pb) Header(header *Header) tmproto.Header { 37 return tmproto.Header{ 38 Version: header.Version, 39 ChainID: header.ChainID, 40 Height: header.Height, 41 Time: header.Time, 42 43 LastBlockId: header.LastBlockID.ToProto(), 44 45 LastCommitHash: header.LastCommitHash, 46 DataHash: header.DataHash, 47 48 ValidatorsHash: header.ValidatorsHash, 49 NextValidatorsHash: header.NextValidatorsHash, 50 ConsensusHash: header.ConsensusHash, 51 AppHash: header.AppHash, 52 LastResultsHash: header.LastResultsHash, 53 54 EvidenceHash: header.EvidenceHash, 55 ProposerAddress: header.ProposerAddress, 56 } 57 } 58 59 func (oc2pb) Validator(val *Validator) abci.Validator { 60 return abci.Validator{ 61 Address: val.PubKey.Address(), 62 Power: val.VotingPower, 63 } 64 } 65 66 func (oc2pb) BlockID(blockID BlockID) tmproto.BlockID { 67 return tmproto.BlockID{ 68 Hash: blockID.Hash, 69 PartSetHeader: OC2PB.PartSetHeader(blockID.PartSetHeader), 70 } 71 } 72 73 func (oc2pb) PartSetHeader(header PartSetHeader) tmproto.PartSetHeader { 74 return tmproto.PartSetHeader{ 75 Total: header.Total, 76 Hash: header.Hash, 77 } 78 } 79 80 // XXX: panics on unknown pubkey type 81 func (oc2pb) ValidatorUpdate(val *Validator) abci.ValidatorUpdate { 82 pk, err := cryptoenc.PubKeyToProto(val.PubKey) 83 if err != nil { 84 panic(err) 85 } 86 return abci.ValidatorUpdate{ 87 PubKey: pk, 88 Power: val.VotingPower, 89 } 90 } 91 92 // XXX: panics on nil or unknown pubkey type 93 func (oc2pb) ValidatorUpdates(vals *ValidatorSet) []abci.ValidatorUpdate { 94 validators := make([]abci.ValidatorUpdate, vals.Size()) 95 for i, val := range vals.Validators { 96 validators[i] = OC2PB.ValidatorUpdate(val) 97 } 98 return validators 99 } 100 101 func (oc2pb) ConsensusParams(params *tmproto.ConsensusParams) *abci.ConsensusParams { 102 return &abci.ConsensusParams{ 103 Block: &abci.BlockParams{ 104 MaxBytes: params.Block.MaxBytes, 105 MaxGas: params.Block.MaxGas, 106 }, 107 Evidence: ¶ms.Evidence, 108 Validator: ¶ms.Validator, 109 } 110 } 111 112 // XXX: panics on nil or unknown pubkey type 113 func (oc2pb) NewValidatorUpdate(pubkey crypto.PubKey, power int64) abci.ValidatorUpdate { 114 pubkeyABCI, err := cryptoenc.PubKeyToProto(pubkey) 115 if err != nil { 116 panic(err) 117 } 118 return abci.ValidatorUpdate{ 119 PubKey: pubkeyABCI, 120 Power: power, 121 } 122 } 123 124 //---------------------------------------------------------------------------- 125 126 // PB2OC is used for converting protobuf ABCI to Ostracon ABCI. 127 // UNSTABLE 128 var PB2OC = pb2tm{} 129 130 type pb2tm struct{} 131 132 func (pb2tm) ValidatorUpdates(vals []abci.ValidatorUpdate) ([]*Validator, error) { 133 tmVals := make([]*Validator, len(vals)) 134 for i, v := range vals { 135 pub, err := cryptoenc.PubKeyFromProto(&v.PubKey) 136 if err != nil { 137 return nil, err 138 } 139 tmVals[i] = NewValidator(pub, v.Power) 140 } 141 return tmVals, nil 142 }