aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/dexon-foundation/dexon-consensus/core/db
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/dexon-foundation/dexon-consensus/core/db')
-rw-r--r--vendor/github.com/dexon-foundation/dexon-consensus/core/db/interfaces.go5
-rw-r--r--vendor/github.com/dexon-foundation/dexon-consensus/core/db/level-db.go36
-rw-r--r--vendor/github.com/dexon-foundation/dexon-consensus/core/db/memory.go27
3 files changed, 36 insertions, 32 deletions
diff --git a/vendor/github.com/dexon-foundation/dexon-consensus/core/db/interfaces.go b/vendor/github.com/dexon-foundation/dexon-consensus/core/db/interfaces.go
index 2c32ebb6e..a571a8021 100644
--- a/vendor/github.com/dexon-foundation/dexon-consensus/core/db/interfaces.go
+++ b/vendor/github.com/dexon-foundation/dexon-consensus/core/db/interfaces.go
@@ -80,8 +80,7 @@ type Reader interface {
GetCompactionChainTipInfo() (common.Hash, uint64)
// DKG Private Key related methods.
- HasDKGPrivateKey(round uint64) (bool, error)
- GetDKGPrivateKey(round uint64) (dkg.PrivateKey, error)
+ GetDKGPrivateKey(round, reset uint64) (dkg.PrivateKey, error)
GetDKGProtocol() (dkgProtocol DKGProtocolInfo, err error)
}
@@ -90,7 +89,7 @@ type Writer interface {
UpdateBlock(block types.Block) error
PutBlock(block types.Block) error
PutCompactionChainTipInfo(common.Hash, uint64) error
- PutDKGPrivateKey(uint64, dkg.PrivateKey) error
+ PutDKGPrivateKey(round, reset uint64, pk dkg.PrivateKey) error
PutOrUpdateDKGProtocol(dkgProtocol DKGProtocolInfo) error
}
diff --git a/vendor/github.com/dexon-foundation/dexon-consensus/core/db/level-db.go b/vendor/github.com/dexon-foundation/dexon-consensus/core/db/level-db.go
index 52968331e..da8bc0bc1 100644
--- a/vendor/github.com/dexon-foundation/dexon-consensus/core/db/level-db.go
+++ b/vendor/github.com/dexon-foundation/dexon-consensus/core/db/level-db.go
@@ -59,6 +59,11 @@ type DKGProtocolInfo struct {
Reset uint64
}
+type dkgPrivateKey struct {
+ PK dkg.PrivateKey
+ Reset uint64
+}
+
// Equal compare with target DKGProtocolInfo.
func (info *DKGProtocolInfo) Equal(target *DKGProtocolInfo) bool {
if !info.ID.Equal(target.ID) ||
@@ -478,13 +483,8 @@ func (lvl *LevelDBBackedDB) GetCompactionChainTipInfo() (
return
}
-// HasDKGPrivateKey check existence of DKG private key of one round.
-func (lvl *LevelDBBackedDB) HasDKGPrivateKey(round uint64) (bool, error) {
- return lvl.db.Has(lvl.getDKGPrivateKeyKey(round), nil)
-}
-
// GetDKGPrivateKey get DKG private key of one round.
-func (lvl *LevelDBBackedDB) GetDKGPrivateKey(round uint64) (
+func (lvl *LevelDBBackedDB) GetDKGPrivateKey(round, reset uint64) (
prv dkg.PrivateKey, err error) {
queried, err := lvl.db.Get(lvl.getDKGPrivateKeyKey(round), nil)
if err != nil {
@@ -493,22 +493,32 @@ func (lvl *LevelDBBackedDB) GetDKGPrivateKey(round uint64) (
}
return
}
- err = rlp.DecodeBytes(queried, &prv)
+ pk := dkgPrivateKey{}
+ err = rlp.DecodeBytes(queried, &pk)
+ if pk.Reset != reset {
+ err = ErrDKGPrivateKeyDoesNotExist
+ return
+ }
+ prv = pk.PK
return
}
// PutDKGPrivateKey save DKG private key of one round.
func (lvl *LevelDBBackedDB) PutDKGPrivateKey(
- round uint64, prv dkg.PrivateKey) error {
+ round, reset uint64, prv dkg.PrivateKey) error {
// Check existence.
- exists, err := lvl.HasDKGPrivateKey(round)
- if err != nil {
+ _, err := lvl.GetDKGPrivateKey(round, reset)
+ if err == nil {
+ return ErrDKGPrivateKeyExists
+ }
+ if err != ErrDKGPrivateKeyDoesNotExist {
return err
}
- if exists {
- return ErrDKGPrivateKeyExists
+ pk := &dkgPrivateKey{
+ PK: prv,
+ Reset: reset,
}
- marshaled, err := rlp.EncodeToBytes(&prv)
+ marshaled, err := rlp.EncodeToBytes(&pk)
if err != nil {
return err
}
diff --git a/vendor/github.com/dexon-foundation/dexon-consensus/core/db/memory.go b/vendor/github.com/dexon-foundation/dexon-consensus/core/db/memory.go
index 971f758d5..6555de855 100644
--- a/vendor/github.com/dexon-foundation/dexon-consensus/core/db/memory.go
+++ b/vendor/github.com/dexon-foundation/dexon-consensus/core/db/memory.go
@@ -49,7 +49,7 @@ type MemBackedDB struct {
compactionChainTipHash common.Hash
compactionChainTipHeight uint64
dkgPrivateKeysLock sync.RWMutex
- dkgPrivateKeys map[uint64]*dkg.PrivateKey
+ dkgPrivateKeys map[uint64]*dkgPrivateKey
dkgProtocolLock sync.RWMutex
dkgProtocolInfo *DKGProtocolInfo
persistantFilePath string
@@ -61,7 +61,7 @@ func NewMemBackedDB(persistantFilePath ...string) (
dbInst = &MemBackedDB{
blockHashSequence: common.Hashes{},
blocksByHash: make(map[common.Hash]*types.Block),
- dkgPrivateKeys: make(map[uint64]*dkg.PrivateKey),
+ dkgPrivateKeys: make(map[uint64]*dkgPrivateKey),
}
if len(persistantFilePath) == 0 || len(persistantFilePath[0]) == 0 {
return
@@ -168,34 +168,29 @@ 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) (
+func (m *MemBackedDB) GetDKGPrivateKey(round, reset uint64) (
dkg.PrivateKey, error) {
m.dkgPrivateKeysLock.RLock()
defer m.dkgPrivateKeysLock.RUnlock()
- if prv, exists := m.dkgPrivateKeys[round]; exists {
- return *prv, nil
+ if prv, exists := m.dkgPrivateKeys[round]; exists && prv.Reset == reset {
+ return prv.PK, nil
}
return dkg.PrivateKey{}, ErrDKGPrivateKeyDoesNotExist
}
// PutDKGPrivateKey save DKG private key of one round.
func (m *MemBackedDB) PutDKGPrivateKey(
- round uint64, prv dkg.PrivateKey) error {
+ round, reset uint64, prv dkg.PrivateKey) error {
m.dkgPrivateKeysLock.Lock()
defer m.dkgPrivateKeysLock.Unlock()
- if _, exists := m.dkgPrivateKeys[round]; exists {
+ if prv, exists := m.dkgPrivateKeys[round]; exists && prv.Reset == reset {
return ErrDKGPrivateKeyExists
}
- m.dkgPrivateKeys[round] = &prv
+ m.dkgPrivateKeys[round] = &dkgPrivateKey{
+ PK: prv,
+ Reset: reset,
+ }
return nil
}