Skip to content

Commit f7c5cee

Browse files
committed
update for consensus
1 parent 81c2068 commit f7c5cee

File tree

4 files changed

+67
-62
lines changed

4 files changed

+67
-62
lines changed

consensus/chain_rw.go

+2-29
Original file line numberDiff line numberDiff line change
@@ -221,33 +221,6 @@ func (self *chainRw) checkSnapshotHashValid(startHeight uint64, startHash types.
221221
return nil
222222
}
223223

224-
func (self *chainRw) groupSnapshotBySeedExist(blocks []*ledger.SnapshotBlock) ([]*ledger.SnapshotBlock, []*ledger.SnapshotBlock) {
225-
var exists []*ledger.SnapshotBlock
226-
var notExists []*ledger.SnapshotBlock
227-
228-
for _, v := range blocks {
229-
if v.SeedHash != nil {
230-
exists = append(exists, v)
231-
} else {
232-
notExists = append(notExists, v)
233-
}
234-
}
235-
236-
return exists, notExists
237-
}
238-
239-
func (self *chainRw) getSeed(top *ledger.SnapshotBlock, prev *ledger.SnapshotBlock) uint64 {
240-
seedHash := prev.SeedHash
241-
if seedHash == nil {
242-
return 0
243-
}
244-
expectedSeedHash := ledger.ComputeSeedHash(top.Seed, prev.PrevHash, prev.Timestamp)
245-
if expectedSeedHash == *seedHash {
246-
return prev.Seed
247-
}
248-
return 0
249-
}
250-
251224
// an hour = 48 * period
252225
func (self *chainRw) GetSuccessRateByHour(index uint64) (map[types.Address]int32, error) {
253226
result := make(map[types.Address]int32)
@@ -257,7 +230,7 @@ func (self *chainRw) GetSuccessRateByHour(index uint64) (map[types.Address]int32
257230
break
258231
}
259232
tmpIndex := index - i
260-
p, err := self.periodPoints.GetByHeight(tmpIndex)
233+
p, err := self.periodPoints.GetByIndex(tmpIndex)
261234
if err != nil {
262235
return nil, err
263236
}
@@ -289,7 +262,7 @@ func (self *chainRw) GetSuccessRateByHour2(index uint64) (map[types.Address]*con
289262
break
290263
}
291264
tmpIndex := index - i
292-
p, err := self.periodPoints.GetByHeight(tmpIndex)
265+
p, err := self.periodPoints.GetByIndex(tmpIndex)
293266
if err != nil {
294267
return nil, err
295268
}

consensus/committee.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,11 @@ func (self *committee) update(gid types.Gid, t DposReader, m *sync.Map) {
207207

208208
if err != nil {
209209
self.mLog.Error("can't get election result. time is "+time.Now().Format(time.RFC3339Nano)+"\".", "err", err)
210-
time.Sleep(time.Second)
210+
select {
211+
case <-self.closed:
212+
return
213+
case <-time.After(time.Second):
214+
}
211215
// error handle
212216
continue
213217
}
@@ -296,11 +300,13 @@ func (self *committee) eventAll(e *subscribeEvent, result *electionResult, voteT
296300
if sub+time.Second < 0 {
297301
continue
298302
}
299-
300303
if sub > time.Millisecond*10 {
301-
time.Sleep(sub)
304+
select {
305+
case <-self.closed:
306+
return
307+
case <-time.After(sub):
308+
}
302309
}
303-
304310
e.fn(newConsensusEvent(result, p, e.gid, voteTime))
305311
}
306312
}
@@ -313,7 +319,11 @@ func (self *committee) eventAddr(e *subscribeEvent, result *electionResult, vote
313319
continue
314320
}
315321
if sub > time.Millisecond*10 {
316-
time.Sleep(sub)
322+
select {
323+
case <-self.closed:
324+
return
325+
case <-time.After(sub):
326+
}
317327
}
318328
e.fn(newConsensusEvent(result, p, e.gid, voteTime))
319329
}

consensus/consensus_chain.go renamed to consensus/consensus_point_array.go

+25-28
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func newPeriodLinkedArray() {
3232
}
3333

3434
type LinkedArray interface {
35-
GetByHeight(height uint64) (*consensus_db.Point, error)
35+
GetByIndex(index uint64) (*consensus_db.Point, error)
3636
}
3737

3838
type linkedArray struct {
@@ -42,24 +42,24 @@ type linkedArray struct {
4242
lowerArr LinkedArray
4343
}
4444

45-
func (self *linkedArray) GetByHeight(height uint64) (*consensus_db.Point, error) {
46-
point, err := self.db.GetPointByHeight(self.prefix, height)
45+
func (self *linkedArray) GetByIndex(index uint64) (*consensus_db.Point, error) {
46+
point, err := self.db.GetPointByHeight(self.prefix, index)
4747
if err != nil {
4848
return nil, err
4949
}
5050
if point != nil {
5151
return point, nil
5252
}
5353

54-
return self.getByHeight(height)
54+
return self.getByIndex(index)
5555
}
5656

57-
func (self *linkedArray) getByHeight(height uint64) (*consensus_db.Point, error) {
57+
func (self *linkedArray) getByIndex(index uint64) (*consensus_db.Point, error) {
5858
result := &consensus_db.Point{}
59-
start := height * self.rate
59+
start := index * self.rate
6060
end := start + self.rate
6161
for i := start; i < end; i++ {
62-
p, err := self.lowerArr.GetByHeight(i)
62+
p, err := self.lowerArr.GetByIndex(i)
6363
if err != nil {
6464
return nil, err
6565
}
@@ -71,6 +71,7 @@ func (self *linkedArray) getByHeight(height uint64) (*consensus_db.Point, error)
7171
return result, nil
7272
}
7373

74+
var PERIOD_TO_SECS = uint64(75)
7475
var HOUR_TO_PERIOD = uint64(48)
7576
var DAY_TO_HOUR = uint64(24)
7677
var DAY_TO_PERIOD = uint64(24 * 48)
@@ -112,15 +113,15 @@ func newPeriodPointArray(rw Chain, cs DposReader) *periodLinkedArray {
112113
return &periodLinkedArray{rw: rw, periods: cache, snapshot: cs}
113114
}
114115

115-
func (self *periodLinkedArray) GetByHeight(height uint64) (*consensus_db.Point, error) {
116-
value, ok := self.periods.Get(height)
116+
func (self *periodLinkedArray) GetByIndex(index uint64) (*consensus_db.Point, error) {
117+
value, ok := self.periods.Get(index)
117118
if !ok || value == nil {
118-
result, err := self.getByHeight(height)
119+
result, err := self.getByIndex(index)
119120
if err != nil {
120121
return nil, err
121122
}
122123
if result != nil {
123-
self.Set(height, result)
124+
self.Set(index, result)
124125
return &result.Point, nil
125126
} else {
126127
return nil, nil
@@ -129,12 +130,12 @@ func (self *periodLinkedArray) GetByHeight(height uint64) (*consensus_db.Point,
129130
point := value.(*periodPoint)
130131
valid := self.checkValid(point)
131132
if !valid {
132-
result, err := self.getByHeight(height)
133+
result, err := self.getByIndex(index)
133134
if err != nil {
134135
return nil, err
135136
}
136137
if result != nil {
137-
self.Set(height, result)
138+
self.Set(index, result)
138139
return &result.Point, nil
139140
} else {
140141
return nil, nil
@@ -143,28 +144,24 @@ func (self *periodLinkedArray) GetByHeight(height uint64) (*consensus_db.Point,
143144
return &point.Point, nil
144145
}
145146

146-
func (self *periodLinkedArray) Set(height uint64, block *periodPoint) error {
147-
self.periods.Add(height, block)
147+
func (self *periodLinkedArray) Set(index uint64, block *periodPoint) error {
148+
self.periods.Add(index, block)
148149
return nil
149150
}
150151

151-
func (self *periodLinkedArray) NextHeight(height uint64) uint64 {
152-
return height + 1
153-
}
154-
155-
func (self *periodLinkedArray) getByHeight(height uint64) (*periodPoint, error) {
156-
stime, etime := self.snapshot.Index2Time(height)
152+
func (self *periodLinkedArray) getByIndex(index uint64) (*periodPoint, error) {
153+
stime, etime := self.snapshot.Index2Time(index)
157154
// todo opt
158155
endSnapshotBlock, err := self.rw.GetSnapshotHeaderBeforeTime(&etime)
159156
if err != nil {
160157
return nil, err
161158
}
162159
if endSnapshotBlock.Timestamp.Before(stime) {
163-
return self.emptyPoint(height, &stime, &etime, endSnapshotBlock)
160+
return self.emptyPoint(index, &stime, &etime, endSnapshotBlock)
164161
}
165162

166163
if self.rw.IsGenesisSnapshotBlock(endSnapshotBlock.Hash) {
167-
return self.emptyPoint(height, &stime, &etime, endSnapshotBlock)
164+
return self.emptyPoint(index, &stime, &etime, endSnapshotBlock)
168165
}
169166

170167
blocks, err := self.rw.GetSnapshotHeadersAfterOrEqualTime(&ledger.HashHeight{Hash: endSnapshotBlock.Hash, Height: endSnapshotBlock.Height}, &stime, nil)
@@ -174,15 +171,15 @@ func (self *periodLinkedArray) getByHeight(height uint64) (*periodPoint, error)
174171

175172
// actually no block
176173
if len(blocks) == 0 {
177-
return self.emptyPoint(height, &stime, &etime, endSnapshotBlock)
174+
return self.emptyPoint(index, &stime, &etime, endSnapshotBlock)
178175
}
179176

180-
result, err := self.snapshot.ElectionIndex(height)
177+
result, err := self.snapshot.ElectionIndex(index)
181178
if err != nil {
182179
return nil, err
183180
}
184181

185-
return self.genPeriodPoint(height, &stime, &etime, endSnapshotBlock, blocks, result)
182+
return self.genPeriodPoint(index, &stime, &etime, endSnapshotBlock, blocks, result)
186183
}
187184

188185
func (self *periodLinkedArray) checkValid(point *periodPoint) bool {
@@ -211,7 +208,7 @@ func (self *periodLinkedArray) checkValid(point *periodPoint) bool {
211208
return false
212209
}
213210

214-
func (self *periodLinkedArray) emptyPoint(height uint64, stime, etime *time.Time, endSnapshotBlock *ledger.SnapshotBlock) (*periodPoint, error) {
211+
func (self *periodLinkedArray) emptyPoint(index uint64, stime, etime *time.Time, endSnapshotBlock *ledger.SnapshotBlock) (*periodPoint, error) {
215212
point := &periodPoint{}
216213
point.stime = stime
217214
point.etime = etime
@@ -228,7 +225,7 @@ func (self *periodLinkedArray) emptyPoint(height uint64, stime, etime *time.Time
228225
}
229226
return point, nil
230227
}
231-
func (self *periodLinkedArray) genPeriodPoint(height uint64, stime *time.Time, etime *time.Time, endSnapshot *ledger.SnapshotBlock, blocks []*ledger.SnapshotBlock, result *electionResult) (*periodPoint, error) {
228+
func (self *periodLinkedArray) genPeriodPoint(index uint64, stime *time.Time, etime *time.Time, endSnapshot *ledger.SnapshotBlock, blocks []*ledger.SnapshotBlock, result *electionResult) (*periodPoint, error) {
232229
point := &periodPoint{}
233230
point.stime = stime
234231
point.etime = etime

consensus/consensus_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"os"
77
"testing"
88

9+
"github.com/stretchr/testify/assert"
10+
911
"github.com/vitelabs/go-vite/chain"
1012
"github.com/vitelabs/go-vite/common/types"
1113
"github.com/vitelabs/go-vite/config"
@@ -688,6 +690,29 @@ func TestChainSnapshot(t *testing.T) {
688690

689691
}
690692

693+
func TestChainAcc(t *testing.T) {
694+
var genesisConfigJson = "{\"GenesisAccountAddress\":\"vite_ab24ef68b84e642c0ddca06beec81c9acb1977bbd7da27a87a\",\"ForkPoints\":{},\"ContractStorageMap\":{\"vite_00000000000000000000000000000000000000042d7ef71894\":{\"0100000000000000000001\":\"0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000006400000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005649544520544f4b454e000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000260000000000000000000000000ab24ef68b84e642c0ddca06beec81c9acb1977bb00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000152d02c7e14af6800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\",\"0100000000000000000002\":\"000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000006400000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000005649544520544f4b454e000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000260000000000000000000000000ab24ef68b84e642c0ddca06beec81c9acb1977bb00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000152d02c7e14af6800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\",\"41ba695ff63caafd5460dcf914387e95ca3a900f00000000000000000001\":\"000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000027339000000000000000000000000000000000000000000000000000000000000\",\"0fde96ad01f981b61ed2aaa52ddc0c030d3607c500000000000000000001\":\"000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000027336000000000000000000000000000000000000000000000000000000000000\",\"1630f8c0cf5eda3ce64bd49a0523b826f67b19a300000000000000000001\":\"000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000027335000000000000000000000000000000000000000000000000000000000000\",\"14edbc9214bd1e5f6082438f707d10bf43463a6d00000000000000000001\":\"000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000027331000000000000000000000000000000000000000000000000000000000000\",\"5a1b5ece654138d035bdd9873c1892fb5817548a00000000000000000001\":\"000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000037331310000000000000000000000000000000000000000000000000000000000\",\"44c6269ea21e79f45902911edb3fac0be431d20100000000000000000001\":\"000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000037331300000000000000000000000000000000000000000000000000000000000\",\"27a258dd1ed0ce0de3f4abd019adacd1b4b163b800000000000000000001\":\"000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000027333000000000000000000000000000000000000000000000000000000000000\",\"31a02e4f4b536e2d6d9bde23910cdffe72d3369e00000000000000000001\":\"000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000027337000000000000000000000000000000000000000000000000000000000000\",\"0acbb1335822c8df4488f3eea6e9000eabb0f19d00000000000000000001\":\"000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000027332000000000000000000000000000000000000000000000000000000000000\",\"70cfd586185e552635d11f398232344f97fc524f00000000000000000001\":\"000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000027338000000000000000000000000000000000000000000000000000000000000\",\"1b1dfa00323aea69465366d839703547fec5359d00000000000000000001\":\"000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000027334000000000000000000000000000000000000000000000000000000000000\",\"000000000000000000013150cb35860d60796c33a33eee0402ad8988cf4697c3\":\"0000000000000000000000000000000000000000000000000000000000000100000000000000000000000000360232b0378111b122685a15e612143dc9a89cfa000000000000000000000000360232b0378111b122685a15e612143dc9a89cfa00000000000000000000000000000000000000000000152d02c7e14af6800000000000000000000000000000000000000000000000000000000000000076a700000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000273350000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000360232b0378111b122685a15e612143dc9a89cfa\",\"000000000000000000015c55b8a2d2e027935faf04d076851512abad04cac5d4\":\"0000000000000000000000000000000000000000000000000000000000000100000000000000000000000000ce18b99b46c70c8e6bf34177d0c5db956a8c3ea7000000000000000000000000ce18b99b46c70c8e6bf34177d0c5db956a8c3ea700000000000000000000000000000000000000000000152d02c7e14af6800000000000000000000000000000000000000000000000000000000000000076a700000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000273380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000ce18b99b46c70c8e6bf34177d0c5db956a8c3ea7\"},\"vite_00000000000000000000000000000000000000056ad6d26692\":{\"5649544520544f4b454e\":\"000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a00000000000000000000000000000000000000000033b2e3c9fd0803ce80000000000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ab24ef68b84e642c0ddca06beec81c9acb1977bb0000000000000000000000000000000000000000000000000000000000000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a5669746520546f6b656e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000045649544500000000000000000000000000000000000000000000000000000000\"}},\"ContractLogsMap\":{\"vite_00000000000000000000000000000000000000056ad6d26692\":[{\"Data\":\"\",\"Topics\":[\"3f9dcc00d5e929040142c3fb2b67a3be1b0e91e98dac18d5bc2b7817a4cfecb6\",\"000000000000000000000000000000000000000000005649544520544f4b454e\"]}]},\"AccountBalanceMap\":{\"vite_ab24ef68b84e642c0ddca06beec81c9acb1977bbd7da27a87a\":{\"tti_5649544520544f4b454e6e40\":900000000000000000000000000},\"vite_56fd05b23ff26cd7b0a40957fb77bde60c9fd6ebc35f809c23\":{\"tti_5649544520544f4b454e6e40\":100000000000000000000000000}}}"
695+
c, err := NewChainInstanceFromDir("/Users/jie/Documents/vite/src/github.com/vitelabs/cluster1/ledger_datas/ledger_1/devdata", false, genesisConfigJson)
696+
if err != nil {
697+
t.Error(err)
698+
t.FailNow()
699+
}
700+
addr := types.HexToAddressPanic("vite_360232b0378111b122685a15e612143dc9a89cfa7e803f4b5a")
701+
prev, err := c.GetLatestAccountBlock(addr)
702+
703+
assert.NoError(t, err)
704+
assert.NotNil(t, prev)
705+
706+
for i := uint64(1); i <= prev.Height; i++ {
707+
block, err := c.GetAccountBlockByHeight(addr, i)
708+
if err != nil {
709+
panic(err)
710+
}
711+
fmt.Printf("height:%d, producer:%s, hash:%s\n", block.Height, block.Producer(), block.Hash)
712+
//fmt.Printf("%+v\n", block)
713+
}
714+
}
715+
691716
//
692717
//func TestChainRw_GetSeedsBeforeHashH(t *testing.T) {
693718
// chainInstance := chain.NewChain(&config.Config{

0 commit comments

Comments
 (0)