From 6cecf9370da480e89decb12d8dc6e5b80dca81d6 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Sat, 21 Aug 2021 18:47:08 +0700 Subject: [PATCH 1/6] save --- txpool/pool.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/txpool/pool.go b/txpool/pool.go index cadcf68e9..052009cf0 100644 --- a/txpool/pool.go +++ b/txpool/pool.go @@ -235,7 +235,14 @@ func (sc *SendersCache) mergeStateChanges(stateChanges map[string]senderInfo, un id = sc.senderID sc.senderIDs[addr] = id } - sc.senderInfo[id] = newSenderInfo(v.nonce, v.balance) + old, ok := sc.senderInfo[id] + if ok { + old.nonce = v.nonce + old.balance = v.balance + v.txNonce2Tx = old.txNonce2Tx + } else { + sc.senderInfo[id] = newSenderInfo(v.nonce, v.balance) + } } /* @@ -253,8 +260,10 @@ func (sc *SendersCache) mergeStateChanges(stateChanges map[string]senderInfo, un id = sc.senderID sc.senderIDs[string(unwindedTxs.senders.At(i))] = id } - if _, ok := stateChanges[string(unwindedTxs.senders.At(i))]; !ok { - sc.senderInfo[id] = newSenderInfo(0, *uint256.NewInt(0)) + if _, ok := sc.senderInfo[id]; !ok { + if _, ok := stateChanges[string(unwindedTxs.senders.At(i))]; !ok { + sc.senderInfo[id] = newSenderInfo(0, *uint256.NewInt(0)) + } } } @@ -265,8 +274,10 @@ func (sc *SendersCache) mergeStateChanges(stateChanges map[string]senderInfo, un id = sc.senderID sc.senderIDs[string(minedTxs.senders.At(i))] = id } - if _, ok := stateChanges[string(minedTxs.senders.At(i))]; !ok { - sc.senderInfo[id] = newSenderInfo(0, *uint256.NewInt(0)) + if _, ok := sc.senderInfo[id]; !ok { + if _, ok := stateChanges[string(minedTxs.senders.At(i))]; !ok { + sc.senderInfo[id] = newSenderInfo(0, *uint256.NewInt(0)) + } } //if v, ok := stateChanges[string(minedTxs.senders.At(i))]; ok { @@ -550,11 +561,11 @@ func (p *TxPool) setBaseFee(protocolBaseFee, pendingBaseFee uint64) (uint64, uin func (p *TxPool) OnNewBlock(stateChanges map[string]senderInfo, unwindTxs, minedTxs TxSlots, protocolBaseFee, pendingBaseFee, blockHeight uint64, senders *SendersCache) error { t := time.Now() + protocolBaseFee, pendingBaseFee = p.setBaseFee(protocolBaseFee, pendingBaseFee) if err := senders.onNewBlock(stateChanges, unwindTxs, minedTxs, blockHeight); err != nil { return err } //log.Debug("[txpool] new block", "unwinded", len(unwindTxs.txs), "mined", len(minedTxs.txs), "protocolBaseFee", protocolBaseFee, "blockHeight", blockHeight) - protocolBaseFee, pendingBaseFee = p.setBaseFee(protocolBaseFee, pendingBaseFee) if err := unwindTxs.Valid(); err != nil { return err } From cf351f8e6432555ba440260a31ed4282d77d7dcc Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Sat, 21 Aug 2021 18:52:36 +0700 Subject: [PATCH 2/6] save --- txpool/pool.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/txpool/pool.go b/txpool/pool.go index 052009cf0..cfa3a82c2 100644 --- a/txpool/pool.go +++ b/txpool/pool.go @@ -460,7 +460,7 @@ func (p *TxPool) Started() bool { } func (p *TxPool) Add(coreDB kv.RoDB, newTxs TxSlots, senders *SendersCache) error { - t := time.Now() + //t := time.Now() if err := senders.onNewTxs(coreDB, newTxs); err != nil { return err } @@ -492,7 +492,7 @@ func (p *TxPool) Add(coreDB kv.RoDB, newTxs TxSlots, senders *SendersCache) erro } } - log.Info("on new txs", "in", time.Since(t)) + //log.Info("on new txs", "in", time.Since(t)) return nil } func onNewTxs(senders *SendersCache, newTxs TxSlots, protocolBaseFee, pendingBaseFee uint64, pending, baseFee, queued *SubPool, byHash map[string]*metaTx, localsHistory *simplelru.LRU) error { From bc1c7bd91cc6ca06a6cee6cd61ffc5326dad3451 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Sat, 21 Aug 2021 19:09:52 +0700 Subject: [PATCH 3/6] save --- txpool/pool.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/txpool/pool.go b/txpool/pool.go index cfa3a82c2..77a2ada89 100644 --- a/txpool/pool.go +++ b/txpool/pool.go @@ -502,7 +502,11 @@ func onNewTxs(senders *SendersCache, newTxs TxSlots, protocolBaseFee, pendingBas } } + changedSenders := map[uint64]*senderInfo{} + unsafeAddToPool(senders, newTxs, pending, PendingSubPool, func(i *metaTx, sender *senderInfo) { + changedSenders[i.Tx.senderID] = sender + if _, ok := localsHistory.Get(i.Tx.idHash); ok { //TODO: also check if sender is in list of local-senders i.subPool |= IsLocal @@ -525,10 +529,9 @@ func onNewTxs(senders *SendersCache, newTxs TxSlots, protocolBaseFee, pendingBas } }) - senders.forEach(func(sender *senderInfo) { - // TODO: aggregate changed senders before call this func + for _, sender := range changedSenders { onSenderChange(sender, protocolBaseFee, pendingBaseFee) - }) + } pending.EnforceInvariants() baseFee.EnforceInvariants() From a12556da38c436aac6e46ac71b8ef7f291a87ca5 Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Sat, 21 Aug 2021 19:13:19 +0700 Subject: [PATCH 4/6] save --- txpool/pool.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/txpool/pool.go b/txpool/pool.go index 77a2ada89..4cef9b59b 100644 --- a/txpool/pool.go +++ b/txpool/pool.go @@ -648,6 +648,8 @@ func onNewBlock(senders *SendersCache, unwindTxs TxSlots, minedTxs []*TxSlot, pr log.Info("remove mined", "removed", j, "minedTxsLen", len(minedTxs)) } + changedSenders := map[uint64]*senderInfo{} + // This can be thought of a reverse operation from the one described before. // When a block that was deemed "the best" of its height, is no longer deemed "the best", the // transactions contained in it, are now viable for inclusion in other blocks, and therefore should @@ -658,6 +660,7 @@ func onNewBlock(senders *SendersCache, unwindTxs TxSlots, minedTxs []*TxSlot, pr // somehow the fact that certain transactions were local, needs to be remembered for some // time (up to some "immutability threshold"). unsafeAddToPool(senders, unwindTxs, pending, PendingSubPool, func(i *metaTx, sender *senderInfo) { + changedSenders[i.Tx.senderID] = sender //fmt.Printf("add: %d,%d\n", i.Tx.senderID, i.Tx.nonce) if _, ok := localsHistory.Get(i.Tx.idHash); ok { //TODO: also check if sender is in list of local-senders @@ -681,10 +684,9 @@ func onNewBlock(senders *SendersCache, unwindTxs TxSlots, minedTxs []*TxSlot, pr } }) - senders.forEach(func(sender *senderInfo) { - // TODO: aggregate changed senders before call this func + for _, sender := range changedSenders { onSenderChange(sender, protocolBaseFee, pendingBaseFee) - }) + } pending.EnforceInvariants() baseFee.EnforceInvariants() From c8cfd689d26c34fe7431a39f9abafa6d281e75fb Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Sat, 21 Aug 2021 19:14:56 +0700 Subject: [PATCH 5/6] save --- txpool/pool.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/txpool/pool.go b/txpool/pool.go index 4cef9b59b..803f5a1fb 100644 --- a/txpool/pool.go +++ b/txpool/pool.go @@ -648,7 +648,7 @@ func onNewBlock(senders *SendersCache, unwindTxs TxSlots, minedTxs []*TxSlot, pr log.Info("remove mined", "removed", j, "minedTxsLen", len(minedTxs)) } - changedSenders := map[uint64]*senderInfo{} + changedSenders := make(map[uint64]*senderInfo, len(unwindTxs.txs)/4) // This can be thought of a reverse operation from the one described before. // When a block that was deemed "the best" of its height, is no longer deemed "the best", the From 17fbbde4c3c03c69db53f9ee68d559fbc0ec757d Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Sat, 21 Aug 2021 19:17:04 +0700 Subject: [PATCH 6/6] save --- txpool/pool.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/txpool/pool.go b/txpool/pool.go index 803f5a1fb..3645ee3eb 100644 --- a/txpool/pool.go +++ b/txpool/pool.go @@ -138,14 +138,6 @@ func (sc *SendersCache) get(senderID uint64) *senderInfo { } return sender } -func (sc *SendersCache) forEach(f func(info *senderInfo)) { - sc.lock.RLock() - defer sc.lock.RUnlock() - - for i := range sc.senderInfo { - f(sc.senderInfo[i]) - } -} func (sc *SendersCache) len() int { sc.lock.RLock() defer sc.lock.RUnlock()