Commit e136c0de authored by vipwzw's avatar vipwzw

merge from master

parents a9213c23 187ee1d3
...@@ -73,7 +73,7 @@ poolCacheSize=10240 ...@@ -73,7 +73,7 @@ poolCacheSize=10240
[mempool.sub.score] [mempool.sub.score]
poolCacheSize=10240 poolCacheSize=10240
timeParam=1 #时间占价格比例 timeParam=1 #时间占价格比例
priceConstant=1544 #手续费相对于时间的一个合适的常量,取当前unxi时间戳前四位数,排序时手续费高1e-5~=快1s priceConstant=10 #手续费相对于时间的一个的常量,排队时手续费高1e3的分数~=快1h的分数
pricePower=1 #常量比例 pricePower=1 #常量比例
[mempool.sub.price] [mempool.sub.price]
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
//主要包含两方面功能 //主要包含两方面功能
// 1. token 的创建 // 1. token 的创建
// 1. token 的转账 // 1. token 的转账
// 1. token 燃烧和增发
// //
//token的创建 //token的创建
// 1. prepare create // 1. prepare create
......
...@@ -113,7 +113,7 @@ func (t *token) ExecLocal_TokenFinishCreate(payload *tokenty.TokenFinishCreate, ...@@ -113,7 +113,7 @@ func (t *token) ExecLocal_TokenFinishCreate(payload *tokenty.TokenFinishCreate,
table := NewLogsTable(t.GetLocalDB()) table := NewLogsTable(t.GetLocalDB())
txIndex := dapp.HeightIndexStr(t.GetHeight(), int64(index)) txIndex := dapp.HeightIndexStr(t.GetHeight(), int64(index))
err = table.Add(&tokenty.LocalLogs{Symbol: payload.Symbol, TxIndex: txIndex, ActionType: tokenty.TokenActionFinishCreate, TxHash: hex.EncodeToString(tx.Hash())}) err = table.Add(&tokenty.LocalLogs{Symbol: payload.Symbol, TxIndex: txIndex, ActionType: tokenty.TokenActionFinishCreate, TxHash: "0x" + hex.EncodeToString(tx.Hash())})
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
package price package price
import ( import (
"bytes"
"encoding/gob"
"github.com/33cn/chain33/common/skiplist" "github.com/33cn/chain33/common/skiplist"
"github.com/33cn/chain33/system/mempool" "github.com/33cn/chain33/system/mempool"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
...@@ -28,14 +25,8 @@ func NewQueue(subcfg subConfig) *Queue { ...@@ -28,14 +25,8 @@ func NewQueue(subcfg subConfig) *Queue {
} }
func (cache *Queue) newSkipValue(item *mempool.Item) (*skiplist.SkipValue, error) { func (cache *Queue) newSkipValue(item *mempool.Item) (*skiplist.SkipValue, error) {
//tx := item.value buf := types.Encode(item.Value)
buf := bytes.NewBuffer(nil) size := len(buf)
enc := gob.NewEncoder(buf)
err := enc.Encode(item.Value)
if err != nil {
return nil, err
}
size := len(buf.Bytes())
return &skiplist.SkipValue{Score: item.Value.Fee / int64(size), Value: item}, nil return &skiplist.SkipValue{Score: item.Value.Fee / int64(size), Value: item}, nil
} }
...@@ -130,3 +121,23 @@ func (cache *Queue) Walk(count int, cb func(value *mempool.Item) bool) { ...@@ -130,3 +121,23 @@ func (cache *Queue) Walk(count int, cb func(value *mempool.Item) bool) {
return i != count return i != count
}) })
} }
// GetProperFee 获取合适的手续费,取前100的平均价格
func (cache *Queue) GetProperFee() int64 {
var sumFee int64
var properFee int64
if cache.Size() == 0 {
return cache.subConfig.ProperFee
}
i := 0
cache.txList.Walk(func(tx interface{}) bool {
if i == 100 {
return false
}
sumFee += tx.(*mempool.Item).Value.Fee
i++
return true
})
properFee = sumFee / int64(i)
return properFee
}
...@@ -113,9 +113,9 @@ func TestTimeCompetition(t *testing.T) { ...@@ -113,9 +113,9 @@ func TestTimeCompetition(t *testing.T) {
func TestPriceCompetition(t *testing.T) { func TestPriceCompetition(t *testing.T) {
cache := initEnv(1) cache := initEnv(1)
cache.Push(item1) cache.Push(item3)
cache.Push(item4) cache.Push(item4)
assert.Equal(t, false, cache.Exist(string(item1.Value.Hash()))) assert.Equal(t, false, cache.Exist(string(item3.Value.Hash())))
assert.Equal(t, true, cache.Exist(string(item4.Value.Hash()))) assert.Equal(t, true, cache.Exist(string(item4.Value.Hash())))
} }
...@@ -149,3 +149,13 @@ func TestQueueDirection(t *testing.T) { ...@@ -149,3 +149,13 @@ func TestQueueDirection(t *testing.T) {
assert.Equal(t, 5, i) assert.Equal(t, 5, i)
assert.Equal(t, true, lastScore == cache.txList.GetIterator().Last().Score) assert.Equal(t, true, lastScore == cache.txList.GetIterator().Last().Score)
} }
func TestGetProperFee(t *testing.T) {
cache := initEnv(0)
assert.Equal(t, cache.subConfig.ProperFee, cache.GetProperFee())
cache.Push(item1)
cache.Push(item4)
cache.GetProperFee()
assert.Equal(t, (item1.Value.Fee+item4.Value.Fee)/2, cache.GetProperFee())
}
...@@ -75,7 +75,7 @@ poolCacheSize=10240 ...@@ -75,7 +75,7 @@ poolCacheSize=10240
[mempool.sub.score] [mempool.sub.score]
poolCacheSize=10240 poolCacheSize=10240
timeParam=1 #时间占价格比例 timeParam=1 #时间占价格比例
priceConstant=1544 #手续费相对于时间的一个合适的常量,取当前unxi时间戳前四位数,排序时手续费高1e-5~=快1s priceConstant=3 #手续费相对于时间的一个的常量,排队时手续费高1e3的分数~=快1h的分数
pricePower=1 #常量比例 pricePower=1 #常量比例
[mempool.sub.price] [mempool.sub.price]
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
type subConfig struct { type subConfig struct {
PoolCacheSize int64 `json:"poolCacheSize"` PoolCacheSize int64 `json:"poolCacheSize"`
ProperFee int64 `json:"properFee"`
} }
func init() { func init() {
...@@ -25,6 +26,9 @@ func New(cfg *types.Mempool, sub []byte) queue.Module { ...@@ -25,6 +26,9 @@ func New(cfg *types.Mempool, sub []byte) queue.Module {
if subcfg.PoolCacheSize == 0 { if subcfg.PoolCacheSize == 0 {
subcfg.PoolCacheSize = cfg.PoolCacheSize subcfg.PoolCacheSize = cfg.PoolCacheSize
} }
if subcfg.ProperFee == 0 {
subcfg.ProperFee = cfg.MinTxFee
}
c.SetQueueCache(NewQueue(subcfg)) c.SetQueueCache(NewQueue(subcfg))
return c return c
} }
package score package score
import ( import (
"bytes" "time"
"encoding/gob"
"github.com/33cn/chain33/common/skiplist" "github.com/33cn/chain33/common/skiplist"
"github.com/33cn/chain33/system/mempool" "github.com/33cn/chain33/system/mempool"
...@@ -11,7 +10,7 @@ import ( ...@@ -11,7 +10,7 @@ import (
var mempoolDupResendInterval int64 = 600 // mempool内交易过期时间,10分钟 var mempoolDupResendInterval int64 = 600 // mempool内交易过期时间,10分钟
// Queue 分数队列模式(分数=常量a*手续费/交易字节数-常量b*时间*定量c,按分数排队,高的优先,常量a,b和定量c可配置) // Queue 分数队列模式(分数=定量a*常量b*手续费/交易字节数-常量c*时间,按分数排队,高的优先,定量a和常量b,c可配置)
type Queue struct { type Queue struct {
txMap map[string]*skiplist.SkipValue txMap map[string]*skiplist.SkipValue
txList *skiplist.SkipList txList *skiplist.SkipList
...@@ -28,15 +27,10 @@ func NewQueue(subcfg subConfig) *Queue { ...@@ -28,15 +27,10 @@ func NewQueue(subcfg subConfig) *Queue {
} }
func (cache *Queue) newSkipValue(item *mempool.Item) (*skiplist.SkipValue, error) { func (cache *Queue) newSkipValue(item *mempool.Item) (*skiplist.SkipValue, error) {
//tx := item.value buf := types.Encode(item.Value)
buf := bytes.NewBuffer(nil) size := len(buf)
enc := gob.NewEncoder(buf) return &skiplist.SkipValue{Score: cache.subConfig.PriceConstant*(item.Value.Fee/int64(size))*
err := enc.Encode(item.Value) cache.subConfig.PricePower - cache.subConfig.TimeParam*item.EnterTime, Value: item}, nil
if err != nil {
return nil, err
}
size := len(buf.Bytes())
return &skiplist.SkipValue{Score: cache.subConfig.PriceConstant*(item.Value.Fee/int64(size))*cache.subConfig.PricePower - cache.subConfig.TimeParam*item.EnterTime, Value: item}, nil
} }
// Exist 是否存在 // Exist 是否存在
...@@ -122,3 +116,25 @@ func (cache *Queue) Walk(count int, cb func(value *mempool.Item) bool) { ...@@ -122,3 +116,25 @@ func (cache *Queue) Walk(count int, cb func(value *mempool.Item) bool) {
return i != count return i != count
}) })
} }
// GetProperFee 获取合适的手续费
func (cache *Queue) GetProperFee() int64 {
var sumScore int64
var properFee int64
if cache.Size() == 0 {
return cache.subConfig.ProperFee
}
i := 0
cache.txList.WalkS(func(node interface{}) bool {
if i == 100 {
return false
}
sumScore += node.(*skiplist.SkipValue).Score
i++
return true
})
//这里的int64(250)是一般交易的大小
properFee = (sumScore/int64(i) + cache.subConfig.TimeParam*time.Now().Unix()) * int64(250) /
(cache.subConfig.PriceConstant * cache.subConfig.PricePower)
return properFee
}
...@@ -2,6 +2,7 @@ package score ...@@ -2,6 +2,7 @@ package score
import ( import (
"testing" "testing"
"time"
"github.com/33cn/chain33/common" "github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/address" "github.com/33cn/chain33/common/address"
...@@ -21,11 +22,11 @@ var ( ...@@ -21,11 +22,11 @@ var (
amount = int64(1e8) amount = int64(1e8)
v = &cty.CoinsAction_Transfer{Transfer: &types.AssetsTransfer{Amount: amount}} v = &cty.CoinsAction_Transfer{Transfer: &types.AssetsTransfer{Amount: amount}}
transfer = &cty.CoinsAction{Value: v, Ty: cty.CoinsActionTransfer} transfer = &cty.CoinsAction{Value: v, Ty: cty.CoinsActionTransfer}
tx1 = &types.Transaction{Execer: []byte("coins"), Payload: types.Encode(transfer), Fee: 1000000, Expire: 1, To: toAddr} tx1 = &types.Transaction{Execer: []byte("coins"), Payload: types.Encode(transfer), Fee: 100000, Expire: 1, To: toAddr}
tx2 = &types.Transaction{Execer: []byte("coins"), Payload: types.Encode(transfer), Fee: 1000000, Expire: 2, To: toAddr} tx2 = &types.Transaction{Execer: []byte("coins"), Payload: types.Encode(transfer), Fee: 100000, Expire: 2, To: toAddr}
tx3 = &types.Transaction{Execer: []byte("coins"), Payload: types.Encode(transfer), Fee: 1000000, Expire: 3, To: toAddr} tx3 = &types.Transaction{Execer: []byte("coins"), Payload: types.Encode(transfer), Fee: 100000, Expire: 3, To: toAddr}
tx4 = &types.Transaction{Execer: []byte("coins"), Payload: types.Encode(transfer), Fee: 2000000, Expire: 4, To: toAddr} tx4 = &types.Transaction{Execer: []byte("coins"), Payload: types.Encode(transfer), Fee: 200000, Expire: 4, To: toAddr}
tx5 = &types.Transaction{Execer: []byte("coins"), Payload: types.Encode(transfer), Fee: 1000000, Expire: 5, To: toAddr} tx5 = &types.Transaction{Execer: []byte("coins"), Payload: types.Encode(transfer), Fee: 100000, Expire: 5, To: toAddr}
item1 = &drivers.Item{Value: tx1, Priority: tx1.Fee, EnterTime: types.Now().Unix()} item1 = &drivers.Item{Value: tx1, Priority: tx1.Fee, EnterTime: types.Now().Unix()}
item2 = &drivers.Item{Value: tx2, Priority: tx2.Fee, EnterTime: types.Now().Unix()} item2 = &drivers.Item{Value: tx2, Priority: tx2.Fee, EnterTime: types.Now().Unix()}
item3 = &drivers.Item{Value: tx3, Priority: tx3.Fee, EnterTime: types.Now().Unix() - 1000} item3 = &drivers.Item{Value: tx3, Priority: tx3.Fee, EnterTime: types.Now().Unix() - 1000}
...@@ -113,9 +114,9 @@ func TestTimeCompetition(t *testing.T) { ...@@ -113,9 +114,9 @@ func TestTimeCompetition(t *testing.T) {
func TestPriceCompetition(t *testing.T) { func TestPriceCompetition(t *testing.T) {
cache := initEnv(1) cache := initEnv(1)
cache.Push(item1) cache.Push(item3)
cache.Push(item4) cache.Push(item4)
assert.Equal(t, false, cache.Exist(string(item1.Value.Hash()))) assert.Equal(t, false, cache.Exist(string(item3.Value.Hash())))
assert.Equal(t, true, cache.Exist(string(item4.Value.Hash()))) assert.Equal(t, true, cache.Exist(string(item4.Value.Hash())))
} }
...@@ -149,3 +150,23 @@ func TestQueueDirection(t *testing.T) { ...@@ -149,3 +150,23 @@ func TestQueueDirection(t *testing.T) {
assert.Equal(t, 5, i) assert.Equal(t, 5, i)
assert.Equal(t, true, lastScore == cache.txList.GetIterator().Last().Score) assert.Equal(t, true, lastScore == cache.txList.GetIterator().Last().Score)
} }
func TestGetProperFee(t *testing.T) {
cache := initEnv(0)
assert.Equal(t, cache.subConfig.ProperFee, cache.GetProperFee())
cache.Push(item3)
cache.Push(item4)
cache.GetProperFee()
buf3 := types.Encode(item3.Value)
size3 := len(buf3)
buf4 := types.Encode(item4.Value)
size4 := len(buf4)
score3 := item3.Value.Fee*cache.subConfig.PriceConstant*cache.subConfig.PricePower/int64(size3) -
item3.EnterTime*cache.subConfig.TimeParam
score4 := item4.Value.Fee*cache.subConfig.PriceConstant*cache.subConfig.PricePower/int64(size4) -
item4.EnterTime*cache.subConfig.TimeParam
properFee := ((score3+score4)/2 + time.Now().Unix()*cache.subConfig.TimeParam) * int64(250) /
(cache.subConfig.PriceConstant * cache.subConfig.PricePower)
assert.Equal(t, int64(1), properFee/cache.GetProperFee())
}
...@@ -75,7 +75,7 @@ poolCacheSize=10240 ...@@ -75,7 +75,7 @@ poolCacheSize=10240
[mempool.sub.score] [mempool.sub.score]
poolCacheSize=10240 poolCacheSize=10240
timeParam=1 #时间占价格比例 timeParam=1 #时间占价格比例
priceConstant=1544 #手续费相对于时间的一个合适的常量,取当前unxi时间戳前四位数,排队时手续费高1e-5的分数~=快1s的分数 priceConstant=10 #手续费相对于时间的一个的常量,排队时手续费高1e3的分数~=快1h的分数
pricePower=1 #常量比例 pricePower=1 #常量比例
[mempool.sub.price] [mempool.sub.price]
......
...@@ -14,6 +14,7 @@ type subConfig struct { ...@@ -14,6 +14,7 @@ type subConfig struct {
TimeParam int64 `json:"timeParam"` TimeParam int64 `json:"timeParam"`
PriceConstant int64 `json:"priceConstant"` PriceConstant int64 `json:"priceConstant"`
PricePower int64 `json:"pricePower"` PricePower int64 `json:"pricePower"`
ProperFee int64 `json:"properFee"`
} }
func init() { func init() {
...@@ -28,6 +29,9 @@ func New(cfg *types.Mempool, sub []byte) queue.Module { ...@@ -28,6 +29,9 @@ func New(cfg *types.Mempool, sub []byte) queue.Module {
if subcfg.PoolCacheSize == 0 { if subcfg.PoolCacheSize == 0 {
subcfg.PoolCacheSize = cfg.PoolCacheSize subcfg.PoolCacheSize = cfg.PoolCacheSize
} }
if subcfg.ProperFee == 0 {
subcfg.ProperFee = cfg.MinTxFee
}
c.SetQueueCache(NewQueue(subcfg)) c.SetQueueCache(NewQueue(subcfg))
return c return c
} }
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