diff options
author | Sonic <sonic@dexon.org> | 2018-11-20 12:05:00 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2018-12-19 20:54:27 +0800 |
commit | 71ff7f2ab77a15ded991e8200eda3e13d0b2f09e (patch) | |
tree | 951451513a34c03e7c91341a1c17fd6aadb87897 | |
parent | aa94dff6df6d6ebfe59aba8de01190ffdc409053 (diff) | |
download | dexon-71ff7f2ab77a15ded991e8200eda3e13d0b2f09e.tar.gz dexon-71ff7f2ab77a15ded991e8200eda3e13d0b2f09e.tar.zst dexon-71ff7f2ab77a15ded991e8200eda3e13d0b2f09e.zip |
dex: add BlockDB, which implements consensus core's blockdb.BlockDatabase (#36)
-rw-r--r-- | core/rawdb/accessors_core_block.go | 51 | ||||
-rw-r--r-- | core/rawdb/schema.go | 7 | ||||
-rw-r--r-- | dex/backend.go | 13 | ||||
-rw-r--r-- | dex/blockdb/db.go | 54 |
4 files changed, 114 insertions, 11 deletions
diff --git a/core/rawdb/accessors_core_block.go b/core/rawdb/accessors_core_block.go new file mode 100644 index 000000000..5fa5c8f86 --- /dev/null +++ b/core/rawdb/accessors_core_block.go @@ -0,0 +1,51 @@ +package rawdb + +import ( + "bytes" + + coreTypes "github.com/dexon-foundation/dexon-consensus/core/types" + + "github.com/dexon-foundation/dexon/common" + "github.com/dexon-foundation/dexon/log" + "github.com/dexon-foundation/dexon/rlp" +) + +func ReadCoreBlockRLP(db DatabaseReader, hash common.Hash) rlp.RawValue { + data, _ := db.Get(coreBlockKey(hash)) + return data +} + +func WriteCoreBlockRLP(db DatabaseWriter, hash common.Hash, rlp rlp.RawValue) { + if err := db.Put(coreBlockKey(hash), rlp); err != nil { + log.Crit("Failed to store core block", "err", err) + } +} + +func HasCoreBlock(db DatabaseReader, hash common.Hash) bool { + if has, err := db.Has(coreBlockKey(hash)); !has || err != nil { + return false + } + return true +} + +func ReadCoreBlock(db DatabaseReader, hash common.Hash) *coreTypes.Block { + data := ReadCoreBlockRLP(db, hash) + if len(data) == 0 { + return nil + } + + block := new(coreTypes.Block) + if err := rlp.Decode(bytes.NewReader(data), block); err != nil { + log.Error("Invalid core block RLP", "hash", hash, "err", err) + return nil + } + return block +} + +func WriteCoreBlock(db DatabaseWriter, hash common.Hash, block *coreTypes.Block) { + data, err := rlp.EncodeToBytes(block) + if err != nil { + log.Crit("Failed to RLP encode core block", "err", err) + } + WriteCoreBlockRLP(db, hash, data) +} diff --git a/core/rawdb/schema.go b/core/rawdb/schema.go index ee1949112..9a820a578 100644 --- a/core/rawdb/schema.go +++ b/core/rawdb/schema.go @@ -53,6 +53,8 @@ var ( txLookupPrefix = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits + coreBlockPrefix = []byte("D") + preimagePrefix = []byte("secure-key-") // preimagePrefix + hash -> preimage configPrefix = []byte("ethereum-config-") // config prefix for the db @@ -113,6 +115,11 @@ func txLookupKey(hash common.Hash) []byte { return append(txLookupPrefix, hash.Bytes()...) } +// coreBlockKey = coreBlockPrefix + hash +func coreBlockKey(hash common.Hash) []byte { + return append(coreBlockPrefix, hash.Bytes()...) +} + // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte { key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...) diff --git a/dex/backend.go b/dex/backend.go index a16c891e1..59593a5d5 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 } |