-
Notifications
You must be signed in to change notification settings - Fork 328
/
stake_restake.go
192 lines (167 loc) · 4.81 KB
/
stake_restake.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
// 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"
"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 (
// RestakePayloadGas represents the Restake payload gas per uint
RestakePayloadGas = uint64(100)
// RestakeBaseIntrinsicGas represents the base intrinsic gas for stake again
RestakeBaseIntrinsicGas = uint64(10000)
_restakeInterfaceABI = `[
{
"inputs": [
{
"internalType": "uint64",
"name": "bucketIndex",
"type": "uint64"
},
{
"internalType": "uint32",
"name": "duration",
"type": "uint32"
},
{
"internalType": "bool",
"name": "autoStake",
"type": "bool"
},
{
"internalType": "uint8[]",
"name": "data",
"type": "uint8[]"
}
],
"name": "restake",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]`
)
var (
// _restakeMethod is the interface of the abi encoding of stake action
_restakeMethod abi.Method
_ EthCompatibleAction = (*Restake)(nil)
)
// Restake defines the action of stake again
type Restake struct {
stake_common
bucketIndex uint64
duration uint32
autoStake bool
payload []byte
}
func init() {
restakeInterface, err := abi.JSON(strings.NewReader(_restakeInterfaceABI))
if err != nil {
panic(err)
}
var ok bool
_restakeMethod, ok = restakeInterface.Methods["restake"]
if !ok {
panic("fail to load the method")
}
}
// NewRestake returns a Restake instance
func NewRestake(
index uint64,
duration uint32,
autoStake bool,
payload []byte,
) *Restake {
return &Restake{
bucketIndex: index,
duration: duration,
autoStake: autoStake,
payload: payload,
}
}
// Payload returns the payload bytes
func (rs *Restake) Payload() []byte { return rs.payload }
// BucketIndex returns bucket index
func (rs *Restake) BucketIndex() uint64 { return rs.bucketIndex }
// Duration returns the updated duration
func (rs *Restake) Duration() uint32 { return rs.duration }
// AutoStake returns the autoStake boolean
func (rs *Restake) AutoStake() bool { return rs.autoStake }
// Serialize returns a raw byte stream of the Stake again struct
func (rs *Restake) Serialize() []byte {
return byteutil.Must(proto.Marshal(rs.Proto()))
}
func (act *Restake) FillAction(core *iotextypes.ActionCore) {
core.Action = &iotextypes.ActionCore_StakeRestake{StakeRestake: act.Proto()}
}
// Proto converts to protobuf Restake Action
func (rs *Restake) Proto() *iotextypes.StakeRestake {
act := &iotextypes.StakeRestake{
BucketIndex: rs.bucketIndex,
Payload: rs.payload,
StakedDuration: rs.duration,
AutoStake: rs.autoStake,
}
return act
}
// LoadProto converts a protobuf's Action to Restake
func (rs *Restake) LoadProto(pbAct *iotextypes.StakeRestake) error {
if pbAct == nil {
return ErrNilProto
}
rs.bucketIndex = pbAct.GetBucketIndex()
rs.payload = pbAct.GetPayload()
rs.duration = pbAct.GetStakedDuration()
rs.autoStake = pbAct.GetAutoStake()
return nil
}
// IntrinsicGas returns the intrinsic gas of a Restake
func (rs *Restake) IntrinsicGas() (uint64, error) {
payloadSize := uint64(len(rs.Payload()))
return CalculateIntrinsicGas(RestakeBaseIntrinsicGas, RestakePayloadGas, payloadSize)
}
func (rs *Restake) SanityCheck() error {
return nil
}
// EthData returns the ABI-encoded data for converting to eth tx
func (rs *Restake) EthData() ([]byte, error) {
data, err := _restakeMethod.Inputs.Pack(rs.bucketIndex, rs.duration, rs.autoStake, rs.payload)
if err != nil {
return nil, err
}
return append(_restakeMethod.ID, data...), nil
}
// NewRestakeFromABIBinary decodes data into Restake action
func NewRestakeFromABIBinary(data []byte) (*Restake, error) {
var (
paramsMap = map[string]interface{}{}
ok bool
rs Restake
)
// sanity check
if len(data) <= 4 || !bytes.Equal(_restakeMethod.ID, data[:4]) {
return nil, errDecodeFailure
}
if err := _restakeMethod.Inputs.UnpackIntoMap(paramsMap, data[4:]); err != nil {
return nil, err
}
if rs.bucketIndex, ok = paramsMap["bucketIndex"].(uint64); !ok {
return nil, errDecodeFailure
}
if rs.duration, ok = paramsMap["duration"].(uint32); !ok {
return nil, errDecodeFailure
}
if rs.autoStake, ok = paramsMap["autoStake"].(bool); !ok {
return nil, errDecodeFailure
}
if rs.payload, ok = paramsMap["data"].([]byte); !ok {
return nil, errDecodeFailure
}
return &rs, nil
}