From b3da44b9a0828785b93a8bd88da457a522d89e4e Mon Sep 17 00:00:00 2001 From: Lucas Ontivero Date: Fri, 22 Dec 2023 11:53:01 -0300 Subject: [PATCH 1/7] 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. --- NBitcoin/Protocol/PayloadFactory.cs | 51 +++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 NBitcoin/Protocol/PayloadFactory.cs diff --git a/NBitcoin/Protocol/PayloadFactory.cs b/NBitcoin/Protocol/PayloadFactory.cs new file mode 100644 index 000000000..108660878 --- /dev/null +++ b/NBitcoin/Protocol/PayloadFactory.cs @@ -0,0 +1,51 @@ +using NBitcoin.Protocol.Payloads; + +namespace NBitcoin.Protocol +{ + public class PayloadFactory + { + public static Payload Create(string command) + { + switch (command) + { +#if !NOSOCKET + case "addr": return new AddrPayload(); + case "addrv2": return new AddrV2Payload(); + case "version": return new VersionPayload(); +#endif + case "block": return new BlockPayload(); + case "blocktxn": return new BlockTxnPayload(); + case "cmpctblock": return new CmpctBlockPayload(); + case "cfilter": return new CompactFilterPayload(); + case "cfcheckpt": return new CompactFilterCheckPointPayload(); + case "cfheaders": return new CompactFilterHeadersPayload(); + case "feefilter": return new FeeFilterPayload(); + case "filteradd": return new FilterAddPayload(); + case "filterload": return new FilterLoadPayload(); + case "getaddr": return new GetAddrPayload(); + case "getblocktxn": return new GetBlockTxnPayload(); + case "getblocks": return new GetBlocksPayload(); + case "getcfilters": return new GetCompactFiltersPayload(); + case "getcfheaders": return new GetCompactFilterHeadersPayload(); + case "getcfcheckpt": return new GetCompactFilterCheckPointPayload(); + case "getdata": return new GetDataPayload(); + case "getheaders": return new GetHeadersPayload(); + case "havewitness": return new HaveWitnessPayload(); + case "headers": return new HeadersPayload(); + case "inv": return new InvPayload(); + case "mempool": return new MempoolPayload(); + case "merkleblock": return new MerkleBlockPayload(); + case "ping": return new PingPayload(); + case "pong": return new PongPayload(); + case "sendaddrv2": return new SendAddrV2Payload(); + case "sendcmpct": return new SendCmpctPayload(); + case "sendheaders": return new SendHeadersPayload(); + case "tx": return new TxPayload(); + case "utxos": return new UTxOutputPayload(); + case "verack": return new VerAckPayload(); + case "wtxidrelay": return new WTxIdRelayPayload(); + default: return new UnknownPayload(command); + } + } + } +} From 59c401508035c6068afb68ce1929c88c0ba06c42 Mon Sep 17 00:00:00 2001 From: Lucas Ontivero Date: Fri, 22 Dec 2023 11:56:31 -0300 Subject: [PATCH 2/7] 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. --- NBitcoin/Protocol/Message.cs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/NBitcoin/Protocol/Message.cs b/NBitcoin/Protocol/Message.cs index 89d2693b2..fe24280f2 100644 --- a/NBitcoin/Protocol/Message.cs +++ b/NBitcoin/Protocol/Message.cs @@ -130,18 +130,12 @@ public void ReadWrite(BitcoinStream stream) BitcoinStream payloadStream = new BitcoinStream(new MemoryStream(payloadBytes, 0, length, false), false); payloadStream.CopyParameters(stream); - var payloadType = PayloadAttribute.GetCommandType(Command); - var unknown = payloadType == typeof(UnknownPayload); - if (unknown) + var payload = PayloadFactory.Create(Command); + if (payload is UnknownPayload) Logs.NodeServer.LogWarning("Unknown command received {command}", Command); - IBitcoinSerializable payload = null; - if (!stream.ConsensusFactory.TryCreateNew(payloadType, out payload)) - payload = (IBitcoinSerializable)Activator.CreateInstance(payloadType); payload.ReadWrite(payloadStream); - if (unknown) - ((UnknownPayload)payload)._Command = Command; - Payload = (Payload)payload; + Payload = payload; } finally { From a43cca5e8ac69329ef065b525c1e85b037c5dd9d Mon Sep 17 00:00:00 2001 From: Lucas Ontivero Date: Fri, 22 Dec 2023 12:21:05 -0300 Subject: [PATCH 3/7] Use a table to get payload command name instead of using reflection --- NBitcoin/Protocol/Payload.cs | 2 +- NBitcoin/Protocol/PayloadFactory.cs | 42 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/NBitcoin/Protocol/Payload.cs b/NBitcoin/Protocol/Payload.cs index 993a36eaa..9ebfef93e 100644 --- a/NBitcoin/Protocol/Payload.cs +++ b/NBitcoin/Protocol/Payload.cs @@ -12,7 +12,7 @@ public virtual string Command { get { - return PayloadAttribute.GetCommandName(this.GetType()); + return PayloadFactory.GetCommand(this); } } diff --git a/NBitcoin/Protocol/PayloadFactory.cs b/NBitcoin/Protocol/PayloadFactory.cs index 108660878..74a05ccd8 100644 --- a/NBitcoin/Protocol/PayloadFactory.cs +++ b/NBitcoin/Protocol/PayloadFactory.cs @@ -1,3 +1,4 @@ +using System; using NBitcoin.Protocol.Payloads; namespace NBitcoin.Protocol @@ -47,5 +48,46 @@ public static Payload Create(string command) default: return new UnknownPayload(command); } } + + public static string GetCommand(Payload payload) + { +#if !NOSOCKET + if (payload is AddrV2Payload) return "addrv2"; + if (payload is AddrPayload) return "addr"; + if (payload is VersionPayload) return "version"; +#endif + if (payload is BlockPayload) return "block"; + if (payload is BlockTxnPayload) return "blocktxn"; + if (payload is CmpctBlockPayload) return "cmpctblock"; + if (payload is CompactFilterPayload) return "cfilter"; + if (payload is CompactFilterHeadersPayload) return "cfheaders"; + if (payload is CompactFilterCheckPointPayload) return "cfcheckpt"; + if (payload is FeeFilterPayload) return "feefilter"; + if (payload is FilterAddPayload) return "filteradd"; + if (payload is FilterLoadPayload) return "filterload"; + if (payload is GetAddrPayload) return "getaddr"; + if (payload is GetBlockTxnPayload) return "getblocktxn"; + if (payload is GetBlocksPayload) return "getblocks"; + if (payload is GetCompactFiltersPayload) return "getcfilters"; + if (payload is GetCompactFilterHeadersPayload) return "getcfheaders"; + if (payload is GetCompactFilterCheckPointPayload) return "getcfcheckpt"; + if (payload is GetDataPayload) return "getdata"; + if (payload is GetHeadersPayload) return "getheaders"; + if (payload is HaveWitnessPayload) return "havewitness"; + if (payload is HeadersPayload) return "headers"; + if (payload is InvPayload) return "inv"; + if (payload is MempoolPayload) return "mempool"; + if (payload is MerkleBlockPayload) return "merkleblock"; + if (payload is PingPayload) return "ping"; + if (payload is PongPayload) return "pong"; + if (payload is SendAddrV2Payload) return "sendaddrv2"; + if (payload is SendCmpctPayload) return "sendcmpct"; + if (payload is SendHeadersPayload) return "sendheaders"; + if (payload is TxPayload) return "tx"; + if (payload is UTxOutputPayload) return "utxos"; + if (payload is VerAckPayload) return "verack"; + if (payload is WTxIdRelayPayload) return "wtxidrelay"; + throw new NotSupportedException($"Unknown command for Payload type: {payload.GetType().Name}"); + } } } From ad74ebede5690f55edffc0bc5ff21fbf6df46aa8 Mon Sep 17 00:00:00 2001 From: Lucas Ontivero Date: Fri, 22 Dec 2023 12:54:44 -0300 Subject: [PATCH 4/7] Remove `PayloadAttribute` completely. --- NBitcoin/Protocol/PayloadAttribute.cs | 82 --------- NBitcoin/Protocol/Payloads/AddrPayload.cs | 162 +++++++++--------- NBitcoin/Protocol/Payloads/BlockPayload.cs | 2 +- NBitcoin/Protocol/Payloads/BlockTxnPayload.cs | 2 +- .../Protocol/Payloads/CmpctBlockPayload.cs | 2 +- .../Protocol/Payloads/CompactFilterPayload.cs | 6 +- .../Protocol/Payloads/FeeFilterPayload.cs | 2 +- .../Protocol/Payloads/FilterAddPayload.cs | 2 +- .../Protocol/Payloads/FilterLoadPayload.cs | 2 +- NBitcoin/Protocol/Payloads/GetAddrPayload.cs | 2 +- .../Protocol/Payloads/GetBlockTxnPayload.cs | 2 +- .../Protocol/Payloads/GetBlocksPayload.cs | 2 +- .../Payloads/GetCompactFiltersPayload.cs | 6 +- NBitcoin/Protocol/Payloads/GetDataPayload.cs | 2 +- .../Protocol/Payloads/GetHeadersPayload.cs | 2 +- .../Protocol/Payloads/HaveWitnessPayload.cs | 2 +- NBitcoin/Protocol/Payloads/HeadersPayload.cs | 2 +- NBitcoin/Protocol/Payloads/InvPayload.cs | 2 +- NBitcoin/Protocol/Payloads/MempoolPayload.cs | 2 +- .../Protocol/Payloads/MerkleBlockPayload.cs | 2 +- NBitcoin/Protocol/Payloads/PingPayload.cs | 2 +- NBitcoin/Protocol/Payloads/PongPayload.cs | 2 +- .../Protocol/Payloads/SendAddrV2Payload.cs | 2 +- .../Protocol/Payloads/SendCmpctPayload.cs | 2 +- .../Protocol/Payloads/SendHeadersPayload.cs | 2 +- NBitcoin/Protocol/Payloads/TxPayload.cs | 2 +- .../Protocol/Payloads/UTxOutputPayload.cs | 80 ++++----- NBitcoin/Protocol/Payloads/VerAckPayload.cs | 2 +- NBitcoin/Protocol/Payloads/VersionPayload.cs | 2 +- .../Protocol/Payloads/WTxIdRelayPayload.cs | 2 +- 30 files changed, 152 insertions(+), 234 deletions(-) delete mode 100644 NBitcoin/Protocol/PayloadAttribute.cs diff --git a/NBitcoin/Protocol/PayloadAttribute.cs b/NBitcoin/Protocol/PayloadAttribute.cs deleted file mode 100644 index 35dc66f05..000000000 --- a/NBitcoin/Protocol/PayloadAttribute.cs +++ /dev/null @@ -1,82 +0,0 @@ -using System; -using System.Reflection; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace NBitcoin.Protocol -{ - [AttributeUsage(AttributeTargets.Class)] - public class PayloadAttribute : Attribute - { - static Dictionary _NameToType; - static Dictionary _TypeToName; - - static PayloadAttribute() - { - _NameToType = new Dictionary(); - _TypeToName = new Dictionary(); - foreach (var pair in - GetLoadableTypes(typeof(PayloadAttribute).GetTypeInfo().Assembly) - .Where(t => t.Namespace == typeof(PayloadAttribute).Namespace) - .Where(t => t.IsDefined(typeof(PayloadAttribute), true)) - .Select(t => - new - { - Attr = t.GetCustomAttributes(typeof(PayloadAttribute), true).OfType().First(), - Type = t - })) - { - _NameToType.Add(pair.Attr.Name, pair.Type.AsType()); - _TypeToName.Add(pair.Type.AsType(), pair.Attr.Name); - } - } - - static IEnumerable GetLoadableTypes(Assembly assembly) - { - try - { - return assembly.DefinedTypes; - } - catch (ReflectionTypeLoadException e) - { - return e.Types.Where(t => t != null).Select(t => t.GetTypeInfo()); - } - } - - public static string GetCommandName() - { - return GetCommandName(typeof(T)); - } - public static Type GetCommandType(string commandName) - { - Type result; - if (!_NameToType.TryGetValue(commandName, out result)) - return typeof(UnknownPayload); - return result; - } - public PayloadAttribute(string commandName) - { - Name = commandName; - } - public string Name - { - get; - set; - } - - internal static string GetCommandName(Type type) - { - string result; - if (!_TypeToName.TryGetValue(type, out result)) - { - // try base type too - if (!_TypeToName.TryGetValue(type.GetTypeInfo().BaseType, out result)) - throw new ArgumentException(type.FullName + " is not a payload"); - } - - return result; - } - } -} diff --git a/NBitcoin/Protocol/Payloads/AddrPayload.cs b/NBitcoin/Protocol/Payloads/AddrPayload.cs index 2f8c30eca..e8bb17da1 100644 --- a/NBitcoin/Protocol/Payloads/AddrPayload.cs +++ b/NBitcoin/Protocol/Payloads/AddrPayload.cs @@ -1,82 +1,82 @@ -#if !NOSOCKET -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace NBitcoin.Protocol -{ - /// - /// An available peer address in the bitcoin network is announced (unsolicited or after a getaddr) - /// - [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)"; - } - } - - /// - /// An available peer address in the bitcoin network is announced (unsolicited or after a getaddrv2) - /// - [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); - } - } - } -} +#if !NOSOCKET +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NBitcoin.Protocol +{ + /// + /// An available peer address in the bitcoin network is announced (unsolicited or after a getaddr) + /// + + 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)"; + } + } + + /// + /// An available peer address in the bitcoin network is announced (unsolicited or after a getaddrv2) + /// + + 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 \ No newline at end of file diff --git a/NBitcoin/Protocol/Payloads/BlockPayload.cs b/NBitcoin/Protocol/Payloads/BlockPayload.cs index c1da30998..cf8a9b8f7 100644 --- a/NBitcoin/Protocol/Payloads/BlockPayload.cs +++ b/NBitcoin/Protocol/Payloads/BlockPayload.cs @@ -9,7 +9,7 @@ namespace NBitcoin.Protocol /// /// A block received after being asked with a getdata message /// - [Payload("block")] + public class BlockPayload : BitcoinSerializablePayload { public BlockPayload() diff --git a/NBitcoin/Protocol/Payloads/BlockTxnPayload.cs b/NBitcoin/Protocol/Payloads/BlockTxnPayload.cs index 87352a390..2b5c7d4f7 100644 --- a/NBitcoin/Protocol/Payloads/BlockTxnPayload.cs +++ b/NBitcoin/Protocol/Payloads/BlockTxnPayload.cs @@ -6,7 +6,7 @@ namespace NBitcoin.Protocol { - [Payload("blocktxn")] + public class BlockTxnPayload : Payload { diff --git a/NBitcoin/Protocol/Payloads/CmpctBlockPayload.cs b/NBitcoin/Protocol/Payloads/CmpctBlockPayload.cs index 42b42bd62..aa7ff74e5 100644 --- a/NBitcoin/Protocol/Payloads/CmpctBlockPayload.cs +++ b/NBitcoin/Protocol/Payloads/CmpctBlockPayload.cs @@ -8,7 +8,7 @@ namespace NBitcoin.Protocol { - [Payload("cmpctblock")] + public class CmpctBlockPayload : Payload { public CmpctBlockPayload() diff --git a/NBitcoin/Protocol/Payloads/CompactFilterPayload.cs b/NBitcoin/Protocol/Payloads/CompactFilterPayload.cs index 8c3ea9381..e5e1a4f65 100644 --- a/NBitcoin/Protocol/Payloads/CompactFilterPayload.cs +++ b/NBitcoin/Protocol/Payloads/CompactFilterPayload.cs @@ -6,7 +6,7 @@ namespace NBitcoin.Protocol /// /// Represents the p2p message payload used for sharing a block's compact filter. /// - [Payload("cfilter")] + public class CompactFilterPayload : Payload { private byte _FilterType = (byte)FilterType.Basic; @@ -58,7 +58,7 @@ public CompactFilterPayload() } } - [Payload("cfcheckpt")] + public class CompactFilterCheckPointPayload : Payload { protected byte _FilterType = (byte)FilterType.Basic; @@ -109,7 +109,7 @@ public override void ReadWriteCore(BitcoinStream stream) } } - [Payload("cfheaders")] + public class CompactFilterHeadersPayload: CompactFilterCheckPointPayload { private uint256 _PreviousFilterHeader = uint256.Zero; diff --git a/NBitcoin/Protocol/Payloads/FeeFilterPayload.cs b/NBitcoin/Protocol/Payloads/FeeFilterPayload.cs index 9a7e3b7e1..02d2b17aa 100644 --- a/NBitcoin/Protocol/Payloads/FeeFilterPayload.cs +++ b/NBitcoin/Protocol/Payloads/FeeFilterPayload.cs @@ -3,7 +3,7 @@ namespace NBitcoin.Protocol { - [Payload("feefilter")] + public class FeeFilterPayload : Payload { public FeeFilterPayload() diff --git a/NBitcoin/Protocol/Payloads/FilterAddPayload.cs b/NBitcoin/Protocol/Payloads/FilterAddPayload.cs index 713ede622..62a47e09d 100644 --- a/NBitcoin/Protocol/Payloads/FilterAddPayload.cs +++ b/NBitcoin/Protocol/Payloads/FilterAddPayload.cs @@ -6,7 +6,7 @@ namespace NBitcoin.Protocol { - [Payload("filteradd")] + public class FilterAddPayload : Payload { public FilterAddPayload() diff --git a/NBitcoin/Protocol/Payloads/FilterLoadPayload.cs b/NBitcoin/Protocol/Payloads/FilterLoadPayload.cs index 0a6322300..fc9bb812a 100644 --- a/NBitcoin/Protocol/Payloads/FilterLoadPayload.cs +++ b/NBitcoin/Protocol/Payloads/FilterLoadPayload.cs @@ -9,7 +9,7 @@ namespace NBitcoin.Protocol /// /// Load a bloomfilter in the peer, used by SPV clients /// - [Payload("filterload")] + public class FilterLoadPayload : BitcoinSerializablePayload { public FilterLoadPayload() diff --git a/NBitcoin/Protocol/Payloads/GetAddrPayload.cs b/NBitcoin/Protocol/Payloads/GetAddrPayload.cs index a31855234..27792a349 100644 --- a/NBitcoin/Protocol/Payloads/GetAddrPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetAddrPayload.cs @@ -9,7 +9,7 @@ namespace NBitcoin.Protocol /// /// Ask for known peer addresses in the network /// - [Payload("getaddr")] + public class GetAddrPayload : Payload { } diff --git a/NBitcoin/Protocol/Payloads/GetBlockTxnPayload.cs b/NBitcoin/Protocol/Payloads/GetBlockTxnPayload.cs index fa375dd46..58345018e 100644 --- a/NBitcoin/Protocol/Payloads/GetBlockTxnPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetBlockTxnPayload.cs @@ -6,7 +6,7 @@ namespace NBitcoin.Protocol { - [Payload("getblocktxn")] + public class GetBlockTxnPayload : Payload { diff --git a/NBitcoin/Protocol/Payloads/GetBlocksPayload.cs b/NBitcoin/Protocol/Payloads/GetBlocksPayload.cs index eab51f4af..03e567c14 100644 --- a/NBitcoin/Protocol/Payloads/GetBlocksPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetBlocksPayload.cs @@ -9,7 +9,7 @@ namespace NBitcoin.Protocol /// /// Ask for the block hashes (inv) that happened since BlockLocators /// - [Payload("getblocks")] + public class GetBlocksPayload : Payload { public GetBlocksPayload(BlockLocator locator) diff --git a/NBitcoin/Protocol/Payloads/GetCompactFiltersPayload.cs b/NBitcoin/Protocol/Payloads/GetCompactFiltersPayload.cs index ea4187dca..1a54d423d 100644 --- a/NBitcoin/Protocol/Payloads/GetCompactFiltersPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetCompactFiltersPayload.cs @@ -62,7 +62,7 @@ public override void ReadWriteCore(BitcoinStream stream) /// /// Represents the p2p message payload used for requesting a range of compact filter. /// - [Payload("getcfilters")] + public class GetCompactFiltersPayload : CompactFiltersQueryPayload { public GetCompactFiltersPayload(FilterType filterType, uint startHeight, uint256 stopHash) @@ -76,7 +76,7 @@ public GetCompactFiltersPayload() { } /// /// Represents the p2p message payload used for requesting a range of compact filter headers. /// - [Payload("getcfheaders")] + public class GetCompactFilterHeadersPayload : CompactFiltersQueryPayload { public GetCompactFilterHeadersPayload(FilterType filterType, uint startHeight, uint256 stopHash) @@ -87,7 +87,7 @@ public GetCompactFilterHeadersPayload(FilterType filterType, uint startHeight, u public GetCompactFilterHeadersPayload() { } } - [Payload("getcfcheckpt")] + public class GetCompactFilterCheckPointPayload : Payload { private byte _FilterType; diff --git a/NBitcoin/Protocol/Payloads/GetDataPayload.cs b/NBitcoin/Protocol/Payloads/GetDataPayload.cs index 9673d79a1..b0030550c 100644 --- a/NBitcoin/Protocol/Payloads/GetDataPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetDataPayload.cs @@ -9,7 +9,7 @@ namespace NBitcoin.Protocol /// /// Ask for transaction, block or merkle block /// - [Payload("getdata")] + public class GetDataPayload : Payload { public GetDataPayload() diff --git a/NBitcoin/Protocol/Payloads/GetHeadersPayload.cs b/NBitcoin/Protocol/Payloads/GetHeadersPayload.cs index f152b182a..c5f3ea9a4 100644 --- a/NBitcoin/Protocol/Payloads/GetHeadersPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetHeadersPayload.cs @@ -9,7 +9,7 @@ namespace NBitcoin.Protocol /// /// Ask block headers that happened since BlockLocators /// - [Payload("getheaders")] + public class GetHeadersPayload : Payload { public GetHeadersPayload() diff --git a/NBitcoin/Protocol/Payloads/HaveWitnessPayload.cs b/NBitcoin/Protocol/Payloads/HaveWitnessPayload.cs index b7e3989d2..47fd343cb 100644 --- a/NBitcoin/Protocol/Payloads/HaveWitnessPayload.cs +++ b/NBitcoin/Protocol/Payloads/HaveWitnessPayload.cs @@ -6,7 +6,7 @@ namespace NBitcoin.Protocol { - [Payload("havewitness")] + public class HaveWitnessPayload : Payload { public HaveWitnessPayload() diff --git a/NBitcoin/Protocol/Payloads/HeadersPayload.cs b/NBitcoin/Protocol/Payloads/HeadersPayload.cs index 8e94046ba..86ec222bf 100644 --- a/NBitcoin/Protocol/Payloads/HeadersPayload.cs +++ b/NBitcoin/Protocol/Payloads/HeadersPayload.cs @@ -10,7 +10,7 @@ namespace NBitcoin.Protocol /// /// Block headers received after a getheaders messages /// - [Payload("headers")] + public class HeadersPayload : Payload { class BlockHeaderWithTxCount : IBitcoinSerializable diff --git a/NBitcoin/Protocol/Payloads/InvPayload.cs b/NBitcoin/Protocol/Payloads/InvPayload.cs index 73117e7de..6e5efcc3f 100644 --- a/NBitcoin/Protocol/Payloads/InvPayload.cs +++ b/NBitcoin/Protocol/Payloads/InvPayload.cs @@ -9,7 +9,7 @@ namespace NBitcoin.Protocol /// /// Announce the hash of a transaction or block /// - [Payload("inv")] + public class InvPayload : Payload, IBitcoinSerializable, IEnumerable { public InvPayload() diff --git a/NBitcoin/Protocol/Payloads/MempoolPayload.cs b/NBitcoin/Protocol/Payloads/MempoolPayload.cs index 5ed87ceae..a34e1d242 100644 --- a/NBitcoin/Protocol/Payloads/MempoolPayload.cs +++ b/NBitcoin/Protocol/Payloads/MempoolPayload.cs @@ -9,7 +9,7 @@ namespace NBitcoin.Protocol /// /// Ask for the mempool, followed by inv messages /// - [Payload("mempool")] + public class MempoolPayload : Payload { } diff --git a/NBitcoin/Protocol/Payloads/MerkleBlockPayload.cs b/NBitcoin/Protocol/Payloads/MerkleBlockPayload.cs index 2e72f8eb3..6c8e4eaee 100644 --- a/NBitcoin/Protocol/Payloads/MerkleBlockPayload.cs +++ b/NBitcoin/Protocol/Payloads/MerkleBlockPayload.cs @@ -9,7 +9,7 @@ namespace NBitcoin.Protocol /// /// A merkle block received after being asked with a getdata message /// - [Payload("merkleblock")] + public class MerkleBlockPayload : BitcoinSerializablePayload { public MerkleBlockPayload() diff --git a/NBitcoin/Protocol/Payloads/PingPayload.cs b/NBitcoin/Protocol/Payloads/PingPayload.cs index c4d76223b..425d54c97 100644 --- a/NBitcoin/Protocol/Payloads/PingPayload.cs +++ b/NBitcoin/Protocol/Payloads/PingPayload.cs @@ -6,7 +6,7 @@ namespace NBitcoin.Protocol { - [Payload("ping")] + public class PingPayload : Payload { diff --git a/NBitcoin/Protocol/Payloads/PongPayload.cs b/NBitcoin/Protocol/Payloads/PongPayload.cs index 27a4f0ba7..7968e82e3 100644 --- a/NBitcoin/Protocol/Payloads/PongPayload.cs +++ b/NBitcoin/Protocol/Payloads/PongPayload.cs @@ -6,7 +6,7 @@ namespace NBitcoin.Protocol { - [Payload("pong")] + public class PongPayload : Payload { private ulong _Nonce; diff --git a/NBitcoin/Protocol/Payloads/SendAddrV2Payload.cs b/NBitcoin/Protocol/Payloads/SendAddrV2Payload.cs index 9c75464b8..872bb9726 100644 --- a/NBitcoin/Protocol/Payloads/SendAddrV2Payload.cs +++ b/NBitcoin/Protocol/Payloads/SendAddrV2Payload.cs @@ -9,7 +9,7 @@ namespace NBitcoin.Protocol /// /// Ask for known peer addresses in the network /// - [Payload("sendaddrv2")] + public class SendAddrV2Payload : Payload { } diff --git a/NBitcoin/Protocol/Payloads/SendCmpctPayload.cs b/NBitcoin/Protocol/Payloads/SendCmpctPayload.cs index 36cb3fb59..985386f2e 100644 --- a/NBitcoin/Protocol/Payloads/SendCmpctPayload.cs +++ b/NBitcoin/Protocol/Payloads/SendCmpctPayload.cs @@ -6,7 +6,7 @@ namespace NBitcoin.Protocol { - [Payload("sendcmpct")] + public class SendCmpctPayload : Payload { public SendCmpctPayload() diff --git a/NBitcoin/Protocol/Payloads/SendHeadersPayload.cs b/NBitcoin/Protocol/Payloads/SendHeadersPayload.cs index 2d1b07fbe..9b390756d 100644 --- a/NBitcoin/Protocol/Payloads/SendHeadersPayload.cs +++ b/NBitcoin/Protocol/Payloads/SendHeadersPayload.cs @@ -6,7 +6,7 @@ namespace NBitcoin.Protocol { - [Payload("sendheaders")] + public class SendHeadersPayload : Payload { } diff --git a/NBitcoin/Protocol/Payloads/TxPayload.cs b/NBitcoin/Protocol/Payloads/TxPayload.cs index 6e751e75a..39fdffc8d 100644 --- a/NBitcoin/Protocol/Payloads/TxPayload.cs +++ b/NBitcoin/Protocol/Payloads/TxPayload.cs @@ -9,7 +9,7 @@ namespace NBitcoin.Protocol /// /// Represents a transaction being sent on the network, is sent after being requested by a getdata (of Transaction or MerkleBlock) message. /// - [Payload("tx")] + public class TxPayload : Payload { public TxPayload() diff --git a/NBitcoin/Protocol/Payloads/UTxOutputPayload.cs b/NBitcoin/Protocol/Payloads/UTxOutputPayload.cs index 13fbde86b..64bc7219c 100644 --- a/NBitcoin/Protocol/Payloads/UTxOutputPayload.cs +++ b/NBitcoin/Protocol/Payloads/UTxOutputPayload.cs @@ -2,7 +2,7 @@ namespace NBitcoin.Protocol.Payloads { - [Payload("utxos")] + public class UTxOutputPayload : Payload { private UTxOutputs _uTxOutputs; @@ -23,45 +23,45 @@ public class UTxOutputs : IBitcoinSerializable public int ChainHeight { - get - { - return _chainHeight; + get + { + return _chainHeight; } - internal set - { - _chainHeight = value; + internal set + { + _chainHeight = value; } } public uint256 ChainTipHash { - get - { - return _chainTipHash; + get + { + return _chainTipHash; } - internal set - { - _chainTipHash = value; + internal set + { + _chainTipHash = value; } } public BitArray Bitmap { - get - { - return new BitArray(_bitmap.ToBytes()); + get + { + return new BitArray(_bitmap.ToBytes()); } } public UTxOut[] Outputs { - get - { - return _outputs; + get + { + return _outputs; } - internal set - { - _outputs = value; + internal set + { + _outputs = value; } } @@ -82,37 +82,37 @@ public class UTxOut : IBitcoinSerializable public uint Version { - get - { - return _version; + get + { + return _version; } - internal set - { - _version = value; + internal set + { + _version = value; } } public uint Height { - get - { - return _height; + get + { + return _height; } - internal set - { - _height = value; + internal set + { + _height = value; } } public TxOut Output { - get - { - return _txOut; + get + { + return _txOut; } - internal set - { - _txOut = value; + internal set + { + _txOut = value; } } diff --git a/NBitcoin/Protocol/Payloads/VerAckPayload.cs b/NBitcoin/Protocol/Payloads/VerAckPayload.cs index 0fe8b5941..70b885744 100644 --- a/NBitcoin/Protocol/Payloads/VerAckPayload.cs +++ b/NBitcoin/Protocol/Payloads/VerAckPayload.cs @@ -6,7 +6,7 @@ namespace NBitcoin.Protocol { - [Payload("verack")] + public class VerAckPayload : Payload, IBitcoinSerializable { #region IBitcoinSerializable Members diff --git a/NBitcoin/Protocol/Payloads/VersionPayload.cs b/NBitcoin/Protocol/Payloads/VersionPayload.cs index 216cc6613..9f4485de6 100644 --- a/NBitcoin/Protocol/Payloads/VersionPayload.cs +++ b/NBitcoin/Protocol/Payloads/VersionPayload.cs @@ -54,7 +54,7 @@ public enum NodeServices : ulong /// NODE_NETWORK_LIMITED = (1 << 10) } - [Payload("version")] + public class VersionPayload : Payload, IBitcoinSerializable { static string _NUserAgent; diff --git a/NBitcoin/Protocol/Payloads/WTxIdRelayPayload.cs b/NBitcoin/Protocol/Payloads/WTxIdRelayPayload.cs index cd7db5296..9fa1771e5 100644 --- a/NBitcoin/Protocol/Payloads/WTxIdRelayPayload.cs +++ b/NBitcoin/Protocol/Payloads/WTxIdRelayPayload.cs @@ -3,7 +3,7 @@ namespace NBitcoin.Protocol /// /// Signaling message about WTXID support (see BIP339) /// - [Payload("wtxidrelay")] + public class WTxIdRelayPayload : Payload { } From 7e2d8b1521a57ac6b41878f8b4145cddd0a1f3e9 Mon Sep 17 00:00:00 2001 From: Lucas Ontivero Date: Tue, 9 Jan 2024 22:01:32 -0300 Subject: [PATCH 5/7] Make PayloadFactory a singleton --- NBitcoin/Protocol/Message.cs | 2 +- NBitcoin/Protocol/Payload.cs | 2 +- NBitcoin/Protocol/PayloadFactory.cs | 11 +++++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/NBitcoin/Protocol/Message.cs b/NBitcoin/Protocol/Message.cs index fe24280f2..06a527027 100644 --- a/NBitcoin/Protocol/Message.cs +++ b/NBitcoin/Protocol/Message.cs @@ -130,7 +130,7 @@ public void ReadWrite(BitcoinStream stream) BitcoinStream payloadStream = new BitcoinStream(new MemoryStream(payloadBytes, 0, length, false), false); payloadStream.CopyParameters(stream); - var payload = PayloadFactory.Create(Command); + var payload = PayloadFactory.Instance.Create(Command); if (payload is UnknownPayload) Logs.NodeServer.LogWarning("Unknown command received {command}", Command); diff --git a/NBitcoin/Protocol/Payload.cs b/NBitcoin/Protocol/Payload.cs index 9ebfef93e..be9036fa9 100644 --- a/NBitcoin/Protocol/Payload.cs +++ b/NBitcoin/Protocol/Payload.cs @@ -12,7 +12,7 @@ public virtual string Command { get { - return PayloadFactory.GetCommand(this); + return PayloadFactory.Instance.GetCommand(this); } } diff --git a/NBitcoin/Protocol/PayloadFactory.cs b/NBitcoin/Protocol/PayloadFactory.cs index 74a05ccd8..ed1704229 100644 --- a/NBitcoin/Protocol/PayloadFactory.cs +++ b/NBitcoin/Protocol/PayloadFactory.cs @@ -5,7 +5,14 @@ namespace NBitcoin.Protocol { public class PayloadFactory { - public static Payload Create(string command) + public static readonly PayloadFactory Instance = new PayloadFactory(); + + private PayloadFactory() + { + } + + + public Payload Create(string command) { switch (command) { @@ -49,7 +56,7 @@ public static Payload Create(string command) } } - public static string GetCommand(Payload payload) + public string GetCommand(Payload payload) { #if !NOSOCKET if (payload is AddrV2Payload) return "addrv2"; From d91b67f9aa9f115759dddde88a5b6de75e1b6aea Mon Sep 17 00:00:00 2001 From: Lucas Ontivero Date: Tue, 9 Jan 2024 22:02:11 -0300 Subject: [PATCH 6/7] Use `switch` and pattern matching --- NBitcoin/Protocol/PayloadFactory.cs | 147 ++++++++++++++-------------- 1 file changed, 75 insertions(+), 72 deletions(-) diff --git a/NBitcoin/Protocol/PayloadFactory.cs b/NBitcoin/Protocol/PayloadFactory.cs index ed1704229..8d564a386 100644 --- a/NBitcoin/Protocol/PayloadFactory.cs +++ b/NBitcoin/Protocol/PayloadFactory.cs @@ -14,87 +14,90 @@ private PayloadFactory() public Payload Create(string command) { - switch (command) + return command switch { #if !NOSOCKET - case "addr": return new AddrPayload(); - case "addrv2": return new AddrV2Payload(); - case "version": return new VersionPayload(); + "addr" => new AddrPayload(), + "addrv2" => new AddrV2Payload(), + "version" => new VersionPayload(), #endif - case "block": return new BlockPayload(); - case "blocktxn": return new BlockTxnPayload(); - case "cmpctblock": return new CmpctBlockPayload(); - case "cfilter": return new CompactFilterPayload(); - case "cfcheckpt": return new CompactFilterCheckPointPayload(); - case "cfheaders": return new CompactFilterHeadersPayload(); - case "feefilter": return new FeeFilterPayload(); - case "filteradd": return new FilterAddPayload(); - case "filterload": return new FilterLoadPayload(); - case "getaddr": return new GetAddrPayload(); - case "getblocktxn": return new GetBlockTxnPayload(); - case "getblocks": return new GetBlocksPayload(); - case "getcfilters": return new GetCompactFiltersPayload(); - case "getcfheaders": return new GetCompactFilterHeadersPayload(); - case "getcfcheckpt": return new GetCompactFilterCheckPointPayload(); - case "getdata": return new GetDataPayload(); - case "getheaders": return new GetHeadersPayload(); - case "havewitness": return new HaveWitnessPayload(); - case "headers": return new HeadersPayload(); - case "inv": return new InvPayload(); - case "mempool": return new MempoolPayload(); - case "merkleblock": return new MerkleBlockPayload(); - case "ping": return new PingPayload(); - case "pong": return new PongPayload(); - case "sendaddrv2": return new SendAddrV2Payload(); - case "sendcmpct": return new SendCmpctPayload(); - case "sendheaders": return new SendHeadersPayload(); - case "tx": return new TxPayload(); - case "utxos": return new UTxOutputPayload(); - case "verack": return new VerAckPayload(); - case "wtxidrelay": return new WTxIdRelayPayload(); - default: return new UnknownPayload(command); - } + "ping" => new PingPayload(), + "pong" => new PongPayload(), + "inv" => new InvPayload(), + "getdata" => new GetDataPayload(), + "tx" => new TxPayload(), + "getaddr" => new GetAddrPayload(), + "headers" => new HeadersPayload(), + "block" => new BlockPayload(), + "blocktxn" => new BlockTxnPayload(), + "cmpctblock" => new CmpctBlockPayload(), + "cfilter" => new CompactFilterPayload(), + "cfcheckpt" => new CompactFilterCheckPointPayload(), + "cfheaders" => new CompactFilterHeadersPayload(), + "feefilter" => new FeeFilterPayload(), + "filteradd" => new FilterAddPayload(), + "filterload" => new FilterLoadPayload(), + "getblocktxn" => new GetBlockTxnPayload(), + "getblocks" => new GetBlocksPayload(), + "getcfilters" => new GetCompactFiltersPayload(), + "getcfheaders" => new GetCompactFilterHeadersPayload(), + "getcfcheckpt" => new GetCompactFilterCheckPointPayload(), + "getheaders" => new GetHeadersPayload(), + "havewitness" => new HaveWitnessPayload(), + "mempool" => new MempoolPayload(), + "merkleblock" => new MerkleBlockPayload(), + "sendaddrv2" => new SendAddrV2Payload(), + "sendcmpct" => new SendCmpctPayload(), + "sendheaders" => new SendHeadersPayload(), + "utxos" => new UTxOutputPayload(), + "verack" => new VerAckPayload(), + "wtxidrelay" => new WTxIdRelayPayload(), + _ => new UnknownPayload(command) + }; } public string GetCommand(Payload payload) { + return payload switch + { #if !NOSOCKET - if (payload is AddrV2Payload) return "addrv2"; - if (payload is AddrPayload) return "addr"; - if (payload is VersionPayload) return "version"; + AddrV2Payload => "addrv2", + AddrPayload => "addr", + VersionPayload => "version", #endif - if (payload is BlockPayload) return "block"; - if (payload is BlockTxnPayload) return "blocktxn"; - if (payload is CmpctBlockPayload) return "cmpctblock"; - if (payload is CompactFilterPayload) return "cfilter"; - if (payload is CompactFilterHeadersPayload) return "cfheaders"; - if (payload is CompactFilterCheckPointPayload) return "cfcheckpt"; - if (payload is FeeFilterPayload) return "feefilter"; - if (payload is FilterAddPayload) return "filteradd"; - if (payload is FilterLoadPayload) return "filterload"; - if (payload is GetAddrPayload) return "getaddr"; - if (payload is GetBlockTxnPayload) return "getblocktxn"; - if (payload is GetBlocksPayload) return "getblocks"; - if (payload is GetCompactFiltersPayload) return "getcfilters"; - if (payload is GetCompactFilterHeadersPayload) return "getcfheaders"; - if (payload is GetCompactFilterCheckPointPayload) return "getcfcheckpt"; - if (payload is GetDataPayload) return "getdata"; - if (payload is GetHeadersPayload) return "getheaders"; - if (payload is HaveWitnessPayload) return "havewitness"; - if (payload is HeadersPayload) return "headers"; - if (payload is InvPayload) return "inv"; - if (payload is MempoolPayload) return "mempool"; - if (payload is MerkleBlockPayload) return "merkleblock"; - if (payload is PingPayload) return "ping"; - if (payload is PongPayload) return "pong"; - if (payload is SendAddrV2Payload) return "sendaddrv2"; - if (payload is SendCmpctPayload) return "sendcmpct"; - if (payload is SendHeadersPayload) return "sendheaders"; - if (payload is TxPayload) return "tx"; - if (payload is UTxOutputPayload) return "utxos"; - if (payload is VerAckPayload) return "verack"; - if (payload is WTxIdRelayPayload) return "wtxidrelay"; - throw new NotSupportedException($"Unknown command for Payload type: {payload.GetType().Name}"); + PingPayload => "ping", + PongPayload => "pong", + InvPayload => "inv", + TxPayload => "tx", + GetDataPayload => "getdata", + HeadersPayload => "headers", + BlockPayload => "block", + BlockTxnPayload => "blocktxn", + CmpctBlockPayload => "cmpctblock", + CompactFilterPayload => "cfilter", + CompactFilterHeadersPayload => "cfheaders", + CompactFilterCheckPointPayload => "cfcheckpt", + FeeFilterPayload => "feefilter", + FilterAddPayload => "filteradd", + FilterLoadPayload => "filterload", + GetAddrPayload => "getaddr", + GetBlockTxnPayload => "getblocktxn", + GetBlocksPayload => "getblocks", + GetCompactFiltersPayload => "getcfilters", + GetCompactFilterHeadersPayload => "getcfheaders", + GetCompactFilterCheckPointPayload => "getcfcheckpt", + GetHeadersPayload => "getheaders", + HaveWitnessPayload => "havewitness", + MempoolPayload => "mempool", + MerkleBlockPayload => "merkleblock", + SendAddrV2Payload => "sendaddrv2", + SendCmpctPayload => "sendcmpct", + SendHeadersPayload => "sendheaders", + UTxOutputPayload => "utxos", + VerAckPayload => "verack", + WTxIdRelayPayload => "wtxidrelay", + _ => throw new NotSupportedException($"Unknown command for Payload type: {payload.GetType().Name}") + }; } } } From a82fd30f89e8431ea3da1cdf8e512f8b16771d18 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Wed, 10 Jan 2024 15:41:57 +0900 Subject: [PATCH 7/7] Allow override of CreatePayload --- NBitcoin/ConsensusFactory.cs | 44 ++++++++ .../Protocol/BitcoinSerializablePayload.cs | 2 +- NBitcoin/Protocol/Message.cs | 3 +- NBitcoin/Protocol/Payload.cs | 14 +-- NBitcoin/Protocol/PayloadFactory.cs | 103 ------------------ NBitcoin/Protocol/Payloads/AddrPayload.cs | 4 +- NBitcoin/Protocol/Payloads/BlockPayload.cs | 1 + NBitcoin/Protocol/Payloads/BlockTxnPayload.cs | 1 + .../Protocol/Payloads/CmpctBlockPayload.cs | 1 + .../Protocol/Payloads/CompactFilterPayload.cs | 3 + .../Protocol/Payloads/FeeFilterPayload.cs | 1 + .../Protocol/Payloads/FilterAddPayload.cs | 1 + .../Protocol/Payloads/FilterLoadPayload.cs | 1 + NBitcoin/Protocol/Payloads/GetAddrPayload.cs | 1 + .../Protocol/Payloads/GetBlockTxnPayload.cs | 1 + .../Protocol/Payloads/GetBlocksPayload.cs | 1 + .../Payloads/GetCompactFiltersPayload.cs | 3 + NBitcoin/Protocol/Payloads/GetDataPayload.cs | 1 + .../Protocol/Payloads/GetHeadersPayload.cs | 1 + .../Protocol/Payloads/HaveWitnessPayload.cs | 1 + NBitcoin/Protocol/Payloads/HeadersPayload.cs | 1 + NBitcoin/Protocol/Payloads/InvPayload.cs | 1 + NBitcoin/Protocol/Payloads/MempoolPayload.cs | 1 + .../Protocol/Payloads/MerkleBlockPayload.cs | 1 + NBitcoin/Protocol/Payloads/NotFoundPayload.cs | 1 + NBitcoin/Protocol/Payloads/PingPayload.cs | 2 +- NBitcoin/Protocol/Payloads/PongPayload.cs | 1 + .../Protocol/Payloads/SendAddrV2Payload.cs | 1 + .../Protocol/Payloads/SendCmpctPayload.cs | 1 + .../Protocol/Payloads/SendHeadersPayload.cs | 1 + NBitcoin/Protocol/Payloads/TxPayload.cs | 1 + .../Protocol/Payloads/UTxOutputPayload.cs | 3 +- NBitcoin/Protocol/Payloads/VerAckPayload.cs | 1 + NBitcoin/Protocol/Payloads/VersionPayload.cs | 3 +- .../Protocol/Payloads/WTxIdRelayPayload.cs | 1 + NBitcoin/RPC/RestClient.cs | 1 - 36 files changed, 91 insertions(+), 118 deletions(-) delete mode 100644 NBitcoin/Protocol/PayloadFactory.cs diff --git a/NBitcoin/ConsensusFactory.cs b/NBitcoin/ConsensusFactory.cs index 30c5d0d7a..3677a7f68 100644 --- a/NBitcoin/ConsensusFactory.cs +++ b/NBitcoin/ConsensusFactory.cs @@ -79,6 +79,50 @@ public bool TryCreateNew(out T result) where T : IBitcoinSerializable return success; } + public virtual Payload CreatePayload(string command) + { + return command switch + { + "inv" => new InvPayload(), + "tx" => new TxPayload(), + "getdata" => new GetDataPayload(), + "headers" => new HeadersPayload(), + "block" => new BlockPayload(), +#if !NOSOCKET + "addr" => new AddrPayload(), + "addrv2" => new AddrV2Payload(), + "version" => new VersionPayload(), +#endif + "ping" => new PingPayload(), + "pong" => new PongPayload(), + "getaddr" => new GetAddrPayload(), + "blocktxn" => new BlockTxnPayload(), + "cmpctblock" => new CmpctBlockPayload(), + "cfilter" => new CompactFilterPayload(), + "cfcheckpt" => new CompactFilterCheckPointPayload(), + "cfheaders" => new CompactFilterHeadersPayload(), + "feefilter" => new FeeFilterPayload(), + "filteradd" => new FilterAddPayload(), + "filterload" => new FilterLoadPayload(), + "getblocktxn" => new GetBlockTxnPayload(), + "getblocks" => new GetBlocksPayload(), + "getcfilters" => new GetCompactFiltersPayload(), + "getcfheaders" => new GetCompactFilterHeadersPayload(), + "getcfcheckpt" => new GetCompactFilterCheckPointPayload(), + "getheaders" => new GetHeadersPayload(), + "havewitness" => new HaveWitnessPayload(), + "mempool" => new MempoolPayload(), + "merkleblock" => new MerkleBlockPayload(), + "sendaddrv2" => new SendAddrV2Payload(), + "sendcmpct" => new SendCmpctPayload(), + "sendheaders" => new SendHeadersPayload(), + "utxos" => new UTxOutputPayload(), + "verack" => new VerAckPayload(), + "wtxidrelay" => new WTxIdRelayPayload(), + _ => new UnknownPayload(command) + }; + } + public virtual ProtocolCapabilities GetProtocolCapabilities(uint protocolVersion) { return new ProtocolCapabilities() diff --git a/NBitcoin/Protocol/BitcoinSerializablePayload.cs b/NBitcoin/Protocol/BitcoinSerializablePayload.cs index c99ec4401..ba668cf13 100644 --- a/NBitcoin/Protocol/BitcoinSerializablePayload.cs +++ b/NBitcoin/Protocol/BitcoinSerializablePayload.cs @@ -6,7 +6,7 @@ namespace NBitcoin.Protocol { - public class BitcoinSerializablePayload : Payload where T : IBitcoinSerializable, new() + public abstract class BitcoinSerializablePayload : Payload where T : IBitcoinSerializable, new() { public BitcoinSerializablePayload() { diff --git a/NBitcoin/Protocol/Message.cs b/NBitcoin/Protocol/Message.cs index 06a527027..b31f31fef 100644 --- a/NBitcoin/Protocol/Message.cs +++ b/NBitcoin/Protocol/Message.cs @@ -72,7 +72,6 @@ public bool IfPayloadIs(Action action) where TPayload : Payl // We use this for big blocks, because the default array pool would allocate a new array. We do not need lot's of bucket such arrays are short lived. readonly static Lazy> BigArrayPool = new Lazy>(() => ArrayPool.Create(0x02000000, 5), false); ArrayPool GetArrayPool(int size) => size < 1_048_576 ? ArrayPool.Shared : BigArrayPool.Value; - public void ReadWrite(BitcoinStream stream) { if (Payload == null && stream.Serializing) @@ -130,7 +129,7 @@ public void ReadWrite(BitcoinStream stream) BitcoinStream payloadStream = new BitcoinStream(new MemoryStream(payloadBytes, 0, length, false), false); payloadStream.CopyParameters(stream); - var payload = PayloadFactory.Instance.Create(Command); + var payload = stream.ConsensusFactory.CreatePayload(Command); if (payload is UnknownPayload) Logs.NodeServer.LogWarning("Unknown command received {command}", Command); diff --git a/NBitcoin/Protocol/Payload.cs b/NBitcoin/Protocol/Payload.cs index be9036fa9..0f878154e 100644 --- a/NBitcoin/Protocol/Payload.cs +++ b/NBitcoin/Protocol/Payload.cs @@ -6,14 +6,14 @@ namespace NBitcoin.Protocol { - public class Payload : IBitcoinSerializable + /// + /// A P2P Bitcoin payload + /// + public abstract class Payload : IBitcoinSerializable { - public virtual string Command + public abstract string Command { - get - { - return PayloadFactory.Instance.GetCommand(this); - } + get; } #region IBitcoinSerializable Members @@ -34,7 +34,7 @@ public virtual void ReadWriteCore(BitcoinStream stream) public override string ToString() { - return this.GetType().Name; + return Command; } } } diff --git a/NBitcoin/Protocol/PayloadFactory.cs b/NBitcoin/Protocol/PayloadFactory.cs deleted file mode 100644 index 8d564a386..000000000 --- a/NBitcoin/Protocol/PayloadFactory.cs +++ /dev/null @@ -1,103 +0,0 @@ -using System; -using NBitcoin.Protocol.Payloads; - -namespace NBitcoin.Protocol -{ - public class PayloadFactory - { - public static readonly PayloadFactory Instance = new PayloadFactory(); - - private PayloadFactory() - { - } - - - public Payload Create(string command) - { - return command switch - { -#if !NOSOCKET - "addr" => new AddrPayload(), - "addrv2" => new AddrV2Payload(), - "version" => new VersionPayload(), -#endif - "ping" => new PingPayload(), - "pong" => new PongPayload(), - "inv" => new InvPayload(), - "getdata" => new GetDataPayload(), - "tx" => new TxPayload(), - "getaddr" => new GetAddrPayload(), - "headers" => new HeadersPayload(), - "block" => new BlockPayload(), - "blocktxn" => new BlockTxnPayload(), - "cmpctblock" => new CmpctBlockPayload(), - "cfilter" => new CompactFilterPayload(), - "cfcheckpt" => new CompactFilterCheckPointPayload(), - "cfheaders" => new CompactFilterHeadersPayload(), - "feefilter" => new FeeFilterPayload(), - "filteradd" => new FilterAddPayload(), - "filterload" => new FilterLoadPayload(), - "getblocktxn" => new GetBlockTxnPayload(), - "getblocks" => new GetBlocksPayload(), - "getcfilters" => new GetCompactFiltersPayload(), - "getcfheaders" => new GetCompactFilterHeadersPayload(), - "getcfcheckpt" => new GetCompactFilterCheckPointPayload(), - "getheaders" => new GetHeadersPayload(), - "havewitness" => new HaveWitnessPayload(), - "mempool" => new MempoolPayload(), - "merkleblock" => new MerkleBlockPayload(), - "sendaddrv2" => new SendAddrV2Payload(), - "sendcmpct" => new SendCmpctPayload(), - "sendheaders" => new SendHeadersPayload(), - "utxos" => new UTxOutputPayload(), - "verack" => new VerAckPayload(), - "wtxidrelay" => new WTxIdRelayPayload(), - _ => new UnknownPayload(command) - }; - } - - public string GetCommand(Payload payload) - { - return payload switch - { -#if !NOSOCKET - AddrV2Payload => "addrv2", - AddrPayload => "addr", - VersionPayload => "version", -#endif - PingPayload => "ping", - PongPayload => "pong", - InvPayload => "inv", - TxPayload => "tx", - GetDataPayload => "getdata", - HeadersPayload => "headers", - BlockPayload => "block", - BlockTxnPayload => "blocktxn", - CmpctBlockPayload => "cmpctblock", - CompactFilterPayload => "cfilter", - CompactFilterHeadersPayload => "cfheaders", - CompactFilterCheckPointPayload => "cfcheckpt", - FeeFilterPayload => "feefilter", - FilterAddPayload => "filteradd", - FilterLoadPayload => "filterload", - GetAddrPayload => "getaddr", - GetBlockTxnPayload => "getblocktxn", - GetBlocksPayload => "getblocks", - GetCompactFiltersPayload => "getcfilters", - GetCompactFilterHeadersPayload => "getcfheaders", - GetCompactFilterCheckPointPayload => "getcfcheckpt", - GetHeadersPayload => "getheaders", - HaveWitnessPayload => "havewitness", - MempoolPayload => "mempool", - MerkleBlockPayload => "merkleblock", - SendAddrV2Payload => "sendaddrv2", - SendCmpctPayload => "sendcmpct", - SendHeadersPayload => "sendheaders", - UTxOutputPayload => "utxos", - VerAckPayload => "verack", - WTxIdRelayPayload => "wtxidrelay", - _ => throw new NotSupportedException($"Unknown command for Payload type: {payload.GetType().Name}") - }; - } - } -} diff --git a/NBitcoin/Protocol/Payloads/AddrPayload.cs b/NBitcoin/Protocol/Payloads/AddrPayload.cs index e8bb17da1..36ebc2c49 100644 --- a/NBitcoin/Protocol/Payloads/AddrPayload.cs +++ b/NBitcoin/Protocol/Payloads/AddrPayload.cs @@ -13,6 +13,7 @@ namespace NBitcoin.Protocol public class AddrPayload : Payload, IBitcoinSerializable { + public override string Command => "addr"; NetworkAddress[] addr_list = new NetworkAddress[0]; public NetworkAddress[] Addresses { @@ -56,6 +57,7 @@ public override string ToString() public class AddrV2Payload : AddrPayload { + public override string Command => "addrv2"; public AddrV2Payload() : base() { @@ -79,4 +81,4 @@ public override void ReadWriteCore(BitcoinStream stream) } } } -#endif \ No newline at end of file +#endif diff --git a/NBitcoin/Protocol/Payloads/BlockPayload.cs b/NBitcoin/Protocol/Payloads/BlockPayload.cs index cf8a9b8f7..6a022808e 100644 --- a/NBitcoin/Protocol/Payloads/BlockPayload.cs +++ b/NBitcoin/Protocol/Payloads/BlockPayload.cs @@ -12,6 +12,7 @@ namespace NBitcoin.Protocol public class BlockPayload : BitcoinSerializablePayload { + public override string Command => "block"; public BlockPayload() { diff --git a/NBitcoin/Protocol/Payloads/BlockTxnPayload.cs b/NBitcoin/Protocol/Payloads/BlockTxnPayload.cs index 2b5c7d4f7..4c322e131 100644 --- a/NBitcoin/Protocol/Payloads/BlockTxnPayload.cs +++ b/NBitcoin/Protocol/Payloads/BlockTxnPayload.cs @@ -9,6 +9,7 @@ namespace NBitcoin.Protocol public class BlockTxnPayload : Payload { + public override string Command => "blocktxn"; uint256 _BlockId; public uint256 BlockId diff --git a/NBitcoin/Protocol/Payloads/CmpctBlockPayload.cs b/NBitcoin/Protocol/Payloads/CmpctBlockPayload.cs index aa7ff74e5..6838ebb72 100644 --- a/NBitcoin/Protocol/Payloads/CmpctBlockPayload.cs +++ b/NBitcoin/Protocol/Payloads/CmpctBlockPayload.cs @@ -11,6 +11,7 @@ namespace NBitcoin.Protocol public class CmpctBlockPayload : Payload { + public override string Command => "cmpctblock"; public CmpctBlockPayload() { diff --git a/NBitcoin/Protocol/Payloads/CompactFilterPayload.cs b/NBitcoin/Protocol/Payloads/CompactFilterPayload.cs index e5e1a4f65..b1e2f7583 100644 --- a/NBitcoin/Protocol/Payloads/CompactFilterPayload.cs +++ b/NBitcoin/Protocol/Payloads/CompactFilterPayload.cs @@ -9,6 +9,7 @@ namespace NBitcoin.Protocol public class CompactFilterPayload : Payload { + public override string Command => "cfilter"; private byte _FilterType = (byte)FilterType.Basic; private byte[] _FilterBytes; private uint256 _BlockHash = new uint256(); @@ -61,6 +62,7 @@ public CompactFilterPayload() public class CompactFilterCheckPointPayload : Payload { + public override string Command => "cfcheckpt"; protected byte _FilterType = (byte)FilterType.Basic; protected uint256 _StopHash = uint256.Zero; protected List _FilterHeaders = new List(); @@ -112,6 +114,7 @@ public override void ReadWriteCore(BitcoinStream stream) public class CompactFilterHeadersPayload: CompactFilterCheckPointPayload { + public override string Command => "cfheaders"; private uint256 _PreviousFilterHeader = uint256.Zero; public override void ReadWriteCore(BitcoinStream stream) diff --git a/NBitcoin/Protocol/Payloads/FeeFilterPayload.cs b/NBitcoin/Protocol/Payloads/FeeFilterPayload.cs index 02d2b17aa..2a118076e 100644 --- a/NBitcoin/Protocol/Payloads/FeeFilterPayload.cs +++ b/NBitcoin/Protocol/Payloads/FeeFilterPayload.cs @@ -6,6 +6,7 @@ namespace NBitcoin.Protocol public class FeeFilterPayload : Payload { + public override string Command => "feefilter"; public FeeFilterPayload() { _feeRate = FeeRate.Zero; diff --git a/NBitcoin/Protocol/Payloads/FilterAddPayload.cs b/NBitcoin/Protocol/Payloads/FilterAddPayload.cs index 62a47e09d..a86c1258d 100644 --- a/NBitcoin/Protocol/Payloads/FilterAddPayload.cs +++ b/NBitcoin/Protocol/Payloads/FilterAddPayload.cs @@ -9,6 +9,7 @@ namespace NBitcoin.Protocol public class FilterAddPayload : Payload { + public override string Command => "filteradd"; public FilterAddPayload() { diff --git a/NBitcoin/Protocol/Payloads/FilterLoadPayload.cs b/NBitcoin/Protocol/Payloads/FilterLoadPayload.cs index fc9bb812a..e0899fa75 100644 --- a/NBitcoin/Protocol/Payloads/FilterLoadPayload.cs +++ b/NBitcoin/Protocol/Payloads/FilterLoadPayload.cs @@ -12,6 +12,7 @@ namespace NBitcoin.Protocol public class FilterLoadPayload : BitcoinSerializablePayload { + public override string Command => "filterload"; public FilterLoadPayload() { diff --git a/NBitcoin/Protocol/Payloads/GetAddrPayload.cs b/NBitcoin/Protocol/Payloads/GetAddrPayload.cs index 27792a349..4803c218f 100644 --- a/NBitcoin/Protocol/Payloads/GetAddrPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetAddrPayload.cs @@ -12,5 +12,6 @@ namespace NBitcoin.Protocol public class GetAddrPayload : Payload { + public override string Command => "getaddr"; } } diff --git a/NBitcoin/Protocol/Payloads/GetBlockTxnPayload.cs b/NBitcoin/Protocol/Payloads/GetBlockTxnPayload.cs index 58345018e..cf66e85f8 100644 --- a/NBitcoin/Protocol/Payloads/GetBlockTxnPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetBlockTxnPayload.cs @@ -9,6 +9,7 @@ namespace NBitcoin.Protocol public class GetBlockTxnPayload : Payload { + public override string Command => "getblocktxn"; uint256 _BlockId = uint256.Zero; public uint256 BlockId diff --git a/NBitcoin/Protocol/Payloads/GetBlocksPayload.cs b/NBitcoin/Protocol/Payloads/GetBlocksPayload.cs index 03e567c14..ecd6b0d1f 100644 --- a/NBitcoin/Protocol/Payloads/GetBlocksPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetBlocksPayload.cs @@ -12,6 +12,7 @@ namespace NBitcoin.Protocol public class GetBlocksPayload : Payload { + public override string Command => "getblocks"; public GetBlocksPayload(BlockLocator locator) { BlockLocators = locator; diff --git a/NBitcoin/Protocol/Payloads/GetCompactFiltersPayload.cs b/NBitcoin/Protocol/Payloads/GetCompactFiltersPayload.cs index 1a54d423d..58843880e 100644 --- a/NBitcoin/Protocol/Payloads/GetCompactFiltersPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetCompactFiltersPayload.cs @@ -65,6 +65,7 @@ public override void ReadWriteCore(BitcoinStream stream) public class GetCompactFiltersPayload : CompactFiltersQueryPayload { + public override string Command => "getcfilters"; public GetCompactFiltersPayload(FilterType filterType, uint startHeight, uint256 stopHash) : base(filterType, startHeight, stopHash) { @@ -79,6 +80,7 @@ public GetCompactFiltersPayload() { } public class GetCompactFilterHeadersPayload : CompactFiltersQueryPayload { + public override string Command => "getcfheaders"; public GetCompactFilterHeadersPayload(FilterType filterType, uint startHeight, uint256 stopHash) : base(filterType, startHeight, stopHash) { @@ -90,6 +92,7 @@ public GetCompactFilterHeadersPayload() { } public class GetCompactFilterCheckPointPayload : Payload { + public override string Command => "getcfcheckpt"; private byte _FilterType; private uint256 _StopHash; diff --git a/NBitcoin/Protocol/Payloads/GetDataPayload.cs b/NBitcoin/Protocol/Payloads/GetDataPayload.cs index b0030550c..b206f8d16 100644 --- a/NBitcoin/Protocol/Payloads/GetDataPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetDataPayload.cs @@ -12,6 +12,7 @@ namespace NBitcoin.Protocol public class GetDataPayload : Payload { + public override string Command => "getdata"; public GetDataPayload() { } diff --git a/NBitcoin/Protocol/Payloads/GetHeadersPayload.cs b/NBitcoin/Protocol/Payloads/GetHeadersPayload.cs index c5f3ea9a4..5769dce9b 100644 --- a/NBitcoin/Protocol/Payloads/GetHeadersPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetHeadersPayload.cs @@ -12,6 +12,7 @@ namespace NBitcoin.Protocol public class GetHeadersPayload : Payload { + public override string Command => "getheaders"; public GetHeadersPayload() { diff --git a/NBitcoin/Protocol/Payloads/HaveWitnessPayload.cs b/NBitcoin/Protocol/Payloads/HaveWitnessPayload.cs index 47fd343cb..7c8360d0c 100644 --- a/NBitcoin/Protocol/Payloads/HaveWitnessPayload.cs +++ b/NBitcoin/Protocol/Payloads/HaveWitnessPayload.cs @@ -9,6 +9,7 @@ namespace NBitcoin.Protocol public class HaveWitnessPayload : Payload { + public override string Command => "havewitness"; public HaveWitnessPayload() { diff --git a/NBitcoin/Protocol/Payloads/HeadersPayload.cs b/NBitcoin/Protocol/Payloads/HeadersPayload.cs index 86ec222bf..24502f7f4 100644 --- a/NBitcoin/Protocol/Payloads/HeadersPayload.cs +++ b/NBitcoin/Protocol/Payloads/HeadersPayload.cs @@ -13,6 +13,7 @@ namespace NBitcoin.Protocol public class HeadersPayload : Payload { + public override string Command => "headers"; class BlockHeaderWithTxCount : IBitcoinSerializable { public BlockHeaderWithTxCount() diff --git a/NBitcoin/Protocol/Payloads/InvPayload.cs b/NBitcoin/Protocol/Payloads/InvPayload.cs index 6e5efcc3f..b00f9f4bf 100644 --- a/NBitcoin/Protocol/Payloads/InvPayload.cs +++ b/NBitcoin/Protocol/Payloads/InvPayload.cs @@ -12,6 +12,7 @@ namespace NBitcoin.Protocol public class InvPayload : Payload, IBitcoinSerializable, IEnumerable { + public override string Command => "inv"; public InvPayload() { diff --git a/NBitcoin/Protocol/Payloads/MempoolPayload.cs b/NBitcoin/Protocol/Payloads/MempoolPayload.cs index a34e1d242..413738cc7 100644 --- a/NBitcoin/Protocol/Payloads/MempoolPayload.cs +++ b/NBitcoin/Protocol/Payloads/MempoolPayload.cs @@ -12,5 +12,6 @@ namespace NBitcoin.Protocol public class MempoolPayload : Payload { + public override string Command => "mempool"; } } diff --git a/NBitcoin/Protocol/Payloads/MerkleBlockPayload.cs b/NBitcoin/Protocol/Payloads/MerkleBlockPayload.cs index 6c8e4eaee..a18999eed 100644 --- a/NBitcoin/Protocol/Payloads/MerkleBlockPayload.cs +++ b/NBitcoin/Protocol/Payloads/MerkleBlockPayload.cs @@ -12,6 +12,7 @@ namespace NBitcoin.Protocol public class MerkleBlockPayload : BitcoinSerializablePayload { + public override string Command => "merkleblock"; public MerkleBlockPayload() { diff --git a/NBitcoin/Protocol/Payloads/NotFoundPayload.cs b/NBitcoin/Protocol/Payloads/NotFoundPayload.cs index 132fb8736..07e4bf27b 100644 --- a/NBitcoin/Protocol/Payloads/NotFoundPayload.cs +++ b/NBitcoin/Protocol/Payloads/NotFoundPayload.cs @@ -11,6 +11,7 @@ namespace NBitcoin.Protocol /// public class NotFoundPayload : Payload, IEnumerable { + public override string Command => "notfound"; public NotFoundPayload() { diff --git a/NBitcoin/Protocol/Payloads/PingPayload.cs b/NBitcoin/Protocol/Payloads/PingPayload.cs index 425d54c97..43a252041 100644 --- a/NBitcoin/Protocol/Payloads/PingPayload.cs +++ b/NBitcoin/Protocol/Payloads/PingPayload.cs @@ -9,7 +9,7 @@ namespace NBitcoin.Protocol public class PingPayload : Payload { - + public override string Command => "ping"; public PingPayload() { _Nonce = RandomUtils.GetUInt64(); diff --git a/NBitcoin/Protocol/Payloads/PongPayload.cs b/NBitcoin/Protocol/Payloads/PongPayload.cs index 7968e82e3..a3a5a9d72 100644 --- a/NBitcoin/Protocol/Payloads/PongPayload.cs +++ b/NBitcoin/Protocol/Payloads/PongPayload.cs @@ -9,6 +9,7 @@ namespace NBitcoin.Protocol public class PongPayload : Payload { + public override string Command => "pong"; private ulong _Nonce; public ulong Nonce { diff --git a/NBitcoin/Protocol/Payloads/SendAddrV2Payload.cs b/NBitcoin/Protocol/Payloads/SendAddrV2Payload.cs index 872bb9726..9305dd912 100644 --- a/NBitcoin/Protocol/Payloads/SendAddrV2Payload.cs +++ b/NBitcoin/Protocol/Payloads/SendAddrV2Payload.cs @@ -12,5 +12,6 @@ namespace NBitcoin.Protocol public class SendAddrV2Payload : Payload { + public override string Command => "sendaddrv2"; } } diff --git a/NBitcoin/Protocol/Payloads/SendCmpctPayload.cs b/NBitcoin/Protocol/Payloads/SendCmpctPayload.cs index 985386f2e..f0444d965 100644 --- a/NBitcoin/Protocol/Payloads/SendCmpctPayload.cs +++ b/NBitcoin/Protocol/Payloads/SendCmpctPayload.cs @@ -9,6 +9,7 @@ namespace NBitcoin.Protocol public class SendCmpctPayload : Payload { + public override string Command => "sendcmpct"; public SendCmpctPayload() { diff --git a/NBitcoin/Protocol/Payloads/SendHeadersPayload.cs b/NBitcoin/Protocol/Payloads/SendHeadersPayload.cs index 9b390756d..c1d3d687a 100644 --- a/NBitcoin/Protocol/Payloads/SendHeadersPayload.cs +++ b/NBitcoin/Protocol/Payloads/SendHeadersPayload.cs @@ -9,5 +9,6 @@ namespace NBitcoin.Protocol public class SendHeadersPayload : Payload { + public override string Command => "sendheaders"; } } diff --git a/NBitcoin/Protocol/Payloads/TxPayload.cs b/NBitcoin/Protocol/Payloads/TxPayload.cs index 39fdffc8d..6c2970226 100644 --- a/NBitcoin/Protocol/Payloads/TxPayload.cs +++ b/NBitcoin/Protocol/Payloads/TxPayload.cs @@ -12,6 +12,7 @@ namespace NBitcoin.Protocol public class TxPayload : Payload { + public override string Command => "tx"; public TxPayload() { diff --git a/NBitcoin/Protocol/Payloads/UTxOutputPayload.cs b/NBitcoin/Protocol/Payloads/UTxOutputPayload.cs index 64bc7219c..189fd8c2e 100644 --- a/NBitcoin/Protocol/Payloads/UTxOutputPayload.cs +++ b/NBitcoin/Protocol/Payloads/UTxOutputPayload.cs @@ -1,10 +1,11 @@ using System.Collections; -namespace NBitcoin.Protocol.Payloads +namespace NBitcoin.Protocol { public class UTxOutputPayload : Payload { + public override string Command => "utxos"; private UTxOutputs _uTxOutputs; public override void ReadWriteCore(BitcoinStream stream) diff --git a/NBitcoin/Protocol/Payloads/VerAckPayload.cs b/NBitcoin/Protocol/Payloads/VerAckPayload.cs index 70b885744..e58d3941c 100644 --- a/NBitcoin/Protocol/Payloads/VerAckPayload.cs +++ b/NBitcoin/Protocol/Payloads/VerAckPayload.cs @@ -9,6 +9,7 @@ namespace NBitcoin.Protocol public class VerAckPayload : Payload, IBitcoinSerializable { + public override string Command => "verack"; #region IBitcoinSerializable Members public override void ReadWriteCore(BitcoinStream stream) diff --git a/NBitcoin/Protocol/Payloads/VersionPayload.cs b/NBitcoin/Protocol/Payloads/VersionPayload.cs index 9f4485de6..7acda8895 100644 --- a/NBitcoin/Protocol/Payloads/VersionPayload.cs +++ b/NBitcoin/Protocol/Payloads/VersionPayload.cs @@ -57,6 +57,7 @@ public enum NodeServices : ulong public class VersionPayload : Payload, IBitcoinSerializable { + public override string Command => "version"; static string _NUserAgent; public static string GetNBitcoinUserAgent() { @@ -237,4 +238,4 @@ public override string ToString() } } } -#endif \ No newline at end of file +#endif diff --git a/NBitcoin/Protocol/Payloads/WTxIdRelayPayload.cs b/NBitcoin/Protocol/Payloads/WTxIdRelayPayload.cs index 9fa1771e5..d2da9f783 100644 --- a/NBitcoin/Protocol/Payloads/WTxIdRelayPayload.cs +++ b/NBitcoin/Protocol/Payloads/WTxIdRelayPayload.cs @@ -6,5 +6,6 @@ namespace NBitcoin.Protocol public class WTxIdRelayPayload : Payload { + public override string Command => "wtxidrelay"; } } diff --git a/NBitcoin/RPC/RestClient.cs b/NBitcoin/RPC/RestClient.cs index 58f8b9e73..edf8656c2 100644 --- a/NBitcoin/RPC/RestClient.cs +++ b/NBitcoin/RPC/RestClient.cs @@ -10,7 +10,6 @@ using System.Threading; using System.Threading.Tasks; using NBitcoin.Protocol; -using NBitcoin.Protocol.Payloads; using Newtonsoft.Json.Linq; using System.Runtime.ExceptionServices; using System.Net.Http;