mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-20 21:54:30 +00:00
* check block header after vote pool reached * refactor test_helper to fix issues with tests randomly failing
59 lines
1.3 KiB
Go
59 lines
1.3 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 {
|
|
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)
|
|
}
|
|
|
|
// Given the pool object, clear all object under the same pool key
|
|
func (p *Pool) ClearPoolKeyByObj(obj PoolObj) {
|
|
poolKey := obj.PoolKey()
|
|
delete(p.objList, poolKey)
|
|
}
|
|
|
|
func (p *Pool) Clear() {
|
|
p.objList = make(map[string]map[common.Hash]PoolObj)
|
|
}
|
|
|
|
func (p *Pool) SetThreshold(t int) {
|
|
p.threshold = t
|
|
}
|