Skip to content

Commit

Permalink
Allow override of CreatePayload
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Jan 10, 2024
1 parent d91b67f commit 8ff2672
Show file tree
Hide file tree
Showing 35 changed files with 91 additions and 117 deletions.
44 changes: 44 additions & 0 deletions NBitcoin/ConsensusFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,50 @@ public bool TryCreateNew<T>(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()
Expand Down
2 changes: 1 addition & 1 deletion NBitcoin/Protocol/BitcoinSerializablePayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace NBitcoin.Protocol
{
public class BitcoinSerializablePayload<T> : Payload where T : IBitcoinSerializable, new()
public abstract class BitcoinSerializablePayload<T> : Payload where T : IBitcoinSerializable, new()
{
public BitcoinSerializablePayload()
{
Expand Down
3 changes: 1 addition & 2 deletions NBitcoin/Protocol/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public bool IfPayloadIs<TPayload>(Action<TPayload> 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<ArrayPool<byte>> BigArrayPool = new Lazy<ArrayPool<byte>>(() => ArrayPool<byte>.Create(0x02000000, 5), false);
ArrayPool<byte> GetArrayPool(int size) => size < 1_048_576 ? ArrayPool<byte>.Shared : BigArrayPool.Value;

public void ReadWrite(BitcoinStream stream)
{
if (Payload == null && stream.Serializing)
Expand Down Expand Up @@ -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);

Expand Down
14 changes: 7 additions & 7 deletions NBitcoin/Protocol/Payload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

namespace NBitcoin.Protocol
{
public class Payload : IBitcoinSerializable
/// <summary>
/// A P2P Bitcoin payload
/// </summary>
public abstract class Payload : IBitcoinSerializable
{
public virtual string Command
public abstract string Command
{
get
{
return PayloadFactory.Instance.GetCommand(this);
}
get;
}

#region IBitcoinSerializable Members
Expand All @@ -34,7 +34,7 @@ public virtual void ReadWriteCore(BitcoinStream stream)

public override string ToString()
{
return this.GetType().Name;
return Command;
}
}
}
103 changes: 0 additions & 103 deletions NBitcoin/Protocol/PayloadFactory.cs

This file was deleted.

4 changes: 3 additions & 1 deletion NBitcoin/Protocol/Payloads/AddrPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -56,6 +57,7 @@ public override string ToString()

public class AddrV2Payload : AddrPayload
{
public override string Command => "addrv2";
public AddrV2Payload()
: base()
{
Expand All @@ -79,4 +81,4 @@ public override void ReadWriteCore(BitcoinStream stream)
}
}
}
#endif
#endif
1 change: 1 addition & 0 deletions NBitcoin/Protocol/Payloads/BlockPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace NBitcoin.Protocol

public class BlockPayload : BitcoinSerializablePayload<Block>
{
public override string Command => "block";
public BlockPayload()
{

Expand Down
1 change: 1 addition & 0 deletions NBitcoin/Protocol/Payloads/BlockTxnPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace NBitcoin.Protocol

public class BlockTxnPayload : Payload
{
public override string Command => "blocktxn";

uint256 _BlockId;
public uint256 BlockId
Expand Down
1 change: 1 addition & 0 deletions NBitcoin/Protocol/Payloads/CmpctBlockPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace NBitcoin.Protocol

public class CmpctBlockPayload : Payload
{
public override string Command => "cmpctblock";
public CmpctBlockPayload()
{

Expand Down
3 changes: 3 additions & 0 deletions NBitcoin/Protocol/Payloads/CompactFilterPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<uint256> _FilterHeaders = new List<uint256>();
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions NBitcoin/Protocol/Payloads/FeeFilterPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace NBitcoin.Protocol

public class FeeFilterPayload : Payload
{
public override string Command => "feefilter";
public FeeFilterPayload()
{
_feeRate = FeeRate.Zero;
Expand Down
1 change: 1 addition & 0 deletions NBitcoin/Protocol/Payloads/FilterAddPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace NBitcoin.Protocol

public class FilterAddPayload : Payload
{
public override string Command => "filteradd";
public FilterAddPayload()
{

Expand Down
1 change: 1 addition & 0 deletions NBitcoin/Protocol/Payloads/FilterLoadPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace NBitcoin.Protocol

public class FilterLoadPayload : BitcoinSerializablePayload<BloomFilter>
{
public override string Command => "filterload";
public FilterLoadPayload()
{

Expand Down
1 change: 1 addition & 0 deletions NBitcoin/Protocol/Payloads/GetAddrPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ namespace NBitcoin.Protocol

public class GetAddrPayload : Payload
{
public override string Command => "getaddr";
}
}
1 change: 1 addition & 0 deletions NBitcoin/Protocol/Payloads/GetBlockTxnPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace NBitcoin.Protocol

public class GetBlockTxnPayload : Payload
{
public override string Command => "getblocktxn";

uint256 _BlockId = uint256.Zero;
public uint256 BlockId
Expand Down
1 change: 1 addition & 0 deletions NBitcoin/Protocol/Payloads/GetBlocksPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace NBitcoin.Protocol

public class GetBlocksPayload : Payload
{
public override string Command => "getblocks";
public GetBlocksPayload(BlockLocator locator)
{
BlockLocators = locator;
Expand Down
3 changes: 3 additions & 0 deletions NBitcoin/Protocol/Payloads/GetCompactFiltersPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -90,6 +92,7 @@ public GetCompactFilterHeadersPayload() { }

public class GetCompactFilterCheckPointPayload : Payload
{
public override string Command => "getcfcheckpt";
private byte _FilterType;
private uint256 _StopHash;

Expand Down
1 change: 1 addition & 0 deletions NBitcoin/Protocol/Payloads/GetDataPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace NBitcoin.Protocol

public class GetDataPayload : Payload
{
public override string Command => "getdata";
public GetDataPayload()
{
}
Expand Down
1 change: 1 addition & 0 deletions NBitcoin/Protocol/Payloads/GetHeadersPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace NBitcoin.Protocol

public class GetHeadersPayload : Payload
{
public override string Command => "getheaders";
public GetHeadersPayload()
{

Expand Down
1 change: 1 addition & 0 deletions NBitcoin/Protocol/Payloads/HaveWitnessPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace NBitcoin.Protocol

public class HaveWitnessPayload : Payload
{
public override string Command => "havewitness";
public HaveWitnessPayload()
{

Expand Down
1 change: 1 addition & 0 deletions NBitcoin/Protocol/Payloads/HeadersPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace NBitcoin.Protocol

public class HeadersPayload : Payload
{
public override string Command => "headers";
class BlockHeaderWithTxCount : IBitcoinSerializable
{
public BlockHeaderWithTxCount()
Expand Down
1 change: 1 addition & 0 deletions NBitcoin/Protocol/Payloads/InvPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace NBitcoin.Protocol

public class InvPayload : Payload, IBitcoinSerializable, IEnumerable<InventoryVector>
{
public override string Command => "inv";
public InvPayload()
{

Expand Down
1 change: 1 addition & 0 deletions NBitcoin/Protocol/Payloads/MempoolPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ namespace NBitcoin.Protocol

public class MempoolPayload : Payload
{
public override string Command => "mempool";
}
}
Loading

0 comments on commit 8ff2672

Please sign in to comment.