/
bucket_pool.go
154 lines (131 loc) · 4.49 KB
/
bucket_pool.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
// Copyright (c) 2022 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 staking
import (
"math/big"
"github.com/iotexproject/go-pkgs/hash"
"google.golang.org/protobuf/proto"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/action/protocol/staking/stakingpb"
"github.com/iotexproject/iotex-core/state"
)
// const
const (
_stakingBucketPool = "bucketPool"
)
var (
_bucketPoolAddr = hash.Hash160b([]byte(_stakingBucketPool))
_bucketPoolAddrKey = append([]byte{_const}, _bucketPoolAddr[:]...)
)
// when a bucket is created, the amount of staked IOTX token is deducted from user, but does not transfer to any address
// in the same way when a bucket are withdrawn, bucket amount is added back to user, but does not come out of any address
//
// for better accounting/auditing, we take protocol's address as the 'bucket pool' address
// 1. at Greenland height we sum up all existing bucket's amount and set the total amount to bucket pool address
// 2. for future bucket creation/deposit/registration, the amount of staked IOTX token will be added to bucket pool (so
// the pool is 'receiving' token)
// 3. for future bucket withdrawal, the bucket amount will be deducted from bucket pool (so the pool is 'releasing' token)
type (
// BucketPool implements the bucket pool
BucketPool struct {
enableSMStorage bool
total *totalAmount
}
totalAmount struct {
amount *big.Int
count uint64
}
)
func (t *totalAmount) Serialize() ([]byte, error) {
gen := stakingpb.TotalAmount{
Amount: t.amount.String(),
Count: t.count,
}
return proto.Marshal(&gen)
}
func (t *totalAmount) Deserialize(data []byte) error {
gen := stakingpb.TotalAmount{}
if err := proto.Unmarshal(data, &gen); err != nil {
return err
}
var ok bool
if t.amount, ok = new(big.Int).SetString(gen.Amount, 10); !ok {
return state.ErrStateDeserialization
}
if t.amount.Cmp(big.NewInt(0)) == -1 {
return state.ErrNotEnoughBalance
}
t.count = gen.Count
return nil
}
func (t *totalAmount) AddBalance(amount *big.Int, newBucket bool) {
t.amount.Add(t.amount, amount)
if newBucket {
t.count++
}
}
func (t *totalAmount) SubBalance(amount *big.Int) error {
if amount.Cmp(t.amount) == 1 || t.count == 0 {
return state.ErrNotEnoughBalance
}
t.amount.Sub(t.amount, amount)
t.count--
return nil
}
// Total returns the total amount staked in bucket pool
func (bp *BucketPool) Total() *big.Int {
return new(big.Int).Set(bp.total.amount)
}
// Count returns the total number of buckets in bucket pool
func (bp *BucketPool) Count() uint64 {
return bp.total.count
}
// Copy returns a copy of the bucket pool
func (bp *BucketPool) Copy(enableSMStorage bool) *BucketPool {
pool := BucketPool{}
pool.enableSMStorage = enableSMStorage
pool.total = &totalAmount{
amount: new(big.Int).Set(bp.total.amount),
count: bp.total.count,
}
return &pool
}
// Sync syncs the data from state manager
func (bp *BucketPool) Sync(sm protocol.StateManager) error {
if bp.enableSMStorage {
_, err := sm.State(bp.total, protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
return err
}
// get stashed total amount
err := sm.Unload(_protocolID, _stakingBucketPool, bp.total)
if err != nil && err != protocol.ErrNoName {
return err
}
return nil
}
// Commit is called upon workingset commit
func (bp *BucketPool) Commit(sr protocol.StateReader) error {
return nil
}
// CreditPool subtracts staked amount out of the pool
func (bp *BucketPool) CreditPool(sm protocol.StateManager, amount *big.Int) error {
if err := bp.total.SubBalance(amount); err != nil {
return err
}
if bp.enableSMStorage {
_, err := sm.PutState(bp.total, protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
return err
}
return sm.Load(_protocolID, _stakingBucketPool, bp.total)
}
// DebitPool adds staked amount into the pool
func (bp *BucketPool) DebitPool(sm protocol.StateManager, amount *big.Int, newBucket bool) error {
bp.total.AddBalance(amount, newBucket)
if bp.enableSMStorage {
_, err := sm.PutState(bp.total, protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey))
return err
}
return sm.Load(_protocolID, _stakingBucketPool, bp.total)
}