Skip to content

Commit

Permalink
Merge pull request #96 from richardschneider/95-multiaddress-hash
Browse files Browse the repository at this point in the history
fix(MultiAddress.GetHashCode): aliased protocols give same hashcode #95
  • Loading branch information
richardschneider authored Jul 31, 2019
2 parents 335a9d7 + be9887e commit 2ee2709
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .codacy.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
exclude_paths:
- 'test/'
- 'test/*'
- 'test/**/*'
- 'doc/'
- 'doc/*'
- 'doc/**/*'
9 changes: 8 additions & 1 deletion src/MultiAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,14 @@ void Read(TextReader stream)
/// <inheritdoc />
public override int GetHashCode()
{
return ToString().GetHashCode();
int code = 0;

foreach (var p in Protocols)
{
code += p.Code.GetHashCode();
code += p.Value.GetHashCode();
}
return code;
}

/// <inheritdoc />
Expand Down
15 changes: 12 additions & 3 deletions src/NetworkProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,18 @@ public override void WriteValue(CodedOutputStream stream)

abstract class ValuelessNetworkProtocol : NetworkProtocol
{
public override void ReadValue(CodedInputStream stream) { }
public override void ReadValue(TextReader stream) { }
public override void WriteValue(CodedOutputStream stream) { }
public override void ReadValue(CodedInputStream stream)
{
// No value to read
}
public override void ReadValue(TextReader stream)
{
// No value to read
}
public override void WriteValue(CodedOutputStream stream)
{
// No value to write
}
}

class QuicNetworkProtocol : ValuelessNetworkProtocol
Expand Down
9 changes: 9 additions & 0 deletions test/MultiAddressTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,16 @@ public void WithoutPeerId()
Assert.AreEqual("/ip4/127.0.0.1/tcp/4001", ma1.WithoutPeerId());
}

[TestMethod]
public void Alias_Equality()
{
var a = new MultiAddress("/ipfs/QmQusTXc1Z9C1mzxsqC9ZTFXCgSkpBRGgW4Jk2QYHxKE22");
var b = new MultiAddress("/p2p/QmQusTXc1Z9C1mzxsqC9ZTFXCgSkpBRGgW4Jk2QYHxKE22");

Assert.AreEqual(a, b);
Assert.IsTrue(a == b);
Assert.AreEqual(a.GetHashCode(), b.GetHashCode());
}
}
}

0 comments on commit 2ee2709

Please sign in to comment.