From aa8b0ab7dc8eba9d541f1e6cfaf0316007a4a25b Mon Sep 17 00:00:00 2001 From: Sonic Date: Sun, 5 May 2019 16:16:51 +0800 Subject: rpc: notary info (#397) --- dex/api.go | 4 +++ dex/handler.go | 79 +++++++++++++++++++++++++++++++++++++++++++++ internal/web3ext/web3ext.go | 4 +++ 3 files changed, 87 insertions(+) diff --git a/dex/api.go b/dex/api.go index 70976f071..991926740 100644 --- a/dex/api.go +++ b/dex/api.go @@ -139,6 +139,10 @@ func (api *PrivateAdminAPI) IsProposing() bool { return api.dex.IsProposing() } +func (api *PrivateAdminAPI) NotaryInfo() (*NotaryInfo, error) { + return api.dex.protocolManager.NotaryInfo() +} + // PublicDebugAPI is the collection of Ethereum full node APIs exposed // over the public debugging endpoint. type PublicDebugAPI struct { diff --git a/dex/handler.go b/dex/handler.go index fed1dc8b7..7bf23758c 100644 --- a/dex/handler.go +++ b/dex/handler.go @@ -36,6 +36,7 @@ package dex import ( "bytes" "context" + "encoding/hex" "encoding/json" "errors" "fmt" @@ -1389,3 +1390,81 @@ func (pm *ProtocolManager) NodeInfo() *NodeInfo { Head: currentBlock.Hash(), } } + +type NotaryInfo struct { + Round uint64 `json:"round"` + IsNotary bool `json:"is_notary"` + Nodes []*NotaryNodeInfo `json:"nodes"` + IsNextNotary bool `json:"is_next_notary"` + Next []*NotaryNodeInfo `json:"next"` +} + +type NotaryNodeInfo struct { + ID enode.ID `json:"id"` + Number uint64 `json:"number"` +} + +func (pm *ProtocolManager) NotaryInfo() (*NotaryInfo, error) { + current := pm.blockchain.CurrentBlock() + pubkeys, err := pm.gov.NotarySet(current.Round()) + if err != nil { + return nil, err + } + + info := &NotaryInfo{ + Round: current.Round(), + } + + currentNodes, in, err := pm.buildNotaryNodeInfo(pubkeys) + if err != nil { + return nil, err + } + + info.Nodes = currentNodes + info.IsNotary = in + + if crsRound := pm.gov.CRSRound(); crsRound != current.Round() { + pubkeys, err := pm.gov.NotarySet(crsRound) + if err != nil { + return nil, err + } + + nextNodes, in, err := pm.buildNotaryNodeInfo(pubkeys) + if err != nil { + return nil, err + } + info.Next = nextNodes + info.IsNextNotary = in + } + return info, nil +} + +func (pm *ProtocolManager) buildNotaryNodeInfo( + pubkeys map[string]struct{}) ([]*NotaryNodeInfo, bool, error) { + + nodes := []*NotaryNodeInfo{} + for pubkey := range pubkeys { + b, err := hex.DecodeString(pubkey) + if err != nil { + return nil, false, err + } + pubkey, err := crypto.UnmarshalPubkey(b) + if err != nil { + return nil, false, err + } + nodes = append(nodes, &NotaryNodeInfo{ID: enode.PubkeyToIDV4(pubkey)}) + } + + var in bool + for _, n := range nodes { + if p := pm.peers.Peer(n.ID.String()); p != nil { + _, number := p.Head() + n.Number = number + } + if n.ID == pm.srvr.Self().ID() { + n.Number = pm.blockchain.CurrentBlock().NumberU64() + in = true + } + } + return nodes, in, nil +} diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 2253142b9..4cd27ab66 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -229,6 +229,10 @@ web3._extend({ name: 'isProposing', getter: 'admin_isProposing' }), + new web3._extend.Property({ + name: 'notaryInfo', + getter: 'admin_notaryInfo' + }), ] }); ` -- cgit v1.2.3