diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2019-04-11 09:02:21 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-11 09:02:21 +0800 |
commit | f1ed7d074fe1d574a84c308c6ad878dc2d153654 (patch) | |
tree | 25a6fd9088ac34c7f0ffdf8ec4d48b26bf6ae70e | |
parent | 675a9441784b14ee4bd996832c4431adc291c5af (diff) | |
download | dexon-f1ed7d074fe1d574a84c308c6ad878dc2d153654.tar.gz dexon-f1ed7d074fe1d574a84c308c6ad878dc2d153654.tar.zst dexon-f1ed7d074fe1d574a84c308c6ad878dc2d153654.zip |
core: add reset to dkg private key db (#355)
* vendor: sync to latest core
* core: dkg private key db
7 files changed, 92 insertions, 78 deletions
diff --git a/core/rawdb/accessors_core_dkg_private_key.go b/core/rawdb/accessors_core_dkg_private_key.go index ac51ca5ec..036e311aa 100644 --- a/core/rawdb/accessors_core_dkg_private_key.go +++ b/core/rawdb/accessors_core_dkg_private_key.go @@ -8,6 +8,11 @@ import ( "github.com/dexon-foundation/dexon/rlp" ) +type dkgPrivateKey struct { + PK *coreDKG.PrivateKey + Reset uint64 +} + func ReadCoreDKGPrivateKeyRLP(db DatabaseReader, round uint64) rlp.RawValue { data, _ := db.Get(coreDKGPrivateKeyKey(round)) return data @@ -21,24 +26,29 @@ func WriteCoreDKGPrivateKeyRLP(db DatabaseWriter, round uint64, rlp rlp.RawValue return err } -func HasCoreDKGPrivateKey(db DatabaseReader, round uint64) (bool, error) { - return db.Has(coreDKGPrivateKeyKey(round)) -} - -func ReadCoreDKGPrivateKey(db DatabaseReader, round uint64) *coreDKG.PrivateKey { +func ReadCoreDKGPrivateKey(db DatabaseReader, round, reset uint64) *coreDKG.PrivateKey { data := ReadCoreDKGPrivateKeyRLP(db, round) if len(data) == 0 { return nil } - key := new(coreDKG.PrivateKey) + key := &dkgPrivateKey{ + PK: 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 + if key.Reset != reset { + return nil + } + return key.PK } -func WriteCoreDKGPrivateKey(db DatabaseWriter, round uint64, key *coreDKG.PrivateKey) error { +func WriteCoreDKGPrivateKey(db DatabaseWriter, round, reset uint64, pk *coreDKG.PrivateKey) error { + key := &dkgPrivateKey{ + PK: pk, + Reset: reset, + } data, err := rlp.EncodeToBytes(key) if err != nil { log.Crit("Failed to RLP encode core DKG private key", "round", round, "err", err) diff --git a/dex/db/db.go b/dex/db/db.go index 2930400b2..5cb809055 100644 --- a/dex/db/db.go +++ b/dex/db/db.go @@ -69,27 +69,24 @@ func (d *DB) PutBlock(block coreTypes.Block) error { return nil } -func (d *DB) HasDKGPrivateKey(round uint64) (bool, error) { - return rawdb.HasCoreDKGPrivateKey(d.db, round) -} - -func (d *DB) GetDKGPrivateKey(round uint64) (coreDKG.PrivateKey, error) { - key := rawdb.ReadCoreDKGPrivateKey(d.db, round) +func (d *DB) GetDKGPrivateKey(round, reset uint64) (coreDKG.PrivateKey, error) { + key := rawdb.ReadCoreDKGPrivateKey(d.db, round, reset) if key == nil { return coreDKG.PrivateKey{}, coreDb.ErrDKGPrivateKeyDoesNotExist } return *key, nil } -func (d *DB) PutDKGPrivateKey(round uint64, key coreDKG.PrivateKey) error { - has, err := d.HasDKGPrivateKey(round) - if err != nil { - return err - } - if has { +func (d *DB) PutDKGPrivateKey(round, reset uint64, key coreDKG.PrivateKey) error { + _, err := d.GetDKGPrivateKey(round, reset) + if err == nil { return coreDb.ErrDKGPrivateKeyExists } - return rawdb.WriteCoreDKGPrivateKey(d.db, round, &key) + if err != coreDb.ErrDKGPrivateKeyDoesNotExist { + return err + } + + return rawdb.WriteCoreDKGPrivateKey(d.db, round, reset, &key) } func (d *DB) PutCompactionChainTipInfo(hash coreCommon.Hash, height uint64) error { diff --git a/vendor/github.com/dexon-foundation/dexon-consensus/core/configuration-chain.go b/vendor/github.com/dexon-foundation/dexon-consensus/core/configuration-chain.go index 76917a310..e9e04a28c 100644 --- a/vendor/github.com/dexon-foundation/dexon-consensus/core/configuration-chain.go +++ b/vendor/github.com/dexon-foundation/dexon-consensus/core/configuration-chain.go @@ -396,7 +396,8 @@ func (cc *configurationChain) runDKGPhaseNine(round uint64, reset uint64) error return err } // Save private shares to DB. - if err = cc.db.PutDKGPrivateKey(round, *signer.privateKey); err != nil { + if err = + cc.db.PutDKGPrivateKey(round, reset, *signer.privateKey); err != nil { return err } cc.dkgResult.Lock() @@ -615,8 +616,9 @@ func (cc *configurationChain) recoverDKGInfo( }() } if !signerExists && !ignoreSigner { + reset := cc.gov.DKGResetCount(round) // Check if we have private shares in DB. - prvKey, err := cc.db.GetDKGPrivateKey(round) + prvKey, err := cc.db.GetDKGPrivateKey(round, reset) if err != nil { cc.logger.Warn("Failed to create DKGPrivateKey", "round", round, "error", err) @@ -638,7 +640,8 @@ func (cc *configurationChain) recoverDKGInfo( "round", round, "error", err) return err } - if err = cc.db.PutDKGPrivateKey(round, *prvKeyRecover); err != nil { + if err = cc.db.PutDKGPrivateKey( + round, reset, *prvKeyRecover); err != nil { cc.logger.Warn("Failed to save DKGPrivateKey", "round", round, "error", err) } 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 } diff --git a/vendor/vendor.json b/vendor/vendor.json index e07c1a557..7cccb564a 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -141,16 +141,16 @@ { "checksumSHA1": "In6vBHYUsX7DUIGiFN2hQggBgvI=", "path": "github.com/dexon-foundation/dexon-consensus/common", - "revision": "b9492bc09f8bb53b2a36e505ddad0d26373e71a3", - "revisionTime": "2019-04-10T01:52:20Z", + "revision": "1d64520274d4e33b3038550083b78c5ebe1ea45d", + "revisionTime": "2019-04-10T14:52:57Z", "version": "single-chain", "versionExact": "single-chain" }, { - "checksumSHA1": "lY1KAGWRuwd7OpzAz5yN7IysMvU=", + "checksumSHA1": "9uNk7orRE3xuTv2m/y6vO/4uDAg=", "path": "github.com/dexon-foundation/dexon-consensus/core", - "revision": "b9492bc09f8bb53b2a36e505ddad0d26373e71a3", - "revisionTime": "2019-04-10T01:52:20Z", + "revision": "1d64520274d4e33b3038550083b78c5ebe1ea45d", + "revisionTime": "2019-04-10T14:52:57Z", "version": "single-chain", "versionExact": "single-chain" }, @@ -165,64 +165,64 @@ { "checksumSHA1": "tQSbYCu5P00lUhKsx3IbBZCuSLY=", "path": "github.com/dexon-foundation/dexon-consensus/core/crypto", - "revision": "b9492bc09f8bb53b2a36e505ddad0d26373e71a3", - "revisionTime": "2019-04-10T01:52:20Z", + "revision": "1d64520274d4e33b3038550083b78c5ebe1ea45d", + "revisionTime": "2019-04-10T14:52:57Z", "version": "single-chain", "versionExact": "single-chain" }, { "checksumSHA1": "m5lUT04qSHKtFukvxjnFX5Jo2hI=", "path": "github.com/dexon-foundation/dexon-consensus/core/crypto/dkg", - "revision": "b9492bc09f8bb53b2a36e505ddad0d26373e71a3", - "revisionTime": "2019-04-10T01:52:20Z", + "revision": "1d64520274d4e33b3038550083b78c5ebe1ea45d", + "revisionTime": "2019-04-10T14:52:57Z", "version": "single-chain", "versionExact": "single-chain" }, { "checksumSHA1": "BhLKK8RveoLaeXc9UyUKMwQqchU=", "path": "github.com/dexon-foundation/dexon-consensus/core/crypto/ecdsa", - "revision": "b9492bc09f8bb53b2a36e505ddad0d26373e71a3", - "revisionTime": "2019-04-10T01:52:20Z", + "revision": "1d64520274d4e33b3038550083b78c5ebe1ea45d", + "revisionTime": "2019-04-10T14:52:57Z", "version": "single-chain", "versionExact": "single-chain" }, { - "checksumSHA1": "hj/KetWUHp+1CX+50V0QnCthfWc=", + "checksumSHA1": "3Ludp/1V4dMBZH/c1oIVjHj0CqY=", "path": "github.com/dexon-foundation/dexon-consensus/core/db", - "revision": "b9492bc09f8bb53b2a36e505ddad0d26373e71a3", - "revisionTime": "2019-04-10T01:52:20Z", + "revision": "1d64520274d4e33b3038550083b78c5ebe1ea45d", + "revisionTime": "2019-04-10T14:52:57Z", "version": "single-chain", "versionExact": "single-chain" }, { "checksumSHA1": "/OcEQKdtWDyRZibazIsAxJWHUyg=", "path": "github.com/dexon-foundation/dexon-consensus/core/syncer", - "revision": "b9492bc09f8bb53b2a36e505ddad0d26373e71a3", - "revisionTime": "2019-04-10T01:52:20Z", + "revision": "1d64520274d4e33b3038550083b78c5ebe1ea45d", + "revisionTime": "2019-04-10T14:52:57Z", "version": "single-chain", "versionExact": "single-chain" }, { "checksumSHA1": "zIgCdN4FJiAuPGMhB+/9YGK/Wgk=", "path": "github.com/dexon-foundation/dexon-consensus/core/types", - "revision": "b9492bc09f8bb53b2a36e505ddad0d26373e71a3", - "revisionTime": "2019-04-10T01:52:20Z", + "revision": "1d64520274d4e33b3038550083b78c5ebe1ea45d", + "revisionTime": "2019-04-10T14:52:57Z", "version": "single-chain", "versionExact": "single-chain" }, { "checksumSHA1": "lbG7yqVgzo2CV/CQPYjG78xp5jg=", "path": "github.com/dexon-foundation/dexon-consensus/core/types/dkg", - "revision": "b9492bc09f8bb53b2a36e505ddad0d26373e71a3", - "revisionTime": "2019-04-10T01:52:20Z", + "revision": "1d64520274d4e33b3038550083b78c5ebe1ea45d", + "revisionTime": "2019-04-10T14:52:57Z", "version": "single-chain", "versionExact": "single-chain" }, { "checksumSHA1": "1VsJIshz0loXnGwCtrMM8SuIo6Y=", "path": "github.com/dexon-foundation/dexon-consensus/core/utils", - "revision": "b9492bc09f8bb53b2a36e505ddad0d26373e71a3", - "revisionTime": "2019-04-10T01:52:20Z", + "revision": "1d64520274d4e33b3038550083b78c5ebe1ea45d", + "revisionTime": "2019-04-10T14:52:57Z", "version": "single-chain", "versionExact": "single-chain" }, |