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

fix linter

parent 11da7ff1
......@@ -5,5 +5,6 @@
package init
import (
_ "github.com/33cn/plugin/plugin/mempool/price"
_ "github.com/33cn/plugin/plugin/mempool/score"
)
......@@ -8,14 +8,16 @@ import (
"github.com/33cn/chain33/types"
)
//PriceQueue 简单队列模式(默认提供一个队列,便于测试)
var mempoolDupResendInterval int64 = 600 // mempool内交易过期时间,10分钟
// PriceQueue 价格队列模式(价格=手续费/交易字节数,价格高者优先,同价则时间早优先)
type PriceQueue struct {
txMap map[string]*SkipValue
txList *SkipList
subConfig subConfig
}
//NewPriceQueue 创建队列
// NewPriceQueue 创建队列
func NewPriceQueue(subcfg subConfig) *PriceQueue {
return &PriceQueue{
txMap: make(map[string]*SkipValue, subcfg.PoolCacheSize),
......@@ -44,7 +46,7 @@ func (cache *PriceQueue) Exist(hash string) bool {
//GetItem 获取数据通过 key
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 nil, types.ErrNotFound
......@@ -57,10 +59,9 @@ func (cache *PriceQueue) Push(item *mempool.Item) error {
s := cache.txMap[string(hash)]
addedItem := s.Value.(*mempool.Item)
addedTime := addedItem.EnterTime
if types.Now().Unix()-addedTime < mempoolDupResendInterval {
return types.ErrTxExist
} else {
}
// 超过2分钟之后的重发交易返回nil,再次发送给P2P,但是不再次加入mempool
// 并修改其enterTime,以避免该交易一直在节点间被重发
newEnterTime := types.Now().Unix()
......@@ -76,7 +77,6 @@ func (cache *PriceQueue) Push(item *mempool.Item) error {
// ------------------
return nil
}
}
it := &mempool.Item{Value: item.Value, Priority: item.Value.Fee, EnterTime: item.EnterTime}
sv, err := cache.newSkipValue(it)
......@@ -85,7 +85,7 @@ func (cache *PriceQueue) Push(item *mempool.Item) error {
}
if int64(cache.txList.Len()) >= cache.subConfig.PoolCacheSize {
tail := cache.txList.GetIterator().Last()
//价格高
//价格高存留
if sv.Compare(tail) == -1 {
cache.Remove(string(tail.Value.(*mempool.Item).Value.Hash()))
} 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
import (
clog "github.com/33cn/chain33/common/log"
log "github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/queue"
drivers "github.com/33cn/chain33/system/mempool"
"github.com/33cn/chain33/types"
)
func SetLogLevel(level string) {
clog.SetLogLevel(level)
}
func DisableLog() {
mlog.SetHandler(log.DiscardHandler())
}
//--------------------------------------------------------------------------------
// Module Mempool
type Mempool struct {
subConfig subConfig
}
type subConfig struct {
PoolCacheSize int64 `json:"poolCacheSize"`
MinTxFee int64 `json:"minTxFee"`
......@@ -33,7 +19,7 @@ func init() {
drivers.Reg("price", New)
}
//New 创建timeline cache 结构的 mempool
//New 创建price cache 结构的 mempool
func New(cfg *types.Mempool, sub []byte) queue.Module {
c := drivers.NewMempool(cfg)
var subcfg subConfig
......
......@@ -10,11 +10,13 @@ import (
const maxLevel = 32
const prob = 0.35
// SkipValue 跳跃表节点
type SkipValue struct {
Price int64
Value interface{}
}
// Compare 比较函数
func (v *SkipValue) Compare(value *SkipValue) int {
if v.Price > value.Price {
return -1
......@@ -45,6 +47,7 @@ type SkipListIterator struct {
node *skipListNode
}
// First 获取第一个节点
func (sli *SkipListIterator) First() *SkipValue {
if sli.list.header.next[0] == nil {
return nil
......@@ -53,6 +56,7 @@ func (sli *SkipListIterator) First() *SkipValue {
return sli.node.Value
}
// Last 获取最后一个节点
func (sli *SkipListIterator) Last() *SkipValue {
if sli.list.tail == nil {
return nil
......@@ -61,13 +65,7 @@ func (sli *SkipListIterator) Last() *SkipValue {
return sli.node.Value
}
func (sli *SkipListIterator) Current() *SkipValue {
if sli.node == nil {
return nil
}
return sli.node.Value
}
// Prev 获取上一个节点
func (node *skipListNode) Prev() *skipListNode {
if node == nil || node.prev == nil {
return nil
......@@ -75,6 +73,7 @@ func (node *skipListNode) Prev() *skipListNode {
return node.prev
}
// Next 获取下一个节点
func (node *skipListNode) Next() *skipListNode {
if node == nil || node.next[0] == nil {
return nil
......@@ -82,15 +81,6 @@ func (node *skipListNode) Next() *skipListNode {
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 {
node := &skipListNode{}
node.next = make([]*skipListNode, level)
......@@ -98,7 +88,7 @@ func newskipListNode(level int, value *SkipValue) *skipListNode {
return node
}
//构建一个value的最小值
//NewSkipList 构建一个value的最小值
func NewSkipList(min *SkipValue) *SkipList {
sl := &SkipList{}
sl.level = 1
......@@ -110,7 +100,7 @@ func randomLevel() int {
level := 1
t := prob * 0xFFFF
for rand.Int()&0xFFFF < int(t) {
level += 1
level++
if level == maxLevel {
break
}
......@@ -118,6 +108,7 @@ func randomLevel() int {
return level
}
// GetIterator 返回第一个节点
func (sl *SkipList) GetIterator() *SkipListIterator {
it := &SkipListIterator{}
it.list = sl
......@@ -125,14 +116,11 @@ func (sl *SkipList) GetIterator() *SkipListIterator {
return it
}
// Len 返回节点数
func (sl *SkipList) Len() int {
return sl.count
}
func (sl *SkipList) Level() int {
return sl.level
}
func (sl *SkipList) find(value *SkipValue) *skipListNode {
x := sl.header
for i := sl.level - 1; i >= 0; i-- {
......@@ -144,10 +132,12 @@ func (sl *SkipList) find(value *SkipValue) *skipListNode {
return x
}
// FindCount 返回查询次数
func (sl *SkipList) FindCount() int {
return sl.findcount
}
// Find 查找skipvalue
func (sl *SkipList) Find(value *SkipValue) *SkipValue {
x := sl.find(value)
if x.next[0] != nil && x.next[0].Value.Compare(value) == 0 {
......@@ -156,14 +146,7 @@ func (sl *SkipList) Find(value *SkipValue) *SkipValue {
return nil
}
func (sl *SkipList) FindGreaterOrEqual(value *SkipValue) *SkipValue {
x := sl.find(value)
if x.next[0] != nil {
return x.next[0].Value
}
return nil
}
// Insert 插入节点
func (sl *SkipList) Insert(value *SkipValue) int {
var update [maxLevel]*skipListNode
x := sl.header
......@@ -202,6 +185,7 @@ func (sl *SkipList) Insert(value *SkipValue) int {
return 1
}
// Delete 删除节点
func (sl *SkipList) Delete(value *SkipValue) int {
var update [maxLevel]*skipListNode
x := sl.header
......@@ -232,7 +216,7 @@ func (sl *SkipList) Delete(value *SkipValue) int {
return 1
}
//测试用的输出函数
// Print 测试用的输出函数
func (l *SkipList) Print() {
if l.count > 0 {
r := l.header
......
......@@ -8,14 +8,16 @@ import (
"github.com/33cn/chain33/types"
)
//ScoreQueue 简单队列模式(默认提供一个队列,便于测试)
var mempoolDupResendInterval int64 = 600 // mempool内交易过期时间,10分钟
// ScoreQueue 分数队列模式(分数=常量a*手续费/交易字节数-常量b*时间*定量c,按分数排队,高的优先,常量a,b和定量c可配置)
type ScoreQueue struct {
txMap map[string]*SkipValue
txList *SkipList
subConfig subConfig
}
//NewScoreQueue 创建队列
// NewScoreQueue 创建队列
func NewScoreQueue(subcfg subConfig) *ScoreQueue {
return &ScoreQueue{
txMap: make(map[string]*SkipValue, subcfg.PoolCacheSize),
......@@ -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
}
//Exist 是否存在
// Exist 是否存在
func (cache *ScoreQueue) Exist(hash string) bool {
_, exists := cache.txMap[hash]
return exists
......@@ -44,7 +46,7 @@ func (cache *ScoreQueue) Exist(hash string) bool {
//GetItem 获取数据通过 key
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 nil, types.ErrNotFound
......@@ -60,7 +62,7 @@ func (cache *ScoreQueue) Push(item *mempool.Item) error {
if types.Now().Unix()-addedTime < mempoolDupResendInterval {
return types.ErrTxExist
} else {
}
// 超过2分钟之后的重发交易返回nil,再次发送给P2P,但是不再次加入mempool
// 并修改其enterTime,以避免该交易一直在节点间被重发
newEnterTime := types.Now().Unix()
......@@ -76,7 +78,6 @@ func (cache *ScoreQueue) Push(item *mempool.Item) error {
// ------------------
return nil
}
}
it := &mempool.Item{Value: item.Value, Priority: item.Value.Fee, EnterTime: item.EnterTime}
sv, err := cache.newSkipValue(it)
......@@ -85,7 +86,7 @@ func (cache *ScoreQueue) Push(item *mempool.Item) error {
}
if int64(cache.txList.Len()) >= cache.subConfig.PoolCacheSize {
tail := cache.txList.GetIterator().Last()
//价格高
//分数高存留
if sv.Compare(tail) == -1 {
cache.Remove(string(tail.Value.(*mempool.Item).Value.Hash()))
} 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
import (
clog "github.com/33cn/chain33/common/log"
log "github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/queue"
drivers "github.com/33cn/chain33/system/mempool"
"github.com/33cn/chain33/types"
)
func SetLogLevel(level string) {
clog.SetLogLevel(level)
}
func DisableLog() {
mlog.SetHandler(log.DiscardHandler())
}
//--------------------------------------------------------------------------------
// Module Mempool
type Mempool struct {
subConfig subConfig
}
type subConfig struct {
PoolCacheSize int64 `json:"poolCacheSize"`
MinTxFee int64 `json:"minTxFee"`
......@@ -36,7 +22,7 @@ func init() {
drivers.Reg("trade", New)
}
//New 创建timeline cache 结构的 mempool
//New 创建score cache 结构的 mempool
func New(cfg *types.Mempool, sub []byte) queue.Module {
c := drivers.NewMempool(cfg)
var subcfg subConfig
......
......@@ -8,11 +8,13 @@ import (
const maxLevel = 32
const prob = 0.35
// SkipValue 跳跃表节点
type SkipValue struct {
Score int64
Value interface{}
}
// Compare 比较函数
func (v *SkipValue) Compare(value *SkipValue) int {
f1 := v.Score
f2 := value.Score
......@@ -42,6 +44,7 @@ type SkipListIterator struct {
node *skipListNode
}
// First 获取第一个节点
func (sli *SkipListIterator) First() *SkipValue {
if sli.list.header.next[0] == nil {
return nil
......@@ -50,6 +53,7 @@ func (sli *SkipListIterator) First() *SkipValue {
return sli.node.Value
}
// Last 获取最后一个节点
func (sli *SkipListIterator) Last() *SkipValue {
if sli.list.tail == nil {
return nil
......@@ -58,13 +62,7 @@ func (sli *SkipListIterator) Last() *SkipValue {
return sli.node.Value
}
func (sli *SkipListIterator) Current() *SkipValue {
if sli.node == nil {
return nil
}
return sli.node.Value
}
// Prev 获取上一个节点
func (node *skipListNode) Prev() *skipListNode {
if node == nil || node.prev == nil {
return nil
......@@ -72,6 +70,7 @@ func (node *skipListNode) Prev() *skipListNode {
return node.prev
}
// Next 获取下一个节点
func (node *skipListNode) Next() *skipListNode {
if node == nil || node.next[0] == nil {
return nil
......@@ -79,15 +78,6 @@ func (node *skipListNode) Next() *skipListNode {
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 {
node := &skipListNode{}
node.next = make([]*skipListNode, level)
......@@ -95,7 +85,7 @@ func newskipListNode(level int, value *SkipValue) *skipListNode {
return node
}
//构建一个value的最小值
//NewSkipList 构建一个value的最小值
func NewSkipList(min *SkipValue) *SkipList {
sl := &SkipList{}
sl.level = 1
......@@ -107,7 +97,7 @@ func randomLevel() int {
level := 1
t := prob * 0xFFFF
for rand.Int()&0xFFFF < int(t) {
level += 1
level++
if level == maxLevel {
break
}
......@@ -115,6 +105,7 @@ func randomLevel() int {
return level
}
// GetIterator 返回第一个节点
func (sl *SkipList) GetIterator() *SkipListIterator {
it := &SkipListIterator{}
it.list = sl
......@@ -122,14 +113,11 @@ func (sl *SkipList) GetIterator() *SkipListIterator {
return it
}
// Len 返回节点数
func (sl *SkipList) Len() int {
return sl.count
}
func (sl *SkipList) Level() int {
return sl.level
}
func (sl *SkipList) find(value *SkipValue) *skipListNode {
x := sl.header
for i := sl.level - 1; i >= 0; i-- {
......@@ -141,10 +129,12 @@ func (sl *SkipList) find(value *SkipValue) *skipListNode {
return x
}
// FindCount 返回查询次数
func (sl *SkipList) FindCount() int {
return sl.findcount
}
// Find 查找skipvalue
func (sl *SkipList) Find(value *SkipValue) *SkipValue {
x := sl.find(value)
if x.next[0] != nil && x.next[0].Value.Compare(value) == 0 {
......@@ -153,14 +143,7 @@ func (sl *SkipList) Find(value *SkipValue) *SkipValue {
return nil
}
func (sl *SkipList) FindGreaterOrEqual(value *SkipValue) *SkipValue {
x := sl.find(value)
if x.next[0] != nil {
return x.next[0].Value
}
return nil
}
// Insert 插入节点
func (sl *SkipList) Insert(value *SkipValue) int {
var update [maxLevel]*skipListNode
x := sl.header
......@@ -199,6 +182,7 @@ func (sl *SkipList) Insert(value *SkipValue) int {
return 1
}
// Delete 删除节点
func (sl *SkipList) Delete(value *SkipValue) int {
var update [maxLevel]*skipListNode
x := sl.header
......@@ -229,7 +213,7 @@ func (sl *SkipList) Delete(value *SkipValue) int {
return 1
}
//测试用的输出函数
// Print 测试用的输出函数
func (l *SkipList) Print() {
if l.count > 0 {
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