forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
staking.go
158 lines (142 loc) · 4.21 KB
/
staking.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
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package system
import (
"encoding/binary"
"errors"
"math/big"
"github.com/aergoio/aergo/state"
"github.com/aergoio/aergo/types"
)
var stakingKey = []byte("staking")
var stakingTotalKey = []byte("stakingtotal")
const StakingDelay = 60 * 60 * 24 //block interval
//const StakingDelay = 5
func staking(txBody *types.TxBody, sender, receiver *state.V,
scs *state.ContractState, blockNo types.BlockNo, context *SystemContext) (*types.Event, error) {
staked := context.Staked
beforeStaked := staked.GetAmountBigInt()
amount := txBody.GetAmountBigInt()
staked.Amount = new(big.Int).Add(beforeStaked, amount).Bytes()
staked.When = blockNo
if err := setStaking(scs, sender.ID(), staked); err != nil {
return nil, err
}
if err := addTotal(scs, amount); err != nil {
return nil, err
}
sender.SubBalance(amount)
receiver.AddBalance(amount)
return &types.Event{
ContractAddress: receiver.ID(),
EventIdx: 0,
EventName: "stake",
JsonArgs: `{"who":"` +
types.EncodeAddress(sender.ID()) +
`", "amount":"` + txBody.GetAmountBigInt().String() + `"}`,
}, nil
}
func unstaking(txBody *types.TxBody, sender, receiver *state.V, scs *state.ContractState,
blockNo types.BlockNo, context *SystemContext) (*types.Event, error) {
staked := context.Staked
amount := txBody.GetAmountBigInt()
var backToBalance *big.Int
if staked.GetAmountBigInt().Cmp(amount) < 0 {
amount = new(big.Int).SetUint64(0)
backToBalance = staked.GetAmountBigInt()
} else {
amount = new(big.Int).Sub(staked.GetAmountBigInt(), txBody.GetAmountBigInt())
backToBalance = txBody.GetAmountBigInt()
}
staked.Amount = amount.Bytes()
//blockNo will be updated in voting
staked.When = blockNo
if err := setStaking(scs, sender.ID(), staked); err != nil {
return nil, err
}
if err := refreshAllVote(txBody, scs, context); err != nil {
return nil, err
}
if err := subTotal(scs, backToBalance); err != nil {
return nil, err
}
sender.AddBalance(backToBalance)
receiver.SubBalance(backToBalance)
return &types.Event{
ContractAddress: receiver.ID(),
EventIdx: 0,
EventName: "unstake",
JsonArgs: `{"who":"` +
types.EncodeAddress(sender.ID()) +
`", "amount":"` + txBody.GetAmountBigInt().String() + `"}`,
}, nil
}
func setStaking(scs *state.ContractState, who []byte, staking *types.Staking) error {
key := append(stakingKey, who...)
return scs.SetData(key, serializeStaking(staking))
}
func getStaking(scs *state.ContractState, who []byte) (*types.Staking, error) {
key := append(stakingKey, who...)
data, err := scs.GetData(key)
if err != nil {
return nil, err
}
var staking types.Staking
if len(data) != 0 {
return deserializeStaking(data), nil
}
return &staking, nil
}
func GetStaking(scs *state.ContractState, address []byte) (*types.Staking, error) {
if address != nil {
return getStaking(scs, address)
}
return nil, errors.New("invalid argument: address should not be nil")
}
func GetTotal(ar AccountStateReader) (*big.Int, error) {
scs, err := ar.GetSystemAccountState()
if err != nil {
return nil, err
}
return GetStakingTotal(scs)
}
func GetStakingTotal(scs *state.ContractState) (*big.Int, error) {
data, err := scs.GetData(stakingTotalKey)
if err != nil {
return nil, err
}
return new(big.Int).SetBytes(data), nil
}
func addTotal(scs *state.ContractState, amount *big.Int) error {
data, err := scs.GetData(stakingTotalKey)
if err != nil {
return err
}
total := new(big.Int).SetBytes(data)
return scs.SetData(stakingTotalKey, new(big.Int).Add(total, amount).Bytes())
}
func subTotal(scs *state.ContractState, amount *big.Int) error {
data, err := scs.GetData(stakingTotalKey)
if err != nil {
return err
}
total := new(big.Int).SetBytes(data)
return scs.SetData(stakingTotalKey, new(big.Int).Sub(total, amount).Bytes())
}
func serializeStaking(v *types.Staking) []byte {
var ret []byte
if v != nil {
when := make([]byte, 8)
binary.LittleEndian.PutUint64(when, v.GetWhen())
ret = append(ret, when...)
ret = append(ret, v.GetAmount()...)
}
return ret
}
func deserializeStaking(data []byte) *types.Staking {
when := binary.LittleEndian.Uint64(data[:8])
amount := data[8:]
return &types.Staking{Amount: amount, When: when}
}