diff options
author | Sonic <sonic@dexon.org> | 2019-05-05 16:16:51 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-09-17 16:57:29 +0800 |
commit | fd5fe8ff4b792bfdeead046297bbf7919c0a0361 (patch) | |
tree | 04abea712db9228e652e65b2e9c5d1b3ca830585 /dex/handler.go | |
parent | ce2badf50de9eb7897e98818822dc23d5d86fa3c (diff) | |
download | go-tangerine-fd5fe8ff4b792bfdeead046297bbf7919c0a0361.tar.gz go-tangerine-fd5fe8ff4b792bfdeead046297bbf7919c0a0361.tar.zst go-tangerine-fd5fe8ff4b792bfdeead046297bbf7919c0a0361.zip |
rpc: notary info (#397)
Diffstat (limited to 'dex/handler.go')
-rw-r--r-- | dex/handler.go | 79 |
1 files changed, 79 insertions, 0 deletions
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 +} |