github.com/iotexproject/iotex-core@v1.14.1-rc1/action/putpollresult.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 action 7 8 import ( 9 "math/big" 10 11 "github.com/iotexproject/iotex-proto/golang/iotextypes" 12 "google.golang.org/protobuf/proto" 13 14 "github.com/iotexproject/iotex-core/pkg/util/byteutil" 15 "github.com/iotexproject/iotex-core/pkg/version" 16 "github.com/iotexproject/iotex-core/state" 17 ) 18 19 // PutPollResult represents put the poll result from gravity chain. 20 type PutPollResult struct { 21 AbstractAction 22 23 height uint64 24 candidates state.CandidateList 25 } 26 27 // NewPutPollResult instantiates a putting poll result action struct. 28 func NewPutPollResult( 29 nonce uint64, 30 height uint64, 31 candidates state.CandidateList, 32 ) *PutPollResult { 33 return &PutPollResult{ 34 AbstractAction: AbstractAction{ 35 version: version.ProtocolVersion, 36 nonce: nonce, 37 gasLimit: 0, 38 gasPrice: big.NewInt(0), 39 }, 40 height: height, 41 candidates: candidates, 42 } 43 } 44 45 // LoadProto converts a proto message into put block action. 46 func (r *PutPollResult) LoadProto(putPollResultPb *iotextypes.PutPollResult) error { 47 if putPollResultPb == nil { 48 return ErrNilProto 49 } 50 if r == nil { 51 return ErrNilAction 52 } 53 *r = PutPollResult{} 54 55 r.height = putPollResultPb.Height 56 57 return r.candidates.LoadProto(putPollResultPb.Candidates) 58 } 59 60 // Proto converts put poll result action into a proto message. 61 func (r *PutPollResult) Proto() *iotextypes.PutPollResult { 62 return &iotextypes.PutPollResult{ 63 Height: r.height, 64 Candidates: r.candidates.Proto(), 65 } 66 } 67 68 // Height returns put poll result height. 69 func (r *PutPollResult) Height() uint64 { return r.height } 70 71 // Candidates returns the list of candidates. 72 func (r *PutPollResult) Candidates() state.CandidateList { return r.candidates } 73 74 // Serialize returns the byte representation of put poll result action. 75 func (r *PutPollResult) Serialize() []byte { 76 return byteutil.Must(proto.Marshal(r.Proto())) 77 } 78 79 // IntrinsicGas returns the intrinsic gas of a put poll result action 80 func (r *PutPollResult) IntrinsicGas() (uint64, error) { 81 return 0, nil 82 } 83 84 // Cost returns the total cost of a put poll result action 85 func (r *PutPollResult) Cost() (*big.Int, error) { 86 return big.NewInt(0), nil 87 }