github.com/iotexproject/iotex-core@v1.14.1-rc1/consensus/scheme/rolldpos/blockproposal.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/go-pkgs/hash" 10 "github.com/iotexproject/iotex-core/blockchain/block" 11 "github.com/iotexproject/iotex-core/endorsement" 12 "github.com/iotexproject/iotex-core/pkg/util/byteutil" 13 "github.com/iotexproject/iotex-proto/golang/iotextypes" 14 "google.golang.org/protobuf/proto" 15 ) 16 17 type blockProposal struct { 18 block *block.Block 19 proofOfLock []*endorsement.Endorsement 20 } 21 22 func newBlockProposal(blk *block.Block, pol []*endorsement.Endorsement) *blockProposal { 23 return &blockProposal{ 24 block: blk, 25 proofOfLock: pol, 26 } 27 } 28 29 func (bp *blockProposal) Height() uint64 { 30 return bp.block.Height() 31 } 32 33 func (bp *blockProposal) Proto() (*iotextypes.BlockProposal, error) { 34 bPb := bp.block.ConvertToBlockPb() 35 endorsements := []*iotextypes.Endorsement{} 36 for _, en := range bp.proofOfLock { 37 ePb, err := en.Proto() 38 if err != nil { 39 return nil, err 40 } 41 endorsements = append(endorsements, ePb) 42 } 43 return &iotextypes.BlockProposal{ 44 Block: bPb, 45 Endorsements: endorsements, 46 }, nil 47 } 48 49 func (bp *blockProposal) Hash() ([]byte, error) { 50 msg, err := bp.Proto() 51 if err != nil { 52 return nil, err 53 } 54 h := hash.Hash256b(byteutil.Must(proto.Marshal(msg))) 55 56 return h[:], nil 57 } 58 59 func (bp *blockProposal) ProposerAddress() string { 60 return bp.block.ProducerAddress() 61 } 62 63 func (bp *blockProposal) LoadProto(msg *iotextypes.BlockProposal, deserializer *block.Deserializer) error { 64 blk, err := deserializer.FromBlockProto(msg.Block) 65 if err != nil { 66 return err 67 } 68 bp.block = blk 69 bp.proofOfLock = []*endorsement.Endorsement{} 70 for _, ePb := range msg.Endorsements { 71 en := &endorsement.Endorsement{} 72 if err := en.LoadProto(ePb); err != nil { 73 return err 74 } 75 bp.proofOfLock = append(bp.proofOfLock, en) 76 } 77 return nil 78 }