-
Notifications
You must be signed in to change notification settings - Fork 37
/
database.go
239 lines (208 loc) · 7.47 KB
/
database.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
// Copyright 2018 The Fractal Team Authors
// This file is part of the fractal project.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package dpos
import (
"encoding/json"
"fmt"
"math"
"math/big"
"strings"
"github.com/fractalplatform/fractal/types"
)
// IDB dpos database
type IDB interface {
SetCandidate(*CandidateInfo) error
DelCandidate(uint64, string) error
GetCandidate(uint64, string) (*CandidateInfo, error)
GetCandidates(uint64) (CandidateInfoArray, error)
CandidatesSize(uint64) (uint64, error)
SetActivatedCandidate(uint64, *CandidateInfo) error
GetActivatedCandidate(uint64) (*CandidateInfo, error)
SetAvailableQuantity(uint64, string, *big.Int) error
GetAvailableQuantity(uint64, string) (*big.Int, error)
SetVoter(*VoterInfo) error
GetVoter(uint64, string, string) (*VoterInfo, error)
GetVotersByVoter(uint64, string) ([]*VoterInfo, error)
GetVotersByCandidate(uint64, string) ([]*VoterInfo, error)
SetState(*GlobalState) error
GetState(uint64) (*GlobalState, error)
SetLastestEpoch(uint64) error
GetLastestEpoch() (uint64, error)
SetTakeOver(uint64) error
GetTakeOver() (uint64, error)
Undelegate(string, *big.Int) (*types.Action, error)
IncAsset2Acct(string, string, *big.Int) (*types.Action, error)
GetBalanceByTime(name string, timestamp uint64) (*big.Int, error)
GetCandidateInfoByTime(epoch uint64, name string, timestamp uint64) (*CandidateInfo, error)
}
type CandidateType uint64
const (
Normal CandidateType = iota
Freeze
Black
Jail
Unkown
)
// MarshalText returns the hex representation of a.
func (t CandidateType) MarshalText() ([]byte, error) {
return t.MarshalJSON()
}
// MarshalJSON returns the hex representation of a.
func (t CandidateType) MarshalJSON() ([]byte, error) {
str := "unkown"
switch t {
case Normal:
str = "normal"
case Freeze:
str = "freeze"
case Black:
str = "black"
case Jail:
str = "jail"
}
return json.Marshal(str)
}
// UnmarshalText parses a hash in syntax.
func (t *CandidateType) UnmarshalText(input []byte) error {
return t.UnmarshalJSON(input)
}
// UnmarshalJSON parses a type in syntax.
func (t *CandidateType) UnmarshalJSON(data []byte) error {
var val string
if err := json.Unmarshal(data, &val); err != nil {
return err
}
switch strings.ToLower(val) {
case "normal":
*t = Normal
case "freeze":
*t = Freeze
case "black":
*t = Black
case "jail":
*t = Jail
default:
*t = Unkown
}
return nil
}
// CandidateInfo info
type CandidateInfo struct {
Epoch uint64 `json:"epoch"`
Name string `json:"name"` // candidate name
URL string `json:"url"` // candidate url
Quantity *big.Int `json:"quantity"` // candidate stake quantity
TotalQuantity *big.Int `json:"totalQuantity"` // candidate total stake quantity
Number uint64 `json:"number"` // timestamp
Counter uint64 `json:"shouldCounter"`
ActualCounter uint64 `json:"actualCounter"`
Type CandidateType `json:"type"`
PrevKey string `json:"-"`
NextKey string `json:"-"`
}
func (candidateInfo *CandidateInfo) copy() *CandidateInfo {
return &CandidateInfo{
Epoch: candidateInfo.Epoch,
Name: candidateInfo.Name,
URL: candidateInfo.URL,
Quantity: candidateInfo.Quantity,
TotalQuantity: candidateInfo.TotalQuantity,
Number: candidateInfo.Number,
Counter: candidateInfo.Counter,
ActualCounter: candidateInfo.ActualCounter,
Type: candidateInfo.Type,
}
}
func (candidateInfo *CandidateInfo) invalid() bool {
return candidateInfo.Type != Normal
}
// VoterInfo info
type VoterInfo struct {
Epoch uint64 `json:"epoch"`
Name string `json:"name"` // voter name
Candidate string `json:"candidate"` // candidate approved by this voter
Quantity *big.Int `json:"quantity"` // stake approved by this voter
Number uint64 `json:"number"` // timestamp
NextKeyForVoter string `json:"-"`
NextKeyForCandidate string `json:"-"`
}
func (voter *VoterInfo) key() string {
return fmt.Sprintf("0x%x_%s_%s", voter.Epoch, voter.Name, voter.Candidate)
}
var (
InvalidIndex = uint64(math.MaxUint64)
)
// GlobalState dpos state
type GlobalState struct {
Epoch uint64 `json:"epoch"` // epoch
PreEpoch uint64 `json:"preEpoch"` // epoch
ActivatedCandidateSchedule []string `json:"activatedCandidateSchedule"` // candidates
ActivatedTotalQuantity *big.Int `json:"activatedTotalQuantity"` // the sum of activate candidate votes
BadCandidateIndexSchedule []uint64 `json:"badCandidateIndexSchedule"` // activated backup candidates
UsingCandidateIndexSchedule []uint64 `json:"usingCandidateIndexSchedule"` // activated backup candidates
TotalQuantity *big.Int `json:"totalQuantity"` // the sum of all candidate votes
TakeOver bool `json:"takeOver"` // systemio take over dpos
Dpos bool `json:"dpos"` // dpos status
Number uint64 `json:"number"` // timestamp
}
// ArrayCandidateInfoForBrowser dpos state
type ArrayCandidateInfoForBrowser struct {
Data []*CandidateInfoForBrowser `json:"data"`
BadCandidateIndexSchedule []uint64 `json:"bad"`
UsingCandidateIndexSchedule []uint64 `json:"using"`
TakeOver bool `json:"takeOver"`
Dpos bool `json:"dpos"`
}
// CandidateInfoForBrowser dpos state
type CandidateInfoForBrowser struct {
Candidate string `json:"candidate"`
Holder string `json:"holder"`
Quantity string `json:"quantity"`
TotalQuantity string `json:"totalQuantity"`
Counter uint64 `json:"shouldCounter"`
ActualCounter uint64 `json:"actualCounter"`
NowCounter uint64 `json:"nowShouldCounter"`
NowActualCounter uint64 `json:"nowActualCounter"`
// Status uint64 `json:"status"` //0:die 1:activate 2:spare
}
// CandidateInfoArray array of candidate
type CandidateInfoArray []*CandidateInfo
type Epochs struct {
Data []*Epoch `json:"data"`
}
type Epoch struct {
Start uint64 `json:"start"`
Epoch uint64 `json:"epoch"`
}
func (prods CandidateInfoArray) Len() int {
return len(prods)
}
func (prods CandidateInfoArray) Less(i, j int) bool {
return more(prods[i], prods[j])
}
func (prods CandidateInfoArray) Swap(i, j int) {
prods[i], prods[j] = prods[j], prods[i]
}
func more(frist *CandidateInfo, second *CandidateInfo) bool {
val := frist.TotalQuantity.Cmp(second.TotalQuantity)
if val == 0 {
if frist.Number == second.Number {
return strings.Compare(frist.Name, second.Name) > 0
}
return frist.Number < second.Number
}
return val > 0
}