Esempio n. 1
0
        //Copied from https://en.bitcoin.it/wiki/Protocol_specification (19/04/2014)
        public void CanParseMessages()
        {
            TimeZoneInfo EST;

            try
            {
                EST = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
            }
            catch (TimeZoneNotFoundException)
            {
                EST = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
            }

            var tests = new[]
            {
                new
                {
                    Version = 209U,
                    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 <uint>(31900, version.Version);
                    })
                },
                new
                {
                    Version = 60002U,
                    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 = 70012U,
                    Message = "f9beb4d976657261636b000000000000000000005df6e0e2",
                    Test    = new Action <object>(o =>
                    {
                        var verack = (VerAckPayload)o;
                    })
                },
                new
                {
                    Version = 60002U,
                    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)));
            }
        }