public static Message ReadNext(Stream stream, Network network, ProtocolVersion version, CancellationToken cancellationToken, byte[] buffer, out PerformanceCounter counter) { BitcoinStream bitStream = new BitcoinStream(stream, false) { ProtocolVersion = version, ReadCancellationToken = cancellationToken }; if(!network.ReadMagic(stream, cancellationToken, true)) throw new FormatException("Magic incorrect, the message comes from another network"); Message message = new Message(); message._Buffer = buffer; using(message.SkipMagicScope(true)) { message.Magic = network.Magic; message.ReadWrite(bitStream); } counter = bitStream.Counter; return message; }
public void CanParseReject() { var hex = "f9beb4d972656a6563740000000000003a000000db7f7e7802747812156261642d74786e732d696e707574732d7370656e74577a9694da4ff41ae999f6591cff3749ad6a7db19435f3d8af5fecbcff824196"; Message message = new Message(); message.ReadWrite(Encoders.Hex.DecodeData(hex)); var reject = (RejectPayload)message.Payload; Assert.True(reject.Message == "tx"); Assert.True(reject.Code == RejectCode.DUPLICATE); Assert.True(reject.CodeType == RejectCodeType.Transaction); Assert.True(reject.Reason == "bad-txns-inputs-spent"); Assert.True(reject.Hash == uint256.Parse("964182ffbcec5fafd8f33594b17d6aad4937ff1c59f699e91af44fda94967a57")); }
public static Message ReadNext(Socket socket, Network network, ProtocolVersion version, CancellationToken cancellationToken) { var stream = new NetworkStream(socket, false); BitcoinStream bitStream = new BitcoinStream(stream, false) { ProtocolVersion = version, ReadCancellationToken = cancellationToken }; network.ReadMagic(stream, cancellationToken); Message message = new Message(); using(message.SkipMagicScope(true)) { message.Magic = network.Magic; NodeServerTrace.Trace.TraceEvent(TraceEventType.Verbose, 0, "Message recieved"); message.ReadWrite(bitStream); } return message; }
//Copied from https://en.bitcoin.it/wiki/Protocol_specification (19/04/2014) public void CanParseMessages() { var EST = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"); var tests = new[] { new { Version = ProtocolVersion.INIT_PROTO_VERSION, Message = "f9beb4d976657273696f6e0000000000550000009c7c00000100000000000000e615104d00000000010000000000000000000000000000000000ffff0a000001208d010000000000000000000000000000000000ffff0a000002208ddd9d202c3ab457130055810100", Test = new Action<object>(o=> { var version = (VersionPayload)o; Assert.Equal((ulong)0x1357B43A2C209DDD, version.Nonce); Assert.Equal("", version.UserAgent); Assert.Equal("::ffff:10.0.0.2", version.AddressFrom.Address.ToString()); Assert.Equal(8333, version.AddressFrom.Port); Assert.Equal(0x00018155, version.StartHeight); Assert.Equal((ProtocolVersion)31900, version.Version); }) }, new { Version = ProtocolVersion.MEMPOOL_GD_VERSION, Message = "f9beb4d976657273696f6e000000000064000000358d493262ea0000010000000000000011b2d05000000000010000000000000000000000000000000000ffff000000000000000000000000000000000000000000000000ffff0000000000003b2eb35d8ce617650f2f5361746f7368693a302e372e322fc03e0300", Test = new Action<object>(o=> { var version = (VersionPayload)o; Assert.Equal("/Satoshi:0.7.2/", version.UserAgent); Assert.Equal(0x00033EC0, version.StartHeight); }) }, new { Version = ProtocolVersion.PROTOCOL_VERSION, Message = "f9beb4d976657261636b000000000000000000005df6e0e2", Test = new Action<object>(o=> { var verack = (VerAckPayload)o; }) }, new { Version = ProtocolVersion.MEMPOOL_GD_VERSION, Message = "f9beb4d96164647200000000000000001f000000ed52399b01e215104d010000000000000000000000000000000000ffff0a000001208d", Test = new Action<object>(o=> { var addr = (AddrPayload)o; Assert.Equal(1, addr.Addresses.Length); //"Mon Dec 20 21:50:10 EST 2010" var date = TimeZoneInfo.ConvertTime(addr.Addresses[0].Time,EST); Assert.Equal(20,date.Day); Assert.Equal(12, date.Month); Assert.Equal(2010, date.Year); Assert.Equal(21, date.Hour); }) }, }; foreach(var test in tests) { var message = Network.Main.ParseMessage(TestUtils.ParseHex(test.Message), test.Version); test.Test(message.Payload); var bytes = message.ToBytes(test.Version); var old = message; message = new Message(); message.FromBytes(bytes, test.Version); test.Test(message.Payload); Assert.Equal(test.Message, Encoders.Hex.EncodeData(message.ToBytes(test.Version))); } }
public IncomingMessage(Payload payload, Network network) { Message = new Message(); Message.Magic = network.Magic; Message.Payload = payload; }