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 89d2693b2..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,18 +129,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 = stream.ConsensusFactory.CreatePayload(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 { diff --git a/NBitcoin/Protocol/Payload.cs b/NBitcoin/Protocol/Payload.cs index 993a36eaa..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 PayloadAttribute.GetCommandName(this.GetType()); - } + 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/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..36ebc2c49 100644 --- a/NBitcoin/Protocol/Payloads/AddrPayload.cs +++ b/NBitcoin/Protocol/Payloads/AddrPayload.cs @@ -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 -{ - /// - /// 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); - } - } - } -} -#endif \ No newline at end of file +#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 + { + 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)"; + } + } + + /// + /// An available peer address in the bitcoin network is announced (unsolicited or after a getaddrv2) + /// + + 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 diff --git a/NBitcoin/Protocol/Payloads/BlockPayload.cs b/NBitcoin/Protocol/Payloads/BlockPayload.cs index c1da30998..6a022808e 100644 --- a/NBitcoin/Protocol/Payloads/BlockPayload.cs +++ b/NBitcoin/Protocol/Payloads/BlockPayload.cs @@ -9,9 +9,10 @@ namespace NBitcoin.Protocol /// /// A block received after being asked with a getdata message /// - [Payload("block")] + 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 87352a390..4c322e131 100644 --- a/NBitcoin/Protocol/Payloads/BlockTxnPayload.cs +++ b/NBitcoin/Protocol/Payloads/BlockTxnPayload.cs @@ -6,9 +6,10 @@ namespace NBitcoin.Protocol { - [Payload("blocktxn")] + 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 42b42bd62..6838ebb72 100644 --- a/NBitcoin/Protocol/Payloads/CmpctBlockPayload.cs +++ b/NBitcoin/Protocol/Payloads/CmpctBlockPayload.cs @@ -8,9 +8,10 @@ namespace NBitcoin.Protocol { - [Payload("cmpctblock")] + 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 8c3ea9381..b1e2f7583 100644 --- a/NBitcoin/Protocol/Payloads/CompactFilterPayload.cs +++ b/NBitcoin/Protocol/Payloads/CompactFilterPayload.cs @@ -6,9 +6,10 @@ namespace NBitcoin.Protocol /// /// Represents the p2p message payload used for sharing a block's compact filter. /// - [Payload("cfilter")] + public class CompactFilterPayload : Payload { + public override string Command => "cfilter"; private byte _FilterType = (byte)FilterType.Basic; private byte[] _FilterBytes; private uint256 _BlockHash = new uint256(); @@ -58,9 +59,10 @@ public CompactFilterPayload() } } - [Payload("cfcheckpt")] + 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(); @@ -109,9 +111,10 @@ public override void ReadWriteCore(BitcoinStream stream) } } - [Payload("cfheaders")] + 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 9a7e3b7e1..2a118076e 100644 --- a/NBitcoin/Protocol/Payloads/FeeFilterPayload.cs +++ b/NBitcoin/Protocol/Payloads/FeeFilterPayload.cs @@ -3,9 +3,10 @@ namespace NBitcoin.Protocol { - [Payload("feefilter")] + 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 713ede622..a86c1258d 100644 --- a/NBitcoin/Protocol/Payloads/FilterAddPayload.cs +++ b/NBitcoin/Protocol/Payloads/FilterAddPayload.cs @@ -6,9 +6,10 @@ namespace NBitcoin.Protocol { - [Payload("filteradd")] + 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 0a6322300..e0899fa75 100644 --- a/NBitcoin/Protocol/Payloads/FilterLoadPayload.cs +++ b/NBitcoin/Protocol/Payloads/FilterLoadPayload.cs @@ -9,9 +9,10 @@ namespace NBitcoin.Protocol /// /// Load a bloomfilter in the peer, used by SPV clients /// - [Payload("filterload")] + 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 a31855234..4803c218f 100644 --- a/NBitcoin/Protocol/Payloads/GetAddrPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetAddrPayload.cs @@ -9,8 +9,9 @@ namespace NBitcoin.Protocol /// /// Ask for known peer addresses in the network /// - [Payload("getaddr")] + public class GetAddrPayload : Payload { + public override string Command => "getaddr"; } } diff --git a/NBitcoin/Protocol/Payloads/GetBlockTxnPayload.cs b/NBitcoin/Protocol/Payloads/GetBlockTxnPayload.cs index fa375dd46..cf66e85f8 100644 --- a/NBitcoin/Protocol/Payloads/GetBlockTxnPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetBlockTxnPayload.cs @@ -6,9 +6,10 @@ namespace NBitcoin.Protocol { - [Payload("getblocktxn")] + 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 eab51f4af..ecd6b0d1f 100644 --- a/NBitcoin/Protocol/Payloads/GetBlocksPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetBlocksPayload.cs @@ -9,9 +9,10 @@ namespace NBitcoin.Protocol /// /// Ask for the block hashes (inv) that happened since BlockLocators /// - [Payload("getblocks")] + 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 ea4187dca..58843880e 100644 --- a/NBitcoin/Protocol/Payloads/GetCompactFiltersPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetCompactFiltersPayload.cs @@ -62,9 +62,10 @@ 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 override string Command => "getcfilters"; public GetCompactFiltersPayload(FilterType filterType, uint startHeight, uint256 stopHash) : base(filterType, startHeight, stopHash) { @@ -76,9 +77,10 @@ public GetCompactFiltersPayload() { } /// /// Represents the p2p message payload used for requesting a range of compact filter headers. /// - [Payload("getcfheaders")] + public class GetCompactFilterHeadersPayload : CompactFiltersQueryPayload { + public override string Command => "getcfheaders"; public GetCompactFilterHeadersPayload(FilterType filterType, uint startHeight, uint256 stopHash) : base(filterType, startHeight, stopHash) { @@ -87,9 +89,10 @@ public GetCompactFilterHeadersPayload(FilterType filterType, uint startHeight, u public GetCompactFilterHeadersPayload() { } } - [Payload("getcfcheckpt")] + 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 9673d79a1..b206f8d16 100644 --- a/NBitcoin/Protocol/Payloads/GetDataPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetDataPayload.cs @@ -9,9 +9,10 @@ namespace NBitcoin.Protocol /// /// Ask for transaction, block or merkle block /// - [Payload("getdata")] + 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 f152b182a..5769dce9b 100644 --- a/NBitcoin/Protocol/Payloads/GetHeadersPayload.cs +++ b/NBitcoin/Protocol/Payloads/GetHeadersPayload.cs @@ -9,9 +9,10 @@ namespace NBitcoin.Protocol /// /// Ask block headers that happened since BlockLocators /// - [Payload("getheaders")] + 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 b7e3989d2..7c8360d0c 100644 --- a/NBitcoin/Protocol/Payloads/HaveWitnessPayload.cs +++ b/NBitcoin/Protocol/Payloads/HaveWitnessPayload.cs @@ -6,9 +6,10 @@ namespace NBitcoin.Protocol { - [Payload("havewitness")] + 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 8e94046ba..24502f7f4 100644 --- a/NBitcoin/Protocol/Payloads/HeadersPayload.cs +++ b/NBitcoin/Protocol/Payloads/HeadersPayload.cs @@ -10,9 +10,10 @@ namespace NBitcoin.Protocol /// /// Block headers received after a getheaders messages /// - [Payload("headers")] + 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 73117e7de..b00f9f4bf 100644 --- a/NBitcoin/Protocol/Payloads/InvPayload.cs +++ b/NBitcoin/Protocol/Payloads/InvPayload.cs @@ -9,9 +9,10 @@ namespace NBitcoin.Protocol /// /// Announce the hash of a transaction or block /// - [Payload("inv")] + 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 5ed87ceae..413738cc7 100644 --- a/NBitcoin/Protocol/Payloads/MempoolPayload.cs +++ b/NBitcoin/Protocol/Payloads/MempoolPayload.cs @@ -9,8 +9,9 @@ namespace NBitcoin.Protocol /// /// Ask for the mempool, followed by inv messages /// - [Payload("mempool")] + public class MempoolPayload : Payload { + public override string Command => "mempool"; } } diff --git a/NBitcoin/Protocol/Payloads/MerkleBlockPayload.cs b/NBitcoin/Protocol/Payloads/MerkleBlockPayload.cs index 2e72f8eb3..a18999eed 100644 --- a/NBitcoin/Protocol/Payloads/MerkleBlockPayload.cs +++ b/NBitcoin/Protocol/Payloads/MerkleBlockPayload.cs @@ -9,9 +9,10 @@ namespace NBitcoin.Protocol /// /// A merkle block received after being asked with a getdata message /// - [Payload("merkleblock")] + 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 c4d76223b..43a252041 100644 --- a/NBitcoin/Protocol/Payloads/PingPayload.cs +++ b/NBitcoin/Protocol/Payloads/PingPayload.cs @@ -6,10 +6,10 @@ namespace NBitcoin.Protocol { - [Payload("ping")] + 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 27a4f0ba7..a3a5a9d72 100644 --- a/NBitcoin/Protocol/Payloads/PongPayload.cs +++ b/NBitcoin/Protocol/Payloads/PongPayload.cs @@ -6,9 +6,10 @@ namespace NBitcoin.Protocol { - [Payload("pong")] + 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 9c75464b8..9305dd912 100644 --- a/NBitcoin/Protocol/Payloads/SendAddrV2Payload.cs +++ b/NBitcoin/Protocol/Payloads/SendAddrV2Payload.cs @@ -9,8 +9,9 @@ namespace NBitcoin.Protocol /// /// Ask for known peer addresses in the network /// - [Payload("sendaddrv2")] + public class SendAddrV2Payload : Payload { + public override string Command => "sendaddrv2"; } } diff --git a/NBitcoin/Protocol/Payloads/SendCmpctPayload.cs b/NBitcoin/Protocol/Payloads/SendCmpctPayload.cs index 36cb3fb59..f0444d965 100644 --- a/NBitcoin/Protocol/Payloads/SendCmpctPayload.cs +++ b/NBitcoin/Protocol/Payloads/SendCmpctPayload.cs @@ -6,9 +6,10 @@ namespace NBitcoin.Protocol { - [Payload("sendcmpct")] + 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 2d1b07fbe..c1d3d687a 100644 --- a/NBitcoin/Protocol/Payloads/SendHeadersPayload.cs +++ b/NBitcoin/Protocol/Payloads/SendHeadersPayload.cs @@ -6,8 +6,9 @@ namespace NBitcoin.Protocol { - [Payload("sendheaders")] + public class SendHeadersPayload : Payload { + public override string Command => "sendheaders"; } } diff --git a/NBitcoin/Protocol/Payloads/TxPayload.cs b/NBitcoin/Protocol/Payloads/TxPayload.cs index 6e751e75a..6c2970226 100644 --- a/NBitcoin/Protocol/Payloads/TxPayload.cs +++ b/NBitcoin/Protocol/Payloads/TxPayload.cs @@ -9,9 +9,10 @@ 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 override string Command => "tx"; public TxPayload() { diff --git a/NBitcoin/Protocol/Payloads/UTxOutputPayload.cs b/NBitcoin/Protocol/Payloads/UTxOutputPayload.cs index 13fbde86b..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 { - [Payload("utxos")] + public class UTxOutputPayload : Payload { + public override string Command => "utxos"; private UTxOutputs _uTxOutputs; public override void ReadWriteCore(BitcoinStream stream) @@ -23,45 +24,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 +83,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..e58d3941c 100644 --- a/NBitcoin/Protocol/Payloads/VerAckPayload.cs +++ b/NBitcoin/Protocol/Payloads/VerAckPayload.cs @@ -6,9 +6,10 @@ namespace NBitcoin.Protocol { - [Payload("verack")] + 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 216cc6613..7acda8895 100644 --- a/NBitcoin/Protocol/Payloads/VersionPayload.cs +++ b/NBitcoin/Protocol/Payloads/VersionPayload.cs @@ -54,9 +54,10 @@ public enum NodeServices : ulong /// NODE_NETWORK_LIMITED = (1 << 10) } - [Payload("version")] + 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 cd7db5296..d2da9f783 100644 --- a/NBitcoin/Protocol/Payloads/WTxIdRelayPayload.cs +++ b/NBitcoin/Protocol/Payloads/WTxIdRelayPayload.cs @@ -3,8 +3,9 @@ namespace NBitcoin.Protocol /// /// Signaling message about WTXID support (see BIP339) /// - [Payload("wtxidrelay")] + 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;