/
candidate_update.go
137 lines (113 loc) · 3.8 KB
/
candidate_update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) 2020 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package action
import (
"math/big"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/pkg/version"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
)
// CandidateUpdateBaseIntrinsicGas represents the base intrinsic gas for CandidateUpdate
const CandidateUpdateBaseIntrinsicGas = uint64(10000)
// CandidateUpdate is the action to register a candidate
type CandidateUpdate struct {
AbstractAction
name string
operatorAddress address.Address
rewardAddress address.Address
}
// NewCandidateUpdate creates a CandidateUpdate instance
func NewCandidateUpdate(
nonce uint64,
name, operatorAddrStr, rewardAddrStr string,
gasLimit uint64,
gasPrice *big.Int,
) (*CandidateUpdate, error) {
cu := &CandidateUpdate{
AbstractAction: AbstractAction{
version: version.ProtocolVersion,
nonce: nonce,
gasLimit: gasLimit,
gasPrice: gasPrice,
},
name: name,
}
var err error
if len(operatorAddrStr) > 0 {
cu.operatorAddress, err = address.FromString(operatorAddrStr)
if err != nil {
return nil, err
}
}
if len(rewardAddrStr) > 0 {
cu.rewardAddress, err = address.FromString(rewardAddrStr)
if err != nil {
return nil, err
}
}
return cu, nil
}
// Name returns candidate name to update
func (cu *CandidateUpdate) Name() string { return cu.name }
// OperatorAddress returns candidate operatorAddress to update
func (cu *CandidateUpdate) OperatorAddress() address.Address { return cu.operatorAddress }
// RewardAddress returns candidate rewardAddress to update
func (cu *CandidateUpdate) RewardAddress() address.Address { return cu.rewardAddress }
// Serialize returns a raw byte stream of the CandidateUpdate struct
func (cu *CandidateUpdate) Serialize() []byte {
return byteutil.Must(proto.Marshal(cu.Proto()))
}
// Proto converts to protobuf CandidateUpdate Action
func (cu *CandidateUpdate) Proto() *iotextypes.CandidateBasicInfo {
act := &iotextypes.CandidateBasicInfo{
Name: cu.name,
}
if cu.operatorAddress != nil {
act.OperatorAddress = cu.operatorAddress.String()
}
if cu.rewardAddress != nil {
act.RewardAddress = cu.rewardAddress.String()
}
return act
}
// LoadProto converts a protobuf's Action to CandidateUpdate
func (cu *CandidateUpdate) LoadProto(pbAct *iotextypes.CandidateBasicInfo) error {
if pbAct == nil {
return errors.New("empty action proto to load")
}
cu.name = pbAct.GetName()
if len(pbAct.GetOperatorAddress()) > 0 {
operatorAddr, err := address.FromString(pbAct.GetOperatorAddress())
if err != nil {
return err
}
cu.operatorAddress = operatorAddr
}
if len(pbAct.GetRewardAddress()) > 0 {
rewardAddr, err := address.FromString(pbAct.GetRewardAddress())
if err != nil {
return err
}
cu.rewardAddress = rewardAddr
}
return nil
}
// IntrinsicGas returns the intrinsic gas of a CandidateUpdate
func (cu *CandidateUpdate) IntrinsicGas() (uint64, error) {
return CandidateUpdateBaseIntrinsicGas, nil
}
// Cost returns the total cost of a CandidateUpdate
func (cu *CandidateUpdate) Cost() (*big.Int, error) {
intrinsicGas, err := cu.IntrinsicGas()
if err != nil {
return nil, errors.Wrap(err, "failed ts get intrinsic gas for the CandidateUpdate")
}
fee := big.NewInt(0).Mul(cu.GasPrice(), big.NewInt(0).SetUint64(intrinsicGas))
return fee, nil
}