forked from FactomProject/FactomCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheblock.go
More file actions
executable file
·387 lines (298 loc) · 10.1 KB
/
eblock.go
File metadata and controls
executable file
·387 lines (298 loc) · 10.1 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package ldb
import (
"encoding/binary"
"errors"
"github.com/FactomProject/FactomCode/common"
"github.com/FactomProject/goleveldb/leveldb"
"log"
"github.com/FactomProject/goleveldb/leveldb/util"
)
// FetchEBEntriesFromQueue gets all of the ebentries that have not been processed
/*func (db *LevelDb) FetchEBEntriesFromQueue(chainID *[]byte, startTime *[]byte) (ebentries []*common.EBEntry, err error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
var fromkey []byte = []byte{byte(TBL_ENTRY_QUEUE)} // Table Name (1 bytes)
fromkey = append(fromkey, *chainID...) // Chain Type (32 bytes)
fromkey = append(fromkey, *startTime...) // Timestamp (8 bytes)
var tokey []byte = []byte{byte(TBL_ENTRY_QUEUE)} // Table Name (4 bytes)
tokey = append(tokey, *chainID...) // Chain Type (4 bytes)
binaryTimestamp := make([]byte, 8)
binary.BigEndian.PutUint64(binaryTimestamp, uint64(time.Now().Unix()))
tokey = append(tokey, binaryTimestamp...) // Timestamp (8 bytes)
ebEntrySlice := make([]*common.EBEntry, 0, 10)
iter := db.lDb.NewIterator(&util.Range{Start: fromkey, Limit: tokey}, db.ro)
for iter.Next() {
if bytes.Equal(iter.Value(), []byte{byte(STATUS_IN_QUEUE)}) {
key := make([]byte, len(iter.Key()))
copy(key, iter.Key())
ebEntry := new(common.EBEntry)
ebEntry.SetTimeStamp(key[33:41])
ebEntry.SetHash(key[41:73])
ebEntrySlice = append(ebEntrySlice, ebEntry)
}
}
iter.Release()
err = iter.Error()
return ebEntrySlice, nil
}
*/
// ProcessEBlockBatche inserts the EBlock and update all it's ebentries in DB
func (db *LevelDb) ProcessEBlockBatch(eblock *common.EBlock) error {
if eblock != nil {
if db.lbatch == nil {
db.lbatch = new(leveldb.Batch)
}
defer db.lbatch.Reset()
if len(eblock.EBEntries) < 1 {
return errors.New("Empty eblock!")
}
binaryEblock, err := eblock.MarshalBinary()
if err != nil {
return err
}
if eblock.EBHash == nil {
eblock.EBHash = common.Sha(binaryEblock)
}
if eblock.MerkleRoot == nil {
eblock.BuildMerkleRoot()
}
// Insert the binary entry block
var key []byte = []byte{byte(TBL_EB)}
key = append(key, eblock.EBHash.Bytes...)
db.lbatch.Put(key, binaryEblock)
// Insert the entry block merkle root cross reference
key = []byte{byte(TBL_EB_MR)}
key = append(key, eblock.MerkleRoot.Bytes...)
binaryEBHash, _ := eblock.EBHash.MarshalBinary()
db.lbatch.Put(key, binaryEBHash)
// Insert the entry block number cross reference
key = []byte{byte(TBL_EB_CHAIN_NUM)}
key = append(key, eblock.Header.ChainID.Bytes...)
bytes := make([]byte, 8)
binary.BigEndian.PutUint32(bytes, eblock.Header.EBHeight)
key = append(key, bytes...)
db.lbatch.Put(key, binaryEBHash)
// Update entry process queue for each entry in eblock
/**************************************
for i := 0; i < len(eblock.EBEntries); i++ {
var ebEntry common.EBEntry = *eblock.EBEntries[i]
if isLookupDB {
// Create an EntryInfo and insert it into db
var entryInfo = new(common.EntryInfo)
entryInfo.EntryHash = ebEntry.EntryHash
entryInfo.EBHash = eblock.EBHash
entryInfo.EBBlockNum = uint64(eblock.Header.EBHeight)
var entryInfoKey []byte = []byte{byte(TBL_ENTRY_INFO)}
entryInfoKey = append(entryInfoKey, entryInfo.EntryHash.Bytes...)
binaryEntryInfo, _ := entryInfo.MarshalBinary()
db.lbatch.Put(entryInfoKey, binaryEntryInfo)
}
}
*****************************************/
err = db.lDb.Write(db.lbatch, db.wo)
if err != nil {
log.Println("batch failed %v\n", err)
return err
}
}
return nil
}
// FetchEBInfoByHash gets an EBInfo obj
func (db *LevelDb) FetchEBInfoByHash(ebHash *common.Hash) (ebInfo *common.EBInfo, err error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
var key []byte = []byte{byte(TBL_EB_INFO)}
key = append(key, ebHash.Bytes...)
data, err := db.lDb.Get(key, db.ro)
if data != nil {
ebInfo = new(common.EBInfo)
ebInfo.UnmarshalBinary(data)
}
return ebInfo, nil
}
// FetchEBlockByMR gets an entry block by merkle root from the database.
func (db *LevelDb) FetchEBlockByMR(eBMR *common.Hash) (eBlock *common.EBlock, err error) {
eBlockHash, _ := db.FetchEBHashByMR(eBMR)
if eBlockHash != nil {
eBlock, _ = db.FetchEBlockByHash(eBlockHash)
}
return eBlock, nil
}
// FetchEntryBlock gets an entry by hash from the database.
func (db *LevelDb) FetchEBlockByHash(eBlockHash *common.Hash) (eBlock *common.EBlock, err error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
var key []byte = []byte{byte(TBL_EB)}
key = append(key, eBlockHash.Bytes...)
data, err := db.lDb.Get(key, db.ro)
if data != nil {
eBlock = new(common.EBlock)
eBlock.UnmarshalBinary(data)
}
return eBlock, nil
}
// FetchEBlockByHeight gets an entry block by height from the database.
func (db *LevelDb) FetchEBlockByHeight(chainID * common.Hash, eBlockHeight uint64) (eBlock *common.EBlock, err error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
var key []byte = []byte{byte(TBL_EB_CHAIN_NUM)}
key = append(key, chainID.Bytes...)
bytes := make([]byte, 8)
binary.BigEndian.PutUint64(bytes, eBlockHeight)
key = append(key, bytes...)
data, err := db.lDb.Get(key, db.ro)
if data != nil {
eBlock = new(common.EBlock)
eBlock.UnmarshalBinary(data)
}
return eBlock, nil
}
// FetchEBHashByMR gets an entry by hash from the database.
func (db *LevelDb) FetchEBHashByMR(eBMR *common.Hash) (eBlockHash *common.Hash, err error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
var key []byte = []byte{byte(TBL_EB_MR)}
key = append(key, eBMR.Bytes...)
data, err := db.lDb.Get(key, db.ro)
if data != nil {
log.Println("data:%v", data)
eBlockHash = new(common.Hash)
eBlockHash.UnmarshalBinary(data)
log.Println("eBlockHash:%v", eBlockHash.Bytes)
}
return eBlockHash, nil
}
// InsertChain inserts the newly created chain into db
func (db *LevelDb) InsertChain(chain *common.EChain) (err error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
if db.lbatch == nil {
db.lbatch = new(leveldb.Batch)
}
defer db.lbatch.Reset()
binaryChain, _ := chain.MarshalBinary()
var chainByHashKey []byte = []byte{byte(TBL_CHAIN_HASH)}
chainByHashKey = append(chainByHashKey, chain.ChainID.Bytes...)
db.lbatch.Put(chainByHashKey, binaryChain)
err = db.lDb.Write(db.lbatch, db.wo)
if err != nil {
log.Println("batch failed %v\n", err)
return err
}
return nil
}
// FetchChainByHash gets a chain by chainID
func (db *LevelDb) FetchChainByHash(chainID *common.Hash) (chain *common.EChain, err error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
var key []byte = []byte{byte(TBL_CHAIN_HASH)}
key = append(key, chainID.Bytes...)
data, err := db.lDb.Get(key, db.ro)
if data != nil {
chain = new(common.EChain)
chain.UnmarshalBinary(data)
}
return chain, nil
}
// FetchChainIDByName gets a chainID by chain name
func (db *LevelDb) FetchChainIDByName(chainName [][]byte) (chainID *common.Hash, err error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
chainID, err = common.GetChainID(chainName)
if err != nil {
return nil,err
}
return
}
// FetchChainByName gets a chain by chain name
func (db *LevelDb) FetchChainByName(chainName [][]byte) (chain *common.EChain, err error) {
chainID, _ := db.FetchChainIDByName(chainName)
if chainID != nil {
chain, _ = db.FetchChainByHash(chainID)
}
return chain, nil
}
// FetchAllChains get all of the cahins
func (db *LevelDb) FetchAllChains() (chains []common.EChain, err error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
var fromkey []byte = []byte{byte(TBL_CHAIN_HASH)} // Table Name (1 bytes)
var tokey []byte = []byte{byte(TBL_CHAIN_HASH + 1)} // Table Name (1 bytes)
chainSlice := make([]common.EChain, 0, 10)
iter := db.lDb.NewIterator(&util.Range{Start: fromkey, Limit: tokey}, db.ro)
for iter.Next() {
var chain common.EChain
chain.UnmarshalBinary(iter.Value())
chainSlice = append(chainSlice, chain)
}
iter.Release()
err = iter.Error()
return chainSlice, err
}
// FetchAllEBlocksByChain gets all of the blocks by chain id
func (db *LevelDb) FetchAllEBlocksByChain(chainID *common.Hash) (eBlocks *[]common.EBlock, err error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
var fromkey []byte = []byte{byte(TBL_EB_CHAIN_NUM)} // Table Name (1 bytes)
fromkey = append(fromkey, []byte(chainID.Bytes)...) // Chain Type (32 bytes)
var tokey []byte = addOneToByteArray(fromkey)
eBlockSlice := make([]common.EBlock, 0, 10)
iter := db.lDb.NewIterator(&util.Range{Start: fromkey, Limit: tokey}, db.ro)
for iter.Next() {
eBlockHash := new(common.Hash)
eBlockHash.UnmarshalBinary(iter.Value())
var key []byte = []byte{byte(TBL_EB)}
key = append(key, eBlockHash.Bytes...)
data, _ := db.lDb.Get(key, db.ro)
if data != nil {
eBlock := new(common.EBlock)
eBlock.UnmarshalBinary(data)
eBlock.EBHash = eBlockHash
eBlockSlice = append(eBlockSlice, *eBlock)
}
}
iter.Release()
err = iter.Error()
return &eBlockSlice, nil
}
// FetchAllEBInfosByChain gets all of the entry block infos by chain id
func (db *LevelDb) FetchAllEBInfosByChain(chainID *common.Hash) (eBInfos *[]common.EBInfo, err error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
var fromkey []byte = []byte{byte(TBL_EB_CHAIN_NUM)} // Table Name (1 bytes)
fromkey = append(fromkey, []byte(chainID.Bytes)...) // Chain Type (32 bytes)
var tokey []byte = addOneToByteArray(fromkey)
eBInfoSlice := make([]common.EBInfo, 0, 10)
iter := db.lDb.NewIterator(&util.Range{Start: fromkey, Limit: tokey}, db.ro)
for iter.Next() {
eBlockHash := new(common.Hash)
eBlockHash.Bytes = make([]byte, len(iter.Value()))
copy(eBlockHash.Bytes, iter.Value())
var key []byte = []byte{byte(TBL_EB_INFO)}
key = append(key, eBlockHash.Bytes...)
data, _ := db.lDb.Get(key, db.ro)
if data != nil {
eBInfo := new(common.EBInfo)
eBInfo.UnmarshalBinary(data)
eBInfoSlice = append(eBInfoSlice, *eBInfo)
}
}
iter.Release()
err = iter.Error()
return &eBInfoSlice, nil
}
// Internal db use only
func addOneToByteArray(input []byte) (output []byte) {
if input == nil {
return []byte{byte(1)}
}
output = make([]byte, len(input))
copy(output, input)
for i := len(input); i > 0; i-- {
if output[i-1] <= 255 {
output[i-1] = output[i-1] + 1
break
}
}
return output
}