diff options
author | Sonic <sonic@dexon.org> | 2018-11-20 12:05:00 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-06-12 17:27:18 +0800 |
commit | d035ac7c8c624fc884dbc300a2ec7dcaf8dc1905 (patch) | |
tree | dff387c6fce56b7c7c38f6fb5986c2934e57af2f /core | |
parent | f80773bea1a400c23ed2b626827a4e50b4921c2e (diff) | |
download | go-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 'core')
-rw-r--r-- | core/rawdb/accessors_core_block.go | 51 | ||||
-rw-r--r-- | core/rawdb/schema.go | 7 |
2 files changed, 58 insertions, 0 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()...) |