diff options
author | Mission Liao <mission.liao@dexon.org> | 2018-12-18 10:02:30 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-09 21:32:54 +0800 |
commit | 1c506268a5385adce7885b7e950707bf9ffd5084 (patch) | |
tree | 20fdeb4fffc2f69e493fba1f8771441fa3db484c /core | |
parent | c972cddcfb5d5b16efdf08135b4f7782d796764c (diff) | |
download | dexon-1c506268a5385adce7885b7e950707bf9ffd5084.tar dexon-1c506268a5385adce7885b7e950707bf9ffd5084.tar.gz dexon-1c506268a5385adce7885b7e950707bf9ffd5084.tar.bz2 dexon-1c506268a5385adce7885b7e950707bf9ffd5084.tar.lz dexon-1c506268a5385adce7885b7e950707bf9ffd5084.tar.xz dexon-1c506268a5385adce7885b7e950707bf9ffd5084.tar.zst dexon-1c506268a5385adce7885b7e950707bf9ffd5084.zip |
vendor: sync to latest core (#91)
- Implement new methods in db to cache DKG
private key.
- Implement new methods in db to cache compaction
chain tip.
Diffstat (limited to 'core')
-rw-r--r-- | core/rawdb/accessors_core_chain_tip.go | 49 | ||||
-rw-r--r-- | core/rawdb/accessors_core_dkg_private_key.go | 48 | ||||
-rw-r--r-- | core/rawdb/schema.go | 12 |
3 files changed, 108 insertions, 1 deletions
diff --git a/core/rawdb/accessors_core_chain_tip.go b/core/rawdb/accessors_core_chain_tip.go new file mode 100644 index 000000000..95fa8854f --- /dev/null +++ b/core/rawdb/accessors_core_chain_tip.go @@ -0,0 +1,49 @@ +package rawdb + +import ( + "bytes" + + coreCommon "github.com/dexon-foundation/dexon-consensus/common" + "github.com/dexon-foundation/dexon/log" + "github.com/dexon-foundation/dexon/rlp" +) + +func ReadCoreCompactionChainTipRLP(db DatabaseReader) (rlp.RawValue, error) { + return db.Get(coreCompactionChainTipKey) +} + +func WriteCoreCompactionChainTipRLP(db DatabaseWriter, rlp rlp.RawValue) error { + if err := db.Put(coreCompactionChainTipKey, rlp); err != nil { + log.Crit("Failed to store core compaction chain tip") + return err + } + return nil +} + +func ReadCoreCompactionChainTip(db DatabaseReader) (coreCommon.Hash, uint64) { + data, err := ReadCoreCompactionChainTipRLP(db) + if err != nil { + return coreCommon.Hash{}, 0 + } + v := struct { + Height uint64 + Hash coreCommon.Hash + }{} + if err := rlp.Decode(bytes.NewReader(data), &v); err != nil { + log.Error("Invalid core compaction chain tip RLP", "err", err) + return coreCommon.Hash{}, 0 + } + return v.Hash, v.Height +} + +func WriteCoreCompactionChainTip(db DatabaseWriter, hash coreCommon.Hash, height uint64) error { + data, err := rlp.EncodeToBytes(&struct { + Height uint64 + Hash coreCommon.Hash + }{height, hash}) + if err != nil { + log.Crit("Failed to RLP encode core compaction chain tip", "err", err) + return err + } + return WriteCoreCompactionChainTipRLP(db, data) +} diff --git a/core/rawdb/accessors_core_dkg_private_key.go b/core/rawdb/accessors_core_dkg_private_key.go new file mode 100644 index 000000000..ac51ca5ec --- /dev/null +++ b/core/rawdb/accessors_core_dkg_private_key.go @@ -0,0 +1,48 @@ +package rawdb + +import ( + "bytes" + + coreDKG "github.com/dexon-foundation/dexon-consensus/core/crypto/dkg" + "github.com/dexon-foundation/dexon/log" + "github.com/dexon-foundation/dexon/rlp" +) + +func ReadCoreDKGPrivateKeyRLP(db DatabaseReader, round uint64) rlp.RawValue { + data, _ := db.Get(coreDKGPrivateKeyKey(round)) + return data +} + +func WriteCoreDKGPrivateKeyRLP(db DatabaseWriter, round uint64, rlp rlp.RawValue) error { + err := db.Put(coreDKGPrivateKeyKey(round), rlp) + if err != nil { + log.Crit("Failed to store core DKG private key", "err", err, "round", round) + } + return err +} + +func HasCoreDKGPrivateKey(db DatabaseReader, round uint64) (bool, error) { + return db.Has(coreDKGPrivateKeyKey(round)) +} + +func ReadCoreDKGPrivateKey(db DatabaseReader, round uint64) *coreDKG.PrivateKey { + data := ReadCoreDKGPrivateKeyRLP(db, round) + if len(data) == 0 { + return nil + } + key := new(coreDKG.PrivateKey) + if err := rlp.Decode(bytes.NewReader(data), key); err != nil { + log.Error("Invalid core DKG private key RLP", "round", round, "err", err) + return nil + } + return key +} + +func WriteCoreDKGPrivateKey(db DatabaseWriter, round uint64, key *coreDKG.PrivateKey) error { + data, err := rlp.EncodeToBytes(key) + if err != nil { + log.Crit("Failed to RLP encode core DKG private key", "round", round, "err", err) + return err + } + return WriteCoreDKGPrivateKeyRLP(db, round, data) +} diff --git a/core/rawdb/schema.go b/core/rawdb/schema.go index 9a820a578..87a56c0ba 100644 --- a/core/rawdb/schema.go +++ b/core/rawdb/schema.go @@ -53,7 +53,9 @@ 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") + coreBlockPrefix = []byte("D") + coreDKGPrivateKeyPrefix = []byte("DPK") + coreCompactionChainTipKey = []byte("CoreChainTip") preimagePrefix = []byte("secure-key-") // preimagePrefix + hash -> preimage configPrefix = []byte("ethereum-config-") // config prefix for the db @@ -120,6 +122,14 @@ func coreBlockKey(hash common.Hash) []byte { return append(coreBlockPrefix, hash.Bytes()...) } +// coreDKGPrivateKeyKey = coreDKGPrivateKeyPrefix + round +func coreDKGPrivateKeyKey(round uint64) []byte { + ret := make([]byte, len(coreDKGPrivateKeyPrefix)+8) + copy(ret, coreDKGPrivateKeyPrefix) + binary.LittleEndian.PutUint64(ret[len(coreDKGPrivateKeyPrefix):], round) + return ret +} + // 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()...) |