diff options
author | Sonic <sonic@dexon.org> | 2018-11-20 12:05:00 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-09 21:32:53 +0800 |
commit | e73d666ae31efc896f9b359a8262e97a7fd36b78 (patch) | |
tree | 7e33b900a126ef4622b262d10f9ca0a2ff939a58 /dex/blockdb | |
parent | 91c679269a435b91396cfd12ff95c86dabe8efd5 (diff) | |
download | dexon-e73d666ae31efc896f9b359a8262e97a7fd36b78.tar dexon-e73d666ae31efc896f9b359a8262e97a7fd36b78.tar.gz dexon-e73d666ae31efc896f9b359a8262e97a7fd36b78.tar.bz2 dexon-e73d666ae31efc896f9b359a8262e97a7fd36b78.tar.lz dexon-e73d666ae31efc896f9b359a8262e97a7fd36b78.tar.xz dexon-e73d666ae31efc896f9b359a8262e97a7fd36b78.tar.zst dexon-e73d666ae31efc896f9b359a8262e97a7fd36b78.zip |
dex: add BlockDB, which implements consensus core's blockdb.BlockDatabase (#36)
Diffstat (limited to 'dex/blockdb')
-rw-r--r-- | dex/blockdb/db.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/dex/blockdb/db.go b/dex/blockdb/db.go new file mode 100644 index 000000000..8282923fb --- /dev/null +++ b/dex/blockdb/db.go @@ -0,0 +1,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 } |