forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chaindb.go
517 lines (426 loc) · 12.4 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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package chain
import (
"bytes"
"encoding/binary"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"sync/atomic"
"github.com/aergoio/aergo-lib/db"
"github.com/aergoio/aergo/consensus"
"github.com/aergoio/aergo/internal/common"
"github.com/aergoio/aergo/internal/enc"
"github.com/aergoio/aergo/types"
"github.com/gogo/protobuf/proto"
)
const (
chainDBName = "chain"
genesisKey = chainDBName + ".genesisInfo"
TxBatchMax = 10000
)
var (
// ErrNoChainDB reports chaindb is not prepared.
ErrNoChainDB = fmt.Errorf("chaindb not prepared")
ErrorLoadBestBlock = errors.New("failed to load latest block from DB")
latestKey = []byte(chainDBName + ".latest")
receiptsPrefix = []byte("r")
)
// ErrNoBlock reports there is no such a block with id (hash or block number).
type ErrNoBlock struct {
id interface{}
}
func (e ErrNoBlock) Error() string {
var idStr string
switch id := e.id.(type) {
case []byte:
idStr = fmt.Sprintf("blockHash=%v", enc.ToString(id))
default:
idStr = fmt.Sprintf("blockNo=%v", id)
}
return fmt.Sprintf("block not found: %s", idStr)
}
type ChainDB struct {
cc consensus.ChainConsensus
latest atomic.Value //types.BlockNo
bestBlock atomic.Value // *types.Block
// blocks []*types.Block
store db.DB
}
func NewChainDB() *ChainDB {
// logger.SetLevel("debug")
cdb := &ChainDB{
//blocks: []*types.Block{},
}
cdb.latest.Store(types.BlockNo(0))
return cdb
}
func (cdb *ChainDB) Init(dbType string, dataDir string) error {
if cdb.store == nil {
dbPath := common.PathMkdirAll(dataDir, chainDBName)
cdb.store = db.NewDB(db.ImplType(dbType), dbPath)
}
// load data
if err := cdb.loadChainData(); err != nil {
return err
}
// // if empty then create new genesis block
// // if cdb.getBestBlockNo() == 0 && len(cdb.blocks) == 0 {
// blockIdx := types.BlockNoToBytes(0)
// blockHash := cdb.store.Get(blockIdx)
// if cdb.getBestBlockNo() == 0 && (blockHash == nil || len(blockHash) == 0) {
// cdb.generateGenesisBlock(seed)
// }
return nil
}
func (cdb *ChainDB) Close() {
if cdb.store != nil {
cdb.store.Close()
}
return
}
// Get returns the value corresponding to key from the chain DB.
func (cdb *ChainDB) Get(key []byte) []byte {
return cdb.store.Get(key)
}
func (cdb *ChainDB) GetBestBlock() (*types.Block, error) {
//logger.Debug().Uint64("blockno", blockNo).Msg("get best block")
var block *types.Block
aopv := cdb.bestBlock.Load()
if aopv != nil {
block = aopv.(*types.Block)
}
return block, nil
}
func (cdb *ChainDB) loadChainData() error {
latestBytes := cdb.store.Get(latestKey)
if latestBytes == nil || len(latestBytes) == 0 {
return nil
}
latestNo := types.BlockNoFromBytes(latestBytes)
/* TODO: just checking DB
cdb.blocks = make([]*types.Block, latestNo+1)
for i := uint32(0); i <= latestNo; i++ {
blockIdx := types.BlockNoToBytes(i)
buf := types.Block{}
err := cdb.loadData(blockIdx, &buf)
if err != nil {
return err
}
bHash := buf.CalculateBlockHash()
if buf.Hash == nil {
buf.Hash = bHash
} else if !bytes.Equal(buf.Hash, bHash) {
return fmt.Errorf("invalid Block Hash: hash=%s, check=%s",
enc.ToString(buf.Hash), enc.ToString(bHash))
}
for _, v := range buf.Body.Txs {
tHash := v.CalculateTxHash()
if v.Hash == nil {
v.Hash = tHash
} else if !bytes.Equal(v.Hash, tHash) {
return fmt.Errorf("invalid Transaction Hash: hash=%s, check=%s",
enc.ToString(v.Hash), enc.ToString(tHash))
}
}
cdb.blocks[i] = &buf
}
*/
latestBlock, err := cdb.GetBlockByNo(latestNo)
if err != nil {
return ErrorLoadBestBlock
}
cdb.setLatest(latestBlock)
// skips := true
// for i, _ := range cdb.blocks {
// if i > 3 && i+3 <= cdb.getBestBlockNo() {
// if skips {
// skips = false
// //logger.Info(" ...")
// }
// continue
// }
// //logger.Info("- loaded:", i, ToJSON(v))
// }
return nil
}
func (cdb *ChainDB) loadData(key []byte, pb proto.Message) error {
buf := cdb.store.Get(key)
if buf == nil || len(buf) == 0 {
return fmt.Errorf("failed to load data: key=%v", key)
}
//logger.Debugf(" loadData: key=%d, len=%d, val=%s\n", Btoi(key), len(buf), enc.ToString(buf))
err := proto.Unmarshal(buf, pb)
if err != nil {
return fmt.Errorf("failed to unmarshal: key=%v, len=%d", key, len(buf))
}
//logger.Debug(" loaded: ", ToJSON(pb))
return nil
}
func (cdb *ChainDB) addGenesisBlock(genesis *types.Genesis) error {
block := genesis.Block()
tx := cdb.store.NewTx()
if err := cdb.addBlock(&tx, block); err != nil {
return err
}
cdb.connectToChain(&tx, block)
tx.Set([]byte(genesisKey), genesis.Bytes())
tx.Commit()
logger.Info().Msg("Genesis Block Added")
return nil
}
// GetGenesisInfo returns Genesis info from cdb.
func (cdb *ChainDB) GetGenesisInfo() *types.Genesis {
if b := cdb.Get([]byte(genesisKey)); len(b) != 0 {
return types.GetGenesisFromBytes(b)
}
return nil
}
func (cdb *ChainDB) setLatest(newBestBlock *types.Block) (oldLatest types.BlockNo) {
oldLatest = cdb.getBestBlockNo()
newLatest := types.BlockNo(newBestBlock.GetHeader().GetBlockNo())
cdb.latest.Store(newLatest)
cdb.bestBlock.Store(newBestBlock)
logger.Debug().Uint64("old", oldLatest).Uint64("new", newLatest).Msg("update latest block")
return
}
func (cdb *ChainDB) connectToChain(dbtx *db.Transaction, block *types.Block) (oldLatest types.BlockNo) {
blockNo := block.GetHeader().GetBlockNo()
blockIdx := types.BlockNoToBytes(blockNo)
// Update best block hash
(*dbtx).Set(latestKey, blockIdx)
(*dbtx).Set(blockIdx, block.BlockHash())
// Save the last consensus status.
if cdb.cc != nil {
if err := cdb.cc.Save(*dbtx); err != nil {
logger.Error().Err(err).Msg("failed to save DPoS status")
}
}
oldLatest = cdb.setLatest(block)
logger.Debug().Str("hash", block.ID()).Msg("connect block to mainchain")
return
}
func (cdb *ChainDB) swapChain(newBlocks []*types.Block) error {
oldNo := cdb.getBestBlockNo()
newNo := newBlocks[0].GetHeader().GetBlockNo()
if oldNo >= newNo {
logger.Error().Uint64("old", oldNo).Uint64("new", newNo).
Msg("New chain is not longger than old chain")
return ErrInvalidSwapChain
}
var blockIdx []byte
var dbTx db.Transaction
txCnt := 0
dbTx = cdb.store.NewTx()
defer dbTx.Discard()
//make newTx because of batchsize limit of DB
getNewTx := func(remainTxCnt int) {
if txCnt+remainTxCnt >= TxBatchMax {
dbTx.Commit()
dbTx = cdb.store.NewTx()
txCnt = 0
}
}
for i := len(newBlocks) - 1; i >= 0; i-- {
block := newBlocks[i]
blockIdx = types.BlockNoToBytes(block.GetHeader().GetBlockNo())
dbTx.Set(blockIdx, block.BlockHash())
txCnt++
getNewTx(0)
}
getNewTx(5)
dbTx.Set(latestKey, blockIdx)
// Save the last consensus status.
cdb.cc.Save(dbTx)
dbTx.Commit()
cdb.setLatest(newBlocks[0])
return nil
}
func (cdb *ChainDB) isMainChain(block *types.Block) (bool, error) {
blockNo := block.GetHeader().GetBlockNo()
bestNo := cdb.getBestBlockNo()
if blockNo > 0 && blockNo != bestNo+1 {
logger.Debug().Uint64("blkno", blockNo).Uint64("latest", bestNo).Msg("block is branch")
return false, nil
}
prevHash := block.GetHeader().GetPrevBlockHash()
latestHash, err := cdb.getHashByNo(cdb.getBestBlockNo())
if err != nil { //need assertion
return false, fmt.Errorf("failed to getting block hash by no(%v)", cdb.getBestBlockNo())
}
isMainChain := bytes.Equal(prevHash, latestHash)
logger.Debug().Bool("isMainChain", isMainChain).Uint64("blkno", blockNo).Str("hash", block.ID()).
Str("latest", enc.ToString(latestHash)).Str("prev", enc.ToString(prevHash)).
Msg("check if block is in main chain")
return isMainChain, nil
}
type txInfo struct {
blockHash []byte
idx int
}
func (cdb *ChainDB) addTxsOfBlock(dbTx *db.Transaction, txs []*types.Tx, blockHash []byte) error {
for i, txEntry := range txs {
if err := cdb.addTx(dbTx, txEntry, blockHash, i); err != nil {
logger.Error().Err(err).Str("hash", enc.ToString(blockHash)).Int("txidx", i).
Msg("failed to add tx")
return err
}
}
return nil
}
// stor tx info to DB
func (cdb *ChainDB) addTx(dbtx *db.Transaction, tx *types.Tx, blockHash []byte, idx int) error {
txidx := types.TxIdx{
BlockHash: blockHash,
Idx: int32(idx),
}
txidxbytes, err := proto.Marshal(&txidx)
if err != nil {
return err
}
(*dbtx).Set(tx.Hash, txidxbytes)
return nil
}
func (cdb *ChainDB) deleteTx(dbtx *db.Transaction, tx *types.Tx) {
(*dbtx).Delete(tx.Hash)
}
// store block info to DB
func (cdb *ChainDB) addBlock(dbtx *db.Transaction, block *types.Block) error {
blockNo := block.GetHeader().GetBlockNo()
// TODO: Is it possible?
// if blockNo != 0 && isMainChain && cdb.getBestBlockNo()+1 != blockNo {
// return fmt.Errorf("failed to add block(%d,%v). blkno != latestNo(%d) + 1", blockNo,
// block.BlockHash(), cdb.getBestBlockNo())
// }
// FIXME: blockNo 0 exception handling
// assumption: not an orphan
// fork can be here
logger.Debug().Uint64("blockNo", blockNo).Str("hash", block.ID()).Msg("add block to db")
blockBytes, err := proto.Marshal(block)
if err != nil {
return err
}
//add block
(*dbtx).Set(block.BlockHash(), blockBytes)
return nil
}
func (cdb *ChainDB) getBestBlockNo() (latestNo types.BlockNo) {
aopv := cdb.latest.Load()
if aopv != nil {
latestNo = aopv.(types.BlockNo)
} else {
panic("ChainDB:latest is nil")
}
return latestNo
}
// GetBlockByNo returns the block with its block number as blockNo.
func (cdb *ChainDB) GetBlockByNo(blockNo types.BlockNo) (*types.Block, error) {
blockHash, err := cdb.getHashByNo(blockNo)
if err != nil {
return nil, err
}
//logger.Debugf("getblockbyNo No=%d Hash=%v", blockNo, enc.ToString(blockHash))
return cdb.getBlock(blockHash)
}
func (cdb *ChainDB) getBlock(blockHash []byte) (*types.Block, error) {
if blockHash == nil {
return nil, fmt.Errorf("block hash invalid(nil)")
}
buf := types.Block{}
err := cdb.loadData(blockHash, &buf)
if err != nil || !bytes.Equal(buf.Hash, blockHash) {
return nil, &ErrNoBlock{id: blockHash}
}
//logger.Debugf("getblockbyHash Hash=%v", enc.ToString(blockHash))
return &buf, nil
}
func (cdb *ChainDB) getHashByNo(blockNo types.BlockNo) ([]byte, error) {
blockIdx := types.BlockNoToBytes(blockNo)
if cdb.store == nil {
return nil, ErrNoChainDB
}
blockHash := cdb.store.Get(blockIdx)
if len(blockHash) == 0 {
return nil, &ErrNoBlock{id: blockNo}
}
return blockHash, nil
}
func (cdb *ChainDB) getTx(txHash []byte) (*types.Tx, *types.TxIdx, error) {
txIdx := &types.TxIdx{}
err := cdb.loadData(txHash, txIdx)
if err != nil {
return nil, nil, fmt.Errorf("tx not found: txHash=%v", enc.ToString(txHash))
}
block, err := cdb.getBlock(txIdx.BlockHash)
if err != nil {
return nil, nil, &ErrNoBlock{txIdx.BlockHash}
}
txs := block.GetBody().GetTxs()
if txIdx.Idx >= int32(len(txs)) {
return nil, nil, fmt.Errorf("wrong tx idx: %d", txIdx.Idx)
}
tx := txs[txIdx.Idx]
logger.Debug().Str("hash", enc.ToString(txHash)).Msg("getTx")
return tx, txIdx, nil
}
func (cdb *ChainDB) getReceipt(blockHash []byte, blockNo types.BlockNo, idx int32) (*types.Receipt, error) {
data := cdb.store.Get(receiptsKey(blockHash, blockNo))
if len(data) == 0 {
return nil, errors.New("cannot find a receipt")
}
var b bytes.Buffer
b.Write(data)
var receipts types.Receipts
decoder := gob.NewDecoder(&b)
decoder.Decode(&receipts)
if idx < 0 || idx > int32(len(receipts)) {
return nil, fmt.Errorf("cannot find a receipt: invalid index (%d)", idx)
}
return receipts[idx], nil
}
type ChainTree struct {
Tree []ChainInfo
}
type ChainInfo struct {
Height types.BlockNo
Hash string
}
func (cdb *ChainDB) GetChainTree() ([]byte, error) {
tree := make([]ChainInfo, 0)
var i uint64
for i = 0; i < cdb.getBestBlockNo(); i++ {
hash, _ := cdb.getHashByNo(i)
tree = append(tree, ChainInfo{
Height: i,
Hash: enc.ToString(hash),
})
logger.Info().Str("hash", enc.ToString(hash)).Msg("GetChainTree")
}
jsonBytes, err := json.Marshal(tree)
if err != nil {
logger.Info().Msg("GetChainTree failed")
}
return jsonBytes, nil
}
func (cdb *ChainDB) writeReceipts(blockHash []byte, blockNo types.BlockNo, receipts types.Receipts) {
dbTx := cdb.store.NewTx()
defer dbTx.Discard()
var val bytes.Buffer
gob := gob.NewEncoder(&val)
gob.Encode(receipts)
dbTx.Set(receiptsKey(blockHash, blockNo), val.Bytes())
dbTx.Commit()
}
func receiptsKey(blockHash []byte, blockNo types.BlockNo) []byte {
var key bytes.Buffer
key.Write(receiptsPrefix)
key.Write(blockHash)
l := make([]byte, 8)
binary.LittleEndian.PutUint64(l[:], blockNo)
key.Write(l)
return key.Bytes()
}