From d6ff39c45adb89578c90c4510b8f70120b369d7d Mon Sep 17 00:00:00 2001 From: Yilun Date: Mon, 5 Aug 2019 23:17:50 -0700 Subject: [PATCH] Add NumLowFeeTxnPerBlock config to control building block logic Signed-off-by: Yilun --- chain/collector.go | 27 ++++++++++++++++++--------- chain/mining.go | 24 ++++++++++++++---------- consensus/config.go | 2 +- consensus/consensus.go | 2 +- consensus/message.go | 2 +- consensus/proposal.go | 2 +- consensus/proposing.go | 2 +- consensus/state.go | 2 +- consensus/util.go | 2 +- consensus/vote.go | 2 +- nknd.go | 4 ++-- pb/transaction.go | 4 ++-- util/config/config.go | 3 +++ 13 files changed, 47 insertions(+), 31 deletions(-) diff --git a/chain/collector.go b/chain/collector.go index c7fe27564..7dc3120e7 100644 --- a/chain/collector.go +++ b/chain/collector.go @@ -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 @@ -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, @@ -97,7 +103,7 @@ 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:] } @@ -105,8 +111,11 @@ func (tc *TxnCollection) Update() error { 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 } diff --git a/chain/mining.go b/chain/mining.go index 74d1c5dda..312457dfa 100644 --- a/chain/mining.go +++ b/chain/mining.go @@ -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 { @@ -75,15 +76,18 @@ 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() @@ -91,11 +95,6 @@ func (bm *BuiltinMining) BuildBlock(ctx context.Context, height uint32, chordID break } - txCount++ - if txCount > int(config.Parameters.NumTxnPerBlock) { - break - } - if err := VerifyTransaction(txn); err != nil { log.Warningf("invalid transaction: %v", err) txnCollection.Pop() @@ -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++ } } diff --git a/consensus/config.go b/consensus/config.go index 5ebb01503..55ddde744 100644 --- a/consensus/config.go +++ b/consensus/config.go @@ -1,4 +1,4 @@ -package moca +package consensus import ( "time" diff --git a/consensus/consensus.go b/consensus/consensus.go index add4095d5..ed2de6134 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -1,4 +1,4 @@ -package moca +package consensus import ( "fmt" diff --git a/consensus/message.go b/consensus/message.go index 1e82eb77b..b192b1aae 100644 --- a/consensus/message.go +++ b/consensus/message.go @@ -1,4 +1,4 @@ -package moca +package consensus import ( "crypto/sha256" diff --git a/consensus/proposal.go b/consensus/proposal.go index 51b1adb96..10e0a8780 100644 --- a/consensus/proposal.go +++ b/consensus/proposal.go @@ -1,4 +1,4 @@ -package moca +package consensus import ( "bytes" diff --git a/consensus/proposing.go b/consensus/proposing.go index 7a2be43e7..10f2d04f5 100644 --- a/consensus/proposing.go +++ b/consensus/proposing.go @@ -1,4 +1,4 @@ -package moca +package consensus import ( "bytes" diff --git a/consensus/state.go b/consensus/state.go index 146a9cb24..cce717efb 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1,4 +1,4 @@ -package moca +package consensus import ( "sync" diff --git a/consensus/util.go b/consensus/util.go index f0b98b8d8..b9802085c 100644 --- a/consensus/util.go +++ b/consensus/util.go @@ -1,4 +1,4 @@ -package moca +package consensus import ( "encoding/binary" diff --git a/consensus/vote.go b/consensus/vote.go index 4928e5845..51f11b736 100644 --- a/consensus/vote.go +++ b/consensus/vote.go @@ -1,4 +1,4 @@ -package moca +package consensus import ( "fmt" diff --git a/nknd.go b/nknd.go index ac4d0f06b..bb609fe2b 100644 --- a/nknd.go +++ b/nknd.go @@ -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 ( @@ -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 } diff --git a/pb/transaction.go b/pb/transaction.go index 73dd804e5..30c6fd55f 100644 --- a/pb/transaction.go +++ b/pb/transaction.go @@ -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, diff --git a/util/config/config.go b/util/config/config.go index 93414811c..771f7d5ff 100644 --- a/util/config/config.go +++ b/util/config/config.go @@ -129,6 +129,8 @@ var ( WalletFile: "wallet.json", MaxGetIDSeeds: 3, DBFilesCacheCapacity: 100, + NumLowFeeTxnPerBlock: 4, + MinTxnFee: 10000000, } ) @@ -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"`