Exemplo n.º 1
0
        /// <summary>
        /// Gets <see cref="Bencodex.Types.Dictionary"/> representation of
        /// <see cref="BlockHeader"/>.
        /// </summary>
        /// <returns><see cref="Bencodex.Types.Dictionary"/> representation of
        /// <see cref="BlockHeader"/>.</returns>
        public Bencodex.Types.Dictionary ToBencodex()
        {
            var dict = Bencodex.Types.Dictionary.Empty
                       .Add(IndexKey, Index)
                       .Add(TimestampKey, Timestamp)
                       .Add(DifficultyKey, Difficulty)
                       .Add(TotalDifficultyKey, (IValue)(Bencodex.Types.Integer)TotalDifficulty)
                       .Add(NonceKey, Nonce.ToArray())
                       .Add(HashKey, Hash.ToArray());

            if (Miner.Any())
            {
                dict = dict.Add(MinerKey, Miner.ToArray());
            }

            if (PreviousHash.Any())
            {
                dict = dict.Add(PreviousHashKey, PreviousHash.ToArray());
            }

            if (TxHash.Any())
            {
                dict = dict.Add(TxHashKey, TxHash.ToArray());
            }

            return(dict);
        }
Exemplo n.º 2
0
        public static void IncrementCarry()
        {
            var expected = new byte[] { 0xFE, 0xED, 0xDC, 0xCB, 0x01, 0x00, 0x00, 0x00 };
            var actual   = new Nonce(new byte[] { 0xFE, 0xED, 0xDC, 0xCB }, new byte[] { 0x00, 0xFF, 0xFF, 0xFF });

            actual++;

            Assert.Equal(expected, actual.ToArray());
        }
Exemplo n.º 3
0
        public static void Layout()
        {
            var fixedField   = new byte[] { 0x01, 0x02, 0x03, 0x04 };
            var counterField = 0x05060708;

            var expected = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
            var actual   = new Nonce(fixedField, 4) + counterField;

            Assert.Equal(expected, actual.ToArray());
        }
Exemplo n.º 4
0
        public static void Ctor()
        {
            var actual = new Nonce();

            Assert.Equal(0, actual.Size);
            Assert.Equal(0, actual.FixedFieldSize);
            Assert.Equal(0, actual.CounterFieldSize);
            Assert.Equal(Array.Empty <byte>(), actual.ToArray());
            Assert.Equal("[][]", actual.GetDebuggerDisplay());
            actual.CopyTo(Span <byte> .Empty);
        }
Exemplo n.º 5
0
        public static void Ctor()
        {
            var actual = new Nonce();

            Assert.Equal(0, actual.Size);
            Assert.Equal(0, actual.FixedFieldSize);
            Assert.Equal(0, actual.CounterFieldSize);
            Assert.Equal(new byte[0], actual.ToArray());
            Assert.Equal("[][]", actual.ToString());
            actual.CopyTo(Span <byte> .Empty);
        }
Exemplo n.º 6
0
        public static void CtorWithCounterSize(int size)
        {
            var expected = new byte[size];
            var actual   = new Nonce(0, size);

            var array = new byte[expected.Length];

            Assert.Equal(expected.Length, actual.Size);
            Assert.Equal(0, actual.FixedFieldSize);
            Assert.Equal(expected.Length, actual.CounterFieldSize);
            Assert.Equal(expected, actual.ToArray());
            Assert.Equal("[][" + expected.EncodeHex() + "]", actual.GetDebuggerDisplay());
            actual.CopyTo(array);
            Assert.Equal(expected, array);
        }
Exemplo n.º 7
0
        public static void CtorWithCounterSize(int size)
        {
            var expected = new byte[size];
            var actual   = new Nonce(0, size);

            var array = new byte[expected.Length];

            Assert.Equal(expected.Length, actual.Size);
            Assert.Equal(0, actual.FixedFieldSize);
            Assert.Equal(expected.Length, actual.CounterFieldSize);
            Assert.Equal(expected, actual.ToArray());
            Assert.Equal("[][" + Base16.Encode(expected) + "]", actual.ToString());
            Assert.Equal(expected.Length, actual.CopyTo(array));
            Assert.Equal(expected, array);
        }
Exemplo n.º 8
0
        public static void Xor(int start)
        {
            var bytes1 = Utilities.RandomBytes.Slice(start * 32, 12 + start);
            var bytes2 = Utilities.RandomBytes.Slice(start * 32 + 100, 12 + start);

            var expected = new byte[bytes1.Length];
            var actual   = new Nonce(ReadOnlySpan <byte> .Empty, bytes1) ^ new Nonce(bytes2, 0);

            for (var i = 0; i < expected.Length; i++)
            {
                expected[i] = (byte)(bytes1[i] ^ bytes2[i]);
            }

            Assert.Equal(expected, actual.ToArray());
            Assert.Equal(expected.Length, actual.Size);
            Assert.Equal(expected.Length, actual.FixedFieldSize);
            Assert.Equal(0, actual.CounterFieldSize);
        }
Exemplo n.º 9
0
        public static void CtorWithFixedAndCounterSize(int size)
        {
            var fixedField   = Utilities.RandomBytes.Slice(0, size / 2).ToArray();
            var counterField = new byte[size - fixedField.Length];

            var expected = new byte[size];
            var actual   = new Nonce(fixedField, counterField.Length);

            var array = new byte[expected.Length];

            fixedField.CopyTo(expected, 0);

            Assert.Equal(expected.Length, actual.Size);
            Assert.Equal(fixedField.Length, actual.FixedFieldSize);
            Assert.Equal(counterField.Length, actual.CounterFieldSize);
            Assert.Equal(expected, actual.ToArray());
            Assert.Equal("[" + fixedField.EncodeHex() + "][" + counterField.EncodeHex() + "]", actual.GetDebuggerDisplay());
            actual.CopyTo(array);
            Assert.Equal(expected, array);
        }
Exemplo n.º 10
0
        public static void CtorWithFixedAndCounterSize(int size)
        {
            var fixedField   = Utilities.RandomBytes.Slice(0, size / 2);
            var counterField = new byte[size - fixedField.Length];

            var expected = new byte[size];
            var actual   = new Nonce(fixedField, counterField.Length);

            var array = new byte[expected.Length];

            fixedField.CopyTo(expected);

            Assert.Equal(expected.Length, actual.Size);
            Assert.Equal(fixedField.Length, actual.FixedFieldSize);
            Assert.Equal(counterField.Length, actual.CounterFieldSize);
            Assert.Equal(expected, actual.ToArray());
            Assert.Equal("[" + Base16.Encode(fixedField) + "][" + Base16.Encode(counterField) + "]", actual.ToString());
            Assert.Equal(expected.Length, actual.CopyTo(array));
            Assert.Equal(expected, array);
        }
Exemplo n.º 11
0
        public static void AddCarry(byte[] expected, byte[] left, int right)
        {
            var actual = new Nonce(new byte[] { 0xFE, 0xED, 0xDC, 0xCB }, left) + right;

            Assert.Equal(expected, actual.ToArray());
        }