標籤:erro ber rollback 原理 timestamp time add nan exe
open-ethereum-pooleth礦池-payouts模組PayoutsProcessor定義
type PayoutsProcessor struct { config *PayoutsConfig backend *storage.RedisClient rpc *rpc.RPCClient halt bool lastFail error}
GetPendingPayments原理
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原理
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原理
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原理
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原理
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原理
func (r *RedisClient) WritePayment(login, txHash string, amount int64) error { tx := r.client.Multi() defer tx.Close() ts := util.MakeTimestamp() / 1000 _, 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 命令 Redis 有序集合(sorted set) Redis Zrem 命令用於移除有序集中的一個或多個成員 tx.ZRem(r.formatKey("payments", "pending"), join(login, amount)) //DEL 命令用於刪除已存在的鍵 //DEL eth:payments:lock tx.Del(r.formatKey("payments", "lock")) return nil }) return err}
兄弟連區塊鏈教程open-ethereum-pool礦池源碼分析payouts模組