Permalink
Browse files

Check PoW backlog on block submission

  • Loading branch information...
sammy007 committed Jul 30, 2016
1 parent d6d6079 commit fabc74ded9d8e93abb5f7f6fbf572bd46119a738
Showing with 23 additions and 7 deletions.
  1. +4 −1 proxy/miner.go
  2. +19 −6 storage/redis.go
View
@@ -54,7 +54,10 @@ func (s *ProxyServer) processShare(login, id, ip string, t *BlockTemplate, param
return false, false
} else {
s.fetchBlockTemplate()
err := s.backend.WriteBlock(login, id, params, shareDiff, h.diff.Int64(), h.height, s.hashrateExpiration)
exist, err := s.backend.WriteBlock(login, id, params, shareDiff, h.diff.Int64(), h.height, s.hashrateExpiration)
if exist {
return true, false
}
if err != nil {
log.Println("Failed to insert block candidate into backend:", err)
} else {
View
@@ -160,16 +160,21 @@ func (r *RedisClient) GetNodeStates() ([]map[string]interface{}, error) {
return v, nil
}
func (r *RedisClient) WriteShare(login, id string, params []string, diff int64, height uint64, window time.Duration) (bool, error) {
func (r *RedisClient) checkPoWExist(height uint64, params []string) (bool, error) {
// Sweep PoW backlog for previous blocks, we have 3 templates back in RAM
r.client.ZRemRangeByScore(r.formatKey("pow"), "-inf", fmt.Sprint("(", height-8))
val, err := r.client.ZAdd(r.formatKey("pow"), redis.Z{Score: float64(height), Member: strings.Join(params, ":")}).Result()
return val == 0, err
}
func (r *RedisClient) WriteShare(login, id string, params []string, diff int64, height uint64, window time.Duration) (bool, error) {
exist, err := r.checkPoWExist(height, params)
if err != nil {
return false, err
}
// Duplicate share, (nonce, powHash, mixDigest) pair exist
if val == 0 {
return true, err
if exist {
return true, nil
}
tx := r.client.Multi()
defer tx.Close()
@@ -185,7 +190,15 @@ func (r *RedisClient) WriteShare(login, id string, params []string, diff int64,
return false, err
}
func (r *RedisClient) WriteBlock(login, id string, params []string, diff, roundDiff int64, height uint64, window time.Duration) error {
func (r *RedisClient) WriteBlock(login, id string, params []string, diff, roundDiff int64, height uint64, window time.Duration) (bool, error) {
exist, err := r.checkPoWExist(height, params)
if err != nil {
return false, err
}
// Duplicate share, (nonce, powHash, mixDigest) pair exist
if exist {
return true, nil
}
tx := r.client.Multi()
defer tx.Close()
@@ -203,7 +216,7 @@ func (r *RedisClient) WriteBlock(login, id string, params []string, diff, roundD
return nil
})
if err != nil {
return err
return false, err
} else {
sharesMap, _ := cmds[10].(*redis.StringStringMapCmd).Result()
totalShares := int64(0)
@@ -214,7 +227,7 @@ func (r *RedisClient) WriteBlock(login, id string, params []string, diff, roundD
hashHex := strings.Join(params, ":")
s := join(hashHex, ts, roundDiff, totalShares)
cmd := r.client.ZAdd(r.formatKey("blocks", "candidates"), redis.Z{Score: float64(height), Member: s})
return cmd.Err()
return false, cmd.Err()
}
}

0 comments on commit fabc74d

Please sign in to comment.