-
Notifications
You must be signed in to change notification settings - Fork 14
/
ledger_sync.go
180 lines (149 loc) · 4.63 KB
/
ledger_sync.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
package ledger
import (
"github.com/qlcchain/go-qlc/common"
"github.com/qlcchain/go-qlc/common/storage"
"github.com/qlcchain/go-qlc/common/types"
)
type SyncStore interface {
AddUncheckedSyncBlock(previous types.Hash, blk *types.StateBlock) error
GetUncheckedSyncBlock(hash types.Hash) (*types.StateBlock, error)
HasUncheckedSyncBlock(hash types.Hash) (bool, error)
CountUncheckedSyncBlocks() (uint64, error)
DeleteUncheckedSyncBlock(hash types.Hash) error
AddUnconfirmedSyncBlock(hash types.Hash, blk *types.StateBlock) error
GetUnconfirmedSyncBlock(hash types.Hash) (*types.StateBlock, error)
HasUnconfirmedSyncBlock(hash types.Hash) (bool, error)
CountUnconfirmedSyncBlocks() (uint64, error)
DeleteUnconfirmedSyncBlock(hash types.Hash) error
WalkSyncCache(visit common.SyncCacheWalkFunc)
CleanSyncCache()
}
func (l *Ledger) getUncheckedSyncBlockKey(hash types.Hash) []byte {
var key [1 + types.HashSize]byte
key[0] = byte(storage.KeyPrefixUncheckedSync)
copy(key[1:], hash[:])
return key[:]
}
func (l *Ledger) AddUncheckedSyncBlock(previous types.Hash, blk *types.StateBlock) error {
blockBytes, err := blk.Serialize()
if err != nil {
return err
}
key := l.getUncheckedSyncBlockKey(previous)
return l.store.Put(key, blockBytes)
}
func (l *Ledger) GetUncheckedSyncBlock(hash types.Hash) (*types.StateBlock, error) {
key := l.getUncheckedSyncBlockKey(hash)
blk := new(types.StateBlock)
val, err := l.store.Get(key)
if err != nil {
if err == storage.KeyNotFound {
return nil, ErrBlockNotFound
}
return nil, err
}
if err = blk.Deserialize(val); err != nil {
return nil, err
}
if blk.IsPrivate() {
pl, err := l.GetBlockPrivatePayload(blk.GetHash())
if err == nil {
blk.SetPrivatePayload(pl)
}
}
return blk, nil
}
func (l *Ledger) HasUncheckedSyncBlock(hash types.Hash) (bool, error) {
key := l.getUncheckedSyncBlockKey(hash)
return l.store.Has(key)
}
func (l *Ledger) CountUncheckedSyncBlocks() (uint64, error) {
var count uint64
count, err := l.store.Count([]byte{byte(storage.KeyPrefixUncheckedSync)})
if err != nil {
return 0, err
}
return count, nil
}
func (l *Ledger) DeleteUncheckedSyncBlock(hash types.Hash) error {
key := l.getUncheckedSyncBlockKey(hash)
return l.store.Delete(key)
}
func (l *Ledger) getUnconfirmedSyncBlockKey(hash types.Hash) []byte {
var key [1 + types.HashSize]byte
key[0] = byte(storage.KeyPrefixUnconfirmedSync)
copy(key[1:], hash[:])
return key[:]
}
func (l *Ledger) AddUnconfirmedSyncBlock(hash types.Hash, blk *types.StateBlock) error {
blockBytes, err := blk.Serialize()
if err != nil {
return err
}
key := l.getUnconfirmedSyncBlockKey(hash)
return l.store.Put(key, blockBytes)
}
func (l *Ledger) GetUnconfirmedSyncBlock(hash types.Hash) (*types.StateBlock, error) {
key := l.getUnconfirmedSyncBlockKey(hash)
blk := new(types.StateBlock)
val, err := l.store.Get(key)
if err != nil {
if err == storage.KeyNotFound {
return nil, ErrBlockNotFound
}
return nil, err
}
if err = blk.Deserialize(val); err != nil {
return nil, err
}
if blk.IsPrivate() {
pl, err := l.GetBlockPrivatePayload(hash)
if err == nil {
blk.SetPrivatePayload(pl)
}
}
return blk, nil
}
func (l *Ledger) HasUnconfirmedSyncBlock(hash types.Hash) (bool, error) {
key := l.getUnconfirmedSyncBlockKey(hash)
return l.store.Has(key)
}
func (l *Ledger) CountUnconfirmedSyncBlocks() (uint64, error) {
var count uint64
prefix, _ := storage.GetKeyOfParts(storage.KeyPrefixUnconfirmedSync)
count, err := l.store.Count(prefix)
if err != nil {
return 0, err
}
return count, nil
}
func (l *Ledger) DeleteUnconfirmedSyncBlock(hash types.Hash) error {
key := l.getUnconfirmedSyncBlockKey(hash)
return l.store.Delete(key)
}
func (l *Ledger) WalkSyncCache(visit common.SyncCacheWalkFunc) {
prefix, _ := storage.GetKeyOfParts(storage.KeyPrefixUnconfirmedSync)
_ = l.store.Iterator(prefix, nil, func(key []byte, val []byte) error {
visit(common.SyncCacheUnconfirmed, key)
return nil
})
prefix2, _ := storage.GetKeyOfParts(storage.KeyPrefixUncheckedSync)
_ = l.store.Iterator(prefix2, nil, func(key []byte, val []byte) error {
visit(common.SyncCacheUnchecked, key)
return nil
})
}
func (l *Ledger) CleanSyncCache() {
prefix, _ := storage.GetKeyOfParts(storage.KeyPrefixUnconfirmedSync)
_ = l.store.Iterator(prefix, nil, func(key []byte, val []byte) error {
hash, _ := types.BytesToHash(key[1:])
_ = l.DeleteUnconfirmedSyncBlock(hash)
return nil
})
prefix2, _ := storage.GetKeyOfParts(storage.KeyPrefixUncheckedSync)
_ = l.store.Iterator(prefix2, nil, func(key []byte, val []byte) error {
hash, _ := types.BytesToHash(key[1:])
_ = l.DeleteUncheckedSyncBlock(hash)
return nil
})
}