Skip to content

Commit

Permalink
Add NumLowFeeTxnPerBlock config to control building block logic
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <zyl.skysniper@gmail.com>
  • Loading branch information
yilunzhang committed Aug 6, 2019
1 parent 98df1a4 commit d6ff39c
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 31 deletions.
27 changes: 18 additions & 9 deletions chain/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,17 @@ func (tc *TxnCollector) Cleanup(txns []*transaction.Transaction) error {
return tc.TxnSource.CleanSubmittedTransactions(txns)
}

type sortTxnsByPrice []*transaction.Transaction
type sortTxnsByPriceSize []*transaction.Transaction

func (s sortTxnsByPrice) Len() int { return len(s) }
func (s sortTxnsByPrice) Less(i, j int) bool { return s[i].UnsignedTx.Fee > s[j].UnsignedTx.Fee }
func (s sortTxnsByPrice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s sortTxnsByPriceSize) Len() int { return len(s) }
func (s sortTxnsByPriceSize) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

func (s sortTxnsByPriceSize) Less(i, j int) bool {
if s[i].UnsignedTx.Fee == s[j].UnsignedTx.Fee {
return s[i].GetSize() < s[j].GetSize()
}
return s[i].UnsignedTx.Fee > s[j].UnsignedTx.Fee
}

type TxnCollection struct {
txns map[Uint160][]*transaction.Transaction
Expand All @@ -75,7 +81,7 @@ func NewTxnCollection(txnLists map[Uint160][]*transaction.Transaction) *TxnColle
txnLists[addr] = txnList[1:]
}

sort.Sort(sort.Reverse(sortTxnsByPrice(tops)))
sort.Sort(sort.Reverse(sortTxnsByPriceSize(tops)))

return &TxnCollection{
txns: txnLists,
Expand All @@ -97,16 +103,19 @@ func (tc *TxnCollection) Update() error {
}
if txnList, ok := tc.txns[hashes[0]]; ok && len(txnList) > 0 {
tc.tops[0], tc.txns[hashes[0]] = txnList[0], txnList[1:]
sort.Sort(sort.Reverse(sortTxnsByPrice(tc.tops)))
sort.Sort(sort.Reverse(sortTxnsByPriceSize(tc.tops)))
} else {
tc.tops = tc.tops[1:]
}

return nil
}

func (tc *TxnCollection) Pop() {
if len(tc.tops) > 0 {
tc.tops = tc.tops[1:]
func (tc *TxnCollection) Pop() *transaction.Transaction {
if len(tc.tops) == 0 {
return nil
}
top := tc.tops[0]
tc.tops = tc.tops[1:]
return top
}
24 changes: 14 additions & 10 deletions chain/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (bm *BuiltinMining) BuildBlock(ctx context.Context, height uint32, chordID
txnHashList = append(txnHashList, coinbase.Hash())
totalTxsSize := coinbase.GetSize()
txCount := 1
lowFeeTxCount := 0

if winnerType == pb.TXN_SIGNER {
if _, err = DefaultLedger.Store.GetTransaction(winnerHash); err != nil {
Expand Down Expand Up @@ -75,27 +76,25 @@ func (bm *BuiltinMining) BuildBlock(ctx context.Context, height uint32, chordID
default:
}

if txCount >= int(config.Parameters.NumTxnPerBlock) {
break
}

txn := txnCollection.Peek()
if txn == nil {
break
}

if txn.UnsignedTx.Fee < int64(config.Parameters.MinTxnFee) {
if txn.UnsignedTx.Fee < int64(config.Parameters.MinTxnFee) && uint32(lowFeeTxCount) >= config.Parameters.NumLowFeeTxnPerBlock {
log.Warning("transaction fee is too low")
txnCollection.Pop()
continue
break
}

totalTxsSize = totalTxsSize + txn.GetSize()
if totalTxsSize > config.MaxBlockSize {
break
}

txCount++
if txCount > int(config.Parameters.NumTxnPerBlock) {
break
}

if err := VerifyTransaction(txn); err != nil {
log.Warningf("invalid transaction: %v", err)
txnCollection.Pop()
Expand All @@ -118,8 +117,13 @@ func (bm *BuiltinMining) BuildBlock(ctx context.Context, height uint32, chordID
if err = txnCollection.Update(); err != nil {
bvs.Reset()
txnCollection.Pop()
} else {
bvs.Commit()
continue
}

bvs.Commit()
txCount++
if txn.UnsignedTx.Fee < int64(config.Parameters.MinTxnFee) {
lowFeeTxCount++
}
}

Expand Down
2 changes: 1 addition & 1 deletion consensus/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package moca
package consensus

import (
"time"
Expand Down
2 changes: 1 addition & 1 deletion consensus/consensus.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package moca
package consensus

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion consensus/message.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package moca
package consensus

import (
"crypto/sha256"
Expand Down
2 changes: 1 addition & 1 deletion consensus/proposal.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package moca
package consensus

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion consensus/proposing.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package moca
package consensus

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion consensus/state.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package moca
package consensus

import (
"sync"
Expand Down
2 changes: 1 addition & 1 deletion consensus/util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package moca
package consensus

import (
"encoding/binary"
Expand Down
2 changes: 1 addition & 1 deletion consensus/vote.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package moca
package consensus

import (
"fmt"
Expand Down
4 changes: 2 additions & 2 deletions nknd.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
)

const (
NetVersionNum = 4 // This is temporary and will be removed soon after mainnet is stabilized
NetVersionNum = 5 // This is temporary and will be removed soon after mainnet is stabilized
)

var (
Expand Down Expand Up @@ -260,7 +260,7 @@ func nknMain(c *cli.Context) error {

go ws.Start()

consensus, err := moca.NewConsensus(account, localNode)
consensus, err := consensus.NewConsensus(account, localNode)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pb/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ func (m *GenerateID) ToMap() map[string]interface{} {

func (m *RegisterName) ToMap() map[string]interface{} {
return map[string]interface{}{
"registrant": common.BytesToUint160(m.Registrant),
"registrant": common.HexStr(m.Registrant),
"name": m.Name,
}
}

func (m *Subscribe) ToMap() map[string]interface{} {
return map[string]interface{}{
"subscriber": common.BytesToUint160(m.Subscriber),
"subscriber": common.HexStr(m.Subscriber),
"identifier": m.Identifier,
"topic": m.Topic,
"bucket": m.Bucket,
Expand Down
3 changes: 3 additions & 0 deletions util/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ var (
WalletFile: "wallet.json",
MaxGetIDSeeds: 3,
DBFilesCacheCapacity: 100,
NumLowFeeTxnPerBlock: 4,
MinTxnFee: 10000000,
}
)

Expand All @@ -150,6 +152,7 @@ type Configuration struct {
CAPath string `json:"CAPath"`
MaxHdrSyncReqs int `json:"MaxConcurrentSyncHeaderReqs"`
GenesisBlockProposer string `json:"GenesisBlockProposer"`
NumLowFeeTxnPerBlock uint32 `json:"NumLowFeeTxnPerBlock"`
MinTxnFee int64 `json:"MinTxnFee"`
RegisterIDFee int64 `json:"RegisterIDFee"`
Hostname string `json:"Hostname"`
Expand Down

0 comments on commit d6ff39c

Please sign in to comment.