github.com/iotexproject/iotex-core@v1.14.1-rc1/consensus/scheme/rolldpos/endorsedconsensusmessage.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package rolldpos 7 8 import ( 9 "github.com/iotexproject/iotex-core/blockchain/block" 10 "github.com/pkg/errors" 11 12 "github.com/iotexproject/iotex-core/endorsement" 13 "github.com/iotexproject/iotex-proto/golang/iotextypes" 14 ) 15 16 // EndorsedConsensusMessage is an endorsement on document 17 type EndorsedConsensusMessage struct { 18 height uint64 19 message endorsement.Document 20 endorsement *endorsement.Endorsement 21 } 22 23 // NewEndorsedConsensusMessage creates an EndorsedConsensusMessage for an consensus vote 24 func NewEndorsedConsensusMessage( 25 height uint64, 26 message endorsement.Document, 27 endorsement *endorsement.Endorsement, 28 ) *EndorsedConsensusMessage { 29 return &EndorsedConsensusMessage{ 30 height: height, 31 message: message, 32 endorsement: endorsement, 33 } 34 } 35 36 // Document returns the endorsed consensus message 37 func (ecm *EndorsedConsensusMessage) Document() endorsement.Document { 38 return ecm.message 39 } 40 41 // Endorsement returns the endorsement 42 func (ecm *EndorsedConsensusMessage) Endorsement() *endorsement.Endorsement { 43 return ecm.endorsement 44 } 45 46 // Height returns the height of this message 47 func (ecm *EndorsedConsensusMessage) Height() uint64 { 48 return ecm.height 49 } 50 51 // Proto converts an endorsement to endorse proto 52 func (ecm *EndorsedConsensusMessage) Proto() (*iotextypes.ConsensusMessage, error) { 53 ebp, err := ecm.endorsement.Proto() 54 if err != nil { 55 return nil, err 56 } 57 cmsg := &iotextypes.ConsensusMessage{ 58 Height: ecm.height, 59 Endorsement: ebp, 60 } 61 switch message := ecm.message.(type) { 62 case *ConsensusVote: 63 mbp, err := message.Proto() 64 if err != nil { 65 return nil, err 66 } 67 cmsg.Msg = &iotextypes.ConsensusMessage_Vote{Vote: mbp} 68 case *blockProposal: 69 mbp, err := message.Proto() 70 if err != nil { 71 return nil, err 72 } 73 cmsg.Msg = &iotextypes.ConsensusMessage_BlockProposal{BlockProposal: mbp} 74 default: 75 return nil, errors.New("unknown consensus message type") 76 } 77 78 return cmsg, nil 79 } 80 81 // LoadProto creates an endorsement message from protobuf message 82 func (ecm *EndorsedConsensusMessage) LoadProto(msg *iotextypes.ConsensusMessage, deserializer *block.Deserializer) error { 83 switch { 84 case msg.GetVote() != nil: 85 vote := &ConsensusVote{} 86 if err := vote.LoadProto(msg.GetVote()); err != nil { 87 return err 88 } 89 ecm.message = vote 90 case msg.GetBlockProposal() != nil: 91 proposal := &blockProposal{} 92 if err := proposal.LoadProto(msg.GetBlockProposal(), deserializer); err != nil { 93 return err 94 } 95 ecm.message = proposal 96 default: 97 return errors.New("unknown message") 98 } 99 ecm.height = msg.Height 100 ecm.endorsement = &endorsement.Endorsement{} 101 102 return ecm.endorsement.LoadProto(msg.GetEndorsement()) 103 }