aboutsummaryrefslogtreecommitdiffstats
path: root/core/db/memory.go
diff options
context:
space:
mode:
Diffstat (limited to 'core/db/memory.go')
-rw-r--r--core/db/memory.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/core/db/memory.go b/core/db/memory.go
index 568c64b..7393de9 100644
--- a/core/db/memory.go
+++ b/core/db/memory.go
@@ -24,6 +24,7 @@ import (
"sync"
"github.com/dexon-foundation/dexon-consensus/common"
+ "github.com/dexon-foundation/dexon-consensus/core/crypto/dkg"
"github.com/dexon-foundation/dexon-consensus/core/types"
)
@@ -47,6 +48,8 @@ type MemBackedDB struct {
compactionChainTipLock sync.RWMutex
compactionChainTipHash common.Hash
compactionChainTipHeight uint64
+ dkgPrivateKeysLock sync.RWMutex
+ dkgPrivateKeys map[uint64]*dkg.PrivateKey
persistantFilePath string
}
@@ -56,6 +59,7 @@ func NewMemBackedDB(persistantFilePath ...string) (
dbInst = &MemBackedDB{
blockHashSequence: common.Hashes{},
blocksByHash: make(map[common.Hash]*types.Block),
+ dkgPrivateKeys: make(map[uint64]*dkg.PrivateKey),
}
if len(persistantFilePath) == 0 || len(persistantFilePath[0]) == 0 {
return
@@ -162,6 +166,37 @@ func (m *MemBackedDB) GetCompactionChainTipInfo() (
return m.compactionChainTipHash, m.compactionChainTipHeight
}
+// HasDKGPrivateKey check existence of DKG private key of one round.
+func (m *MemBackedDB) HasDKGPrivateKey(round uint64) (bool, error) {
+ m.dkgPrivateKeysLock.RLock()
+ defer m.dkgPrivateKeysLock.RUnlock()
+ _, exists := m.dkgPrivateKeys[round]
+ return exists, nil
+}
+
+// GetDKGPrivateKey get DKG private key of one round.
+func (m *MemBackedDB) GetDKGPrivateKey(round uint64) (
+ dkg.PrivateKey, error) {
+ m.dkgPrivateKeysLock.RLock()
+ defer m.dkgPrivateKeysLock.RUnlock()
+ if prv, exists := m.dkgPrivateKeys[round]; exists {
+ return *prv, nil
+ }
+ return dkg.PrivateKey{}, ErrDKGPrivateKeyDoesNotExist
+}
+
+// PutDKGPrivateKey save DKG private key of one round.
+func (m *MemBackedDB) PutDKGPrivateKey(
+ round uint64, prv dkg.PrivateKey) error {
+ m.dkgPrivateKeysLock.Lock()
+ defer m.dkgPrivateKeysLock.Unlock()
+ if _, exists := m.dkgPrivateKeys[round]; exists {
+ return ErrDKGPrivateKeyExists
+ }
+ m.dkgPrivateKeys[round] = &prv
+ return nil
+}
+
// Close implement Closer interface, which would release allocated resource.
func (m *MemBackedDB) Close() (err error) {
// Save internal state to a pretty-print json file. It's a temporary way