@@ -18,6 +18,7 @@ module Ouroboros.Network.TxSubmission.Inbound.V2.State
1818
1919import Control.Monad.Class.MonadTime.SI (DiffTime , Time , addTime , diffTime )
2020import Data.Foldable (foldl' , toList )
21+ import Data.List (sortOn )
2122import Data.IntMap.Strict qualified as IntMap
2223import Data.IntSet (IntSet )
2324import Data.IntSet qualified as IntSet
@@ -373,13 +374,14 @@ pickRequestTxsAction ctx@PeerActionContext { pacNow, pacPolicy, pacPeerState, pa
373374
374375 leaseUntil = addTime (interTxSpace pacPolicy) pacNow
375376
376- -- Iterate the peer's unacknowledged queue, which preserves the peer's
377- -- advertisement order. Peers are expected to advertise in chain-
378- -- topological order (parents before children), so walking in that
379- -- order aligns fetch order with submission-validity order and a
380- -- child is never requested ahead of its parent when the same peer
381- -- carries both .
377+ -- Order candidates by raw txid bytes so every node in the network
378+ -- prioritises the same fetches, regardless of which peer
379+ -- advertised them first locally. This mirrors V1's
380+ -- 'Map.splitAt' on 'Map txid SizeInBytes' priority and produces
381+ -- convergent cross-node propagation. Submission still happens
382+ -- in advertisement order via 'pickSubmitAction' .
382383 candidates =
384+ sortOn (\ (k, _) -> IntMap. lookup k (sharedKeyToRawTxId pacSharedState))
383385 [ (k, txSize)
384386 | TxKey k <- toList (peerUnacknowledgedTxIds pacPeerState)
385387 , IntSet. notMember k (peerRequestedTxs pacPeerState)
@@ -771,14 +773,15 @@ dropTxKeys :: HasRawTxId txid
771773 -> SharedTxState peeraddr txid
772774 -> SharedTxState peeraddr txid
773775dropTxKeys keys st@ SharedTxState { sharedTxTable, sharedRetainedTxs, sharedTxIdToKey
774- , sharedKeyToTxId }
776+ , sharedKeyToTxId, sharedKeyToRawTxId }
775777 | IntSet. null keys = st
776778 | otherwise =
777779 st {
778- sharedTxTable = IntMap. withoutKeys sharedTxTable keys,
779- sharedRetainedTxs = retainedDeleteKeys keys sharedRetainedTxs,
780- sharedTxIdToKey = IntSet. foldl' deleteTxId sharedTxIdToKey keys,
781- sharedKeyToTxId = IntMap. withoutKeys sharedKeyToTxId keys
780+ sharedTxTable = IntMap. withoutKeys sharedTxTable keys,
781+ sharedRetainedTxs = retainedDeleteKeys keys sharedRetainedTxs,
782+ sharedTxIdToKey = IntSet. foldl' deleteTxId sharedTxIdToKey keys,
783+ sharedKeyToTxId = IntMap. withoutKeys sharedKeyToTxId keys,
784+ sharedKeyToRawTxId = IntMap. withoutKeys sharedKeyToRawTxId keys
782785 }
783786 where
784787 deleteTxId txIdToKey k =
@@ -794,12 +797,13 @@ dropLookupOnly :: HasRawTxId txid
794797 => IntSet. IntSet
795798 -> SharedTxState peeraddr txid
796799 -> SharedTxState peeraddr txid
797- dropLookupOnly keys st@ SharedTxState { sharedTxIdToKey, sharedKeyToTxId }
800+ dropLookupOnly keys st@ SharedTxState { sharedTxIdToKey, sharedKeyToTxId, sharedKeyToRawTxId }
798801 | IntSet. null keys = st
799802 | otherwise =
800803 st {
801- sharedTxIdToKey = IntSet. foldl' deleteTxId sharedTxIdToKey keys,
802- sharedKeyToTxId = IntMap. withoutKeys sharedKeyToTxId keys
804+ sharedTxIdToKey = IntSet. foldl' deleteTxId sharedTxIdToKey keys,
805+ sharedKeyToTxId = IntMap. withoutKeys sharedKeyToTxId keys,
806+ sharedKeyToRawTxId = IntMap. withoutKeys sharedKeyToRawTxId keys
803807 }
804808 where
805809 deleteTxId txIdToKey k =
@@ -1308,16 +1312,18 @@ handleReceivedTxIds mempoolHasTx now policy requestedTxIds txidsAndSizes
13081312 sharedChangedAcc' = sharedChangedAcc || txKeyWasNew
13091313 (txKey@ (TxKey k), txKeyWasNew, sharedAcc') = lookupOrInternTxId txid sharedAcc
13101314
1311- lookupOrInternTxId txid st@ SharedTxState { sharedTxIdToKey, sharedKeyToTxId, sharedNextTxKey }
1315+ lookupOrInternTxId txid st@ SharedTxState { sharedTxIdToKey, sharedKeyToTxId
1316+ , sharedKeyToRawTxId, sharedNextTxKey }
13121317 | Just key <- Map. lookup rawId sharedTxIdToKey = (key, False , st)
13131318 | otherwise =
13141319 let key = TxKey sharedNextTxKey
13151320 in ( key
13161321 , True
13171322 , st {
1318- sharedTxIdToKey = Map. insert rawId key sharedTxIdToKey,
1319- sharedKeyToTxId = IntMap. insert sharedNextTxKey txid sharedKeyToTxId,
1320- sharedNextTxKey = sharedNextTxKey + 1
1323+ sharedTxIdToKey = Map. insert rawId key sharedTxIdToKey,
1324+ sharedKeyToTxId = IntMap. insert sharedNextTxKey txid sharedKeyToTxId,
1325+ sharedKeyToRawTxId = IntMap. insert sharedNextTxKey rawId sharedKeyToRawTxId,
1326+ sharedNextTxKey = sharedNextTxKey + 1
13211327 }
13221328 )
13231329 where rawId = getRawTxId txid
0 commit comments