From ae1acb351266ee6964a5fe254c55d8b153281c7a Mon Sep 17 00:00:00 2001 From: yuriy0803 Date: Sun, 14 Aug 2022 19:01:36 +0200 Subject: [PATCH] update --- payouts/unlocker.go | 8 +++--- proxy/handlers.go | 41 +++++++++++++----------------- proxy/proxy.go | 37 +++++++++++++++++++++------ proxy/stratum.go | 2 +- rpc/rpc.go | 1 + storage/redis.go | 3 ++- www/app/controllers/application.js | 15 +++++++++-- 7 files changed, 67 insertions(+), 40 deletions(-) diff --git a/payouts/unlocker.go b/payouts/unlocker.go index 17b2819..f965247 100644 --- a/payouts/unlocker.go +++ b/payouts/unlocker.go @@ -669,10 +669,10 @@ func getConstRewardUbiq(height int64) *big.Int { reward = big.NewInt(4e+18) // Year 4 } - // If Orion use new MP - if headerNumber.Cmp(big.NewInt(1791793)) >= 0 { - reward = big.NewInt(15e+17) - } + // If Orion use new MP + if headerNumber.Cmp(big.NewInt(1791793)) >= 0 { + reward = big.NewInt(15e+17) + } return reward } diff --git a/proxy/handlers.go b/proxy/handlers.go index 921dc93..17d6931 100644 --- a/proxy/handlers.go +++ b/proxy/handlers.go @@ -1,7 +1,6 @@ package proxy import ( - "errors" "log" "regexp" "strings" @@ -69,36 +68,30 @@ func (s *ProxyServer) handleSubmitRPC(cs *Session, login, id string, params []st log.Printf("Malformed PoW result from %s@%s %v", login, cs.ip, params) return false, &ErrorReply{Code: -1, Message: "Malformed PoW result"} } - go func(s *ProxyServer, cs *Session, login, id string, params []string) { - t := s.currentBlockTemplate() - exist, validShare := s.processShare(login, id, cs.ip, t, params) - ok := s.policy.ApplySharePolicy(cs.ip, !exist && validShare) - - if exist { - log.Printf("Duplicate share from %s@%s %v", login, cs.ip, params) - cs.lastErr = errors.New("Duplicate share") - } - - if !validShare { - log.Printf("Invalid share from %s@%s", login, cs.ip) - // Bad shares limit reached, return error and close - if !ok { - cs.lastErr = errors.New("Invalid share") - } - } + t := s.currentBlockTemplate() + exist, validShare := s.processShare(login, id, cs.ip, t, params) + ok := s.policy.ApplySharePolicy(cs.ip, !exist && validShare) - if s.config.Proxy.Debug { - log.Printf("Valid share from %s@%s", login, cs.ip) - } + if exist { + log.Printf("Duplicate share from %s@%s %v", login, cs.ip, params) + return false, &ErrorReply{Code: 22, Message: "Duplicate share"} + } + if !validShare { + log.Printf("Invalid share from %s@%s", login, cs.ip) + // Bad shares limit reached, return error and close if !ok { - cs.lastErr = errors.New("High rate of invalid shares") + return false, &ErrorReply{Code: 23, Message: "Invalid share"} } - }(s, cs, login, id, params) + return false, nil + } + log.Printf("Valid share from %s@%s", login, cs.ip) + if !ok { + return true, &ErrorReply{Code: -1, Message: "High rate of invalid shares"} + } return true, nil } - func (s *ProxyServer) handleGetBlockByNumberRPC() *rpc.GetBlockReplyPart { t := s.currentBlockTemplate() var reply *rpc.GetBlockReplyPart diff --git a/proxy/proxy.go b/proxy/proxy.go index 0214b47..b0f606d 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -6,6 +6,7 @@ import ( "log" "net" "net/http" + "strconv" "strings" "sync" "sync/atomic" @@ -42,9 +43,8 @@ type Session struct { // Stratum sync.Mutex - conn net.Conn - login string - lastErr error + conn net.Conn + login string } func NewProxy(cfg *Config, backend *storage.RedisClient) *ProxyServer { @@ -108,12 +108,33 @@ func NewProxy(cfg *Config, backend *storage.RedisClient) *ProxyServer { case <-stateUpdateTimer.C: t := proxy.currentBlockTemplate() if t != nil { - err := backend.WriteNodeState(cfg.Name, t.Height, t.Difficulty) - if err != nil { - log.Printf("Failed to write node state to backend: %v", err) - proxy.markSick() + rpc := proxy.rpc() + // get the latest block height + height := int64(t.Height) - 1 + block, _ := rpc.GetBlockByHeight(height) + timestamp, _ := strconv.ParseInt(strings.Replace(block.Timestamp, "0x", "", -1), 16, 64) + prev := height - 100 + if prev < 0 { + prev = 0 + } + n := height - prev + if n > 0 { + prevblock, err := rpc.GetBlockByHeight(prev) + if err != nil || prevblock == nil { + log.Fatalf("Error while retrieving block from node: %v", err) + } else { + 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.markOk() + proxy.markSick() } } stateUpdateTimer.Reset(stateUpdateIntv) diff --git a/proxy/stratum.go b/proxy/stratum.go index 6c2932f..c6568c7 100644 --- a/proxy/stratum.go +++ b/proxy/stratum.go @@ -66,7 +66,7 @@ func (s *ProxyServer) ListenTCP() { accept <- n go func(cs *Session) { err = s.handleTCPClient(cs) - if err != nil || cs.lastErr != nil { + if err != nil { s.removeSession(cs) conn.Close() } diff --git a/rpc/rpc.go b/rpc/rpc.go index e0c7b95..61eb8f1 100644 --- a/rpc/rpc.go +++ b/rpc/rpc.go @@ -35,6 +35,7 @@ type GetBlockReply struct { Difficulty string `json:"difficulty"` GasLimit string `json:"gasLimit"` GasUsed string `json:"gasUsed"` + Timestamp string `json:"timestamp"` Transactions []Tx `json:"transactions"` Uncles []string `json:"uncles"` // https://github.com/ethereum/EIPs/issues/95 diff --git a/storage/redis.go b/storage/redis.go index 52d3dd2..4eece70 100644 --- a/storage/redis.go +++ b/storage/redis.go @@ -450,7 +450,7 @@ func (r *RedisClient) GetPaymentCharts(login string) (stats []*PaymentCharts, er return stats, nil } -func (r *RedisClient) WriteNodeState(id string, height uint64, diff *big.Int) error { +func (r *RedisClient) WriteNodeState(id string, height uint64, diff *big.Int, blocktime float64) error { tx := r.client.Multi() defer tx.Close() @@ -461,6 +461,7 @@ func (r *RedisClient) WriteNodeState(id string, height uint64, diff *big.Int) er tx.HSet(r.formatKey("nodes"), join(id, "height"), strconv.FormatUint(height, 10)) tx.HSet(r.formatKey("nodes"), join(id, "difficulty"), diff.String()) tx.HSet(r.formatKey("nodes"), join(id, "lastBeat"), strconv.FormatInt(now, 10)) + tx.HSet(r.formatKey("nodes"), join(id, "blocktime"), strconv.FormatFloat(blocktime, 'f', 4, 64)) return nil }) return err diff --git a/www/app/controllers/application.js b/www/app/controllers/application.js index f48c4e9..f45d840 100644 --- a/www/app/controllers/application.js +++ b/www/app/controllers/application.js @@ -43,10 +43,21 @@ export default Ember.Controller.extend({ return parseFloat(this.get('model.exchangedata.current_price')); } }), + + blockTime: Ember.computed('model.nodes', { + get() { + var node = this.get('bestNode'); + if (node && node.blocktime) { + return node.blocktime; + } + return config.APP.BlockTime; + } + }), hashrate: Ember.computed('difficulty', { get() { - return this.getWithDefault('difficulty', 0) / config.APP.BlockTime; + var blockTime = this.get('blockTime'); + return this.getWithDefault('difficulty', 0) / blockTime; } }), @@ -89,7 +100,7 @@ export default Ember.Controller.extend({ nextEpoch: Ember.computed('height', { get() { - var epochOffset = (60000 - (this.getWithDefault('height', 1) % 60000)) * 1000 * this.get('config').BlockTime; + var epochOffset = (60000 - (this.getWithDefault('height', 1) % 60000)) * 1000 * this.get('blockTime'); return Date.now() + epochOffset; } })