-
Notifications
You must be signed in to change notification settings - Fork 16
/
withdraw.go
151 lines (118 loc) · 3.78 KB
/
withdraw.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
package staking
import (
"encoding/json"
"github.com/tendermint/tendermint/libs/kv"
"github.com/pkg/errors"
"github.com/Oneledger/protocol/action"
"github.com/Oneledger/protocol/data/evidence"
"github.com/Oneledger/protocol/data/keys"
)
var _ action.Msg = &Withdraw{}
type Withdraw struct {
ValidatorAddress keys.Address
StakeAddress keys.Address
Stake action.Amount
}
func (s Withdraw) Marshal() ([]byte, error) {
return json.Marshal(s)
}
func (s *Withdraw) Unmarshal(data []byte) error {
return json.Unmarshal(data, s)
}
func (s Withdraw) Signers() []action.Address {
return []action.Address{s.StakeAddress.Bytes(), s.ValidatorAddress.Bytes()}
}
func (s Withdraw) Type() action.Type {
return action.WITHDRAW
}
func (s Withdraw) Tags() kv.Pairs {
tags := make([]kv.Pair, 0)
tag := kv.Pair{
Key: []byte("tx.type"),
Value: []byte(s.Type().String()),
}
tag2 := kv.Pair{
Key: []byte("tx.validator"),
Value: s.ValidatorAddress.Bytes(),
}
tag3 := kv.Pair{
Key: []byte("tx.delegator"),
Value: s.StakeAddress.Bytes(),
}
tag4 := kv.Pair{
Key: []byte("tx.amount"),
Value: s.Stake.Value.BigInt().Bytes(),
}
tags = append(tags, tag, tag2, tag3, tag4)
return tags
}
var _ action.Tx = withdrawTx{}
type withdrawTx struct {
}
func (withdrawTx) Validate(ctx *action.Context, tx action.SignedTx) (bool, error) {
draw := &Withdraw{}
err := draw.Unmarshal(tx.Data)
if err != nil {
return false, errors.Wrap(action.ErrWrongTxType, err.Error())
}
//validate basic signature
err = action.ValidateBasic(tx.RawBytes(), draw.Signers(), tx.Signatures)
if err != nil {
return false, err
}
err = action.ValidateFee(ctx.FeePool.GetOpt(), tx.Fee)
if err != nil {
return false, err
}
if err := draw.StakeAddress.Err(); err != nil {
return false, err
}
if err := draw.ValidatorAddress.Err(); err != nil {
return false, err
}
val, err := ctx.Validators.Get(draw.ValidatorAddress)
if err == nil && !val.StakeAddress.Equal(draw.StakeAddress) {
return false, action.ErrStakeAddressMismatch
}
coin := draw.Stake.ToCoinWithBase(ctx.Currencies)
if coin.LessThanEqualCoin(coin.Currency.NewCoinFromInt(0)) {
return false, action.ErrInvalidAmount
}
if coin.Currency.Name != "OLT" {
return false, action.ErrInvalidAmount
}
return true, nil
}
func (s withdrawTx) ProcessCheck(ctx *action.Context, tx action.RawTx) (ok bool, result action.Response) {
ctx.Logger.Debug("Processing Withdraw Transaction for CheckTx", tx)
ok, result = runWithdraw(ctx, tx)
return
}
func (s withdrawTx) ProcessDeliver(ctx *action.Context, tx action.RawTx) (ok bool, result action.Response) {
ctx.Logger.Debug("Processing Withdraw Transaction for DeliverTx", tx)
ok, result = runWithdraw(ctx, tx)
return
}
func (withdrawTx) ProcessFee(ctx *action.Context, signedTx action.SignedTx, start action.Gas, size action.Gas, gasUsed action.Gas) (bool, action.Response) {
return action.BasicFeeHandling(ctx, signedTx, start, size, 2)
}
func runWithdraw(ctx *action.Context, tx action.RawTx) (bool, action.Response) {
draw := &Withdraw{}
err := draw.Unmarshal(tx.Data)
if err != nil {
return false, action.Response{Log: err.Error()}
}
if ctx.EvidenceStore.IsFrozenValidator(draw.ValidatorAddress) {
return false, action.Response{Log: evidence.ErrFrozenValidator.Error()}
}
coin := draw.Stake.ToCoinWithBase(ctx.Currencies)
err = ctx.Delegators.Withdraw(draw.ValidatorAddress, draw.StakeAddress, draw.Stake.Value)
if err != nil {
return false, action.Response{Log: errors.Wrap(err, draw.StakeAddress.String()).Error()}
}
err = ctx.Balances.AddToAddress(draw.StakeAddress, coin)
if err != nil {
return false, action.Response{Log: errors.Wrap(err, "add to balance").Error()}
}
return true, action.Response{Events: action.GetEvent(draw.Tags(), "apply_withdraw"), Info: coin.String()}
}