Commit 8e729b5d authored by 陈德海's avatar 陈德海

fix linter

parent 11da7ff1
...@@ -117,9 +117,9 @@ type RoundState struct { ...@@ -117,9 +117,9 @@ type RoundState struct {
// RoundStateMessage ... // RoundStateMessage ...
func (rs *RoundState) RoundStateMessage() *tmtypes.NewRoundStepMsg { func (rs *RoundState) RoundStateMessage() *tmtypes.NewRoundStepMsg {
return &tmtypes.NewRoundStepMsg{ return &tmtypes.NewRoundStepMsg{
Height: rs.Height, Height: rs.Height,
Round: int32(rs.Round), Round: int32(rs.Round),
Step: int32(rs.Step), Step: int32(rs.Step),
SecondsSinceStartTime: int32(time.Since(rs.StartTime).Seconds()), SecondsSinceStartTime: int32(time.Since(rs.StartTime).Seconds()),
LastCommitRound: int32(rs.LastCommit.Round()), LastCommitRound: int32(rs.LastCommit.Round()),
} }
......
...@@ -78,9 +78,9 @@ func ParseX509CertificateToSm2(x509Cert *x509.Certificate) *sm2.Certificate { ...@@ -78,9 +78,9 @@ func ParseX509CertificateToSm2(x509Cert *x509.Certificate) *sm2.Certificate {
UnknownExtKeyUsage: x509Cert.UnknownExtKeyUsage, UnknownExtKeyUsage: x509Cert.UnknownExtKeyUsage,
BasicConstraintsValid: x509Cert.BasicConstraintsValid, BasicConstraintsValid: x509Cert.BasicConstraintsValid,
IsCA: x509Cert.IsCA, IsCA: x509Cert.IsCA,
MaxPathLen: x509Cert.MaxPathLen, MaxPathLen: x509Cert.MaxPathLen,
MaxPathLenZero: x509Cert.MaxPathLenZero, MaxPathLenZero: x509Cert.MaxPathLenZero,
SubjectKeyId: x509Cert.SubjectKeyId, SubjectKeyId: x509Cert.SubjectKeyId,
AuthorityKeyId: x509Cert.AuthorityKeyId, AuthorityKeyId: x509Cert.AuthorityKeyId,
...@@ -141,9 +141,9 @@ func ParseSm2CertificateToX509(sm2Cert *sm2.Certificate) *x509.Certificate { ...@@ -141,9 +141,9 @@ func ParseSm2CertificateToX509(sm2Cert *sm2.Certificate) *x509.Certificate {
UnknownExtKeyUsage: sm2Cert.UnknownExtKeyUsage, UnknownExtKeyUsage: sm2Cert.UnknownExtKeyUsage,
BasicConstraintsValid: sm2Cert.BasicConstraintsValid, BasicConstraintsValid: sm2Cert.BasicConstraintsValid,
IsCA: sm2Cert.IsCA, IsCA: sm2Cert.IsCA,
MaxPathLen: sm2Cert.MaxPathLen, MaxPathLen: sm2Cert.MaxPathLen,
MaxPathLenZero: sm2Cert.MaxPathLenZero, MaxPathLenZero: sm2Cert.MaxPathLenZero,
SubjectKeyId: sm2Cert.SubjectKeyId, SubjectKeyId: sm2Cert.SubjectKeyId,
AuthorityKeyId: sm2Cert.AuthorityKeyId, AuthorityKeyId: sm2Cert.AuthorityKeyId,
......
...@@ -5,5 +5,6 @@ ...@@ -5,5 +5,6 @@
package init package init
import ( import (
_ "github.com/33cn/plugin/plugin/mempool/price"
_ "github.com/33cn/plugin/plugin/mempool/score" _ "github.com/33cn/plugin/plugin/mempool/score"
) )
...@@ -8,14 +8,16 @@ import ( ...@@ -8,14 +8,16 @@ import (
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
) )
//PriceQueue 简单队列模式(默认提供一个队列,便于测试) var mempoolDupResendInterval int64 = 600 // mempool内交易过期时间,10分钟
// PriceQueue 价格队列模式(价格=手续费/交易字节数,价格高者优先,同价则时间早优先)
type PriceQueue struct { type PriceQueue struct {
txMap map[string]*SkipValue txMap map[string]*SkipValue
txList *SkipList txList *SkipList
subConfig subConfig subConfig subConfig
} }
//NewPriceQueue 创建队列 // NewPriceQueue 创建队列
func NewPriceQueue(subcfg subConfig) *PriceQueue { func NewPriceQueue(subcfg subConfig) *PriceQueue {
return &PriceQueue{ return &PriceQueue{
txMap: make(map[string]*SkipValue, subcfg.PoolCacheSize), txMap: make(map[string]*SkipValue, subcfg.PoolCacheSize),
...@@ -44,7 +46,7 @@ func (cache *PriceQueue) Exist(hash string) bool { ...@@ -44,7 +46,7 @@ func (cache *PriceQueue) Exist(hash string) bool {
//GetItem 获取数据通过 key //GetItem 获取数据通过 key
func (cache *PriceQueue) GetItem(hash string) (*mempool.Item, error) { func (cache *PriceQueue) GetItem(hash string) (*mempool.Item, error) {
if k, exist := cache.txMap[string(hash)]; exist { if k, exist := cache.txMap[hash]; exist {
return k.Value.(*mempool.Item), nil return k.Value.(*mempool.Item), nil
} }
return nil, types.ErrNotFound return nil, types.ErrNotFound
...@@ -57,25 +59,23 @@ func (cache *PriceQueue) Push(item *mempool.Item) error { ...@@ -57,25 +59,23 @@ func (cache *PriceQueue) Push(item *mempool.Item) error {
s := cache.txMap[string(hash)] s := cache.txMap[string(hash)]
addedItem := s.Value.(*mempool.Item) addedItem := s.Value.(*mempool.Item)
addedTime := addedItem.EnterTime addedTime := addedItem.EnterTime
if types.Now().Unix()-addedTime < mempoolDupResendInterval { if types.Now().Unix()-addedTime < mempoolDupResendInterval {
return types.ErrTxExist return types.ErrTxExist
} else {
// 超过2分钟之后的重发交易返回nil,再次发送给P2P,但是不再次加入mempool
// 并修改其enterTime,以避免该交易一直在节点间被重发
newEnterTime := types.Now().Unix()
resendItem := &mempool.Item{Value: item.Value, Priority: item.Value.Fee, EnterTime: newEnterTime}
var err error
sv, err := cache.newSkipValue(resendItem)
if err != nil {
return err
}
cache.Remove(string(hash))
cache.txList.Insert(sv)
cache.txMap[string(hash)] = sv
// ------------------
return nil
} }
// 超过2分钟之后的重发交易返回nil,再次发送给P2P,但是不再次加入mempool
// 并修改其enterTime,以避免该交易一直在节点间被重发
newEnterTime := types.Now().Unix()
resendItem := &mempool.Item{Value: item.Value, Priority: item.Value.Fee, EnterTime: newEnterTime}
var err error
sv, err := cache.newSkipValue(resendItem)
if err != nil {
return err
}
cache.Remove(string(hash))
cache.txList.Insert(sv)
cache.txMap[string(hash)] = sv
// ------------------
return nil
} }
it := &mempool.Item{Value: item.Value, Priority: item.Value.Fee, EnterTime: item.EnterTime} it := &mempool.Item{Value: item.Value, Priority: item.Value.Fee, EnterTime: item.EnterTime}
...@@ -85,7 +85,7 @@ func (cache *PriceQueue) Push(item *mempool.Item) error { ...@@ -85,7 +85,7 @@ func (cache *PriceQueue) Push(item *mempool.Item) error {
} }
if int64(cache.txList.Len()) >= cache.subConfig.PoolCacheSize { if int64(cache.txList.Len()) >= cache.subConfig.PoolCacheSize {
tail := cache.txList.GetIterator().Last() tail := cache.txList.GetIterator().Last()
//价格高 //价格高存留
if sv.Compare(tail) == -1 { if sv.Compare(tail) == -1 {
cache.Remove(string(tail.Value.(*mempool.Item).Value.Hash())) cache.Remove(string(tail.Value.(*mempool.Item).Value.Hash()))
} else { } else {
......
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package price
import (
"runtime"
log "github.com/33cn/chain33/common/log/log15"
)
var (
mlog = log.New("module", "mempool")
poolCacheSize int64 = 10240 // mempool容量
mempoolExpiredInterval int64 = 600 // mempool内交易过期时间,10分钟
mempoolReSendInterval int64 = 60 // mempool内交易重发时间,1分钟
mempoolDupResendInterval int64 = 120 // mempool重复交易可再次发送间隔,120秒
mempoolAddedTxSize = 102400 // 已添加过的交易缓存大小
maxTxNumPerAccount int64 = 100 // TODO 每个账户在mempool中最大交易数量,10
processNum int
)
// TODO
func init() {
processNum = runtime.NumCPU()
}
package price package price
import ( import (
clog "github.com/33cn/chain33/common/log"
log "github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/queue" "github.com/33cn/chain33/queue"
drivers "github.com/33cn/chain33/system/mempool" drivers "github.com/33cn/chain33/system/mempool"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
) )
func SetLogLevel(level string) {
clog.SetLogLevel(level)
}
func DisableLog() {
mlog.SetHandler(log.DiscardHandler())
}
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// Module Mempool // Module Mempool
type Mempool struct {
subConfig subConfig
}
type subConfig struct { type subConfig struct {
PoolCacheSize int64 `json:"poolCacheSize"` PoolCacheSize int64 `json:"poolCacheSize"`
MinTxFee int64 `json:"minTxFee"` MinTxFee int64 `json:"minTxFee"`
...@@ -33,7 +19,7 @@ func init() { ...@@ -33,7 +19,7 @@ func init() {
drivers.Reg("price", New) drivers.Reg("price", New)
} }
//New 创建timeline cache 结构的 mempool //New 创建price cache 结构的 mempool
func New(cfg *types.Mempool, sub []byte) queue.Module { func New(cfg *types.Mempool, sub []byte) queue.Module {
c := drivers.NewMempool(cfg) c := drivers.NewMempool(cfg)
var subcfg subConfig var subcfg subConfig
......
...@@ -10,11 +10,13 @@ import ( ...@@ -10,11 +10,13 @@ import (
const maxLevel = 32 const maxLevel = 32
const prob = 0.35 const prob = 0.35
// SkipValue 跳跃表节点
type SkipValue struct { type SkipValue struct {
Price int64 Price int64
Value interface{} Value interface{}
} }
// Compare 比较函数
func (v *SkipValue) Compare(value *SkipValue) int { func (v *SkipValue) Compare(value *SkipValue) int {
if v.Price > value.Price { if v.Price > value.Price {
return -1 return -1
...@@ -45,6 +47,7 @@ type SkipListIterator struct { ...@@ -45,6 +47,7 @@ type SkipListIterator struct {
node *skipListNode node *skipListNode
} }
// First 获取第一个节点
func (sli *SkipListIterator) First() *SkipValue { func (sli *SkipListIterator) First() *SkipValue {
if sli.list.header.next[0] == nil { if sli.list.header.next[0] == nil {
return nil return nil
...@@ -53,6 +56,7 @@ func (sli *SkipListIterator) First() *SkipValue { ...@@ -53,6 +56,7 @@ func (sli *SkipListIterator) First() *SkipValue {
return sli.node.Value return sli.node.Value
} }
// Last 获取最后一个节点
func (sli *SkipListIterator) Last() *SkipValue { func (sli *SkipListIterator) Last() *SkipValue {
if sli.list.tail == nil { if sli.list.tail == nil {
return nil return nil
...@@ -61,13 +65,7 @@ func (sli *SkipListIterator) Last() *SkipValue { ...@@ -61,13 +65,7 @@ func (sli *SkipListIterator) Last() *SkipValue {
return sli.node.Value return sli.node.Value
} }
func (sli *SkipListIterator) Current() *SkipValue { // Prev 获取上一个节点
if sli.node == nil {
return nil
}
return sli.node.Value
}
func (node *skipListNode) Prev() *skipListNode { func (node *skipListNode) Prev() *skipListNode {
if node == nil || node.prev == nil { if node == nil || node.prev == nil {
return nil return nil
...@@ -75,6 +73,7 @@ func (node *skipListNode) Prev() *skipListNode { ...@@ -75,6 +73,7 @@ func (node *skipListNode) Prev() *skipListNode {
return node.prev return node.prev
} }
// Next 获取下一个节点
func (node *skipListNode) Next() *skipListNode { func (node *skipListNode) Next() *skipListNode {
if node == nil || node.next[0] == nil { if node == nil || node.next[0] == nil {
return nil return nil
...@@ -82,15 +81,6 @@ func (node *skipListNode) Next() *skipListNode { ...@@ -82,15 +81,6 @@ func (node *skipListNode) Next() *skipListNode {
return node.next[0] return node.next[0]
} }
func (sli *SkipListIterator) Seek(value *SkipValue) *SkipValue {
x := sli.list.find(value)
if x.next[0] == nil {
return nil
}
sli.node = x.next[0]
return sli.node.Value
}
func newskipListNode(level int, value *SkipValue) *skipListNode { func newskipListNode(level int, value *SkipValue) *skipListNode {
node := &skipListNode{} node := &skipListNode{}
node.next = make([]*skipListNode, level) node.next = make([]*skipListNode, level)
...@@ -98,7 +88,7 @@ func newskipListNode(level int, value *SkipValue) *skipListNode { ...@@ -98,7 +88,7 @@ func newskipListNode(level int, value *SkipValue) *skipListNode {
return node return node
} }
//构建一个value的最小值 //NewSkipList 构建一个value的最小值
func NewSkipList(min *SkipValue) *SkipList { func NewSkipList(min *SkipValue) *SkipList {
sl := &SkipList{} sl := &SkipList{}
sl.level = 1 sl.level = 1
...@@ -110,7 +100,7 @@ func randomLevel() int { ...@@ -110,7 +100,7 @@ func randomLevel() int {
level := 1 level := 1
t := prob * 0xFFFF t := prob * 0xFFFF
for rand.Int()&0xFFFF < int(t) { for rand.Int()&0xFFFF < int(t) {
level += 1 level++
if level == maxLevel { if level == maxLevel {
break break
} }
...@@ -118,6 +108,7 @@ func randomLevel() int { ...@@ -118,6 +108,7 @@ func randomLevel() int {
return level return level
} }
// GetIterator 返回第一个节点
func (sl *SkipList) GetIterator() *SkipListIterator { func (sl *SkipList) GetIterator() *SkipListIterator {
it := &SkipListIterator{} it := &SkipListIterator{}
it.list = sl it.list = sl
...@@ -125,14 +116,11 @@ func (sl *SkipList) GetIterator() *SkipListIterator { ...@@ -125,14 +116,11 @@ func (sl *SkipList) GetIterator() *SkipListIterator {
return it return it
} }
// Len 返回节点数
func (sl *SkipList) Len() int { func (sl *SkipList) Len() int {
return sl.count return sl.count
} }
func (sl *SkipList) Level() int {
return sl.level
}
func (sl *SkipList) find(value *SkipValue) *skipListNode { func (sl *SkipList) find(value *SkipValue) *skipListNode {
x := sl.header x := sl.header
for i := sl.level - 1; i >= 0; i-- { for i := sl.level - 1; i >= 0; i-- {
...@@ -144,10 +132,12 @@ func (sl *SkipList) find(value *SkipValue) *skipListNode { ...@@ -144,10 +132,12 @@ func (sl *SkipList) find(value *SkipValue) *skipListNode {
return x return x
} }
// FindCount 返回查询次数
func (sl *SkipList) FindCount() int { func (sl *SkipList) FindCount() int {
return sl.findcount return sl.findcount
} }
// Find 查找skipvalue
func (sl *SkipList) Find(value *SkipValue) *SkipValue { func (sl *SkipList) Find(value *SkipValue) *SkipValue {
x := sl.find(value) x := sl.find(value)
if x.next[0] != nil && x.next[0].Value.Compare(value) == 0 { if x.next[0] != nil && x.next[0].Value.Compare(value) == 0 {
...@@ -156,14 +146,7 @@ func (sl *SkipList) Find(value *SkipValue) *SkipValue { ...@@ -156,14 +146,7 @@ func (sl *SkipList) Find(value *SkipValue) *SkipValue {
return nil return nil
} }
func (sl *SkipList) FindGreaterOrEqual(value *SkipValue) *SkipValue { // Insert 插入节点
x := sl.find(value)
if x.next[0] != nil {
return x.next[0].Value
}
return nil
}
func (sl *SkipList) Insert(value *SkipValue) int { func (sl *SkipList) Insert(value *SkipValue) int {
var update [maxLevel]*skipListNode var update [maxLevel]*skipListNode
x := sl.header x := sl.header
...@@ -202,6 +185,7 @@ func (sl *SkipList) Insert(value *SkipValue) int { ...@@ -202,6 +185,7 @@ func (sl *SkipList) Insert(value *SkipValue) int {
return 1 return 1
} }
// Delete 删除节点
func (sl *SkipList) Delete(value *SkipValue) int { func (sl *SkipList) Delete(value *SkipValue) int {
var update [maxLevel]*skipListNode var update [maxLevel]*skipListNode
x := sl.header x := sl.header
...@@ -232,7 +216,7 @@ func (sl *SkipList) Delete(value *SkipValue) int { ...@@ -232,7 +216,7 @@ func (sl *SkipList) Delete(value *SkipValue) int {
return 1 return 1
} }
//测试用的输出函数 // Print 测试用的输出函数
func (l *SkipList) Print() { func (l *SkipList) Print() {
if l.count > 0 { if l.count > 0 {
r := l.header r := l.header
......
...@@ -8,14 +8,16 @@ import ( ...@@ -8,14 +8,16 @@ import (
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
) )
//ScoreQueue 简单队列模式(默认提供一个队列,便于测试) var mempoolDupResendInterval int64 = 600 // mempool内交易过期时间,10分钟
// ScoreQueue 分数队列模式(分数=常量a*手续费/交易字节数-常量b*时间*定量c,按分数排队,高的优先,常量a,b和定量c可配置)
type ScoreQueue struct { type ScoreQueue struct {
txMap map[string]*SkipValue txMap map[string]*SkipValue
txList *SkipList txList *SkipList
subConfig subConfig subConfig subConfig
} }
//NewScoreQueue 创建队列 // NewScoreQueue 创建队列
func NewScoreQueue(subcfg subConfig) *ScoreQueue { func NewScoreQueue(subcfg subConfig) *ScoreQueue {
return &ScoreQueue{ return &ScoreQueue{
txMap: make(map[string]*SkipValue, subcfg.PoolCacheSize), txMap: make(map[string]*SkipValue, subcfg.PoolCacheSize),
...@@ -36,7 +38,7 @@ func (cache *ScoreQueue) newSkipValue(item *mempool.Item) (*SkipValue, error) { ...@@ -36,7 +38,7 @@ func (cache *ScoreQueue) newSkipValue(item *mempool.Item) (*SkipValue, error) {
return &SkipValue{Score: cache.subConfig.PriceConstant*(item.Value.Fee/int64(size))*cache.subConfig.PricePower - cache.subConfig.TimeParam*item.EnterTime, Value: item}, nil return &SkipValue{Score: cache.subConfig.PriceConstant*(item.Value.Fee/int64(size))*cache.subConfig.PricePower - cache.subConfig.TimeParam*item.EnterTime, Value: item}, nil
} }
//Exist 是否存在 // Exist 是否存在
func (cache *ScoreQueue) Exist(hash string) bool { func (cache *ScoreQueue) Exist(hash string) bool {
_, exists := cache.txMap[hash] _, exists := cache.txMap[hash]
return exists return exists
...@@ -44,7 +46,7 @@ func (cache *ScoreQueue) Exist(hash string) bool { ...@@ -44,7 +46,7 @@ func (cache *ScoreQueue) Exist(hash string) bool {
//GetItem 获取数据通过 key //GetItem 获取数据通过 key
func (cache *ScoreQueue) GetItem(hash string) (*mempool.Item, error) { func (cache *ScoreQueue) GetItem(hash string) (*mempool.Item, error) {
if k, exist := cache.txMap[string(hash)]; exist { if k, exist := cache.txMap[hash]; exist {
return k.Value.(*mempool.Item), nil return k.Value.(*mempool.Item), nil
} }
return nil, types.ErrNotFound return nil, types.ErrNotFound
...@@ -60,22 +62,21 @@ func (cache *ScoreQueue) Push(item *mempool.Item) error { ...@@ -60,22 +62,21 @@ func (cache *ScoreQueue) Push(item *mempool.Item) error {
if types.Now().Unix()-addedTime < mempoolDupResendInterval { if types.Now().Unix()-addedTime < mempoolDupResendInterval {
return types.ErrTxExist return types.ErrTxExist
} else {
// 超过2分钟之后的重发交易返回nil,再次发送给P2P,但是不再次加入mempool
// 并修改其enterTime,以避免该交易一直在节点间被重发
newEnterTime := types.Now().Unix()
resendItem := &mempool.Item{Value: item.Value, Priority: item.Value.Fee, EnterTime: newEnterTime}
var err error
sv, err := cache.newSkipValue(resendItem)
if err != nil {
return err
}
cache.Remove(string(hash))
cache.txList.Insert(sv)
cache.txMap[string(hash)] = sv
// ------------------
return nil
} }
// 超过2分钟之后的重发交易返回nil,再次发送给P2P,但是不再次加入mempool
// 并修改其enterTime,以避免该交易一直在节点间被重发
newEnterTime := types.Now().Unix()
resendItem := &mempool.Item{Value: item.Value, Priority: item.Value.Fee, EnterTime: newEnterTime}
var err error
sv, err := cache.newSkipValue(resendItem)
if err != nil {
return err
}
cache.Remove(string(hash))
cache.txList.Insert(sv)
cache.txMap[string(hash)] = sv
// ------------------
return nil
} }
it := &mempool.Item{Value: item.Value, Priority: item.Value.Fee, EnterTime: item.EnterTime} it := &mempool.Item{Value: item.Value, Priority: item.Value.Fee, EnterTime: item.EnterTime}
...@@ -85,7 +86,7 @@ func (cache *ScoreQueue) Push(item *mempool.Item) error { ...@@ -85,7 +86,7 @@ func (cache *ScoreQueue) Push(item *mempool.Item) error {
} }
if int64(cache.txList.Len()) >= cache.subConfig.PoolCacheSize { if int64(cache.txList.Len()) >= cache.subConfig.PoolCacheSize {
tail := cache.txList.GetIterator().Last() tail := cache.txList.GetIterator().Last()
//价格高 //分数高存留
if sv.Compare(tail) == -1 { if sv.Compare(tail) == -1 {
cache.Remove(string(tail.Value.(*mempool.Item).Value.Hash())) cache.Remove(string(tail.Value.(*mempool.Item).Value.Hash()))
} else { } else {
......
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package score
import (
"runtime"
log "github.com/33cn/chain33/common/log/log15"
)
var (
mlog = log.New("module", "mempool")
poolCacheSize int64 = 10240 // mempool容量
mempoolExpiredInterval int64 = 600 // mempool内交易过期时间,10分钟
mempoolReSendInterval int64 = 60 // mempool内交易重发时间,1分钟
mempoolDupResendInterval int64 = 120 // mempool重复交易可再次发送间隔,120秒
mempoolAddedTxSize = 102400 // 已添加过的交易缓存大小
maxTxNumPerAccount int64 = 100 // TODO 每个账户在mempool中最大交易数量,10
processNum int
)
// TODO
func init() {
processNum = runtime.NumCPU()
}
package score package score
import ( import (
clog "github.com/33cn/chain33/common/log"
log "github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/queue" "github.com/33cn/chain33/queue"
drivers "github.com/33cn/chain33/system/mempool" drivers "github.com/33cn/chain33/system/mempool"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
) )
func SetLogLevel(level string) {
clog.SetLogLevel(level)
}
func DisableLog() {
mlog.SetHandler(log.DiscardHandler())
}
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// Module Mempool // Module Mempool
type Mempool struct {
subConfig subConfig
}
type subConfig struct { type subConfig struct {
PoolCacheSize int64 `json:"poolCacheSize"` PoolCacheSize int64 `json:"poolCacheSize"`
MinTxFee int64 `json:"minTxFee"` MinTxFee int64 `json:"minTxFee"`
...@@ -36,7 +22,7 @@ func init() { ...@@ -36,7 +22,7 @@ func init() {
drivers.Reg("trade", New) drivers.Reg("trade", New)
} }
//New 创建timeline cache 结构的 mempool //New 创建score cache 结构的 mempool
func New(cfg *types.Mempool, sub []byte) queue.Module { func New(cfg *types.Mempool, sub []byte) queue.Module {
c := drivers.NewMempool(cfg) c := drivers.NewMempool(cfg)
var subcfg subConfig var subcfg subConfig
......
...@@ -8,11 +8,13 @@ import ( ...@@ -8,11 +8,13 @@ import (
const maxLevel = 32 const maxLevel = 32
const prob = 0.35 const prob = 0.35
// SkipValue 跳跃表节点
type SkipValue struct { type SkipValue struct {
Score int64 Score int64
Value interface{} Value interface{}
} }
// Compare 比较函数
func (v *SkipValue) Compare(value *SkipValue) int { func (v *SkipValue) Compare(value *SkipValue) int {
f1 := v.Score f1 := v.Score
f2 := value.Score f2 := value.Score
...@@ -42,6 +44,7 @@ type SkipListIterator struct { ...@@ -42,6 +44,7 @@ type SkipListIterator struct {
node *skipListNode node *skipListNode
} }
// First 获取第一个节点
func (sli *SkipListIterator) First() *SkipValue { func (sli *SkipListIterator) First() *SkipValue {
if sli.list.header.next[0] == nil { if sli.list.header.next[0] == nil {
return nil return nil
...@@ -50,6 +53,7 @@ func (sli *SkipListIterator) First() *SkipValue { ...@@ -50,6 +53,7 @@ func (sli *SkipListIterator) First() *SkipValue {
return sli.node.Value return sli.node.Value
} }
// Last 获取最后一个节点
func (sli *SkipListIterator) Last() *SkipValue { func (sli *SkipListIterator) Last() *SkipValue {
if sli.list.tail == nil { if sli.list.tail == nil {
return nil return nil
...@@ -58,13 +62,7 @@ func (sli *SkipListIterator) Last() *SkipValue { ...@@ -58,13 +62,7 @@ func (sli *SkipListIterator) Last() *SkipValue {
return sli.node.Value return sli.node.Value
} }
func (sli *SkipListIterator) Current() *SkipValue { // Prev 获取上一个节点
if sli.node == nil {
return nil
}
return sli.node.Value
}
func (node *skipListNode) Prev() *skipListNode { func (node *skipListNode) Prev() *skipListNode {
if node == nil || node.prev == nil { if node == nil || node.prev == nil {
return nil return nil
...@@ -72,6 +70,7 @@ func (node *skipListNode) Prev() *skipListNode { ...@@ -72,6 +70,7 @@ func (node *skipListNode) Prev() *skipListNode {
return node.prev return node.prev
} }
// Next 获取下一个节点
func (node *skipListNode) Next() *skipListNode { func (node *skipListNode) Next() *skipListNode {
if node == nil || node.next[0] == nil { if node == nil || node.next[0] == nil {
return nil return nil
...@@ -79,15 +78,6 @@ func (node *skipListNode) Next() *skipListNode { ...@@ -79,15 +78,6 @@ func (node *skipListNode) Next() *skipListNode {
return node.next[0] return node.next[0]
} }
func (sli *SkipListIterator) Seek(value *SkipValue) *SkipValue {
x := sli.list.find(value)
if x.next[0] == nil {
return nil
}
sli.node = x.next[0]
return sli.node.Value
}
func newskipListNode(level int, value *SkipValue) *skipListNode { func newskipListNode(level int, value *SkipValue) *skipListNode {
node := &skipListNode{} node := &skipListNode{}
node.next = make([]*skipListNode, level) node.next = make([]*skipListNode, level)
...@@ -95,7 +85,7 @@ func newskipListNode(level int, value *SkipValue) *skipListNode { ...@@ -95,7 +85,7 @@ func newskipListNode(level int, value *SkipValue) *skipListNode {
return node return node
} }
//构建一个value的最小值 //NewSkipList 构建一个value的最小值
func NewSkipList(min *SkipValue) *SkipList { func NewSkipList(min *SkipValue) *SkipList {
sl := &SkipList{} sl := &SkipList{}
sl.level = 1 sl.level = 1
...@@ -107,7 +97,7 @@ func randomLevel() int { ...@@ -107,7 +97,7 @@ func randomLevel() int {
level := 1 level := 1
t := prob * 0xFFFF t := prob * 0xFFFF
for rand.Int()&0xFFFF < int(t) { for rand.Int()&0xFFFF < int(t) {
level += 1 level++
if level == maxLevel { if level == maxLevel {
break break
} }
...@@ -115,6 +105,7 @@ func randomLevel() int { ...@@ -115,6 +105,7 @@ func randomLevel() int {
return level return level
} }
// GetIterator 返回第一个节点
func (sl *SkipList) GetIterator() *SkipListIterator { func (sl *SkipList) GetIterator() *SkipListIterator {
it := &SkipListIterator{} it := &SkipListIterator{}
it.list = sl it.list = sl
...@@ -122,14 +113,11 @@ func (sl *SkipList) GetIterator() *SkipListIterator { ...@@ -122,14 +113,11 @@ func (sl *SkipList) GetIterator() *SkipListIterator {
return it return it
} }
// Len 返回节点数
func (sl *SkipList) Len() int { func (sl *SkipList) Len() int {
return sl.count return sl.count
} }
func (sl *SkipList) Level() int {
return sl.level
}
func (sl *SkipList) find(value *SkipValue) *skipListNode { func (sl *SkipList) find(value *SkipValue) *skipListNode {
x := sl.header x := sl.header
for i := sl.level - 1; i >= 0; i-- { for i := sl.level - 1; i >= 0; i-- {
...@@ -141,10 +129,12 @@ func (sl *SkipList) find(value *SkipValue) *skipListNode { ...@@ -141,10 +129,12 @@ func (sl *SkipList) find(value *SkipValue) *skipListNode {
return x return x
} }
// FindCount 返回查询次数
func (sl *SkipList) FindCount() int { func (sl *SkipList) FindCount() int {
return sl.findcount return sl.findcount
} }
// Find 查找skipvalue
func (sl *SkipList) Find(value *SkipValue) *SkipValue { func (sl *SkipList) Find(value *SkipValue) *SkipValue {
x := sl.find(value) x := sl.find(value)
if x.next[0] != nil && x.next[0].Value.Compare(value) == 0 { if x.next[0] != nil && x.next[0].Value.Compare(value) == 0 {
...@@ -153,14 +143,7 @@ func (sl *SkipList) Find(value *SkipValue) *SkipValue { ...@@ -153,14 +143,7 @@ func (sl *SkipList) Find(value *SkipValue) *SkipValue {
return nil return nil
} }
func (sl *SkipList) FindGreaterOrEqual(value *SkipValue) *SkipValue { // Insert 插入节点
x := sl.find(value)
if x.next[0] != nil {
return x.next[0].Value
}
return nil
}
func (sl *SkipList) Insert(value *SkipValue) int { func (sl *SkipList) Insert(value *SkipValue) int {
var update [maxLevel]*skipListNode var update [maxLevel]*skipListNode
x := sl.header x := sl.header
...@@ -199,6 +182,7 @@ func (sl *SkipList) Insert(value *SkipValue) int { ...@@ -199,6 +182,7 @@ func (sl *SkipList) Insert(value *SkipValue) int {
return 1 return 1
} }
// Delete 删除节点
func (sl *SkipList) Delete(value *SkipValue) int { func (sl *SkipList) Delete(value *SkipValue) int {
var update [maxLevel]*skipListNode var update [maxLevel]*skipListNode
x := sl.header x := sl.header
...@@ -229,7 +213,7 @@ func (sl *SkipList) Delete(value *SkipValue) int { ...@@ -229,7 +213,7 @@ func (sl *SkipList) Delete(value *SkipValue) int {
return 1 return 1
} }
//测试用的输出函数 // Print 测试用的输出函数
func (l *SkipList) Print() { func (l *SkipList) Print() {
if l.count > 0 { if l.count > 0 {
r := l.header r := l.header
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment