go-ethereum/consensus/XDPoS/utils/pool.go
wgr523 d47d9a2a99 Consensus V2 variable, timeout pool (#19)
* fill in XDPoS_v2 variables and processQC/TC

* add timeout pool, refine engine variables

* refactor type functions

* solve a small pointer bug

* create general pool and its test, refine engine

* refine pool, add xdpos v2 config cert threshold

* refine config
2021-12-30 11:00:20 +11:00

55 lines
1.1 KiB
Go

package utils
import (
"fmt"
"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
onThresholdFn func(map[common.Hash]PoolObj) error
}
func NewPool(threshold int) *Pool {
return &Pool{
objList: make(map[string]map[common.Hash]PoolObj),
threshold: threshold,
}
}
func (p *Pool) Add(obj PoolObj) error {
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
if len(objListKeyed) >= p.threshold {
delete(p.objList, poolKey)
if p.onThresholdFn != nil {
return p.onThresholdFn(objListKeyed)
} else {
return fmt.Errorf("no call back function for pool")
}
}
return nil
}
func (p *Pool) Clear() {
p.objList = make(map[string]map[common.Hash]PoolObj)
}
func (p *Pool) SetThreshold(t int) {
p.threshold = t
}
func (p *Pool) SetOnThresholdFn(f func(map[common.Hash]PoolObj) error) {
p.onThresholdFn = f
}