forked from YuraFjrosr/xfsgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chaindb.go
114 lines (100 loc) · 2.79 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
// Copyright 2018 The xfsgo Authors
// This file is part of the xfsgo library.
//
// The xfsgo library is free software: you can redistribute it and/or modify
// it under the terms of the MIT Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The xfsgo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// MIT Lesser General Public License for more details.
//
// You should have received a copy of the MIT Lesser General Public License
// along with the xfsgo library. If not, see <https://mit-license.org/>.
package xfsgo
import (
"encoding/binary"
"xfsgo/common"
"xfsgo/common/rawencode"
"xfsgo/storage/badger"
)
var (
blockHashPre = []byte("bh:")
blockNumPre = []byte("bn:")
lastBlockKey = []byte("LastBlock")
)
type chainDB struct {
storage *badger.Storage
}
func newChainDB(db *badger.Storage) *chainDB {
tdb := &chainDB{
storage: db,
}
return tdb
}
func (db *chainDB) newWriteBatch() *badger.StorageWriteBatch {
return db.storage.NewWriteBatch()
}
func (db *chainDB) commitBatch(batch *badger.StorageWriteBatch) error {
return db.storage.CommitWriteBatch(batch)
}
func (db *chainDB) GetBlockByHash(hash common.Hash) *Block {
key := append(blockHashPre, hash.Bytes()...)
val, err := db.storage.GetData(key)
if err != nil {
return nil
}
block := &Block{}
if err := rawencode.Decode(val, block); err != nil {
return nil
}
return block
}
func (db *chainDB) GetBlockByNumber(num uint64) *Block {
var numBuf [8]byte
binary.LittleEndian.PutUint64(numBuf[:], num)
key := append(blockNumPre, numBuf[:]...)
val, err := db.storage.GetData(key)
if err != nil {
return nil
}
hash := common.Bytes2Hash(val)
return db.GetBlockByHash(hash)
}
func (db *chainDB) GetHeadBlock() *Block {
val, err := db.storage.GetData(lastBlockKey)
if err != nil {
return nil
}
hash := common.Bytes2Hash(val)
return db.GetBlockByHash(hash)
}
func (db *chainDB) WriteBlock(block *Block) error {
hash := block.Hash()
key := append(blockHashPre, hash.Bytes()...)
val, err := rawencode.Encode(block)
if err != nil {
return err
}
return db.storage.SetData(key, val)
}
func (db *chainDB) WriteCanonNumber(block *Block) error {
var numBuf [8]byte
binary.LittleEndian.PutUint64(numBuf[:], block.Height())
key := append(blockNumPre, numBuf[:]...)
blockHash := block.Hash()
err := db.storage.SetData(key, blockHash.Bytes())
if err != nil {
return err
}
return nil
}
func (db *chainDB) WriteHead(block *Block) error {
if err := db.WriteCanonNumber(block); err != nil {
return err
}
blockHash := block.Hash()
return db.storage.SetData(lastBlockKey, blockHash.Bytes())
}