Skip to content

Commit 24fa7e3

Browse files
authored
Merge pull request #528 from gzliudan/tx-pool
upgrade tx pool
2 parents bfa3bb4 + 742a7f9 commit 24fa7e3

File tree

25 files changed

+1305
-978
lines changed

25 files changed

+1305
-978
lines changed

XDCx/XDCx.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ import (
99

1010
"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
1111
"github.com/XinFinOrg/XDPoSChain/XDCxDAO"
12-
"github.com/XinFinOrg/XDPoSChain/consensus"
13-
"github.com/XinFinOrg/XDPoSChain/core/types"
14-
"github.com/XinFinOrg/XDPoSChain/p2p"
15-
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
16-
1712
"github.com/XinFinOrg/XDPoSChain/common"
13+
"github.com/XinFinOrg/XDPoSChain/common/prque"
14+
"github.com/XinFinOrg/XDPoSChain/consensus"
1815
"github.com/XinFinOrg/XDPoSChain/core/state"
16+
"github.com/XinFinOrg/XDPoSChain/core/types"
1917
"github.com/XinFinOrg/XDPoSChain/log"
18+
"github.com/XinFinOrg/XDPoSChain/p2p"
2019
"github.com/XinFinOrg/XDPoSChain/rpc"
2120
lru "github.com/hashicorp/golang-lru"
2221
"golang.org/x/sync/syncmap"
@@ -105,7 +104,7 @@ func New(cfg *Config) *XDCX {
105104
}
106105
XDCX := &XDCX{
107106
orderNonce: make(map[common.Address]*big.Int),
108-
Triegc: prque.New(),
107+
Triegc: prque.New(nil),
109108
tokenDecimalCache: tokenDecimalCache,
110109
orderCache: orderCache,
111110
}

XDCxlending/XDCxlending.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ import (
1212
"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
1313
"github.com/XinFinOrg/XDPoSChain/XDCxDAO"
1414
"github.com/XinFinOrg/XDPoSChain/XDCxlending/lendingstate"
15-
"github.com/XinFinOrg/XDPoSChain/consensus"
16-
"github.com/XinFinOrg/XDPoSChain/core/types"
17-
"github.com/XinFinOrg/XDPoSChain/p2p"
18-
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
19-
2015
"github.com/XinFinOrg/XDPoSChain/common"
16+
"github.com/XinFinOrg/XDPoSChain/common/prque"
17+
"github.com/XinFinOrg/XDPoSChain/consensus"
2118
"github.com/XinFinOrg/XDPoSChain/core/state"
19+
"github.com/XinFinOrg/XDPoSChain/core/types"
2220
"github.com/XinFinOrg/XDPoSChain/log"
21+
"github.com/XinFinOrg/XDPoSChain/p2p"
2322
"github.com/XinFinOrg/XDPoSChain/rpc"
2423
lru "github.com/hashicorp/golang-lru"
2524
)
@@ -67,7 +66,7 @@ func New(XDCx *XDCx.XDCX) *Lending {
6766
lendingTradeCache, _ := lru.New(defaultCacheLimit)
6867
lending := &Lending{
6968
orderNonce: make(map[common.Address]*big.Int),
70-
Triegc: prque.New(),
69+
Triegc: prque.New(nil),
7170
lendingItemHistory: itemCache,
7271
lendingTradeHistory: lendingTradeCache,
7372
}

consensus/XDPoS/utils/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
88
"github.com/XinFinOrg/XDPoSChain/XDCxlending/lendingstate"
99
"github.com/XinFinOrg/XDPoSChain/common"
10+
"github.com/XinFinOrg/XDPoSChain/common/prque"
1011
"github.com/XinFinOrg/XDPoSChain/consensus"
1112
"github.com/XinFinOrg/XDPoSChain/consensus/clique"
1213
"github.com/XinFinOrg/XDPoSChain/core/state"
1314
"github.com/XinFinOrg/XDPoSChain/core/types"
14-
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
1515
)
1616

1717
type Masternode struct {

contracts/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func CreateTransactionSign(chainConfig *params.ChainConfig, pool *core.TxPool, m
8686
}
8787

8888
// Create and send tx to smart contract for sign validate block.
89-
nonce := pool.State().GetNonce(account.Address)
89+
nonce := pool.Nonce(account.Address)
9090
tx := CreateTxSign(block.Number(), block.Hash(), nonce, common.HexToAddress(common.BlockSigners))
9191
txSigned, err := wallet.SignTx(account, tx, chainConfig.ChainId)
9292
if err != nil {

core/bench_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func genValueTx(nbytes int) func(int, *BlockGen) {
8585
return func(i int, gen *BlockGen) {
8686
toaddr := common.Address{}
8787
data := make([]byte, nbytes)
88-
gas, _ := IntrinsicGas(data, false, false)
88+
gas, _ := IntrinsicGas(data, false, false, false)
8989
tx, _ := types.SignTx(types.NewTransaction(gen.TxNonce(benchRootAddr), toaddr, big.NewInt(1), gas, nil, data), types.HomesteadSigner{}, benchRootKey)
9090
gen.AddTx(tx)
9191
}

core/blockchain.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ import (
2828
"sync/atomic"
2929
"time"
3030

31-
"github.com/XinFinOrg/XDPoSChain/XDCxlending/lendingstate"
32-
3331
"github.com/XinFinOrg/XDPoSChain/XDCx/tradingstate"
32+
"github.com/XinFinOrg/XDPoSChain/XDCxlending/lendingstate"
3433
"github.com/XinFinOrg/XDPoSChain/accounts/abi/bind"
35-
3634
"github.com/XinFinOrg/XDPoSChain/common"
3735
"github.com/XinFinOrg/XDPoSChain/common/mclock"
36+
"github.com/XinFinOrg/XDPoSChain/common/prque"
3837
"github.com/XinFinOrg/XDPoSChain/common/sort"
3938
"github.com/XinFinOrg/XDPoSChain/consensus"
4039
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
@@ -53,7 +52,6 @@ import (
5352
"github.com/XinFinOrg/XDPoSChain/rlp"
5453
"github.com/XinFinOrg/XDPoSChain/trie"
5554
lru "github.com/hashicorp/golang-lru"
56-
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
5755
)
5856

5957
var (
@@ -201,7 +199,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
201199
chainConfig: chainConfig,
202200
cacheConfig: cacheConfig,
203201
db: db,
204-
triegc: prque.New(),
202+
triegc: prque.New(nil),
205203
stateCache: state.NewDatabase(db),
206204
quit: make(chan struct{}),
207205
bodyCache: bodyCache,
@@ -1268,18 +1266,18 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
12681266
} else {
12691267
// Full but not archive node, do proper garbage collection
12701268
triedb.Reference(root, common.Hash{}) // metadata reference to keep trie alive
1271-
bc.triegc.Push(root, -float32(block.NumberU64()))
1269+
bc.triegc.Push(root, -int64(block.NumberU64()))
12721270
if tradingTrieDb != nil {
12731271
tradingTrieDb.Reference(tradingRoot, common.Hash{})
12741272
}
12751273
if tradingService != nil {
1276-
tradingService.GetTriegc().Push(tradingRoot, -float32(block.NumberU64()))
1274+
tradingService.GetTriegc().Push(tradingRoot, -int64(block.NumberU64()))
12771275
}
12781276
if lendingTrieDb != nil {
12791277
lendingTrieDb.Reference(lendingRoot, common.Hash{})
12801278
}
12811279
if lendingService != nil {
1282-
lendingService.GetTriegc().Push(lendingRoot, -float32(block.NumberU64()))
1280+
lendingService.GetTriegc().Push(lendingRoot, -int64(block.NumberU64()))
12831281
}
12841282
if current := block.NumberU64(); current > triesInMemory {
12851283
// Find the next state trie we need to commit
@@ -1450,6 +1448,10 @@ func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) {
14501448
// only reason this method exists as a separate one is to make locking cleaner
14511449
// with deferred statements.
14521450
func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []interface{}, []*types.Log, error) {
1451+
// Sanity check that we have something meaningful to import
1452+
if len(chain) == 0 {
1453+
return 0, nil, nil, nil
1454+
}
14531455
engine, _ := bc.Engine().(*XDPoS.XDPoS)
14541456

14551457
// Do a sanity check that the provided chain is actually ordered and linked
@@ -1491,6 +1493,9 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, []
14911493
abort, results := bc.engine.VerifyHeaders(bc, headers, seals)
14921494
defer close(abort)
14931495

1496+
// Start a parallel signature recovery (signer will fluke on fork transition, minimal perf loss)
1497+
senderCacher.recoverFromBlocks(types.MakeSigner(bc.chainConfig, chain[0].Number()), chain)
1498+
14941499
// Iterate over the blocks and insert when the verifier permits
14951500
for i, block := range chain {
14961501
// If the chain is terminating, stop processing blocks

core/lending_pool.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,16 @@ import (
2424
"sync"
2525
"time"
2626

27-
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
28-
2927
"github.com/XinFinOrg/XDPoSChain/XDCxlending/lendingstate"
30-
"github.com/XinFinOrg/XDPoSChain/consensus"
31-
3228
"github.com/XinFinOrg/XDPoSChain/common"
29+
"github.com/XinFinOrg/XDPoSChain/common/prque"
30+
"github.com/XinFinOrg/XDPoSChain/consensus"
31+
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS"
3332
"github.com/XinFinOrg/XDPoSChain/core/state"
3433
"github.com/XinFinOrg/XDPoSChain/core/types"
3534
"github.com/XinFinOrg/XDPoSChain/event"
3635
"github.com/XinFinOrg/XDPoSChain/log"
3736
"github.com/XinFinOrg/XDPoSChain/params"
38-
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
3937
)
4038

4139
var (
@@ -671,7 +669,7 @@ func (pool *LendingPool) add(tx *types.LendingTransaction, local bool) (bool, er
671669
// If the transaction fails basic validation, discard it
672670
if err := pool.validateTx(tx, local); err != nil {
673671
log.Debug("Discarding invalid lending transaction", "hash", hash, "userAddress", tx.UserAddress, "status", tx.Status, "err", err)
674-
invalidTxCounter.Inc(1)
672+
invalidTxMeter.Mark(1)
675673
return false, err
676674
}
677675
from, _ := types.LendingSender(pool.signer, tx) // already validated
@@ -685,12 +683,12 @@ func (pool *LendingPool) add(tx *types.LendingTransaction, local bool) (bool, er
685683
if list := pool.pending[from]; list != nil && list.Overlaps(tx) {
686684
inserted, old := list.Add(tx)
687685
if !inserted {
688-
pendingDiscardCounter.Inc(1)
686+
pendingDiscardMeter.Mark(1)
689687
return false, ErrPendingNonceTooLow
690688
}
691689
if old != nil {
692690
delete(pool.all, old.Hash())
693-
pendingReplaceCounter.Inc(1)
691+
pendingReplaceMeter.Mark(1)
694692
}
695693
pool.all[tx.Hash()] = tx
696694
pool.journalTx(from, tx)
@@ -726,13 +724,13 @@ func (pool *LendingPool) enqueueTx(hash common.Hash, tx *types.LendingTransactio
726724
inserted, old := pool.queue[from].Add(tx)
727725
if !inserted {
728726
// An older transaction was better, discard this
729-
queuedDiscardCounter.Inc(1)
727+
pendingDiscardMeter.Mark(1)
730728
return false, ErrPendingNonceTooLow
731729
}
732730
// Discard any previous transaction and mark this
733731
if old != nil {
734732
delete(pool.all, old.Hash())
735-
queuedReplaceCounter.Inc(1)
733+
queuedReplaceMeter.Mark(1)
736734
}
737735
pool.all[hash] = tx
738736
return old != nil, nil
@@ -764,13 +762,13 @@ func (pool *LendingPool) promoteTx(addr common.Address, hash common.Hash, tx *ty
764762
if !inserted {
765763
// An older transaction was better, discard this
766764
delete(pool.all, hash)
767-
pendingDiscardCounter.Inc(1)
765+
pendingDiscardMeter.Mark(1)
768766
return
769767
}
770768
// Otherwise discard any previous transaction and mark this
771769
if old != nil {
772770
delete(pool.all, old.Hash())
773-
pendingReplaceCounter.Inc(1)
771+
pendingReplaceMeter.Mark(1)
774772
}
775773
// Failsafe to work around direct pending inserts (tests)
776774
if pool.all[hash] == nil {
@@ -981,7 +979,7 @@ func (pool *LendingPool) promoteExecutables(accounts []common.Address) {
981979
hash := tx.Hash()
982980
delete(pool.all, hash)
983981

984-
queuedRateLimitCounter.Inc(1)
982+
queuedRateLimitMeter.Mark(1)
985983
log.Trace("Removed cap-exceeding queued transaction", "hash", hash)
986984
}
987985
}
@@ -998,11 +996,11 @@ func (pool *LendingPool) promoteExecutables(accounts []common.Address) {
998996
if pending > pool.config.GlobalSlots {
999997
pendingBeforeCap := pending
1000998
// Assemble a spam order to penalize large transactors first
1001-
spammers := prque.New()
999+
spammers := prque.New(nil)
10021000
for addr, list := range pool.pending {
10031001
// Only evict transactions from high rollers
10041002
if !pool.locals.contains(addr) && uint64(list.Len()) > pool.config.AccountSlots {
1005-
spammers.Push(addr, float32(list.Len()))
1003+
spammers.Push(addr, int64(list.Len()))
10061004
}
10071005
}
10081006
// Gradually drop transactions from offenders
@@ -1057,7 +1055,7 @@ func (pool *LendingPool) promoteExecutables(accounts []common.Address) {
10571055
}
10581056
}
10591057
}
1060-
pendingRateLimitCounter.Inc(int64(pendingBeforeCap - pending))
1058+
pendingRateLimitMeter.Mark(int64(pendingBeforeCap - pending))
10611059
}
10621060
// If we've queued more transactions than the hard limit, drop oldest ones
10631061
queued := uint64(0)
@@ -1066,7 +1064,7 @@ func (pool *LendingPool) promoteExecutables(accounts []common.Address) {
10661064
}
10671065
if queued > pool.config.GlobalQueue {
10681066
// Sort all accounts with queued transactions by heartbeat
1069-
addresses := make(addresssByHeartbeat, 0, len(pool.queue))
1067+
addresses := make(addressesByHeartbeat, 0, len(pool.queue))
10701068
for addr := range pool.queue {
10711069
if !pool.locals.contains(addr) { // don't drop locals
10721070
addresses = append(addresses, addressByHeartbeat{addr, pool.beats[addr]})
@@ -1087,15 +1085,15 @@ func (pool *LendingPool) promoteExecutables(accounts []common.Address) {
10871085
pool.removeTx(tx.Hash())
10881086
}
10891087
drop -= size
1090-
queuedRateLimitCounter.Inc(int64(size))
1088+
queuedRateLimitMeter.Mark(int64(size))
10911089
continue
10921090
}
10931091
// Otherwise drop only last few transactions
10941092
txs := list.Flatten()
10951093
for i := len(txs) - 1; i >= 0 && drop > 0; i-- {
10961094
pool.removeTx(txs[i].Hash())
10971095
drop--
1098-
queuedRateLimitCounter.Inc(1)
1096+
queuedRateLimitMeter.Mark(1)
10991097
}
11001098
}
11011099
}

0 commit comments

Comments
 (0)