Skip to content

Commit

Permalink
Fix peer add(offer) & remove(offer) event order problem
Browse files Browse the repository at this point in the history
Use LinkedHashSet to maintain NetworkEnvelope ordering when Connection#onBundleOfEnvelopes
calls listeners.

Connection#onBundleOfEnvelopes builds a set from an ordered list of NetworkEnvelopes,
then calls listeners during set iteration.  The envelope list ordering is lost if a
HashSet is built, but maintained by switching to a LinkedHashSet.

Losing the envelope ordering becomes a problem if the peer receives a RemoveDataMessage
and an AddDataMessage in the same batch of envelopes, and relays them to listeners
in the wrong (random) order.  For example, an API 'editoffer' call may result in the
edited offer being added, then immediately remove from their UI's offer book.
  • Loading branch information
ghubstan committed Jul 30, 2021
1 parent 95bbb41 commit add6536
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion p2p/src/main/java/bisq/network/p2p/network/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -439,7 +440,7 @@ public void onMessage(NetworkEnvelope networkEnvelope, Connection connection) {

private void onBundleOfEnvelopes(BundleOfEnvelopes bundleOfEnvelopes, Connection connection) {
Map<P2PDataStorage.ByteArray, Set<NetworkEnvelope>> itemsByHash = new HashMap<>();
Set<NetworkEnvelope> envelopesToProcess = new HashSet<>();
Set<NetworkEnvelope> envelopesToProcess = new LinkedHashSet<>();
List<NetworkEnvelope> networkEnvelopes = bundleOfEnvelopes.getEnvelopes();
for (NetworkEnvelope networkEnvelope : networkEnvelopes) {
// If SendersNodeAddressMessage we do some verifications and apply if successful, otherwise we return false.
Expand Down

0 comments on commit add6536

Please sign in to comment.