diff options
author | Sonic <sonic@cobinhood.com> | 2018-10-14 22:39:11 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-06-12 17:23:39 +0800 |
commit | f4d15de52885a472ffb4ae5982662ec3f41ebebc (patch) | |
tree | 7ac7bdb3724e63a55930382edfb30d2283ce028b /dex/governance.go | |
parent | 2e1556d551420e5f52d6e4e157b46963277eaccd (diff) | |
download | go-tangerine-f4d15de52885a472ffb4ae5982662ec3f41ebebc.tar go-tangerine-f4d15de52885a472ffb4ae5982662ec3f41ebebc.tar.gz go-tangerine-f4d15de52885a472ffb4ae5982662ec3f41ebebc.tar.bz2 go-tangerine-f4d15de52885a472ffb4ae5982662ec3f41ebebc.tar.lz go-tangerine-f4d15de52885a472ffb4ae5982662ec3f41ebebc.tar.xz go-tangerine-f4d15de52885a472ffb4ae5982662ec3f41ebebc.tar.zst go-tangerine-f4d15de52885a472ffb4ae5982662ec3f41ebebc.zip |
dex: add method to get NumChains, NotarySet, DKGSet easily
Diffstat (limited to 'dex/governance.go')
-rw-r--r-- | dex/governance.go | 68 |
1 files changed, 56 insertions, 12 deletions
diff --git a/dex/governance.go b/dex/governance.go index 37985cec4..32c2d79af 100644 --- a/dex/governance.go +++ b/dex/governance.go @@ -12,12 +12,11 @@ import ( coreTypes "github.com/dexon-foundation/dexon-consensus-core/core/types" "github.com/dexon-foundation/dexon/common" - "github.com/dexon-foundation/dexon/core" "github.com/dexon-foundation/dexon/core/types" "github.com/dexon-foundation/dexon/core/vm" "github.com/dexon-foundation/dexon/crypto" - "github.com/dexon-foundation/dexon/event" "github.com/dexon-foundation/dexon/log" + "github.com/dexon-foundation/dexon/p2p/discover" "github.com/dexon-foundation/dexon/params" "github.com/dexon-foundation/dexon/rlp" "github.com/dexon-foundation/dexon/rpc" @@ -280,19 +279,64 @@ func (d *DexconGovernance) IsDKGFinal(round uint64) bool { return count >= threshold } -// TODO(sonic): finish these -func (d *DexconGovernance) GetChainNum(uint64) uint32 { - return 3 +func (d *DexconGovernance) GetNumChains(round uint64) uint32 { + return d.Configuration(round).NumChains } -func (d *DexconGovernance) GetNotarySet(uint32, uint64) map[string]struct{} { - return nil -} +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) + } + + cfg := d.Configuration(round) + crs := d.CRS(round) -func (d *DexconGovernance) GetDKGSet(uint64) map[string]struct{} { - return nil + notarySet := nodeSet.GetSubSet( + int(cfg.NotarySetSize), coreTypes.NewNotarySetTarget(crs, chainID)) + + r := map[string]struct{}{} + for id := range notarySet { + compressed := id2Key[id] + // 33 bytes pubkey to 65 bytes pubkey + key, err := crypto.DecompressPubkey(compressed.Bytes()) + if err != nil { + continue + } + r[discover.PubkeyID(key).String()] = struct{}{} + } + return r } -func (d *DexconGovernance) SubscribeNewCRSEvent(ch chan core.NewCRSEvent) event.Subscription { - return 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) + } + + cfg := d.Configuration(round) + crs := d.CRS(round) + + dkgSet := nodeSet.GetSubSet( + int(cfg.DKGSetSize), coreTypes.NewDKGSetTarget(crs)) + + r := map[string]struct{}{} + for id := range dkgSet { + compressed := id2Key[id] + // 33 bytes pubkey to 65 bytes pubkey + key, err := crypto.DecompressPubkey(compressed.Bytes()) + if err != nil { + continue + } + r[discover.PubkeyID(key).String()] = struct{}{} + } + return r } |