open-ethereum-pool以太坊礦池原始碼分析(6)-redis模組

尹成發表於2018-05-21
# open-ethereum-pool以太坊礦池-storage模組(redis儲存)

## Redis基礎

### Set

```
Set:String 型別的無序集合,集合成員是唯一的
SMEMBERS key 返回集合中的所有成員
```

## sorted set

```
sorted set:有序集合和集合一樣也是string型別元素的集合,且不允許重複的成員
不同的是每個元素都會關聯一個double型別的分數
ZRANGE key start stop [WITHSCORES] 通過索引區間返回有序集合成指定區間內的成員
ZREVRANGE key start stop [WITHSCORES] 返回有序集中指定區間內的成員,通過索引,分數從高到底
ZCARD key 獲取有序集合的成員數

ZREMRANGEBYSCORE key min max 移除有序集合中給定的分數區間的所有成員
ZADD key score1 member1 [score2 member2] 向有序集合新增一個或多個成員,或者更新已存在成員的分數
ZINCRBY key increment member 有序集合中對指定成員的分數加上增量 increment
ZREM key member [member ...] 移除有序集合中的一個或多個成員
```

### Hash

```
Hash:是一個string型別的field和value的對映表,hash特別適合用於儲存物件
HGETALL key 獲取在雜湊表中指定 key 的所有欄位和值
HGET key field 獲取儲存在雜湊表中指定欄位的值

HSET key field value 將雜湊表 key 中的欄位 field 的值設為 value
HINCRBY key field increment 為雜湊表 key 中的指定欄位的整數值加上增量 increment
HDEL key field1 [field2] 刪除一個或多個雜湊表欄位
```

### 鍵(key)

```
SCAN cursor [MATCH pattern] [COUNT count] 用於迭代當前資料庫中的資料庫鍵

RENAME key newkey 修改 key 的名稱
EXPIRE key seconds 為給定 key 設定過期時間
DEL key 該命令用於在 key 存在時刪除 key
EXISTS key 檢查給定 key 是否存在
```

### 字串(String)

```
GET key 獲取指定 key 的值

SETNX key value 只有在 key 不存在時設定 key 的值
```

### Redis 事務

```
WATCH key [key ...] 監視一個(或多個) key ,如果在事務執行之前這個(或這些) key 被其他命令所改動,那麼事務將被打斷
MULTI 標記一個事務塊的開始
EXEC 取消 WATCH 命令對所有 key 的監視
```

## Redis重要資料結構

### 節點狀態eth:nodes

```
型別:Hash
HGETALL eth:nodes

> HGETALL eth:nodes
1) "main:name"
2) "main"
3) "main:height"
4) "2795569"
5) "main:difficulty"
6) "1329012570"
7) "main:lastBeat"
8) "1520509038"

//如下為更新操作,慎重使用
HSET eth:nodes <name>:name <name>
HSET eth:nodes <name>:height height
HSET eth:nodes <name>:difficulty diff
HSET eth:nodes <name>:lastBeat now
```

### 統計eth:stats

```
型別:Hash
HGETALL eth:stats

> HGETALL eth:stats
1) "lastBlockFound"
2) "1520402736"
3) "roundShares"
4) "1160480000000000"

//如下為更新操作,慎重使用
HSET eth:stats lastBlockFound ts
HINCRBY eth:stats roundShares diff
HDEL eth:stats roundShares
```

### 財務統計eth:finances

```
型別:Hash
HGETALL eth:finances

> HGETALL eth:finances
1) "immature"
2) "0"
3) "lastCreditHeight"
4) "2795263"
5) "totalMined"
6) "8730817950017"
7) "balance"
8) "8730817950029"
9) "lastCreditHash"
10) "0xd52c77ffe9266158df3e72a92ea25b55238089b6ab99c92d4aa181ba83721a12"

//如下為更新操作,慎重使用
HINCRBY eth:finances balance -amount
HINCRBY eth:finances pending amount
//如下兩行為回滾
HINCRBY eth:finances balance amount
HINCRBY eth:finances pending -amount

HINCRBY eth:finances paid amount
HINCRBY eth:finances immature total

HSET eth:finances lastCreditHeight blockHeight
HSET eth:finances lastCreditHash blockHash
HSET eth:finances totalMined blockRewardInShannon
```

### 信用 eth:credits:all

```
型別:sorted set
ZREVRANGE eth:credits:all 0 -1 WITHSCORES

> ZREVRANGE eth:credits:all 0 5 WITHSCORES
1) "0xd52c77ffe9266158df3e72a92ea25b55238089b6ab99c92d4aa181ba83721a12:1520506196:3125000000000000000"
2) "2795263"
3) "0x546fdb7c60976ea84b44da0d940c1c7cb33ffc1b182fc625087ca1185e212abe:1520506196:5064246008000000000"
4) "2795260"
5) "0x0d7b4f760f83281cccf312ce0d67a4176115de9c62b5875ed56b6704d022b978:1520506196:5000240510000000000"
6) "2795259"
7) "0x3a563e8d16a492157d46835e8d488f02b8cf3712775d68875b91680f1ed02a98:1520506196:5019661474000000000"
8) "2795257"
9) "0xde58d9a1d7b02ae4e2bcde436943fe75ba273b327b605d4aa01f8f3f663e3c7b:1520506196:5001432583000000000"
10) "2795256"
11) "0x662cda661fcd90ad2919baf48c3a5fd707f309606e9e604627a133006dbff393:1520506196:5000416532000000000"
12) "2795255

//如下為更新操作,慎重使用
ZADD eth:credits:all Height blockHash:ts:blockReward
SETNX eth:credits:Height:blockHash login amount
```

### 信用事務 eth:credits:immature:Height:Hash

```
型別:Hash
HGETALL eth:credits:immature:Height:Hash

//如下為更新操作,慎重使用
SETNX eth:credits:immature:Height:Hash login amount
WATCH eth:credits:immature:Height:Hash
```

## 其他資料結構

### 黑名單eth:blacklist

```
型別:Set
SMEMBERS eth:blacklist
```

### 白名單eth:whitelist

```
型別:Set
SMEMBERS eth:whitelist
```

### 礦機eth:miners:login

```
型別:Hash
SCAN 0 MATCH eth:miners:* COUNT 100
HGETALL eth:miners:login
HGET eth:miners:login balance

//如下為更新操作,慎重使用
HINCRBY eth:miners:login blocksFound 1
HSET eth:miners:login lastShare ts

//如下為更新操作,慎重使用
HINCRBY eth:miners:login balance -amount
HINCRBY eth:miners:login pending amount
//如下兩行為回滾
HINCRBY eth:miners:login balance amount
HINCRBY eth:miners:login pending -amount

HINCRBY eth:miners:login paid amount
HINCRBY eth:miners:login immature amount
HINCRBY eth:miners:login immature -amount

EXISTS eth:miners:login
```

### Shares eth:shares

```
型別:Hash
HGETALL eth:shares:roundCurrent
HGET eth:shares:roundCurrent login
HGETALL eth:shares:round<height>:nonce

//如下為更新操作,慎重使用
HINCRBY eth:shares:roundCurrent login diff
RENAME eth:shares:roundCurrent eth:shares:round<height>:nonce
```

### 提交Share eth:pow

```
型別:sorted set
ZRANGEBYSCORE eth:pow 0 -1 WITHSCORES

//如下為更新操作,慎重使用
ZREMRANGEBYSCORE eth:pow -inf (height-8
ZADD eth:pow height nonce:powHash:mixDigest
```

### 爆塊者eth:finders

```
型別:sorted set
ZRANGEBYSCORE eth:finders 0 -1 WITHSCORES

//如下為更新操作,慎重使用
ZINCRBY eth:finders 1 login
```

### 候選塊eth:blocks:candidates

```
型別:sorted set
ZRANGEBYSCORE eth:blocks:candidates 0 -1 WITHSCORES
ZRANGEBYSCORE eth:blocks:candidates 0 maxHeight WITHSCORES
ZCARD eth:blocks:candidates

//如下為更新操作,慎重使用
ZADD eth:blocks:candidates height hashHex:ts:roundDiff:totalShares
ZREM eth:blocks:candidates candidateKey
```

### 未成年塊 eth:blocks:immature

```
型別:sorted set
ZRANGEBYSCORE eth:blocks:candidates 0 -1 WITHSCORES
ZREVRANGE eth:blocks:immature 0 -1 WITHSCORES
ZCARD eth:blocks:immature

ZRANGEBYSCORE eth:blocks:candidates 0 maxHeight WITHSCORES

//如下為更新操作,慎重使用
ZADD eth:blocks:immature Height key()
ZREM eth:blocks:immature immatureKey
```

### 成熟塊 eth:blocks:matured

```
型別:sorted set
ZRANGEBYSCORE eth:blocks:matured 0 maxBlocks-1 WITHSCORES
ZREVRANGE eth:blocks:matured 0 max-1 WITHSCORES
ZCARD eth:blocks:matured

//如下為更新操作,慎重使用
ZADD eth:blocks:matured Height key()
```

### 算力統計 eth:hashrate

```
型別:sorted set
ZRANGEBYSCORE eth:hashrate 0 -1 WITHSCORES

//如下為更新操作,慎重使用
ZADD eth:hashrate ts diff:login:id:ms
ZREMRANGEBYSCORE eth:hashrate -inf (now-window

SCAN eth:hashrate:* 100
ZRANGEBYSCORE eth:hashrate:login 0 -1 WITHSCORES

//如下為更新操作,慎重使用
ZADD eth:hashrate:login ts diff:id:ms
EXPIRE eth:hashrate:login expire
ZREMRANGEBYSCORE eth:hashrate:login -inf (now-largeWindow
```

### 未支付 eth:payments:pending

```
型別:sorted set
ZREVRANGE eth:payments:pending 0 -1 WITHSCORES

//如下為更新操作,慎重使用
ZADD eth:payments:pending ts login:amount
//如下一行為回滾
ZREM eth:payments:pending login:amount
```

### 所有支付 eth:payments:all

```
型別:sorted set
ZRANGEBYSCORE eth:payments:all 0 maxPayments-1 WITHSCORES
ZCARD eth:payments:all

//如下為更新操作,慎重使用
ZADD eth:payments:all ts txHash:login:amount
```

### 單個支付 eth:payments:login

```
型別:sorted set
ZREVRANGE eth:payments:login 0 maxPayments-1 WITHSCORES
ZCARD eth:payments:login

//如下為更新操作,慎重使用
ZADD eth:payments:all ts txHash:login:amount
```

### 支付鎖eth:payments:lock

```
型別:String
GET eth:payments:lock

//如下為更新操作,慎重使用
SETNX eth:payments:lock login:amount 0
DEL eth:payments:lock
```

## 參考文件

* [Redis 集合(Set)](http://www.runoob.com/redis/redis-sets.html)
* [Redis 有序集合(sorted set)](http://www.runoob.com/redis/redis-sorted-sets.html)
* [Redis 雜湊(Hash)](http://www.runoob.com/redis/redis-hashes.html)
* [MULTI](http://redisdoc.com/transaction/multi.html)
* [Redis Hset 命令](http://www.runoob.com/redis/hashes-hset.html)
* [gopkg.in/redis.v3](https://godoc.org/gopkg.in/redis.v3)







網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN




網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN







網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN




網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN

相關文章