/
chaindb.go
497 lines (424 loc) · 12.1 KB
/
chaindb.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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package ldb
import (
"encoding/binary"
"math"
"os"
"path/filepath"
"sync"
"github.com/wangxinyu2018/mass-core/database"
"github.com/wangxinyu2018/mass-core/database/disk"
"github.com/wangxinyu2018/mass-core/database/storage"
_ "github.com/wangxinyu2018/mass-core/database/storage/ldbstorage"
_ "github.com/wangxinyu2018/mass-core/database/storage/rdbstorage"
"github.com/wangxinyu2018/mass-core/errors"
"github.com/wangxinyu2018/mass-core/logging"
"github.com/wangxinyu2018/mass-core/wire"
)
const (
// params for ldb batch
dbBatchCount = 2
blockBatch = 0
addrIndexBatch = 1
blockStorageMetaDataLength = 40
)
var (
dbStorageMetaDataKey = []byte("DBSTORAGEMETA")
)
type dbStorageMeta struct {
currentHeight uint64
currentHash wire.Hash
}
func decodeDBStorageMetaData(bs []byte) (meta dbStorageMeta, err error) {
if length := len(bs); length != blockStorageMetaDataLength {
logging.CPrint(logging.ERROR, "invalid blockStorageMetaData", logging.LogFormat{"length": length, "data": bs})
return dbStorageMeta{}, database.ErrInvalidBlockStorageMeta
}
copy(meta.currentHash[:], bs[:32])
meta.currentHeight = binary.LittleEndian.Uint64(bs[32:])
return meta, nil
}
func encodeDBStorageMetaData(meta dbStorageMeta) []byte {
bs := make([]byte, blockStorageMetaDataLength)
copy(bs, meta.currentHash[:])
binary.LittleEndian.PutUint64(bs[32:], meta.currentHeight)
return bs
}
// ChainDb holds internal state for databse.
type ChainDb struct {
// lock preventing multiple entry
dbLock sync.Mutex
stor storage.Storage
blkFileKeeper *disk.BlockFileKeeper
dbBatch storage.Batch
batches [dbBatchCount]*LBatch
dbStorageMeta dbStorageMeta
txUpdateMap map[wire.Hash]*txUpdateObj
txSpentUpdateMap map[wire.Hash]*spentTxUpdate
stakingTxMap map[stakingTxMapKey]*stakingTx
expiredStakingTxMap map[stakingTxMapKey]*stakingTx
}
func init() {
for _, dbtype := range storage.RegisteredDbTypes() {
tp := dbtype
database.AddDBDriver(database.DriverDB{
DbType: tp,
CreateDB: func(args ...interface{}) (database.Db, error) {
if len(args) < 1 {
return nil, storage.ErrInvalidArgument
}
dbpath, ok := args[0].(string)
if !ok {
return nil, storage.ErrInvalidArgument
}
stor, err := storage.CreateStorage(tp, dbpath, nil)
if err != nil {
return nil, err
}
return NewChainDb(dbpath, stor)
},
OpenDB: func(path string, readonly bool, args ...interface{}) (database.Db, error) {
stor, err := storage.OpenStorage(tp, path, readonly, args...)
if err != nil {
return nil, err
}
return NewChainDb(path, stor)
},
})
}
}
func NewChainDb(dbpath string, stor storage.Storage) (*ChainDb, error) {
cdb := &ChainDb{
stor: stor,
dbBatch: stor.NewBatch(),
txUpdateMap: make(map[wire.Hash]*txUpdateObj),
txSpentUpdateMap: make(map[wire.Hash]*spentTxUpdate),
stakingTxMap: make(map[stakingTxMapKey]*stakingTx),
expiredStakingTxMap: make(map[stakingTxMapKey]*stakingTx),
}
blockMeta, err := cdb.getBlockStorageMeta()
if err != nil {
if err != storage.ErrNotFound {
return nil, err
}
blockMeta = dbStorageMeta{
currentHeight: UnknownHeight,
}
// if err = cdb.stor.Put(dbStorageMetaDataKey, encodeDBStorageMetaData(blockMeta)); err != nil {
// return nil, err
// }
}
cdb.dbStorageMeta = blockMeta
// Load address indexer
if err = cdb.checkAddrIndexVersion(); err == nil {
logging.CPrint(logging.INFO, "address index good, continuing")
} else {
if err != storage.ErrNotFound {
return nil, err
}
var b2 [2]byte
binary.LittleEndian.PutUint16(b2[:], uint16(addrIndexCurrentVersion))
if err = cdb.stor.Put(addrIndexVersionKey, b2[:]); err != nil {
return nil, err
}
}
// init or load blkXXXXX.dat
blkDir := filepath.Join(filepath.Dir(dbpath), "blocks")
fi, err := os.Stat(blkDir)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
err = os.MkdirAll(blkDir, 0755)
if err != nil {
return nil, err
}
} else if !fi.IsDir() {
return nil, errors.New("file already exist: " + blkDir)
}
records, err := cdb.getAllBlockFileMeta()
if err != nil {
return nil, err
}
if len(records) == 0 {
file0, err := cdb.initBlockFileMeta()
if err != nil {
logging.CPrint(logging.ERROR, "init block file meta failed", logging.LogFormat{"err": err})
return nil, err
}
records = append(records, file0)
}
cdb.blkFileKeeper = disk.NewBlockFileKeeper(blkDir, records)
return cdb, nil
}
func (db *ChainDb) close() error {
return db.stor.Close()
}
// Sync verifies that the database is coherent on disk,
// and no outstanding transactions are in flight.
func (db *ChainDb) Sync() error {
// while specified by the API, does nothing
// however does grab lock to verify it does not return until other operations are complete.
return nil
}
// Close cleanly shuts down database, syncing all data.
func (db *ChainDb) Close() error {
db.blkFileKeeper.Close()
return db.close()
}
func (db *ChainDb) getBlockStorageMeta() (dbStorageMeta, error) {
if data, err := db.stor.Get(dbStorageMetaDataKey); err == nil {
return decodeDBStorageMetaData(data)
} else {
return dbStorageMeta{}, err
}
}
func (db *ChainDb) expire(currentHeight uint64) error {
searchKey := stakingTxSearchKey(currentHeight)
iter := db.stor.NewIterator(storage.BytesPrefix(searchKey))
defer iter.Release()
for iter.Next() {
mapKeyBytes := iter.Key()[stakingTxSearchKeyLength:]
value := iter.Value()
mapKey := mustDecodeStakingTxMapKey(mapKeyBytes)
scriptHash, val := mustDecodeStakingTxValue(value)
// stakingTxMap
db.stakingTxMap[mapKey] = &stakingTx{
txSha: &mapKey.txID,
index: mapKey.index,
expiredHeight: currentHeight,
rsh: scriptHash,
value: val,
blkHeight: mapKey.blockHeight,
delete: true,
}
// expiredStakingTxMap
db.expiredStakingTxMap[mapKey] = &stakingTx{
txSha: &mapKey.txID,
index: mapKey.index,
expiredHeight: currentHeight,
rsh: scriptHash,
value: val,
blkHeight: mapKey.blockHeight,
}
}
if err := iter.Error(); err != nil {
return err
}
return nil
}
func (db *ChainDb) freeze(currentHeight uint64) error {
searchKey := expiredStakingTxSearchKey(currentHeight)
iter := db.stor.NewIterator(storage.BytesPrefix(searchKey))
defer iter.Release()
for iter.Next() {
mapKeyBytes := iter.Key()[stakingTxSearchKeyLength:]
value := iter.Value()
mapKey := mustDecodeStakingTxMapKey(mapKeyBytes)
scriptHash, val := mustDecodeStakingTxValue(value)
// expiredStakingTxMap
db.expiredStakingTxMap[mapKey] = &stakingTx{
txSha: &mapKey.txID,
index: mapKey.index,
expiredHeight: currentHeight,
rsh: scriptHash,
value: val,
blkHeight: mapKey.blockHeight,
delete: true,
}
// stakingTxMap
db.stakingTxMap[mapKey] = &stakingTx{
txSha: &mapKey.txID,
index: mapKey.index,
expiredHeight: currentHeight,
rsh: scriptHash,
value: val,
blkHeight: mapKey.blockHeight,
}
}
if err := iter.Error(); err != nil {
return err
}
return nil
}
// doSpend iterates all TxIn in a mass transaction marking each associated
// TxOut as spent.
func (db *ChainDb) doSpend(tx *wire.MsgTx) error {
if isCoinBaseTx(tx) {
return nil
}
for _, txIn := range tx.TxIn {
inTxSha := txIn.PreviousOutPoint.Hash
inTxIdx := txIn.PreviousOutPoint.Index
if inTxIdx == ^uint32(0) {
continue
}
err := db.setSpentData(&inTxSha, inTxIdx)
if err != nil {
return err
}
}
return nil
}
// unSpend iterates all TxIn in a mass transaction marking each associated
// TxOut as unspent.
func (db *ChainDb) unSpend(tx *wire.MsgTx) error {
for txinidx := range tx.TxIn {
txin := tx.TxIn[txinidx]
inTxSha := txin.PreviousOutPoint.Hash
inTxidx := txin.PreviousOutPoint.Index
if inTxidx == ^uint32(0) {
continue
}
err := db.clearSpentData(&inTxSha, inTxidx)
if err != nil {
return err
}
}
return nil
}
func (db *ChainDb) setSpentData(sha *wire.Hash, idx uint32) error {
return db.setclearSpentData(sha, idx, true)
}
func (db *ChainDb) clearSpentData(sha *wire.Hash, idx uint32) error {
return db.setclearSpentData(sha, idx, false)
}
func (db *ChainDb) setclearSpentData(txsha *wire.Hash, idx uint32, set bool) error {
var txUo *txUpdateObj
var ok bool
if txUo, ok = db.txUpdateMap[*txsha]; !ok {
// not cached, load from db
var txU txUpdateObj
blkHeight, txOff, txLen, spentData, err := db.getTxData(txsha)
if err != nil {
// setting a fully spent tx is an error.
if err != storage.ErrNotFound || set {
return err
}
// if we are clearing a tx and it wasn't found
// in the tx table, it could be in the fully spent
// (duplicates) table.
spentTxList, err := db.getTxFullySpent(txsha)
if err != nil {
return err
}
// need to reslice the list to exclude the most recent.
sTx := spentTxList[len(spentTxList)-1]
if len(spentTxList) == 1 {
// write entry to delete tx from spent pool
db.txSpentUpdateMap[*txsha] = &spentTxUpdate{delete: true}
} else {
db.txSpentUpdateMap[*txsha] = &spentTxUpdate{txl: spentTxList[:len(spentTxList)-1]}
}
// Create 'new' Tx update data.
blkHeight = sTx.blkHeight
txOff = sTx.txoff
txLen = sTx.txlen
spentbuflen := (sTx.numTxO + 7) / 8
spentData = make([]byte, spentbuflen)
for i := range spentData {
spentData[i] = ^byte(0)
}
}
txU.txSha = txsha
txU.blkHeight = blkHeight
txU.txoff = txOff
txU.txlen = txLen
txU.spentData = spentData
txUo = &txU
}
byteidx := idx / 8
byteoff := idx % 8
if set {
txUo.spentData[byteidx] |= (byte(1) << byteoff)
} else {
txUo.spentData[byteidx] &= ^(byte(1) << byteoff)
}
// check for fully spent Tx
fullySpent := true
for _, val := range txUo.spentData {
if val != ^byte(0) {
fullySpent = false
break
}
}
if fullySpent {
var txSu *spentTxUpdate
// Look up Tx in fully spent table
if txSuOld, ok := db.txSpentUpdateMap[*txsha]; ok {
txSu = txSuOld
} else {
txSu = &spentTxUpdate{}
txSuOld, err := db.getTxFullySpent(txsha)
if err == nil {
txSu.txl = txSuOld
} else if err != storage.ErrNotFound {
return err
}
}
// Fill in spentTx
var sTx spentTx
sTx.blkHeight = txUo.blkHeight
sTx.txoff = txUo.txoff
sTx.txlen = txUo.txlen
// XXX -- there is no way to comput the real TxOut
// from the spent array.
sTx.numTxO = 8 * len(txUo.spentData)
// append this txdata to fully spent txlist
txSu.txl = append(txSu.txl, &sTx)
// mark txsha as deleted in the txUpdateMap
logging.CPrint(logging.TRACE, "tx %v is fully spent", logging.LogFormat{"txHash": txsha})
db.txSpentUpdateMap[*txsha] = txSu
txUo.delete = true
}
db.txUpdateMap[*txsha] = txUo
return nil
}
func makeBlockHeightKey(height uint64) []byte {
var bs [blockHeightKeyLength]byte
copy(bs[:], blockHeightKeyPrefix)
binary.LittleEndian.PutUint64(bs[blockHeightKeyPrefixLength:], height)
return bs[:]
}
func makeBlockShaKey(sha *wire.Hash) []byte {
var bs [blockShaKeyLength]byte
copy(bs[:], blockShaKeyPrefix)
copy(bs[blockShaKeyPrefixLength:], (*sha)[:])
return bs[:]
}
var recordSuffixTx = []byte("TXD")
var recordSuffixSpentTx = []byte("TXS")
func shaTxToKey(sha *wire.Hash) []byte {
key := make([]byte, len(recordSuffixTx)+len(sha))
copy(key, recordSuffixTx)
copy(key[len(recordSuffixTx):], sha[:])
return key
}
func shaSpentTxToKey(sha *wire.Hash) []byte {
key := make([]byte, len(recordSuffixSpentTx)+len(sha))
copy(key, recordSuffixSpentTx)
copy(key[len(recordSuffixSpentTx):], sha[:])
return key
}
func isCoinBaseTx(msgTx *wire.MsgTx) bool {
prevOut := &msgTx.TxIn[0].PreviousOutPoint
if prevOut.Index != math.MaxUint32 || !prevOut.Hash.IsEqual(&wire.Hash{}) {
return false
}
return true
}
func (db *ChainDb) Batch(index int) *LBatch {
if db.batches[index] == nil {
db.batches[index] = NewLBatch(db.dbBatch)
}
return db.batches[index]
}
// For testing purpose
func (db *ChainDb) TestExportDbEntries() map[string][]byte {
it := db.stor.NewIterator(nil)
defer it.Release()
all := make(map[string][]byte)
for it.Next() {
all[string(it.Key())] = it.Value()
}
return all
}