Open-ethereum-pooleth Mineral pool-policy module Policyserver definition
type PolicyServer struct { sync.RWMutex statsMu sync.Mutex config *Config stats map[string]*Stats banChannel chan string startedAt int64 grace int64 timeout int64 blacklist []string whitelist []string storage *storage.RedisClient}
Getblacklist and Getwhitelist
// Always returns list of addresses. If Redis fails it will return empty list.func (r *RedisClient) GetBlacklist() ([]string, error) { //SMEMBERS eth:blacklist //Smembers 命令返回集合中的所有的成员 cmd := r.client.SMembers(r.formatKey("blacklist")) if cmd.Err() != nil { return []string{}, cmd.Err() } return cmd.Val(), nil}// Always returns list of IPs. If Redis fails it will return empty list.func (r *RedisClient) GetWhitelist() ([]string, error) { //SMEMBERS eth:whitelist //Smembers 命令返回集合中的所有的成员 cmd := r.client.SMembers(r.formatKey("whitelist")) if cmd.Err() != nil { return []string{}, cmd.Err() } return cmd.Val(), nil}
isbanned
func (s *PolicyServer) IsBanned(ip string) bool { x := s.Get(ip) return atomic.LoadInt32(&x.Banned) > 0}func (s *PolicyServer) Get(ip string) *Stats { s.statsMu.Lock() defer s.statsMu.Unlock() if x, ok := s.stats[ip]; !ok { x = s.NewStats() s.stats[ip] = x return x } else { x.heartbeat() return x }}func (s *PolicyServer) NewStats() *Stats { x := &Stats{ ConnLimit: s.config.Limits.Limit, } x.heartbeat() return x}
Handling Applymalformedpolicy
//应用格式错误的策略//malformedLimit为5次func (s *PolicyServer) ApplyMalformedPolicy(ip string) bool { x := s.Get(ip) n := x.incrMalformed() if n >= s.config.Banning.MalformedLimit { s.forceBan(x, ip) return false } return true}func (x *Stats) incrMalformed() int32 { return atomic.AddInt32(&x.Malformed, 1)}func (s *PolicyServer) forceBan(x *Stats, ip string) { //没启用Banning或在白名单 if !s.config.Banning.Enabled || s.InWhiteList(ip) { return } atomic.StoreInt64(&x.BannedAt, util.MakeTimestamp()) //x.Banned赋值为1 if atomic.CompareAndSwapInt32(&x.Banned, 0, 1) { //"ipset": "blacklist", if len(s.config.Banning.IPSet) > 0 { s.banChannel <- ip } else { log.Println("Banned peer", ip) } }}
Handling Applyloginpolicy
func (s *PolicyServer) ApplyLoginPolicy(addy, ip string) bool { if s.InBlackList(addy) { x := s.Get(ip) s.forceBan(x, ip) return false } return true}func (s *PolicyServer) InBlackList(addy string) bool { s.RLock() defer s.RUnlock() return util.StringInSlice(addy, s.blacklist)}
Handling Applyloginpolicy
Func (S *policyserver) applysharepolicy (IP string, validshare bool) bool {x: = S.get (IP) x.lock () if Validshare {x.validshares++ If s.config.limits.enabled {//increase allowed number of connections on each V Alid share//per effective share can increase the number of connections//"Limitjump": Ten X.incrlimit (S.config.limits.limitjump) }} else {x.invalidshares++} totalshares: = X.validshares + x.invalidshares//check after Miner submitted this number of shares//30 shares after check//"Checkthreshold":, if totalshares < S.config.bannin G.checkthreshold {X.unlock () return true} validshares: = float32 (x.validshares) Invalidshares: = float32 (X.invalidshares) x.resetshares () X.unlock ()//Invalid share proportional ratio: = invalidshares/validshares//per cent of invalid shares from all shares to ban miner//"invalidpercent": +, if ratio >= S.CONFIG.BANNING.INVALIDP ercent/100.0 {s.Forceban (x, IP) return false} return true}//plus func (x *stats) Incrlimit (n int32) {atomic. AddInt32 (&x.connlimit, N)}//resetfunc (x *stats) Resetshares () {x.validshares = 0 x.invalidshares = 0}
Handling Applylimitpolicy
func (s *PolicyServer) ApplyLimitPolicy(ip string) bool { if !s.config.Limits.Enabled { return true } now := util.MakeTimestamp() //"grace": "5m", if now-s.startedAt > s.grace { //减1 return s.Get(ip).decrLimit() > 0 } return true}func (x *Stats) decrLimit() int32 {return atomic.AddInt32(&x.ConnLimit, -1)}
Handling Banclient
func (s *PolicyServer) BanClient(ip string) { x := s.Get(ip) s.forceBan(x, ip)}
Brother even blockchain tutorial Open-ethereum-pool mine pool source Code Analysis policy module