兄弟連區塊鏈教程open-ethereum-pool礦池源碼分析policy模組

來源:互聯網
上載者:User

標籤:returns   limit   slice   ted   log   its   ase   submit   hit   

open-ethereum-pooleth礦池-policy模組PolicyServer定義
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和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}
處理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)        }    }}
處理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)}
處理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            //每個有效share可以增加的串連數            //"limitJump": 10            x.incrLimit(s.config.Limits.LimitJump)        }    } else {        x.InvalidShares++    }    totalShares := x.ValidShares + x.InvalidShares    //Check after after miner submitted this number of shares    //30個shares後檢查    //"checkThreshold": 30,    if totalShares < s.config.Banning.CheckThreshold {        x.Unlock()        return true    }    validShares := float32(x.ValidShares)    invalidShares := float32(x.InvalidShares)    x.resetShares()    x.Unlock()    //無效share比例    ratio := invalidShares / validShares    // Percent of invalid shares from all shares to ban miner    //"invalidPercent": 30,    if ratio >= s.config.Banning.InvalidPercent/100.0 {        s.forceBan(x, ip)        return false    }    return true}//加func (x *Stats) incrLimit(n int32) {    atomic.AddInt32(&x.ConnLimit, n)}//resetfunc (x *Stats) resetShares() {    x.ValidShares = 0    x.InvalidShares = 0}
處理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)}
處理BanClient
func (s *PolicyServer) BanClient(ip string) {    x := s.Get(ip)    s.forceBan(x, ip)}

兄弟連區塊鏈教程open-ethereum-pool礦池源碼分析policy模組

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.