Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.44 KB | None | 0 0
  1. package proxy
  2.  
  3. import (
  4.     "log"
  5.     "math/big"
  6.     "strconv"
  7.     "strings"
  8.  
  9.     "github.com/ethereum/ethash"
  10.     "github.com/ethereum/go-ethereum/common"
  11. )
  12.  
  13. var hasher = ethash.New()
  14.  
  15. func (s *ProxyServer) processShare(login, id, ip string, t *BlockTemplate, params []string) (bool, bool) {
  16.     nonceHex := params[0]
  17.     hashNoNonce := params[1]
  18.     mixDigest := params[2]
  19.     nonce, _ := strconv.ParseUint(strings.Replace(nonceHex, "0x", "", -1), 16, 64)
  20.     shareDiff := s.config.Proxy.Difficulty
  21.  
  22.     h, ok := t.headers[hashNoNonce]
  23.     if !ok {
  24.         log.Printf("Stale share from %v@%v", login, ip)
  25.         return false, false
  26.     }
  27.  
  28.     share := Block{
  29.         number:      h.height,
  30.         hashNoNonce: common.HexToHash(hashNoNonce),
  31.         difficulty:  big.NewInt(shareDiff),
  32.         nonce:       nonce,
  33.         mixDigest:   common.HexToHash(mixDigest),
  34.     }
  35.  
  36.     block := Block{
  37.         number:      h.height,
  38.         hashNoNonce: common.HexToHash(hashNoNonce),
  39.         difficulty:  h.diff,
  40.         nonce:       nonce,
  41.         mixDigest:   common.HexToHash(mixDigest),
  42.     }
  43.  
  44.     //this is to stop people in wallet blacklist, from getting shares into the db.
  45.     //rare instances of hacks require letting the hacks waste thier money on occassion
  46.     if !s.policy.ApplyLoginWalletPolicy(login) {
  47.         // check to see if this wallet login is blocked
  48.         log.Printf("Blacklisted wallet share, skipped from %v", login)
  49.         return false, false
  50.         //return codes need work here, a lot of it.
  51.     }
  52.  
  53.     if !hasher.Verify(share) {
  54.         return false, false
  55.     }
  56.  
  57.     if hasher.Verify(block) {
  58.         ok, err := s.rpc().SubmitBlock(params)
  59.         if err != nil {
  60.             log.Printf("Block submission failure at height %v for %v: %v", h.height, t.Header, err)
  61.         } else if !ok {
  62.             log.Printf("Block rejected at height %v for %v", h.height, t.Header)
  63.             return false, false
  64.         } else {
  65.             s.fetchBlockTemplate()
  66.             exist, err := s.backend.WriteBlock(login, id, params, shareDiff, h.diff.Int64(), h.height, s.hashrateExpiration)
  67.             if exist {
  68.                 return true, false
  69.             }
  70.             if err != nil {
  71.                 log.Println("Failed to insert block candidate into backend:", err)
  72.             } else {
  73.                 log.Printf("Inserted block %v to backend", h.height)
  74.             }
  75.             log.Printf("Block found by miner %v@%v at height %d", login, ip, h.height)
  76.         }
  77.     } else {
  78.         exist, err := s.backend.WriteShare(login, id, params, shareDiff, h.height, s.hashrateExpiration)
  79.         if exist {
  80.             return true, false
  81.         }
  82.         if err != nil {
  83.             log.Println("Failed to insert share data into backend:", err)
  84.         }
  85.     }
  86.     return false, true
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement