-
Notifications
You must be signed in to change notification settings - Fork 5
/
store.go
318 lines (257 loc) · 9 KB
/
store.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package store
import (
"bytes"
"encoding/binary"
"github.com/drep-project/DREP-Chain/common/trie"
"github.com/drep-project/DREP-Chain/crypto"
"github.com/drep-project/DREP-Chain/database"
"github.com/drep-project/DREP-Chain/database/dbinterface"
dlog "github.com/drep-project/DREP-Chain/pkgs/log"
"github.com/drep-project/DREP-Chain/types"
"math/big"
)
const (
MODULENAME = "store"
ChangeInterval = "changeInterval"
)
var (
log = dlog.EnsureLogger(MODULENAME)
)
type StoreInterface interface {
DeleteStorage(addr *crypto.CommonAddress) error
GetStorageAlias(addr *crypto.CommonAddress) string
AliasGet(alias string) (*crypto.CommonAddress, error)
AliasExist(alias string) bool
AliasSet(addr *crypto.CommonAddress, alias string) (err error)
GetBalance(addr *crypto.CommonAddress, height uint64) *big.Int
PutBalance(addr *crypto.CommonAddress, height uint64, balance *big.Int) error
AddBalance(addr *crypto.CommonAddress, height uint64, amount *big.Int) error
SubBalance(addr *crypto.CommonAddress, height uint64, amount *big.Int) error
GetNonce(addr *crypto.CommonAddress) uint64
PutNonce(addr *crypto.CommonAddress, nonce uint64) error
GetByteCode(addr *crypto.CommonAddress) []byte
GetCodeHash(addr *crypto.CommonAddress) crypto.Hash
PutByteCode(addr *crypto.CommonAddress, byteCode []byte) error
GetReputation(addr *crypto.CommonAddress) *big.Int
GetStateRoot() []byte
RecoverTrie(root []byte) bool
TrieDB() *trie.Database
Get(key []byte) ([]byte, error)
Put(key []byte, value []byte) error
Commit()
CopyState() *database.SnapShot
RevertState(shot *database.SnapShot)
Empty(addr *crypto.CommonAddress) bool
GetChangeInterval() (uint64, error)
//pos
GetCandidateAddrs() ([]crypto.CommonAddress, error)
GetVoteCreditCount(addr *crypto.CommonAddress) *big.Int
CancelVoteCredit(fromAddr, toAddr *crypto.CommonAddress, cancelBalance *big.Int, height uint64) (*types.IntersetDetail, error)
VoteCredit(addresses *crypto.CommonAddress, to *crypto.CommonAddress, addBalance *big.Int, height uint64) error
CandidateCredit(addresses *crypto.CommonAddress, addBalance *big.Int, data []byte, height uint64) error
CancelCandidateCredit(fromAddr *crypto.CommonAddress, cancelBalance *big.Int, height uint64) (*types.IntersetDetail, error)
GetCandidateData(addr *crypto.CommonAddress) ([]byte, error)
AddCandidateAddr(addr *crypto.CommonAddress) error
}
type Store struct {
stake *trieStakeStore
account *trieAccountStore
db *StoreDB
}
//Show interest rates in 3 months, 3-6 months, 6-12 months and more
func GetInterestRate() (threeMonth, sixMonth, oneYear, moreOneYear uint64) {
var rate uint64 = 0
rate = interestRate * 8
bigDiff := new(big.Int).SetUint64(threeMonthHeight * 100)
//小于3个月
threeMonth = bigDiff.Div(bigDiff, new(big.Int).SetUint64(rate)).Uint64()
threeMonth = threeMonth * (12 / 3) //Annualized interest rate
rate = interestRate * 4
bigDiff = new(big.Int).SetUint64(sixMonthHeight * 100)
//3-6个月
sixMonth = bigDiff.Div(bigDiff, new(big.Int).SetUint64(rate)).Uint64()
sixMonth = sixMonth * (12 / 6) //Annualized interest rate
rate = interestRate * 2
bigDiff = new(big.Int).SetUint64(oneYearHeight * 100)
//6 - 12个月
oneYear = bigDiff.Div(bigDiff, new(big.Int).SetUint64(rate)).Uint64()
oneYear = oneYear * (12 / 12) //Annualized interest rate
rate = interestRate
bigDiff = new(big.Int).SetUint64(oneYearHeight * 100)
//大于12个月
moreOneYear = bigDiff.Div(bigDiff, new(big.Int).SetUint64(rate)).Uint64()
return
}
func (s Store) AddCandidateAddr(addr *crypto.CommonAddress) error {
return s.stake.AddCandidateAddr(addr)
}
func (s Store) GetChangeInterval() (uint64, error) {
value, err := s.db.store.Get([]byte(ChangeInterval))
if err != nil {
log.Error("CancelCandidateCredit get change interval ", "err", err)
return 0, err
}
var changeInterval uint64
buf := bytes.NewBuffer(value)
err = binary.Read(buf, binary.BigEndian, &changeInterval)
if err != nil {
log.Error("CancelCandidateCredit parse change interval ", "err", err)
return 0, err
}
return changeInterval, nil
}
func (s Store) CancelCandidateCredit(fromAddr *crypto.CommonAddress, cancelBalance *big.Int, height uint64) (*types.IntersetDetail, error) {
ci, err := s.GetChangeInterval()
if err != nil {
return nil, err
}
return s.stake.CancelCandidateCredit(fromAddr, cancelBalance, height, ci)
}
func (s Store) GetCandidateData(addr *crypto.CommonAddress) ([]byte, error) {
return s.stake.GetCandidateData(addr)
}
func (s Store) GetCandidateAddrs() ([]crypto.CommonAddress, error) {
return s.stake.GetCandidateAddrs()
}
func (s Store) GetVoteCreditCount(addr *crypto.CommonAddress) *big.Int {
return s.stake.GetCreditCount(addr)
}
func (s Store) Empty(addr *crypto.CommonAddress) bool {
return s.Empty(addr)
}
func (s Store) GetStorageAlias(addr *crypto.CommonAddress) string {
return s.account.GetStorageAlias(addr)
}
func (s Store) DeleteStorage(addr *crypto.CommonAddress) error {
return s.account.DeleteStorage(addr)
}
func (s Store) AliasGet(alias string) (*crypto.CommonAddress, error) {
return s.account.AliasGet(alias)
}
func (s Store) AliasExist(alias string) bool {
return s.account.AliasExist(alias)
}
func (s Store) AddBalance(addr *crypto.CommonAddress, height uint64, amount *big.Int) error {
ci, err := s.GetChangeInterval()
if err != nil {
return err
}
voteCredit, err := s.stake.CancelCreditToBalance(addr, height, ci)
if err != nil {
return err
}
return s.account.AddBalance(addr, amount.Add(amount, voteCredit))
}
func (s Store) SubBalance(addr *crypto.CommonAddress, height uint64, amount *big.Int) error {
ci, err := s.GetChangeInterval()
if err != nil {
return err
}
voteCredit, err := s.stake.CancelCreditToBalance(addr, height, ci)
if err != nil {
return err
}
err = s.account.AddBalance(addr, voteCredit)
if err != nil {
return err
}
return s.account.SubBalance(addr, amount)
}
func (s Store) GetBalance(addr *crypto.CommonAddress, height uint64) *big.Int {
ci, err := s.GetChangeInterval()
if err != nil {
return nil
}
return new(big.Int).Add(s.stake.GetCancelCreditForBalance(addr, height, ci), s.account.GetBalance(addr))
}
func (s Store) PutBalance(addr *crypto.CommonAddress, height uint64, balance *big.Int) error {
if height != 0 {
ci, err := s.GetChangeInterval()
if err != nil {
return err
}
voteCredit, err := s.stake.CancelCreditToBalance(addr, height, ci)
if err != nil {
return err
}
err = s.account.AddBalance(addr, voteCredit)
if err != nil {
return err
}
}
return s.account.PutBalance(addr, balance)
}
func (s Store) GetNonce(addr *crypto.CommonAddress) uint64 {
return s.account.GetNonce(addr)
}
func (s Store) PutNonce(addr *crypto.CommonAddress, nonce uint64) error {
return s.account.PutNonce(addr, nonce)
}
func (s Store) GetByteCode(addr *crypto.CommonAddress) []byte {
return s.account.GetByteCode(addr)
}
func (s Store) GetCodeHash(addr *crypto.CommonAddress) crypto.Hash {
return s.account.GetCodeHash(addr)
}
func (s Store) PutByteCode(addr *crypto.CommonAddress, byteCode []byte) error {
return s.account.PutByteCode(addr, byteCode)
}
func (s Store) GetReputation(addr *crypto.CommonAddress) *big.Int {
return s.account.GetReputation(addr)
}
func (s Store) AliasSet(addr *crypto.CommonAddress, alias string) (err error) {
return s.account.AliasSet(addr, alias)
}
func (s Store) CancelVoteCredit(fromAddr, toAddr *crypto.CommonAddress, cancelBalance *big.Int, height uint64) (*types.IntersetDetail, error) {
ci, err := s.GetChangeInterval()
if err != nil {
return nil, err
}
return s.stake.CancelVoteCredit(fromAddr, toAddr, cancelBalance, height, ci)
}
func (s Store) VoteCredit(fromAddr *crypto.CommonAddress, to *crypto.CommonAddress, addBalance *big.Int, height uint64) error {
return s.stake.VoteCredit(fromAddr, to, addBalance, height)
}
func (s Store) CandidateCredit(fromAddr *crypto.CommonAddress, addBalance *big.Int, data []byte, height uint64) error {
return s.stake.CandidateCredit(fromAddr, addBalance, data, height)
}
func (s Store) TrieDB() *trie.Database {
return s.account.TrieDB()
}
func (s Store) Get(key []byte) ([]byte, error) {
return s.db.Get(key)
}
func (s Store) Put(key []byte, value []byte) error {
return s.db.Put(key, value)
}
func TrieStoreFromStore(diskDB dbinterface.KeyValueStore, stateRoot []byte) (StoreInterface, error) {
db := NewStoreDB(diskDB, nil, nil, trie.NewDatabaseWithCache(diskDB, 0))
store := &Store{
stake: NewStakeStorage(db),
account: NewTrieAccoutStore(db),
db: db,
}
if !store.RecoverTrie(stateRoot) {
return nil, ErrRecoverRoot
}
return store, nil
}
func (s *Store) RevertState(shot *database.SnapShot) {
s.db.RevertState(shot)
}
func (s Store) CopyState() *database.SnapShot {
return s.db.CopyState()
}
func (s Store) Commit() {
s.db.Flush()
}
func (s Store) GetStateRoot() []byte {
s.db.Flush()
return s.db.getStateRoot()
}
func (s *Store) RecoverTrie(root []byte) bool {
return s.db.RecoverTrie(root)
}
func (s *Store) GetCreditDetails(addr *crypto.CommonAddress) map[crypto.CommonAddress]big.Int {
return s.stake.GetCreditDetails(addr)
}