forked from FactomProject/FactomCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdblock.go
More file actions
executable file
·455 lines (366 loc) · 13 KB
/
dblock.go
File metadata and controls
executable file
·455 lines (366 loc) · 13 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package ldb
import (
"github.com/FactomProject/FactomCode/notaryapi"
"github.com/conformal/goleveldb/leveldb"
"errors"
"log"
"github.com/conformal/goleveldb/leveldb/util"
"bytes"
"time"
"encoding/binary"
)
// FetchDBEntriesFromQueue gets all of the dbentries that have not been processed
func (db *LevelDb) FetchDBEntriesFromQueue(startTime *[]byte) (dbentries []*notaryapi.DBEntry, err error){
db.dbLock.Lock()
defer db.dbLock.Unlock()
var fromkey [] byte = []byte{byte(TBL_EB_QUEUE)} // Table Name (1 bytes)
fromkey = append(fromkey, *startTime ...) // Timestamp (8 bytes)
var tokey [] byte = []byte{byte(TBL_EB_QUEUE)} // Table Name (4 bytes)
binaryTimestamp := make([]byte, 8)
binary.BigEndian.PutUint64(binaryTimestamp, uint64(time.Now().Unix()))
tokey = append(tokey, binaryTimestamp ...) // Timestamp (8 bytes)
fbEntrySlice := make([]*notaryapi.DBEntry, 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())
dbEntry := new (notaryapi.DBEntry)
dbEntry.SetTimeStamp(key[1:9]) // Timestamp (8 bytes)
cid := key[9:41]
dbEntry.ChainID = new (notaryapi.Hash)
dbEntry.ChainID.Bytes = cid // Chain id (32 bytes)
dbEntry.SetHash(key[41:73]) // Entry Hash (32 bytes)
fbEntrySlice = append(fbEntrySlice, dbEntry)
}
}
iter.Release()
err = iter.Error()
return fbEntrySlice, nil
}
// ProcessDBlockBatche inserts the DBlock and update all it's dbentries in DB
func (db *LevelDb) ProcessDBlockBatch(dblock *notaryapi.DBlock) error {
if dblock != nil {
if db.lbatch == nil {
db.lbatch = new(leveldb.Batch)
}
defer db.lbatch.Reset()
if len(dblock.DBEntries) < 1 {
return errors.New("Empty dblock!")
}
binaryDblock, err := dblock.MarshalBinary()
if err != nil{
return err
}
// Insert the binary factom block
var key [] byte = []byte{byte(TBL_DB)}
key = append (key, dblock.DBHash.Bytes ...)
db.lbatch.Put(key, binaryDblock)
// Update DBEntry process queue for each dbEntry in dblock
for i:=0; i< len(dblock.DBEntries); i++ {
var dbEntry notaryapi.DBEntry = *dblock.DBEntries[i]
var fbEntryKey [] byte = []byte{byte(TBL_EB_QUEUE)} // Table Name (1 bytes)
fbEntryKey = append(fbEntryKey, dbEntry.GetBinaryTimeStamp() ...) // Timestamp (8 bytes)
fbEntryKey = append(fbEntryKey, dbEntry.ChainID.Bytes ...) // Chain id (32 bytes)
fbEntryKey = append(fbEntryKey, dbEntry.Hash().Bytes ...) // Entry Hash (32 bytes)
db.lbatch.Put(fbEntryKey, []byte{byte(STATUS_PROCESSED)})
if isLookupDB {
// Create an EBInfo and insert it into db
var ebInfo = new (notaryapi.EBInfo)
ebInfo.EBHash = dbEntry.Hash()
ebInfo.MerkleRoot = dbEntry.MerkleRoot
ebInfo.DBHash = dblock.DBHash
ebInfo.DBBlockNum = dblock.Header.BlockID
ebInfo.ChainID = dbEntry.ChainID
var ebInfoKey [] byte = []byte{byte(TBL_EB_INFO)}
ebInfoKey = append(ebInfoKey, ebInfo.EBHash.Bytes ...)
binaryEbInfo, _ := ebInfo.MarshalBinary()
db.lbatch.Put(ebInfoKey, binaryEbInfo)
}
}
err = db.lDb.Write(db.lbatch, db.wo)
if err != nil {
log.Println("batch failed %v\n", err)
return err
}
}
return nil
}
// Insert the Directory Block meta data into db
func (db *LevelDb)InsertDBBatch(dbBatch *notaryapi.DBBatch) (err error){
if dbBatch.BTCBlockHash == nil || dbBatch.BTCTxHash == nil {
return
}
db.dbLock.Lock()
defer db.dbLock.Unlock()
if db.lbatch == nil {
db.lbatch = new(leveldb.Batch)
}
defer db.lbatch.Reset()
// Insert DBBatch - using the first block id in the batch as the key
var Key [] byte = []byte{byte(TBL_DBATCH)}
var buf bytes.Buffer
binary.Write(&buf, binary.BigEndian, dbBatch.DBlocks[0].Header.BlockID)
Key = append (Key, buf.Bytes() ...)
binaryDBBatch, _ := dbBatch.MarshalBinary()
db.lbatch.Put(Key, binaryDBBatch)
// Insert dblock - DBBatch cross reference
for _, dblock := range (dbBatch.DBlocks) {
var dbKey [] byte = []byte{byte(TBL_DB_BATCH)} // Table Name (1 bytes)
dbKey = append(dbKey, dblock.DBHash.Bytes ...)
db.lbatch.Put(dbKey, buf.Bytes())
}
err = db.lDb.Write(db.lbatch, db.wo)
if err != nil {
log.Println("batch failed %v\n", err)
return err
}
return nil
}
// FetchDBBatchByHash gets an DBBatch obj
func (db *LevelDb) FetchDBBatchByHash(dbHash *notaryapi.Hash) (dbBatch *notaryapi.DBBatch, err error) {
dBlockID,_ := db.FetchDBlockIDByHash(dbHash)
if dBlockID > -1 {
dbBatch, err = db.FetchDBBatchByDBlockID(uint64(dBlockID))
}
return dbBatch, err
}
// FetchDBBatchByDBlockID gets an DBBatch obj
func (db *LevelDb) FetchDBBatchByDBlockID(dBlockID uint64) (dbBatch *notaryapi.DBBatch, err error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
var buf bytes.Buffer
binary.Write(&buf, binary.BigEndian, dBlockID)
var key [] byte = []byte{byte(TBL_DBATCH)}
key = append (key, buf.Bytes() ...)
data, err := db.lDb.Get(key, db.ro)
if data != nil{
dbBatch = new (notaryapi.DBBatch)
dbBatch.UnmarshalBinary(data)
}
return dbBatch, nil
}
// FetchDBlockIDByHash gets an dBlockID
func (db *LevelDb) FetchDBlockIDByHash(dbHash *notaryapi.Hash) (dBlockID int64, err error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
var key [] byte = []byte{byte(TBL_DB_BATCH)}
key = append (key, dbHash.Bytes ...)
data, err := db.lDb.Get(key, db.ro)
if data != nil{
dBlockID = int64(binary.BigEndian.Uint64(data[:8]))
} else {
dBlockID = int64(-1)
}
return dBlockID, nil
}
// FetchDBlock gets an entry by hash from the database.
func (db *LevelDb) FetchDBlockByHash(dBlockHash *notaryapi.Hash) (dBlock *notaryapi.DBlock, err error) {
db.dbLock.Lock()
defer db.dbLock.Unlock()
var key [] byte = []byte{byte(TBL_DB)}
key = append (key, dBlockHash.Bytes ...)
data, err := db.lDb.Get(key, db.ro)
if data != nil{
dBlock = new (notaryapi.DBlock)
dBlock.UnmarshalBinary(data)
}
log.Println("dBlock.Header.MerkleRoot:%v", dBlock.Header.MerkleRoot.String())
for _, entry := range dBlock.DBEntries{
log.Println("entry.MerkleRoot:%v", entry.MerkleRoot.String())
}
return dBlock, nil
}
// FetchAllDBInfo gets all of the fbInfo
func (db *LevelDb) FetchAllDBlocks() (dBlocks []notaryapi.DBlock, err error){
db.dbLock.Lock()
defer db.dbLock.Unlock()
var fromkey [] byte = []byte{byte(TBL_DB)} // Table Name (1 bytes) // Timestamp (8 bytes)
var tokey [] byte = []byte{byte(TBL_DB+1)} // Table Name (1 bytes)
dBlockSlice := make([]notaryapi.DBlock, 0, 10)
iter := db.lDb.NewIterator(&util.Range{Start: fromkey, Limit: tokey}, db.ro)
for iter.Next() {
var dBlock notaryapi.DBlock
dBlock.UnmarshalBinary(iter.Value())
dBlock.DBHash = notaryapi.Sha(iter.Value()) //to be optimized??
dBlockSlice = append(dBlockSlice, dBlock)
}
iter.Release()
err = iter.Error()
return dBlockSlice, nil
}
// FetchDBInfoByHash gets an DBInfo obj
func (db *LevelDb) FetchAllDBRecordsByDBHash(dbHash *notaryapi.Hash) (ldbMap map[string]string, err error) {
// db.dbLock.Lock()
// defer db.dbLock.Unlock()
if ldbMap == nil {
ldbMap = make(map[string]string)
}
var dblock notaryapi.DBlock
var eblock notaryapi.EBlock
//DBlock
var key [] byte = []byte{byte(TBL_DB)}
key = append (key, dbHash.Bytes ...)
data, err := db.lDb.Get(key, db.ro)
if data == nil {
return nil, errors.New("DBlock not found for DBHash: " + dbHash.String())
} else {
dblock.UnmarshalBinary(data)
ldbMap[notaryapi.EncodeBinary(&key)] = notaryapi.EncodeBinary(&data)
}
f, _:=db.FetchEBInfoByHash(dbHash)
if f==nil{
log.Println("f is null")
}
// Chain Num cross references --- to be removed ???
fromkey := []byte{byte(TBL_EB_CHAIN_NUM)}
tokey := []byte{byte(TBL_EB_CHAIN_NUM+1)}
iter := db.lDb.NewIterator(&util.Range{Start: fromkey, Limit: tokey}, db.ro)
for iter.Next() {
k := iter.Key()
v := iter.Value()
ldbMap[notaryapi.EncodeBinary(&k)] = notaryapi.EncodeBinary(&v)
}
iter.Release()
err = iter.Error()
//EBlocks
for _, dbEntry := range dblock.DBEntries{
//EB Merkle root and EBHash Cross Reference
key = []byte{byte(TBL_EB_MR)}
key = append (key, dbEntry.MerkleRoot.Bytes ...)
data, err = db.lDb.Get(key, db.ro)
if data == nil {
return nil, errors.New("EBHash not found for MR: " + dbEntry.MerkleRoot.String())
} else {
dbHash := new(notaryapi.Hash)
dbHash.UnmarshalBinary(data)
dbEntry.SetHash(dbHash.Bytes)
ldbMap[notaryapi.EncodeBinary(&key)] = notaryapi.EncodeBinary(&data)
}
//EBlock
key = []byte{byte(TBL_EB)}
key = append (key, dbEntry.Hash().Bytes ...)
data, err = db.lDb.Get(key, db.ro)
if data == nil {
return nil, errors.New("EBlock not found for EBHash: " + dbEntry.Hash().String())
} else {
eblock.UnmarshalBinary(data)
ldbMap[notaryapi.EncodeBinary(&key)] = notaryapi.EncodeBinary(&data)
}
//EBInfo
key = []byte{byte(TBL_EB_INFO)}
key = append (key, dbEntry.Hash().Bytes ...)
data, err = db.lDb.Get(key, db.ro)
if data == nil {
return nil, errors.New("EBInfo not found for EBHash: " + dbEntry.Hash().String())
} else {
ldbMap[notaryapi.EncodeBinary(&key)] = notaryapi.EncodeBinary(&data)
}
//Entries
for _, ebentry := range eblock.EBEntries{
//Entry
key = []byte{byte(TBL_ENTRY)}
key = append (key, ebentry.Hash().Bytes ...)
data, err = db.lDb.Get(key, db.ro)
if data == nil {
return nil, errors.New("Entry not found for entry hash: " + ebentry.Hash().String())
} else {
ldbMap[notaryapi.EncodeBinary(&key)] = notaryapi.EncodeBinary(&data)
}
//EntryInfo
key = []byte{byte(TBL_ENTRY_INFO)}
key = append (key, ebentry.Hash().Bytes ...)
data, err = db.lDb.Get(key, db.ro)
if data == nil {
return nil, errors.New("EntryInfo not found for entry hash: " + ebentry.Hash().String())
} else {
ldbMap[notaryapi.EncodeBinary(&key)] = notaryapi.EncodeBinary(&data)
}
}
}
return ldbMap, nil
}
// FetchSupportDBRecords gets all supporting db records
func (db *LevelDb) FetchSupportDBRecords() (ldbMap map[string]string, err error) {
// db.dbLock.Lock()
// defer db.dbLock.Unlock()
if ldbMap == nil {
ldbMap = make(map[string]string)
}
// Chains
fromkey := []byte{byte(TBL_CHAIN_HASH)}
tokey := []byte{byte(TBL_CHAIN_HASH+1)}
iter := db.lDb.NewIterator(&util.Range{Start: fromkey, Limit: tokey}, db.ro)
for iter.Next() {
k := iter.Key()
v := iter.Value()
ldbMap[notaryapi.EncodeBinary(&k)] = notaryapi.EncodeBinary(&v)
}
iter.Release()
err = iter.Error()
// Chain Name cross references
fromkey = []byte{byte(TBL_CHAIN_NAME)}
tokey = []byte{byte(TBL_CHAIN_NAME+1)}
iter = db.lDb.NewIterator(&util.Range{Start: fromkey, Limit: tokey}, db.ro)
for iter.Next() {
k := iter.Key()
v := iter.Value()
ldbMap[notaryapi.EncodeBinary(&k)] = notaryapi.EncodeBinary(&v)
}
iter.Release()
err = iter.Error()
// Chain Num cross references
fromkey = []byte{byte(TBL_EB_CHAIN_NUM)}
tokey = []byte{byte(TBL_EB_CHAIN_NUM+1)}
iter = db.lDb.NewIterator(&util.Range{Start: fromkey, Limit: tokey}, db.ro)
for iter.Next() {
k := iter.Key()
v := iter.Value()
ldbMap[notaryapi.EncodeBinary(&k)] = notaryapi.EncodeBinary(&v)
}
iter.Release()
err = iter.Error()
//DBBatch -- to be optimized??
fromkey = []byte{byte(TBL_DBATCH)}
tokey = []byte{byte(TBL_DBATCH+1)}
iter = db.lDb.NewIterator(&util.Range{Start: fromkey, Limit: tokey}, db.ro)
for iter.Next() {
k := iter.Key()
v := iter.Value()
ldbMap[notaryapi.EncodeBinary(&k)] = notaryapi.EncodeBinary(&v)
}
iter.Release()
err = iter.Error()
// DB_Batch -- to be optimized??
fromkey = []byte{byte(TBL_DB_BATCH)}
tokey = []byte{byte(TBL_DB_BATCH+1)}
iter = db.lDb.NewIterator(&util.Range{Start: fromkey, Limit: tokey}, db.ro)
for iter.Next() {
k := iter.Key()
v := iter.Value()
ldbMap[notaryapi.EncodeBinary(&k)] = notaryapi.EncodeBinary(&v)
}
iter.Release()
err = iter.Error()
return ldbMap, nil
}
// InsertAllDBRecords inserts all key value pairs from map into db
func (db *LevelDb) InsertAllDBRecords(ldbMap map[string]string) (err error){
db.dbLock.Lock()
defer db.dbLock.Unlock()
if db.lbatch == nil {
db.lbatch = new(leveldb.Batch)
}
defer db.lbatch.Reset()
for key, value := range ldbMap{
binaryKey, _ := notaryapi.DecodeBinary(&key)
banaryValue, _ := notaryapi.DecodeBinary(&value)
db.lbatch.Put(binaryKey, banaryValue)
}
err = db.lDb.Write(db.lbatch, db.wo)
if err != nil {
log.Println("batch failed %v\n", err)
return err
}
return nil
}