区块链教程open-ethereum-pool矿池源码分析policy模块

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)}


作者:兄弟连区块链入门教程
链接:https://www.imooc.com/article/253287
来源:慕课网


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31557831/viewspace-2216009/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/31557831/viewspace-2216009/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值