From 3ac5e2363b00fde97288baa173c3d89c1b2e5ec1 Mon Sep 17 00:00:00 2001
From: Jimmy Hu <jimmy.hu@dexon.org>
Date: Wed, 13 Mar 2019 18:19:53 +0800
Subject: vendor: sync to latest core (#253)

---
 .../accessors_core_dkg_master_private_shares.go    | 44 ++++++++++++++++++++++
 core/rawdb/schema.go                               | 15 ++++++--
 2 files changed, 56 insertions(+), 3 deletions(-)
 create mode 100644 core/rawdb/accessors_core_dkg_master_private_shares.go

(limited to 'core/rawdb')

diff --git a/core/rawdb/accessors_core_dkg_master_private_shares.go b/core/rawdb/accessors_core_dkg_master_private_shares.go
new file mode 100644
index 000000000..23b37f361
--- /dev/null
+++ b/core/rawdb/accessors_core_dkg_master_private_shares.go
@@ -0,0 +1,44 @@
+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 ReadCoreDKGMasterPrivateSharesRLP(db DatabaseReader, round uint64) rlp.RawValue {
+	data, _ := db.Get(coreDKGMasterPrivateSharesKey(round))
+	return data
+}
+
+func WriteCoreDKGMasterPrivateSharesRLP(db DatabaseWriter, round uint64, rlp rlp.RawValue) error {
+	err := db.Put(coreDKGMasterPrivateSharesKey(round), rlp)
+	if err != nil {
+		log.Crit("Failed to store core DKG private key", "err", err, "round", round)
+	}
+	return err
+}
+
+func ReadCoreDKGMasterPrivateShares(db DatabaseReader, round uint64) *coreDKG.PrivateKeyShares {
+	data := ReadCoreDKGMasterPrivateSharesRLP(db, round)
+	if len(data) == 0 {
+		return nil
+	}
+	shares := new(coreDKG.PrivateKeyShares)
+	if err := rlp.Decode(bytes.NewReader(data), shares); err != nil {
+		log.Error("Invalid core DKG master private shares RLP", "round", round, "err", err)
+		return nil
+	}
+	return shares
+}
+
+func WriteCoreDKGMasterPrivateShares(db DatabaseWriter, round uint64, shares *coreDKG.PrivateKeyShares) error {
+	data, err := rlp.EncodeToBytes(shares)
+	if err != nil {
+		log.Crit("Failed to RLP encode core DKG master private shares", "round", round, "err", err)
+		return err
+	}
+	return WriteCoreDKGMasterPrivateSharesRLP(db, round, data)
+}
diff --git a/core/rawdb/schema.go b/core/rawdb/schema.go
index 87a56c0ba..b469468f4 100644
--- a/core/rawdb/schema.go
+++ b/core/rawdb/schema.go
@@ -53,9 +53,10 @@ 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")
-	coreDKGPrivateKeyPrefix   = []byte("DPK")
-	coreCompactionChainTipKey = []byte("CoreChainTip")
+	coreBlockPrefix                  = []byte("D")
+	coreDKGPrivateKeyPrefix          = []byte("DPK")
+	coreCompactionChainTipKey        = []byte("CoreChainTip")
+	coreDKGMasterPrivateSharesPrefix = []byte("CoreDKGPrv")
 
 	preimagePrefix = []byte("secure-key-")      // preimagePrefix + hash -> preimage
 	configPrefix   = []byte("ethereum-config-") // config prefix for the db
@@ -130,6 +131,14 @@ func coreDKGPrivateKeyKey(round uint64) []byte {
 	return ret
 }
 
+// coreDKGMasterPrivateSharesKey = coreDKGMasterPrivateSharesPrefix + round
+func coreDKGMasterPrivateSharesKey(round uint64) []byte {
+	ret := make([]byte, len(coreDKGMasterPrivateSharesPrefix)+8)
+	copy(ret, coreDKGMasterPrivateSharesPrefix)
+	binary.LittleEndian.PutUint64(ret[len(coreDKGMasterPrivateSharesPrefix):], 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()...)
-- 
cgit v1.2.3