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 | |
parent | ce2badf50de9eb7897e98818822dc23d5d86fa3c (diff) | |
download | go-tangerine-fd5fe8ff4b792bfdeead046297bbf7919c0a0361.tar go-tangerine-fd5fe8ff4b792bfdeead046297bbf7919c0a0361.tar.gz go-tangerine-fd5fe8ff4b792bfdeead046297bbf7919c0a0361.tar.bz2 go-tangerine-fd5fe8ff4b792bfdeead046297bbf7919c0a0361.tar.lz go-tangerine-fd5fe8ff4b792bfdeead046297bbf7919c0a0361.tar.xz go-tangerine-fd5fe8ff4b792bfdeead046297bbf7919c0a0361.tar.zst go-tangerine-fd5fe8ff4b792bfdeead046297bbf7919c0a0361.zip |
rpc: notary info (#397)
-rw-r--r-- | dex/api.go | 4 | ||||
-rw-r--r-- | dex/handler.go | 79 | ||||
-rw-r--r-- | internal/web3ext/web3ext.go | 4 |
3 files changed, 87 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 +} 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' + }), ] }); ` |