github.com/aergoio/aergo@v1.3.1/mempool/whitelist.go (about)

     1  package mempool
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  type whitelistConf struct {
     8  	sync.RWMutex
     9  	whitelist map[string]bool
    10  	on        bool
    11  	mp        *MemPool //for log
    12  }
    13  
    14  func newWhitelistConf(mp *MemPool, addresses []string, on bool) *whitelistConf {
    15  	out := &whitelistConf{}
    16  	out.mp = mp
    17  	out.on = on
    18  	out.SetWhitelist(addresses)
    19  	return out
    20  }
    21  
    22  func (w *whitelistConf) GetWhitelist() []string {
    23  	w.RLock()
    24  	defer w.RUnlock()
    25  	ret := []string{}
    26  	for k := range w.whitelist {
    27  		ret = append(ret, k)
    28  	}
    29  	return ret
    30  }
    31  
    32  func (w *whitelistConf) GetOn() bool {
    33  	w.RLock()
    34  	defer w.RUnlock()
    35  	return w.on
    36  }
    37  
    38  func (w *whitelistConf) SetWhitelist(addresses []string) {
    39  	w.Lock()
    40  	defer w.Unlock()
    41  	w.whitelist = make(map[string]bool)
    42  	for _, v := range addresses {
    43  		w.whitelist[v] = true
    44  		w.mp.Debug().Str("address", v).Msg("set account white list")
    45  	}
    46  }
    47  
    48  func (w *whitelistConf) Check(address string) bool {
    49  	if w == nil {
    50  		return true
    51  	}
    52  	w.RLock()
    53  	defer w.RUnlock()
    54  	if !w.on {
    55  		return true
    56  	}
    57  	return w.whitelist[address]
    58  }
    59  
    60  func (w *whitelistConf) Enable(enable bool) {
    61  	w.Lock()
    62  	defer w.Unlock()
    63  	w.on = enable
    64  	w.mp.Debug().Bool("enable", enable).Msg("switch account white list")
    65  }