diff options
Diffstat (limited to 'dex')
-rw-r--r-- | dex/api.go | 4 | ||||
-rw-r--r-- | dex/handler.go | 79 |
2 files changed, 83 insertions, 0 deletions
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 +} |