github.com/DxChainNetwork/dxc@v0.8.1-0.20220824085222-1162e304b6e7/consensus/dpos/blacklist_validator.go (about)

     1  package dpos
     2  
     3  import (
     4  	"github.com/DxChainNetwork/dxc/common"
     5  	"github.com/DxChainNetwork/dxc/core/types"
     6  	"github.com/DxChainNetwork/dxc/log"
     7  )
     8  
     9  type EventCheckRule struct {
    10  	EventSig common.Hash
    11  	Checks   map[int]common.AddressCheckType
    12  }
    13  
    14  type blacklistValidator struct {
    15  	blacks map[common.Address]blacklistDirection
    16  	rules  map[common.Hash]*EventCheckRule
    17  }
    18  
    19  func (b *blacklistValidator) IsAddressDenied(address common.Address, cType common.AddressCheckType) (hit bool) {
    20  	d, exist := b.blacks[address]
    21  	if exist {
    22  		switch cType {
    23  		case common.CheckFrom:
    24  			hit = d != DirectionTo // equals to : d == DirectionFrom || d == DirectionBoth
    25  		case common.CheckTo:
    26  			hit = d != DirectionFrom
    27  		case common.CheckBothInAny:
    28  			hit = true
    29  		default:
    30  			log.Warn("blacklist, unsupported AddressCheckType", "type", cType)
    31  			// Unsupported value, not denied by default
    32  			hit = false
    33  		}
    34  	}
    35  	if hit {
    36  		log.Trace("Hit blacklist", "addr", address.String(), "direction", d, "checkType", cType)
    37  	}
    38  	return
    39  }
    40  
    41  func (b *blacklistValidator) IsLogDenied(evLog *types.Log) bool {
    42  	if nil == evLog || len(evLog.Topics) <= 1 {
    43  		return false
    44  	}
    45  	if rule, exist := b.rules[evLog.Topics[0]]; exist {
    46  		for idx, checkType := range rule.Checks {
    47  			// do a basic check
    48  			if idx >= len(evLog.Topics) {
    49  				log.Error("check index in rule out to range", "sig", rule.EventSig.String(), "checkIdx", idx, "topicsLen", len(evLog.Topics))
    50  				continue
    51  			}
    52  			addr := common.BytesToAddress(evLog.Topics[idx].Bytes())
    53  			if b.IsAddressDenied(addr, checkType) {
    54  				return true
    55  			}
    56  		}
    57  	}
    58  	return false
    59  }