Skip to content

Commit

Permalink
Display general information about the state of upstream node
Browse files Browse the repository at this point in the history
  • Loading branch information
sammy007 committed Apr 22, 2018
1 parent 8fa6edc commit a9ae6bf
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
35 changes: 35 additions & 0 deletions rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type RPCClient struct {
Name string
sick bool
client *http.Client
info atomic.Value
}

type GetBlockTemplateReply struct {
Expand All @@ -38,6 +39,14 @@ type GetBlockTemplateReply struct {
PrevHash string `json:"prev_hash"`
}

type GetInfoReply struct {
IncomingConnections int64 `json:"incoming_connections_count"`
OutgoingConnections int64 `json:"outgoing_connections_count"`
Status string `json:"status"`
Height int64 `json:"height"`
TxPoolSize int64 `json:"tx_pool_size"`
}

type JSONRpcResp struct {
Id *json.RawMessage `json:"id"`
Result *json.RawMessage `json:"result"`
Expand Down Expand Up @@ -71,6 +80,19 @@ func (r *RPCClient) GetBlockTemplate(reserveSize int, address string) (*GetBlock
return reply, err
}

func (r *RPCClient) GetInfo() (*GetInfoReply, error) {
params := make(map[string]interface{})
rpcResp, err := r.doPost(r.Url.String(), "get_info", params)
var reply *GetInfoReply
if err != nil {
return nil, err
}
if rpcResp.Result != nil {
err = json.Unmarshal(*rpcResp.Result, &reply)
}
return reply, err
}

func (r *RPCClient) SubmitBlock(hash string) (*JSONRpcResp, error) {
return r.doPost(r.Url.String(), "submitblock", []string{hash})
}
Expand Down Expand Up @@ -145,3 +167,16 @@ func (r *RPCClient) markAlive() {
}
r.Unlock()
}

func (r *RPCClient) UpdateInfo() (*GetInfoReply, error) {
info, err := r.GetInfo()
if err == nil {
r.info.Store(info)
}
return info, err
}

func (r *RPCClient) Info() *GetInfoReply {
reply, _ := r.info.Load().(*GetInfoReply)
return reply
}
1 change: 1 addition & 0 deletions stratum/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func convertUpstream(u *rpc.RPCClient) map[string]interface{} {
"rejects": atomic.LoadInt64(&u.Rejects),
"lastSubmissionAt": atomic.LoadInt64(&u.LastSubmissionAt),
"failsCount": atomic.LoadInt64(&u.FailsCount),
"info": u.Info(),
}
return upstream
}
Expand Down
29 changes: 29 additions & 0 deletions stratum/stratum.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func NewStratum(cfg *pool.Config) *StratumServer {
checkIntv, _ := time.ParseDuration(cfg.UpstreamCheckInterval)
checkTimer := time.NewTimer(checkIntv)

infoIntv, _ := time.ParseDuration(cfg.UpstreamCheckInterval)
infoTimer := time.NewTimer(infoIntv)

// Init block template
go stratum.refreshBlockTemplate(false)

Expand All @@ -123,6 +126,32 @@ func NewStratum(cfg *pool.Config) *StratumServer {
}
}()

go func() {
for {
select {
case <-infoTimer.C:
poll := func(v *rpc.RPCClient) {
_, err := v.UpdateInfo()
if err != nil {
log.Printf("Unable to update info on upstream %s: %v", v.Name, err)
}
}
current := stratum.rpc()
poll(current)

// Async rpc call to not block on rpc timeout, ignoring current
go func() {
for _, v := range stratum.upstreams {
if v != current {
poll(v)
}
}
}()
infoTimer.Reset(infoIntv)
}
}
}()

return stratum
}

Expand Down
22 changes: 16 additions & 6 deletions www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
</div>
<div class="col-xs-12">
<h4>Upstream</h4>
<table class="table table-condensed">
<table class="table table-condensed table-striped">
<tr>
<th>Name</th>
<th>Url</th>
Expand All @@ -89,11 +89,21 @@ <h4>Upstream</h4>
{{else}}
<td>{{name}}</td>
{{/if}}
<td>{{url}}</td>
<td>{{formatNumber accepts}}</td>
<td><strong>{{formatNumber rejects}}</strong></td>
<td>{{failsCount}}</td>
</tr>
<td>{{url}}</td>
<td>{{formatNumber accepts}}</td>
<td><strong>{{formatNumber rejects}}</strong></td>
<td>{{failsCount}}</td>
{{#if info}}
<tr>
<td colspan="5" class="small">
<strong>Status:</strong> <span class="label label-default">{{info.status}}</span>
<strong>Height:</strong> <span class="label label-default">{{info.height}}</span>
<strong>Tx Pool Size:</strong> <span class="label label-default">{{info.tx_pool_size}}</span>
<strong>In:</strong> <span class="label label-default">{{info.incoming_connections_count}}</span>
<strong>Out:</strong> <span class="label label-default">{{info.outgoing_connections_count}}</span>
</td>
</tr>
{{/if}}
{{/each}}
</table>
</div>
Expand Down

0 comments on commit a9ae6bf

Please sign in to comment.