Browse Source

fix error

fix error
master
yuriy0803 2 years ago
parent
commit
1d1b75d590
  1. 97
      proxy/proxy.go

97
proxy/proxy.go

@ -50,16 +50,17 @@ type Session struct {
// Stratum // Stratum
sync.Mutex sync.Mutex
conn net.Conn conn net.Conn
login string login string
worker string worker string
stratum int stratum int
JobDeatils jobDetails subscriptionID string
Extranonce string JobDeatils jobDetails
ExtranonceSub bool Extranonce string
JobDetails jobDetails ExtranonceSub bool
staleJobs map[string]staleJob JobDetails jobDetails
staleJobIDs []string staleJobs map[string]staleJob
staleJobIDs []string
} }
type jobDetails struct { type jobDetails struct {
@ -72,7 +73,7 @@ type jobDetails struct {
func NewProxy(cfg *Config, backend *storage.RedisClient) *ProxyServer { func NewProxy(cfg *Config, backend *storage.RedisClient) *ProxyServer {
if len(cfg.Name) == 0 { if len(cfg.Name) == 0 {
log.Fatal("You must set the instance name") log.Fatal("You must set instance name")
} }
policy := policy.Start(&cfg.Proxy.Policy, backend) policy := policy.Start(&cfg.Proxy.Policy, backend)
@ -107,49 +108,61 @@ func NewProxy(cfg *Config, backend *storage.RedisClient) *ProxyServer {
stateUpdateTimer := time.NewTimer(stateUpdateIntv) stateUpdateTimer := time.NewTimer(stateUpdateIntv)
go func() { go func() {
for range refreshTimer.C { for {
proxy.fetchBlockTemplate() select {
case <-refreshTimer.C:
proxy.fetchBlockTemplate()
refreshTimer.Reset(refreshIntv)
}
} }
}() }()
go func() { go func() {
for range checkTimer.C { for {
proxy.checkUpstreams() select {
case <-checkTimer.C:
proxy.checkUpstreams()
checkTimer.Reset(checkIntv)
}
} }
}() }()
go func() { go func() {
for range stateUpdateTimer.C { for {
t := proxy.currentBlockTemplate() select {
if t != nil { case <-stateUpdateTimer.C:
rpc := proxy.rpc() t := proxy.currentBlockTemplate()
// get the latest block height if t != nil {
height := int64(t.Height) - 1 rpc := proxy.rpc()
block, _ := rpc.GetBlockByHeight(height) // get the latest block height
timestamp, _ := strconv.ParseInt(strings.Replace(block.Timestamp, "0x", "", -1), 16, 64) height := int64(t.Height) - 1
prev := height - 100 block, _ := rpc.GetBlockByHeight(height)
if prev < 0 { timestamp, _ := strconv.ParseInt(strings.Replace(block.Timestamp, "0x", "", -1), 16, 64)
prev = 0 prev := height - 100
} if prev < 0 {
n := height - prev prev = 0
if n > 0 { }
prevblock, err := rpc.GetBlockByHeight(prev) n := height - prev
if err != nil || prevblock == nil { if n > 0 {
log.Fatalf("Error while retrieving block from the node: %v", err) prevblock, err := rpc.GetBlockByHeight(prev)
} else { if err != nil || prevblock == nil {
prevtime, _ := strconv.ParseInt(strings.Replace(prevblock.Timestamp, "0x", "", -1), 16, 64) log.Fatalf("Error while retrieving block from node: %v", err)
blocktime := float64(timestamp-prevtime) / float64(n)
err = backend.WriteNodeState(cfg.Name, t.Height, t.Difficulty, blocktime)
if err != nil {
log.Printf("Failed to write node state to the backend: %v", err)
proxy.markSick()
} else { } else {
proxy.markOk() prevtime, _ := strconv.ParseInt(strings.Replace(prevblock.Timestamp, "0x", "", -1), 16, 64)
blocktime := float64(timestamp-prevtime) / float64(n)
err = backend.WriteNodeState(cfg.Name, t.Height, t.Difficulty, blocktime)
if err != nil {
log.Printf("Failed to write node state to backend: %v", err)
proxy.markSick()
} else {
proxy.markOk()
}
} }
} else {
proxy.markSick()
} }
} else {
proxy.markSick()
} }
stateUpdateTimer.Reset(stateUpdateIntv)
} }
} }
}() }()

Loading…
Cancel
Save