Open-ethereum-pooleth Mineral pool-payouts module Payoutsprocessor definition
type PayoutsProcessor struct { config *PayoutsConfig backend *storage.RedisClient rpc *rpc.RPCClient halt bool lastFail error}
Getpendingpayments principle
func (r *RedisClient) GetPendingPayments() []*PendingPayment { //ZREVRANGE eth:payments:pending 0 -1 WITHSCORES raw := r.client.ZRevRangeWithScores(r.formatKey("payments", "pending"), 0, -1) var result []*PendingPayment for _, v := range raw.Val() { // timestamp -> "address:amount" payment := PendingPayment{} payment.Timestamp = int64(v.Score) fields := strings.Split(v.Member.(string), ":") payment.Address = fields[0] payment.Amount, _ = strconv.ParseInt(fields[1], 10, 64) result = append(result, &payment) } return result}
Getpayees principle
func (r *RedisClient) GetPayees() ([]string, error) { payees := make(map[string]struct{}) var result []string var c int64 for { var keys []string var err error c, keys, err = r.client.Scan(c, r.formatKey("miners", "*"), 100).Result() if err != nil { return nil, err } for _, row := range keys { login := strings.Split(row, ":")[2] payees[login] = struct{}{} } if c == 0 { break } } for login, _ := range payees { result = append(result, login) } return result, nil}
Lockpayouts principle
func (r *RedisClient) LockPayouts(login string, amount int64) error { //eth:payments:lock key := r.formatKey("payments", "lock") //SETNX eth:payments:lock login:amount 0 //Setnx(SET if Not eXists) 命令在指定的 key 不存在时,为 key 设置指定的值 result := r.client.SetNX(key, join(login, amount), 0).Val() if !result { return fmt.Errorf("Unable to acquire lock ‘%s‘", key) } return nil}
UpdateBalance principle
func (r *RedisClient) UpdateBalance(login string, amount int64) error {tx := r.client.Multi()defer tx.Close()ts := util.MakeTimestamp() / 1000_, err := tx.Exec(func() error { //Hincrby 命令用于为哈希表中的字段值加上指定增量值 //HINCRBY eth:miners:login balance -amounttx.HIncrBy(r.formatKey("miners", login), "balance", (amount * -1)) //HINCRBY eth:miners:login pending amounttx.HIncrBy(r.formatKey("miners", login), "pending", amount) //HINCRBY eth:finances balance -amounttx.HIncrBy(r.formatKey("finances"), "balance", (amount * -1)) //HINCRBY eth:finances pending amounttx.HIncrBy(r.formatKey("finances"), "pending", amount) //ZADD eth:payments:pending ts login:amount //Zadd 命令用于将一个或多个成员元素及其分数值加入到有序集当中tx.ZAdd(r.formatKey("payments", "pending"), redis.Z{Score: float64(ts), Member: join(login, amount)})return nil})return err}
Rollbackbalance principle
func (r *RedisClient) RollbackBalance(login string, amount int64) error { tx := r.client.Multi() defer tx.Close() _, err := tx.Exec(func() error { tx.HIncrBy(r.formatKey("miners", login), "balance", amount) tx.HIncrBy(r.formatKey("miners", login), "pending", (amount * -1)) tx.HIncrBy(r.formatKey("finances"), "balance", amount) tx.HIncrBy(r.formatKey("finances"), "pending", (amount * -1)) tx.ZRem(r.formatKey("payments", "pending"), join(login, amount)) return nil }) return err}
Writepayment principle
Func (R *redisclient) writepayment (login, Txhash string, amount Int64) error {tx: = R.client.multi () defer TX. Close () TS: = util. Maketimestamp ()/$ _, Err: = Tx. Exec (func () error {//hincrby Eth:miners:login pending-amount tx. Hincrby (R.formatkey ("miners", login), "Pending", (Amount *-1))//hincrby Eth:miners:login paid Amount TX. Hincrby (R.formatkey ("miners", login), "paid", amount)//hincrby eth:finances pending-amount TX. Hincrby (R.formatkey ("finances"), "Pending", (Amount *-1))//hincrby Eth:finances paid Amount TX. Hincrby (R.formatkey ("finances"), "paid", amount)//zadd eth:payments:all ts txHash:login:amount TX. Zadd (R.formatkey ("payments", "all"), Redis. Z{score:float64 (TS), Member:join (txhash, login, amount)})//zadd eth:payments:login ts txhash:amount TX. Zadd (R.formatkey ("payments", login), Redis. Z{score:float64 (TS), Member:join (Txhash, amount)})//zrem Eth:payments:pending login:amount//zrem Command Redis ordered collection (sorted set) Redis zrem command to remove one or more member TX from an ordered set. Zrem (R.formatkey ("payments", "Pending"), join (login, amount))//del command to delete an existing key//del Eth:payments:lock Tx. Del (R.formatkey ("payments", "lock")) return nil}) return err}
Brother chain Tutorial Open-ethereum-pool Mining pool Source Code Analysis payouts module