-
Notifications
You must be signed in to change notification settings - Fork 319
/
stake_reclaim.go
323 lines (289 loc) · 8.34 KB
/
stake_reclaim.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright (c) 2020 IoTeX Foundation
// 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 (
"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 (
// ReclaimStakePayloadGas represents the stake reclaim payload gas per uint
ReclaimStakePayloadGas = uint64(100)
// ReclaimStakeBaseIntrinsicGas represents the base intrinsic gas for stake reclaim
ReclaimStakeBaseIntrinsicGas = uint64(10000)
_reclaimStakeInterfaceABI = `[
{
"inputs": [
{
"internalType": "uint64",
"name": "bucketIndex",
"type": "uint64"
},
{
"internalType": "uint8[]",
"name": "data",
"type": "uint8[]"
}
],
"name": "unstake",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint64",
"name": "bucketIndex",
"type": "uint64"
},
{
"internalType": "uint8[]",
"name": "data",
"type": "uint8[]"
}
],
"name": "withdrawStake",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]`
)
var (
// _unstakeMethod is the interface of the abi encoding of unstake action
_unstakeMethod abi.Method
// _withdrawStakeMethod is the interface of the abi encoding of withdrawStake action
_withdrawStakeMethod abi.Method
_ EthCompatibleAction = (*Unstake)(nil)
_ EthCompatibleAction = (*WithdrawStake)(nil)
)
func init() {
reclaimStakeInterface, err := abi.JSON(strings.NewReader(_reclaimStakeInterfaceABI))
if err != nil {
panic(err)
}
var ok bool
_unstakeMethod, ok = reclaimStakeInterface.Methods["unstake"]
if !ok {
panic("fail to load the method")
}
_withdrawStakeMethod, ok = reclaimStakeInterface.Methods["withdrawStake"]
if !ok {
panic("fail to load the method")
}
}
// reclaimStake defines the action of stake restake/withdraw
type reclaimStake struct {
AbstractAction
bucketIndex uint64
payload []byte
}
// BucketIndex returns bucket index
func (sr *reclaimStake) BucketIndex() uint64 { return sr.bucketIndex }
// Payload returns the payload bytes
func (sr *reclaimStake) Payload() []byte { return sr.payload }
// Serialize returns a raw byte stream of the stake reclaim action struct
func (sr *reclaimStake) Serialize() []byte {
return byteutil.Must(proto.Marshal(sr.Proto()))
}
// Proto converts to protobuf stake reclaim action struct
func (sr *reclaimStake) Proto() *iotextypes.StakeReclaim {
act := &iotextypes.StakeReclaim{
BucketIndex: sr.bucketIndex,
Payload: sr.payload,
}
return act
}
// LoadProto converts a protobuf's Action to reclaimStake
func (sr *reclaimStake) LoadProto(pbAct *iotextypes.StakeReclaim) error {
if pbAct == nil {
return ErrNilProto
}
sr.bucketIndex = pbAct.GetBucketIndex()
sr.payload = pbAct.GetPayload()
return nil
}
// Unstake defines the action of unstake
type Unstake struct {
reclaimStake
}
// NewUnstake returns a Unstake instance
func NewUnstake(
nonce uint64,
bucketIndex uint64,
payload []byte,
gasLimit uint64,
gasPrice *big.Int,
) (*Unstake, error) {
return &Unstake{
reclaimStake{
AbstractAction: AbstractAction{
version: version.ProtocolVersion,
nonce: nonce,
gasLimit: gasLimit,
gasPrice: gasPrice,
},
bucketIndex: bucketIndex,
payload: payload,
},
}, nil
}
// IntrinsicGas returns the intrinsic gas of a Unstake
func (su *Unstake) IntrinsicGas() (uint64, error) {
payloadSize := uint64(len(su.Payload()))
return CalculateIntrinsicGas(ReclaimStakeBaseIntrinsicGas, ReclaimStakePayloadGas, payloadSize)
}
// Cost returns the total cost of a Unstake
func (su *Unstake) Cost() (*big.Int, error) {
intrinsicGas, err := su.IntrinsicGas()
if err != nil {
return nil, errors.Wrap(err, "failed to get intrinsic gas for the unstake")
}
unstakeFee := big.NewInt(0).Mul(su.GasPrice(), big.NewInt(0).SetUint64(intrinsicGas))
return unstakeFee, nil
}
// EncodeABIBinary encodes data in abi encoding
func (su *Unstake) EncodeABIBinary() ([]byte, error) {
return su.encodeABIBinary()
}
func (su *Unstake) encodeABIBinary() ([]byte, error) {
data, err := _unstakeMethod.Inputs.Pack(su.bucketIndex, su.payload)
if err != nil {
return nil, err
}
return append(_unstakeMethod.ID, data...), nil
}
// NewUnstakeFromABIBinary decodes data into WithdrawStake action
func NewUnstakeFromABIBinary(data []byte) (*Unstake, error) {
var (
paramsMap = map[string]interface{}{}
ok bool
su Unstake
)
// sanity check
if len(data) <= 4 || !bytes.Equal(_unstakeMethod.ID, data[:4]) {
return nil, errDecodeFailure
}
if err := _unstakeMethod.Inputs.UnpackIntoMap(paramsMap, data[4:]); err != nil {
return nil, err
}
if su.bucketIndex, ok = paramsMap["bucketIndex"].(uint64); !ok {
return nil, errDecodeFailure
}
if su.payload, ok = paramsMap["data"].([]byte); !ok {
return nil, errDecodeFailure
}
return &su, nil
}
// ToEthTx converts action to eth-compatible tx
func (su *Unstake) ToEthTx(_ uint32) (*types.Transaction, error) {
data, err := su.encodeABIBinary()
if err != nil {
return nil, err
}
return types.NewTx(&types.LegacyTx{
Nonce: su.Nonce(),
GasPrice: su.GasPrice(),
Gas: su.GasLimit(),
To: &_stakingProtocolEthAddr,
Value: big.NewInt(0),
Data: data,
}), nil
}
// WithdrawStake defines the action of stake withdraw
type WithdrawStake struct {
reclaimStake
}
// NewWithdrawStake returns a WithdrawStake instance
func NewWithdrawStake(
nonce uint64,
bucketIndex uint64,
payload []byte,
gasLimit uint64,
gasPrice *big.Int,
) (*WithdrawStake, error) {
return &WithdrawStake{
reclaimStake{
AbstractAction: AbstractAction{
version: version.ProtocolVersion,
nonce: nonce,
gasLimit: gasLimit,
gasPrice: gasPrice,
},
bucketIndex: bucketIndex,
payload: payload,
},
}, nil
}
// IntrinsicGas returns the intrinsic gas of a WithdrawStake
func (sw *WithdrawStake) IntrinsicGas() (uint64, error) {
payloadSize := uint64(len(sw.Payload()))
return CalculateIntrinsicGas(ReclaimStakeBaseIntrinsicGas, ReclaimStakePayloadGas, payloadSize)
}
// Cost returns the total cost of a WithdrawStake
func (sw *WithdrawStake) Cost() (*big.Int, error) {
intrinsicGas, err := sw.IntrinsicGas()
if err != nil {
return nil, errors.Wrap(err, "failed to get intrinsic gas for the WithdrawStake")
}
withdrawFee := big.NewInt(0).Mul(sw.GasPrice(), big.NewInt(0).SetUint64(intrinsicGas))
return withdrawFee, nil
}
// EncodeABIBinary encodes data in abi encoding
func (sw *WithdrawStake) EncodeABIBinary() ([]byte, error) {
return sw.encodeABIBinary()
}
func (sw *WithdrawStake) encodeABIBinary() ([]byte, error) {
data, err := _withdrawStakeMethod.Inputs.Pack(sw.bucketIndex, sw.payload)
if err != nil {
return nil, err
}
return append(_withdrawStakeMethod.ID, data...), nil
}
// NewWithdrawStakeFromABIBinary decodes data into WithdrawStake action
func NewWithdrawStakeFromABIBinary(data []byte) (*WithdrawStake, error) {
var (
paramsMap = map[string]interface{}{}
ok bool
sw WithdrawStake
)
// sanity check
if len(data) <= 4 || !bytes.Equal(_withdrawStakeMethod.ID, data[:4]) {
return nil, errDecodeFailure
}
if err := _withdrawStakeMethod.Inputs.UnpackIntoMap(paramsMap, data[4:]); err != nil {
return nil, err
}
if sw.bucketIndex, ok = paramsMap["bucketIndex"].(uint64); !ok {
return nil, errDecodeFailure
}
if sw.payload, ok = paramsMap["data"].([]byte); !ok {
return nil, errDecodeFailure
}
return &sw, nil
}
// ToEthTx converts action to eth-compatible tx
func (sw *WithdrawStake) ToEthTx(_ uint32) (*types.Transaction, error) {
data, err := sw.encodeABIBinary()
if err != nil {
return nil, err
}
return types.NewTx(&types.LegacyTx{
Nonce: sw.Nonce(),
GasPrice: sw.GasPrice(),
Gas: sw.GasLimit(),
To: &_stakingProtocolEthAddr,
Value: big.NewInt(0),
Data: data,
}), nil
}