go-ethereum/consensus/XDPoS/utils/pool.go
Liam 35eebabae0 xin-106 add generated message into its pool (#32)
* add debug log and change to contain or add for cache

* add generated message into its pool
2021-12-30 11:45:22 +11:00

54 lines
1.1 KiB
Go

package utils
import (
"github.com/XinFinOrg/XDPoSChain/common"
)
type PoolObj interface {
Hash() common.Hash
PoolKey() string
}
type Pool struct {
objList map[string]map[common.Hash]PoolObj
threshold int
}
func NewPool(threshold int) *Pool {
return &Pool{
objList: make(map[string]map[common.Hash]PoolObj),
threshold: threshold,
}
}
// return true if it has reached threshold
func (p *Pool) Add(obj PoolObj) (bool, int, map[common.Hash]PoolObj) {
poolKey := obj.PoolKey()
objListKeyed, ok := p.objList[poolKey]
if !ok {
p.objList[poolKey] = make(map[common.Hash]PoolObj)
objListKeyed = p.objList[poolKey]
}
objListKeyed[obj.Hash()] = obj
numOfItems := len(objListKeyed)
if numOfItems >= p.threshold {
delete(p.objList, poolKey)
return true, numOfItems, objListKeyed
}
return false, numOfItems, objListKeyed
}
func (p *Pool) Size(obj PoolObj) int {
poolKey := obj.PoolKey()
objListKeyed, ok := p.objList[poolKey]
if !ok {
return 0
}
return len(objListKeyed)
}
func (p *Pool) Clear() {
p.objList = make(map[string]map[common.Hash]PoolObj)
}
func (p *Pool) SetThreshold(t int) {
p.threshold = t
}