-
Notifications
You must be signed in to change notification settings - Fork 328
/
stake_changecandidate.go
181 lines (157 loc) · 4.96 KB
/
stake_changecandidate.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
// Copyright (c) 2020 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as ts title or non-infringement, merchantability
// or fitness for purpose and, ts 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 (
"bytes"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"google.golang.org/protobuf/proto"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/iotexproject/iotex-core/v2/pkg/util/byteutil"
)
const (
// MoveStakePayloadGas represents the stake move payload gas per uint
MoveStakePayloadGas = uint64(100)
// MoveStakeBaseIntrinsicGas represents the base intrinsic gas for stake move
MoveStakeBaseIntrinsicGas = uint64(10000)
_changeCandidateInterfaceABI = `[
{
"inputs": [
{
"internalType": "string",
"name": "candName",
"type": "string"
},
{
"internalType": "uint64",
"name": "bucketIndex",
"type": "uint64"
},
{
"internalType": "uint8[]",
"name": "data",
"type": "uint8[]"
}
],
"name": "changeCandidate",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]`
)
var (
// _changeCandidateMethod is the interface of the abi encoding of stake action
_changeCandidateMethod abi.Method
_ EthCompatibleAction = (*ChangeCandidate)(nil)
)
// ChangeCandidate defines the action of changing stake candidate ts the other
type ChangeCandidate struct {
stake_common
candidateName string
bucketIndex uint64
payload []byte
}
func init() {
var err error
changeCandidateInterface, err := abi.JSON(strings.NewReader(_changeCandidateInterfaceABI))
if err != nil {
panic(err)
}
var ok bool
_changeCandidateMethod, ok = changeCandidateInterface.Methods["changeCandidate"]
if !ok {
panic("fail to load the method")
}
}
// NewChangeCandidate returns a ChangeCandidate instance
func NewChangeCandidate(
candName string,
bucketIndex uint64,
payload []byte,
) *ChangeCandidate {
return &ChangeCandidate{
candidateName: candName,
bucketIndex: bucketIndex,
payload: payload,
}
}
// Candidate returns the address of recipient
func (cc *ChangeCandidate) Candidate() string { return cc.candidateName }
// BucketIndex returns bucket index
func (cc *ChangeCandidate) BucketIndex() uint64 { return cc.bucketIndex }
// Payload returns the payload bytes
func (cc *ChangeCandidate) Payload() []byte { return cc.payload }
// Serialize returns a raw byte stream of the stake move action struct
func (cc *ChangeCandidate) Serialize() []byte {
return byteutil.Must(proto.Marshal(cc.Proto()))
}
func (act *ChangeCandidate) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_StakeChangeCandidate{StakeChangeCandidate: act.Proto()}
}
// Proto converts change candidate to protobuf
func (cc *ChangeCandidate) Proto() *iotextypes.StakeChangeCandidate {
act := &iotextypes.StakeChangeCandidate{
CandidateName: cc.candidateName,
BucketIndex: cc.bucketIndex,
Payload: cc.payload,
}
return act
}
// LoadProto loads change candidate from protobuf
func (cc *ChangeCandidate) LoadProto(pbAct *iotextypes.StakeChangeCandidate) error {
if pbAct == nil {
return ErrNilProto
}
cc.candidateName = pbAct.GetCandidateName()
cc.bucketIndex = pbAct.GetBucketIndex()
cc.payload = pbAct.GetPayload()
return nil
}
// IntrinsicGas returns the intrinsic gas of a ChangeCandidate
func (cc *ChangeCandidate) IntrinsicGas() (uint64, error) {
payloadSize := uint64(len(cc.Payload()))
return CalculateIntrinsicGas(MoveStakeBaseIntrinsicGas, MoveStakePayloadGas, payloadSize)
}
// SanityCheck validates the variables in the action
func (cc *ChangeCandidate) SanityCheck() error {
if !IsValidCandidateName(cc.candidateName) {
return ErrInvalidCanName
}
return nil
}
// EthData returns the ABI-encoded data for converting to eth tx
func (cc *ChangeCandidate) EthData() ([]byte, error) {
data, err := _changeCandidateMethod.Inputs.Pack(cc.candidateName, cc.bucketIndex, cc.payload)
if err != nil {
return nil, err
}
return append(_changeCandidateMethod.ID, data...), nil
}
// NewChangeCandidateFromABIBinary decodes data into ChangeCandidate action
func NewChangeCandidateFromABIBinary(data []byte) (*ChangeCandidate, error) {
var (
paramsMap = map[string]interface{}{}
ok bool
cc ChangeCandidate
)
// sanity check
if len(data) <= 4 || !bytes.Equal(_changeCandidateMethod.ID, data[:4]) {
return nil, errDecodeFailure
}
if err := _changeCandidateMethod.Inputs.UnpackIntoMap(paramsMap, data[4:]); err != nil {
return nil, err
}
if cc.candidateName, ok = paramsMap["candName"].(string); !ok {
return nil, errDecodeFailure
}
if cc.bucketIndex, ok = paramsMap["bucketIndex"].(uint64); !ok {
return nil, errDecodeFailure
}
if cc.payload, ok = paramsMap["data"].([]byte); !ok {
return nil, errDecodeFailure
}
return &cc, nil
}