Skip to content

Commit

Permalink
Make luck estimation windows configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
sammy007 committed Dec 23, 2015
1 parent 18decb4 commit 769b870
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 30 deletions.
4 changes: 3 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"clientTimeout": "3m",
"blockRefreshInterval": "100ms",
"hashrateWindow": "15m",
"submitHashrate": false
"submitHashrate": false,
"luckWindow": "24h",
"largeLuckWindow": "72h"
},

"frontend": {
Expand Down
25 changes: 12 additions & 13 deletions proxy/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,7 @@ func (s *ProxyServer) StatsIndex(w http.ResponseWriter, r *http.Request) {
stats["height"] = t.Height
stats["diff"] = t.Difficulty
stats["now"] = util.MakeTimestamp()

blocksCount, variance, totalBlocksCount, totalVariance := s.getBlockStats()
stats["blocksCount24h"] = blocksCount
stats["variance24h"] = variance
stats["blocksCount1w"] = totalBlocksCount
stats["variance1w"] = totalVariance

stats["luck"] = s.getLuckStats()
json.NewEncoder(w).Encode(stats)
}

Expand Down Expand Up @@ -101,10 +95,8 @@ func (s *ProxyServer) collectMinersStats() (int64, int64, int, []interface{}) {
return totalHashrate, totalHashrate24h, totalOnline, result
}

func (s *ProxyServer) getBlockStats() (int, float64, int, float64) {
func (s *ProxyServer) getLuckStats() map[string]interface{} {
now := util.MakeTimestamp()
smallWindow := int64(time.Hour * 24 / time.Millisecond)
largeWindow := smallWindow * 7
var variance float64
var totalVariance float64
var blocksCount int
Expand All @@ -114,11 +106,11 @@ func (s *ProxyServer) getBlockStats() (int, float64, int, float64) {
defer s.blocksMu.Unlock()

for k, v := range s.blockStats {
if k >= now-smallWindow {
if k >= now-int64(s.luckWindow) {
blocksCount++
variance += v
}
if k >= now-largeWindow {
if k >= now-int64(s.luckLargeWindow) {
totalBlocksCount++
totalVariance += v
} else {
Expand All @@ -131,5 +123,12 @@ func (s *ProxyServer) getBlockStats() (int, float64, int, float64) {
if totalBlocksCount != 0 {
totalVariance = totalVariance / float64(totalBlocksCount)
}
return blocksCount, variance, totalBlocksCount, totalVariance
result := make(map[string]interface{})
result["variance"] = variance
result["blocksCount"] = blocksCount
result["window"] = s.config.Proxy.LuckWindow
result["totalVariance"] = totalVariance
result["totalBlocksCount"] = totalBlocksCount
result["largeWindow"] = s.config.Proxy.LargeLuckWindow
return result
}
2 changes: 2 additions & 0 deletions proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Proxy struct {
BlockRefreshInterval string `json:"blockRefreshInterval"`
HashrateWindow string `json:"hashrateWindow"`
SubmitHashrate bool `json:"submitHashrate"`
LuckWindow string `json:"luckWindow"`
LargeLuckWindow string `json:"largeLuckWindow"`
}

type Frontend struct {
Expand Down
27 changes: 17 additions & 10 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ import (
)

type ProxyServer struct {
config *Config
miners MinersMap
blockTemplate atomic.Value
upstream int32
upstreams []*rpc.RPCClient
hashrateWindow time.Duration
timeout time.Duration
roundShares int64
blocksMu sync.RWMutex
blockStats map[int64]float64
config *Config
miners MinersMap
blockTemplate atomic.Value
upstream int32
upstreams []*rpc.RPCClient
hashrateWindow time.Duration
timeout time.Duration
roundShares int64
blocksMu sync.RWMutex
blockStats map[int64]float64
luckWindow int64
luckLargeWindow int64
}

type Session struct {
Expand Down Expand Up @@ -61,6 +63,11 @@ func NewEndpoint(cfg *Config) *ProxyServer {
hashrateWindow, _ := time.ParseDuration(cfg.Proxy.HashrateWindow)
proxy.hashrateWindow = hashrateWindow

luckWindow, _ := time.ParseDuration(cfg.Proxy.LuckWindow)
proxy.luckWindow = int64(luckWindow / time.Millisecond)
luckLargeWindow, _ := time.ParseDuration(cfg.Proxy.LargeLuckWindow)
proxy.luckLargeWindow = int64(luckLargeWindow / time.Millisecond)

proxy.blockTemplate.Store(&BlockTemplate{})
proxy.fetchBlockTemplate()

Expand Down
12 changes: 6 additions & 6 deletions www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@
</div>
<div class="col-xs-12">
<p>
<strong>Blocks 24h:</strong> <span class="label label-primary">{{formatNumber blocksCount24h}}</span>
<strong>Shares/Diff 24h:</strong>
<span class="label label-primary">{{formatNumber variance24h style="percent" minimumFractionDigits=2 maximumFractionDigits=2}}</span>
<strong>Blocks 1w:</strong> <span class="label label-primary">{{formatNumber blocksCount1w}}</span>
<strong>Shares/Diff 1w:</strong>
<span class="label label-primary">{{formatNumber variance1w style="percent" minimumFractionDigits=2 maximumFractionDigits=2}}</span>
<strong>Blocks {{luck.window}}:</strong> <span class="label label-primary">{{formatNumber luck.blocksCount}}</span>
<strong>Shares/Diff {{luck.window}}:</strong>
<span class="label label-primary">{{formatNumber luck.variance style="percent" minimumFractionDigits=2 maximumFractionDigits=2}}</span>
<strong>Blocks {{luck.largeWindow}}:</strong> <span class="label label-primary">{{formatNumber luck.totalBlocksCount}}</span>
<strong>Shares/Diff {{luck.largeWindow}}:</strong>
<span class="label label-primary">{{formatNumber luck.totalVariance style="percent" minimumFractionDigits=2 maximumFractionDigits=2}}</span>
</p>
<p class="hidden-sm hidden-xs"><strong>Run:</strong> <code>ethminer -F {{url}}</code></p>
</div>
Expand Down

0 comments on commit 769b870

Please sign in to comment.