-
Notifications
You must be signed in to change notification settings - Fork 328
/
candidate_endorsement.go
278 lines (257 loc) · 7.96 KB
/
candidate_endorsement.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package action
import (
"bytes"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
)
const (
// CandidateEndorsementBaseIntrinsicGas represents the base intrinsic gas for CandidateEndorsement
CandidateEndorsementBaseIntrinsicGas = uint64(10000)
candidateEndorsementInterfaceABI = `[
{
"inputs": [
{
"internalType": "uint64",
"name": "bucketIndex",
"type": "uint64"
},
{
"internalType": "bool",
"name": "endorse",
"type": "bool"
}
],
"name": "candidateEndorsement",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint64",
"name": "bucketIndex",
"type": "uint64"
}
],
"name": "endorseCandidate",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint64",
"name": "bucketIndex",
"type": "uint64"
}
],
"name": "intentToRevokeEndorsement",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint64",
"name": "bucketIndex",
"type": "uint64"
}
],
"name": "revokeEndorsement",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]`
)
// CandidateEndorsementOp defines the operation of CandidateEndorsement
const (
// CandidateEndorsementOpLegacy is the operation to endorse or unendorse a candidate in legacy version
CandidateEndorsementOpLegacy CandidateEndorsementOp = iota
// CandidateEndorsementOpEndorse is the operation to endorse a candidate
CandidateEndorsementOpEndorse
// CandidateEndorsementOpIntentToRevoke is the operation to intent to revoke an endorsement
CandidateEndorsementOpIntentToRevoke
// CandidateEndorsementOpRevoke is the operation to revoke an endorsement
CandidateEndorsementOpRevoke
)
var (
candidateEndorsementLegacyMethod abi.Method
candidateEndorsementEndorseMethod abi.Method
caniddateEndorsementIntentToRevokeMethod abi.Method
candidateEndorsementRevokeMethod abi.Method
_ EthCompatibleAction = (*CandidateEndorsement)(nil)
)
type (
// CandidateEndorsement is the action to endorse or unendorse a candidate
CandidateEndorsement struct {
stake_common
// bucketIndex is the bucket index want to be endorsed or unendorsed
bucketIndex uint64
// endorse is true if the action is to endorse a candidate, false if unendorse
endorse bool
// op is the operation of the endorsement
op CandidateEndorsementOp
}
// CandidateEndorsementOp defines the operation of CandidateEndorsement
CandidateEndorsementOp uint8
)
func init() {
candidateEndorsementInterface, err := abi.JSON(strings.NewReader(candidateEndorsementInterfaceABI))
if err != nil {
panic(err)
}
var ok bool
candidateEndorsementLegacyMethod, ok = candidateEndorsementInterface.Methods["candidateEndorsement"]
if !ok {
panic("fail to load the candidateEndorsement method")
}
candidateEndorsementEndorseMethod, ok = candidateEndorsementInterface.Methods["endorseCandidate"]
if !ok {
panic("fail to load the endorseCandidate method")
}
caniddateEndorsementIntentToRevokeMethod, ok = candidateEndorsementInterface.Methods["intentToRevokeEndorsement"]
if !ok {
panic("fail to load the intentToRevokeEndorsement method")
}
candidateEndorsementRevokeMethod, ok = candidateEndorsementInterface.Methods["revokeEndorsement"]
if !ok {
panic("fail to load the revokeEndorsement method")
}
}
// NewCandidateEndorsementLegacy returns a CandidateEndorsement action
func NewCandidateEndorsementLegacy(bucketIndex uint64, endorse bool) *CandidateEndorsement {
return &CandidateEndorsement{
bucketIndex: bucketIndex,
endorse: endorse,
}
}
// NewCandidateEndorsement returns a CandidateEndorsement action
func NewCandidateEndorsement(bucketIndex uint64, op CandidateEndorsementOp) (*CandidateEndorsement, error) {
if op == CandidateEndorsementOpLegacy {
return nil, errors.New("invalid operation")
}
return &CandidateEndorsement{
bucketIndex: bucketIndex,
op: op,
}, nil
}
// BucketIndex returns the bucket index of the action
func (act *CandidateEndorsement) BucketIndex() uint64 {
return act.bucketIndex
}
// IntrinsicGas returns the intrinsic gas of a CandidateEndorsement
func (act *CandidateEndorsement) IntrinsicGas() (uint64, error) {
return CandidateEndorsementBaseIntrinsicGas, nil
}
func (act *CandidateEndorsement) SanityCheck() error {
return nil
}
// IsLegacy returns true if the action is in legacy version
func (act *CandidateEndorsement) IsLegacy() bool {
return act.op == CandidateEndorsementOpLegacy
}
// Op returns the operation of the endorsement
func (act *CandidateEndorsement) Op() CandidateEndorsementOp {
if act.op == CandidateEndorsementOpLegacy {
if act.endorse {
return CandidateEndorsementOpEndorse
}
return CandidateEndorsementOpIntentToRevoke
}
return act.op
}
func (act *CandidateEndorsement) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_CandidateEndorsement{CandidateEndorsement: act.Proto()}
}
// Proto converts CandidateEndorsement to protobuf's Action
func (act *CandidateEndorsement) Proto() *iotextypes.CandidateEndorsement {
return &iotextypes.CandidateEndorsement{
BucketIndex: act.bucketIndex,
Endorse: act.endorse,
Op: uint32(act.op),
}
}
// LoadProto converts a protobuf's Action to CandidateEndorsement
func (act *CandidateEndorsement) LoadProto(pbAct *iotextypes.CandidateEndorsement) error {
if pbAct == nil {
return ErrNilProto
}
act.bucketIndex = pbAct.GetBucketIndex()
act.op = CandidateEndorsementOp(pbAct.GetOp())
act.endorse = pbAct.GetEndorse()
return nil
}
// EthData returns the ABI-encoded data for converting to eth tx
func (act *CandidateEndorsement) EthData() ([]byte, error) {
var method abi.Method
switch act.op {
case CandidateEndorsementOpLegacy:
data, err := candidateEndorsementLegacyMethod.Inputs.Pack(act.bucketIndex, act.endorse)
if err != nil {
return nil, err
}
return append(candidateEndorsementLegacyMethod.ID, data...), nil
case CandidateEndorsementOpEndorse:
method = candidateEndorsementEndorseMethod
case CandidateEndorsementOpIntentToRevoke:
method = caniddateEndorsementIntentToRevokeMethod
case CandidateEndorsementOpRevoke:
method = candidateEndorsementRevokeMethod
default:
return nil, errors.New("invalid operation")
}
data, err := method.Inputs.Pack(act.bucketIndex)
if err != nil {
return nil, err
}
return append(method.ID, data...), nil
}
// NewCandidateEndorsementFromABIBinary parses the smart contract input and creates an action
func NewCandidateEndorsementFromABIBinary(data []byte) (*CandidateEndorsement, error) {
if len(data) <= 4 {
return nil, errDecodeFailure
}
var (
paramsMap = map[string]any{}
cr CandidateEndorsement
method abi.Method
)
switch {
case bytes.Equal(candidateEndorsementLegacyMethod.ID, data[:4]):
method = candidateEndorsementLegacyMethod
cr.op = CandidateEndorsementOpLegacy
case bytes.Equal(candidateEndorsementEndorseMethod.ID, data[:4]):
method = candidateEndorsementEndorseMethod
cr.op = CandidateEndorsementOpEndorse
case bytes.Equal(caniddateEndorsementIntentToRevokeMethod.ID, data[:4]):
method = caniddateEndorsementIntentToRevokeMethod
cr.op = CandidateEndorsementOpIntentToRevoke
case bytes.Equal(candidateEndorsementRevokeMethod.ID, data[:4]):
method = candidateEndorsementRevokeMethod
cr.op = CandidateEndorsementOpRevoke
default:
return nil, errDecodeFailure
}
if err := method.Inputs.UnpackIntoMap(paramsMap, data[4:]); err != nil {
return nil, err
}
bucketID, ok := paramsMap["bucketIndex"].(uint64)
if !ok {
return nil, errDecodeFailure
}
cr.bucketIndex = bucketID
if cr.op == CandidateEndorsementOpLegacy {
endorse, ok := paramsMap["endorse"].(bool)
if !ok {
return nil, errDecodeFailure
}
cr.endorse = endorse
}
return &cr, nil
}