-
Notifications
You must be signed in to change notification settings - Fork 203
/
dynasty.go
199 lines (174 loc) · 5.22 KB
/
dynasty.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
// Copyright (C) 2017-2019 go-nebulas authors
//
// This file is part of the go-nebulas library.
//
// the go-nebulas library 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.
//
// the go-nebulas library 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 the go-nebulas library. If not, see <http://www.gnu.org/licenses/>.
//
package dpos
import (
"io/ioutil"
"github.com/gogo/protobuf/proto"
"github.com/nebulasio/go-nebulas/common/trie"
"github.com/nebulasio/go-nebulas/core"
"github.com/nebulasio/go-nebulas/core/pb"
"github.com/nebulasio/go-nebulas/util/logging"
"github.com/sirupsen/logrus"
)
// Dynasty dpos dynasty
type Dynasty struct {
chain *core.BlockChain
genesisTimestamp int64
dynasty *corepb.Dynasty
tries map[int64]*trie.Trie
}
// NewDynasty create dynasty
func NewDynasty(neb core.Neblet) (*Dynasty, error) {
dynasty, err := loadDynastyConf(neb.Genesis(), neb.Config().Chain.Dynasty)
if err != nil {
return nil, err
}
tries := make(map[int64]*trie.Trie)
for _, v := range dynasty.Candidate {
dynastyTrie, err := trie.NewTrie(nil, neb.Storage(), false)
if err != nil {
return nil, err
}
for _, miner := range v.Dynasty {
addr, err := core.AddressParse(miner)
if err != nil {
return nil, err
}
v := addr.Bytes()
if _, err = dynastyTrie.Put(v, v); err != nil {
return nil, err
}
}
tries[int64(v.Serial)] = dynastyTrie
}
return &Dynasty{
chain: neb.BlockChain(),
dynasty: dynasty,
tries: tries,
}, nil
}
// loadDynastyConf ..
func loadDynastyConf(genesis *corepb.Genesis, filePath string) (*corepb.Dynasty, error) {
dynasty := &corepb.Dynasty{
Meta: &corepb.DynastyMeta{
ChainId: genesis.Meta.ChainId,
},
Candidate: []*corepb.DynastyCandidate{
{
Serial: GenesisDynastySerial,
Dynasty: genesis.Consensus.Dpos.Dynasty,
},
},
}
if len(filePath) > 0 {
conf, err := ioutil.ReadFile(filePath)
if err != nil {
logging.VLog().WithFields(logrus.Fields{
"err": err,
"filePath": filePath,
}).Error("Failed to load dynasty file.")
return nil, ErrFailedToLoadDynasty
}
content := string(conf)
dynastyConf := new(corepb.Dynasty)
if err = proto.UnmarshalText(content, dynastyConf); err != nil {
logging.VLog().WithFields(logrus.Fields{
"err": err,
"filePath": filePath,
}).Error("Failed to parse dynasty.conf.")
return nil, ErrFailedToParseDynasty
}
if dynastyConf.Meta.ChainId != genesis.Meta.ChainId {
logging.VLog().WithFields(logrus.Fields{
"GenesisChainId": genesis.Meta.ChainId,
"DynastyChainId": dynastyConf.Meta.ChainId,
}).Error("ChainId in dynasty.conf differs from that in genesis.conf.")
return nil, ErrCheckDynastyChainID
}
for _, v := range dynastyConf.Candidate {
if len(v.Dynasty) != len(genesis.Consensus.Dpos.Dynasty) {
logging.VLog().WithFields(logrus.Fields{
"Serial": v.Serial,
"dynasty": v.Dynasty,
"DynastySize": len(v.Dynasty),
}).Error("Miners count in dynasty.conf differs from that in genesis.conf.")
return nil, ErrCheckDynastyMinersCount
}
}
dynasty.Candidate = append(dynasty.Candidate, dynastyConf.Candidate...)
}
return dynasty, nil
}
// getDynastyTrie query dynasty trie
func (d *Dynasty) getDynasty(timestamp int64) (*trie.Trie, error) {
var (
interval int64
curDynasty int64
tmpDynasty int64
dt *trie.Trie
)
if d.genesisTimestamp == 0 {
curDynasty = GenesisDynasty
secondBlock := d.chain.GetBlockOnCanonicalChainByHeight(2)
if secondBlock != nil {
d.genesisTimestamp = secondBlock.Timestamp() - BlockIntervalInMs/SecondInMs
} else {
interval = BlockIntervalInMs
}
}
if d.genesisTimestamp > 0 {
// for the real genesis block, the timestamp is 0.
if timestamp < d.genesisTimestamp {
timestamp += d.genesisTimestamp
}
interval = (timestamp - d.genesisTimestamp) * SecondInMs
curDynasty = interval/DynastyIntervalInMs + 1
}
tmpDynasty = 0
// give a default dynasty trie
dt = d.tries[GenesisDynastySerial]
// eg: dynasty is: 1----3-----6, if serial={1,2} dynasty=1, serial={3,4,5}, dynasty=3
for k, v := range d.tries {
start := int64(k)
if start < curDynasty && start >= tmpDynasty && interval > start*DynastyIntervalInMs {
tmpDynasty = start
dt = v
}
}
if dt == nil {
logging.CLog().WithFields(logrus.Fields{
"genesis": d.genesisTimestamp,
"interval": interval,
"timestamp": timestamp,
"tmpDynasty": tmpDynasty,
"curDynasty": curDynasty,
}).Fatal("Failed to get dynasty with current genesis and dynasty.")
}
logging.VLog().WithFields(logrus.Fields{
"interval": interval,
"tmpDynasty": tmpDynasty,
"timestamp": timestamp,
"curDynasty": curDynasty,
"dt": dt,
}).Debug("dynasty info.")
dynastyTrie, err := dt.Clone()
if err != nil {
return nil, err
}
return dynastyTrie, nil
}