-
Notifications
You must be signed in to change notification settings - Fork 851
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove the need of reflection of p2p message (de)serialization (#1195)
* Add `PayloadFactory` to create payload instances based on the command string instead of the using the reflection `Activator` class based on the type decorated info. * Simplify payload deserialization A `Message` comes always with a Payload type and never with any other type. This means that a `Message` can come with a `BlockPayload` type but never with a `Block` type. So, the `ConsensusFactory` is unnecessary. * Use a table to get payload command name instead of using reflection * Remove `PayloadAttribute` completely. * Make PayloadFactory a singleton * Use `switch` and pattern matching * Allow override of CreatePayload --------- Co-authored-by: nicolas.dorier <[email protected]>
- Loading branch information
1 parent
40ca72b
commit c06f086
Showing
36 changed files
with
245 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,84 @@ | ||
#if !NOSOCKET | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NBitcoin.Protocol | ||
{ | ||
/// <summary> | ||
/// An available peer address in the bitcoin network is announced (unsolicited or after a getaddr) | ||
/// </summary> | ||
[Payload("addr")] | ||
public class AddrPayload : Payload, IBitcoinSerializable | ||
{ | ||
NetworkAddress[] addr_list = new NetworkAddress[0]; | ||
public NetworkAddress[] Addresses | ||
{ | ||
get | ||
{ | ||
return addr_list; | ||
} | ||
} | ||
|
||
public AddrPayload() | ||
{ | ||
|
||
} | ||
public AddrPayload(NetworkAddress address) | ||
{ | ||
addr_list = new NetworkAddress[] { address }; | ||
} | ||
public AddrPayload(NetworkAddress[] addresses) | ||
{ | ||
addr_list = addresses.ToArray(); | ||
} | ||
|
||
#region IBitcoinSerializable Members | ||
|
||
public override void ReadWriteCore(BitcoinStream stream) | ||
{ | ||
stream.ReadWrite(ref addr_list); | ||
} | ||
|
||
#endregion | ||
|
||
public override string ToString() | ||
{ | ||
return Addresses.Length + " address(es)"; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// An available peer address in the bitcoin network is announced (unsolicited or after a getaddrv2) | ||
/// </summary> | ||
[Payload("addrv2")] | ||
public class AddrV2Payload : AddrPayload | ||
{ | ||
public AddrV2Payload() | ||
: base() | ||
{ | ||
} | ||
public AddrV2Payload(NetworkAddress address) | ||
: base(address) | ||
{ | ||
} | ||
|
||
public AddrV2Payload(NetworkAddress[] addresses) | ||
: base(addresses) | ||
{ | ||
} | ||
|
||
public override void ReadWriteCore(BitcoinStream stream) | ||
{ | ||
using (stream.ProtocolVersionScope(NetworkAddress.AddrV2Format)) | ||
{ | ||
base.ReadWriteCore(stream); | ||
} | ||
} | ||
} | ||
} | ||
#endif | ||
#if !NOSOCKET | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NBitcoin.Protocol | ||
{ | ||
/// <summary> | ||
/// An available peer address in the bitcoin network is announced (unsolicited or after a getaddr) | ||
/// </summary> | ||
|
||
public class AddrPayload : Payload, IBitcoinSerializable | ||
{ | ||
public override string Command => "addr"; | ||
NetworkAddress[] addr_list = new NetworkAddress[0]; | ||
public NetworkAddress[] Addresses | ||
{ | ||
get | ||
{ | ||
return addr_list; | ||
} | ||
} | ||
|
||
public AddrPayload() | ||
{ | ||
|
||
} | ||
public AddrPayload(NetworkAddress address) | ||
{ | ||
addr_list = new NetworkAddress[] { address }; | ||
} | ||
public AddrPayload(NetworkAddress[] addresses) | ||
{ | ||
addr_list = addresses.ToArray(); | ||
} | ||
|
||
#region IBitcoinSerializable Members | ||
|
||
public override void ReadWriteCore(BitcoinStream stream) | ||
{ | ||
stream.ReadWrite(ref addr_list); | ||
} | ||
|
||
#endregion | ||
|
||
public override string ToString() | ||
{ | ||
return Addresses.Length + " address(es)"; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// An available peer address in the bitcoin network is announced (unsolicited or after a getaddrv2) | ||
/// </summary> | ||
|
||
public class AddrV2Payload : AddrPayload | ||
{ | ||
public override string Command => "addrv2"; | ||
public AddrV2Payload() | ||
: base() | ||
{ | ||
} | ||
public AddrV2Payload(NetworkAddress address) | ||
: base(address) | ||
{ | ||
} | ||
|
||
public AddrV2Payload(NetworkAddress[] addresses) | ||
: base(addresses) | ||
{ | ||
} | ||
|
||
public override void ReadWriteCore(BitcoinStream stream) | ||
{ | ||
using (stream.ProtocolVersionScope(NetworkAddress.AddrV2Format)) | ||
{ | ||
base.ReadWriteCore(stream); | ||
} | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.