blob: 8282923fbbc8f6c6ce1ff50eff1531ba84f06b5b (
plain) (
blame)
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
|
package blockdb
import (
coreCommon "github.com/dexon-foundation/dexon-consensus/common"
coreBlockdb "github.com/dexon-foundation/dexon-consensus/core/blockdb"
coreTypes "github.com/dexon-foundation/dexon-consensus/core/types"
"github.com/dexon-foundation/dexon/common"
"github.com/dexon-foundation/dexon/core/rawdb"
"github.com/dexon-foundation/dexon/ethdb"
)
// BlockDB implement dexon-consensus BlockDatabase interface.
type BlockDB struct {
db ethdb.Database
}
func NewDatabase(db ethdb.Database) *BlockDB {
return &BlockDB{db}
}
func (d *BlockDB) Has(hash coreCommon.Hash) bool {
return rawdb.HasCoreBlock(d.db, common.Hash(hash))
}
func (d *BlockDB) Get(hash coreCommon.Hash) (coreTypes.Block, error) {
block := rawdb.ReadCoreBlock(d.db, common.Hash(hash))
if block == nil {
return coreTypes.Block{}, coreBlockdb.ErrBlockDoesNotExist
}
return *block, nil
}
func (d *BlockDB) GetAll() (coreBlockdb.BlockIterator, error) {
return nil, coreBlockdb.ErrNotImplemented
}
func (d *BlockDB) Update(block coreTypes.Block) error {
if !d.Has(block.Hash) {
return coreBlockdb.ErrBlockDoesNotExist
}
rawdb.WriteCoreBlock(d.db, common.Hash(block.Hash), &block)
return nil
}
func (d *BlockDB) Put(block coreTypes.Block) error {
if d.Has(block.Hash) {
return coreBlockdb.ErrBlockExists
}
rawdb.WriteCoreBlock(d.db, common.Hash(block.Hash), &block)
return nil
}
func (d *BlockDB) Close() error { return nil }
|