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 valid share???????????? The number of connections that can be increased per valid share???????????? "Limitjump": Ten???????????? X.incrlimit (s.config.limits.limitjump)????????}????} else {???????? x.invalidshares++????}???? Totalshares: = x.validshares + x.invalidshares???? Check after the miner submitted this number of shares???? 30 shares after check???? "Checkthreshold":,???? If Totalshares < S.config.banning.checkthreshold {???????? X.unlock ()???????? return true????}???? Validshares: = float32 (x.validshares)???? Invalidshares: = float32 (x.invalidshares)???? X.resetshares ()???? X.unlock ()???? Invalid share scale???? Ratio: = Invalidshares/validshares???? Percent of invalid shares from all shares to ban miner???? "Invalidpercent":,???? If ratio >= s.config.banning.invalidpercent/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