/
stake_reclaim.go
152 lines (129 loc) · 4.19 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
// Copyright (c) 2020 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. 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 (
"math/big"
"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)
)
// 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 errors.New("empty action proto to load")
}
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
}
// 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
}