aboutsummaryrefslogtreecommitdiffstats
path: root/dex/governance.go
diff options
context:
space:
mode:
authorSonic <sonic@cobinhood.com>2018-10-16 10:33:24 +0800
committerWei-Ning Huang <w@byzantine-lab.io>2019-06-12 17:23:39 +0800
commite5137c6582c1ffb6943842aed88b3a31a31c34f1 (patch)
treeb0d24a70b69d429e7c4a95de1fb3852aafd7fb5a /dex/governance.go
parent16f356dcf34722510c59a1f946a5ffa138bd6da7 (diff)
downloadgo-tangerine-e5137c6582c1ffb6943842aed88b3a31a31c34f1.tar
go-tangerine-e5137c6582c1ffb6943842aed88b3a31a31c34f1.tar.gz
go-tangerine-e5137c6582c1ffb6943842aed88b3a31a31c34f1.tar.bz2
go-tangerine-e5137c6582c1ffb6943842aed88b3a31a31c34f1.tar.lz
go-tangerine-e5137c6582c1ffb6943842aed88b3a31a31c34f1.tar.xz
go-tangerine-e5137c6582c1ffb6943842aed88b3a31a31c34f1.tar.zst
go-tangerine-e5137c6582c1ffb6943842aed88b3a31a31c34f1.zip
dex: gov: using dex-consensus-core NodeSetCache
Diffstat (limited to 'dex/governance.go')
-rw-r--r--dex/governance.go83
1 files changed, 34 insertions, 49 deletions
diff --git a/dex/governance.go b/dex/governance.go
index 32c2d79af..22452dea3 100644
--- a/dex/governance.go
+++ b/dex/governance.go
@@ -7,6 +7,7 @@ import (
"time"
coreCommon "github.com/dexon-foundation/dexon-consensus-core/common"
+ dexCore "github.com/dexon-foundation/dexon-consensus-core/core"
coreCrypto "github.com/dexon-foundation/dexon-consensus-core/core/crypto"
coreEcdsa "github.com/dexon-foundation/dexon-consensus-core/core/crypto/ecdsa"
coreTypes "github.com/dexon-foundation/dexon-consensus-core/core/types"
@@ -23,22 +24,25 @@ import (
)
type DexconGovernance struct {
- b *DexAPIBackend
- chainConfig *params.ChainConfig
- privateKey *ecdsa.PrivateKey
- address common.Address
+ b *DexAPIBackend
+ chainConfig *params.ChainConfig
+ privateKey *ecdsa.PrivateKey
+ address common.Address
+ nodeSetCache *dexCore.NodeSetCache
}
// NewDexconGovernance retruns a governance implementation of the DEXON
// consensus governance interface.
func NewDexconGovernance(backend *DexAPIBackend, chainConfig *params.ChainConfig,
privKey *ecdsa.PrivateKey) *DexconGovernance {
- return &DexconGovernance{
+ g := &DexconGovernance{
b: backend,
chainConfig: chainConfig,
privateKey: privKey,
address: crypto.PubkeyToAddress(privKey.PublicKey),
}
+ g.nodeSetCache = dexCore.NewNodeSetCache(g)
+ return g
}
func (d *DexconGovernance) getRoundHeight(ctx context.Context, round uint64) (uint64, error) {
@@ -283,60 +287,41 @@ func (d *DexconGovernance) GetNumChains(round uint64) uint32 {
return d.Configuration(round).NumChains
}
-func (d *DexconGovernance) NotarySet(chainID uint32, round uint64) map[string]struct{} {
- id2Key := map[coreTypes.NodeID]coreCrypto.PublicKey{}
-
- nodeSet := coreTypes.NewNodeSet()
- for _, key := range d.NodeSet(round) {
- id := coreTypes.NewNodeID(key)
- id2Key[id] = key
- nodeSet.Add(id)
+func (d *DexconGovernance) NotarySet(
+ round uint64, chainID uint32) (map[string]struct{}, error) {
+ notarySet, err := d.nodeSetCache.GetNotarySet(round, chainID)
+ if err != nil {
+ return nil, err
}
- cfg := d.Configuration(round)
- crs := d.CRS(round)
-
- notarySet := nodeSet.GetSubSet(
- int(cfg.NotarySetSize), coreTypes.NewNotarySetTarget(crs, chainID))
-
- r := map[string]struct{}{}
+ r := make(map[string]struct{}, len(notarySet))
for id := range notarySet {
- compressed := id2Key[id]
- // 33 bytes pubkey to 65 bytes pubkey
- key, err := crypto.DecompressPubkey(compressed.Bytes())
- if err != nil {
- continue
+ if key, exists := d.nodeSetCache.GetPublicKey(id); exists {
+ uncompressedKey, err := crypto.DecompressPubkey(key.Bytes())
+ if err != nil {
+ log.Error("decompress key fail", "err", err)
+ }
+ r[discover.PubkeyID(uncompressedKey).String()] = struct{}{}
}
- r[discover.PubkeyID(key).String()] = struct{}{}
}
- return r
+ return r, nil
}
-func (d *DexconGovernance) DKGSet(round uint64) map[string]struct{} {
- id2Key := map[coreTypes.NodeID]coreCrypto.PublicKey{}
-
- nodeSet := coreTypes.NewNodeSet()
- for _, key := range d.NodeSet(round) {
- id := coreTypes.NewNodeID(key)
- id2Key[id] = key
- nodeSet.Add(id)
+func (d *DexconGovernance) DKGSet(round uint64) (map[string]struct{}, error) {
+ dkgSet, err := d.nodeSetCache.GetDKGSet(round)
+ if err != nil {
+ return nil, err
}
- cfg := d.Configuration(round)
- crs := d.CRS(round)
-
- dkgSet := nodeSet.GetSubSet(
- int(cfg.DKGSetSize), coreTypes.NewDKGSetTarget(crs))
-
- r := map[string]struct{}{}
+ r := make(map[string]struct{}, len(dkgSet))
for id := range dkgSet {
- compressed := id2Key[id]
- // 33 bytes pubkey to 65 bytes pubkey
- key, err := crypto.DecompressPubkey(compressed.Bytes())
- if err != nil {
- continue
+ if key, exists := d.nodeSetCache.GetPublicKey(id); exists {
+ uncompressedKey, err := crypto.DecompressPubkey(key.Bytes())
+ if err != nil {
+ log.Error("decompress key fail", "err", err)
+ }
+ r[discover.PubkeyID(uncompressedKey).String()] = struct{}{}
}
- r[discover.PubkeyID(key).String()] = struct{}{}
}
- return r
+ return r, nil
}