aboutsummaryrefslogtreecommitdiffstats
path: root/dex
diff options
context:
space:
mode:
authorSonic <sonic@dexon.org>2018-11-20 12:05:00 +0800
committerWei-Ning Huang <w@byzantine-lab.io>2019-06-12 17:27:18 +0800
commitd035ac7c8c624fc884dbc300a2ec7dcaf8dc1905 (patch)
treedff387c6fce56b7c7c38f6fb5986c2934e57af2f /dex
parentf80773bea1a400c23ed2b626827a4e50b4921c2e (diff)
downloadgo-tangerine-d035ac7c8c624fc884dbc300a2ec7dcaf8dc1905.tar
go-tangerine-d035ac7c8c624fc884dbc300a2ec7dcaf8dc1905.tar.gz
go-tangerine-d035ac7c8c624fc884dbc300a2ec7dcaf8dc1905.tar.bz2
go-tangerine-d035ac7c8c624fc884dbc300a2ec7dcaf8dc1905.tar.lz
go-tangerine-d035ac7c8c624fc884dbc300a2ec7dcaf8dc1905.tar.xz
go-tangerine-d035ac7c8c624fc884dbc300a2ec7dcaf8dc1905.tar.zst
go-tangerine-d035ac7c8c624fc884dbc300a2ec7dcaf8dc1905.zip
dex: add BlockDB, which implements consensus core's blockdb.BlockDatabase (#36)
Diffstat (limited to 'dex')
-rw-r--r--dex/backend.go13
-rw-r--r--dex/blockdb/db.go54
2 files changed, 56 insertions, 11 deletions
diff --git a/dex/backend.go b/dex/backend.go
index 07646fd76..36b28345c 100644
--- a/dex/backend.go
+++ b/dex/backend.go
@@ -19,11 +19,9 @@ package dex
import (
"fmt"
- "path/filepath"
"time"
dexCore "github.com/dexon-foundation/dexon-consensus/core"
- "github.com/dexon-foundation/dexon-consensus/core/blockdb"
coreEcdsa "github.com/dexon-foundation/dexon-consensus/core/crypto/ecdsa"
coreTypes "github.com/dexon-foundation/dexon-consensus/core/types"
@@ -34,6 +32,7 @@ import (
"github.com/dexon-foundation/dexon/core/bloombits"
"github.com/dexon-foundation/dexon/core/rawdb"
"github.com/dexon-foundation/dexon/core/vm"
+ "github.com/dexon-foundation/dexon/dex/blockdb"
"github.com/dexon-foundation/dexon/eth/downloader"
"github.com/dexon-foundation/dexon/eth/filters"
"github.com/dexon-foundation/dexon/eth/gasprice"
@@ -76,7 +75,6 @@ type Dexon struct {
app *DexconApp
governance *DexconGovernance
network *DexconNetwork
- blockdb blockdb.BlockDatabase
consensus *dexCore.Consensus
networkID uint64
@@ -85,12 +83,6 @@ type Dexon struct {
func New(ctx *node.ServiceContext, config *Config) (*Dexon, error) {
// Consensus.
- blockDBPath := filepath.Join(ctx.Config.DataDir, "dexcon", "blockdb")
- db, err := blockdb.NewLevelDBBackedBlockDB(blockDBPath)
- if err != nil {
- panic(err)
- }
-
chainDb, err := CreateDB(ctx, config, "chaindata")
if err != nil {
return nil, err
@@ -122,7 +114,6 @@ func New(ctx *node.ServiceContext, config *Config) (*Dexon, error) {
networkID: config.NetworkId,
bloomRequests: make(chan chan *bloombits.Retrieval),
bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks, params.BloomConfirms),
- blockdb: db,
engine: engine,
}
@@ -184,7 +175,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Dexon, error) {
0, now.Location())
dex.consensus = dexCore.NewConsensus(dMoment,
- dex.app, dex.governance, db, dex.network, privKey, log.Root())
+ dex.app, dex.governance, blockdb.NewDatabase(chainDb), dex.network, privKey, log.Root())
return dex, nil
}
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 }