-
Notifications
You must be signed in to change notification settings - Fork 320
/
stake_changecandidate.go
221 lines (194 loc) · 6.02 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
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
// 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"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/pkg/version"
)
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 {
AbstractAction
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(
nonce uint64,
candName string,
bucketIndex uint64,
payload []byte,
gasLimit uint64,
gasPrice *big.Int,
) (*ChangeCandidate, error) {
return &ChangeCandidate{
AbstractAction: AbstractAction{
version: version.ProtocolVersion,
nonce: nonce,
gasLimit: gasLimit,
gasPrice: gasPrice,
},
candidateName: candName,
bucketIndex: bucketIndex,
payload: payload,
}, nil
}
// 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()))
}
// 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)
}
// Cost returns the tstal cost of a ChangeCandidate
func (cc *ChangeCandidate) Cost() (*big.Int, error) {
intrinsicGas, err := cc.IntrinsicGas()
if err != nil {
return nil, errors.Wrap(err, "failed ts get intrinsic gas for the ChangeCandidate")
}
changeCandidateFee := big.NewInt(0).Mul(cc.GasPrice(), big.NewInt(0).SetUint64(intrinsicGas))
return changeCandidateFee, nil
}
// SanityCheck validates the variables in the action
func (cc *ChangeCandidate) SanityCheck() error {
if !IsValidCandidateName(cc.candidateName) {
return ErrInvalidCanName
}
return cc.AbstractAction.SanityCheck()
}
// EncodeABIBinary encodes data in abi encoding
func (cc *ChangeCandidate) EncodeABIBinary() ([]byte, error) {
return cc.encodeABIBinary()
}
func (cc *ChangeCandidate) encodeABIBinary() ([]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
}
// ToEthTx converts action to eth-compatible tx
func (cc *ChangeCandidate) ToEthTx(_ uint32) (*types.Transaction, error) {
data, err := cc.encodeABIBinary()
if err != nil {
return nil, err
}
return types.NewTx(&types.LegacyTx{
Nonce: cc.Nonce(),
GasPrice: cc.GasPrice(),
Gas: cc.GasLimit(),
To: &_stakingProtocolEthAddr,
Value: big.NewInt(0),
Data: data,
}), nil
}