public UInt256 ComputeHash(BlockHeader header, int protocolVersion)
        {
            var buffer = new ArrayBufferWriter <byte>(80);

            _blockHeaderSerializer.Serialize(header, protocolVersion, buffer);

            //slicing first 80 bytes because the header includes the tx varint value that doesn't need to be included to compute the hash
            return(HashGenerator.DoubleSha256AsUInt256(buffer.WrittenSpan.Slice(0, 80)));
        }
Example #2
0
        public UInt256 ComputeHash(Transaction transaction, int protocolVersion)
        {
            var buffer = new ArrayBufferWriter <byte>();

            _transactionSerializer.Serialize(transaction,
                                             protocolVersion,
                                             buffer,
                                             new ProtocolTypeSerializerOptions((SerializerOptions.SERIALIZE_WITNESS, false)));

            return(HashGenerator.DoubleSha256AsUInt256(buffer.WrittenSpan));
        }
Example #3
0
        public UInt256 ComputeMerkleRoot(IList <UInt256> hashes)
        {
            if (hashes.Count == 0)
            {
                return(UInt256.Zero);
            }
            if (hashes.Count == 1)
            {
                return(hashes[0]);
            }

            bool oddHashes = (hashes.Count & 1) == 1;
            //ensure to allocate one more item if hashes are odd.
            var hashesList = new List <UInt256>(oddHashes ? hashes.Count + 1 : hashes.Count);

            for (int i = 0; i < hashes.Count; i++)
            {
                hashesList.Add(hashes[i]);
            }

            // if odd, duplicate last element
            if (oddHashes)
            {
                hashesList.Add(hashes[hashes.Count - 1]);
            }

            int elementsCount = hashesList.Count;

            while (elementsCount > 1)
            {
                int newHashPosition = 0;
                for (int pos = 0; pos + 1 < elementsCount; pos += 2)
                {
                    Span <byte> pairOfHashes = stackalloc byte[64];
                    hashesList[pos].GetBytes().CopyTo(pairOfHashes);
                    hashesList[pos + 1].GetBytes().CopyTo(pairOfHashes.Slice(32));

                    hashesList[newHashPosition++] = HashGenerator.DoubleSha256AsUInt256(pairOfHashes);
                }

                if (newHashPosition > 1 && (newHashPosition & 1) == 1)
                {
                    hashesList[newHashPosition] = hashesList[newHashPosition - 1];
                    newHashPosition++;
                }

                hashesList.RemoveRange(newHashPosition, elementsCount - newHashPosition);
                elementsCount = newHashPosition;
            }

            return(hashesList[0]);
        }
Example #4
0
        public UInt256 ComputeWitnessHash(Transaction transaction, int protocolVersion)
        {
            var buffer = new ArrayBufferWriter <byte>();

            _transactionSerializer.Serialize(transaction,
                                             protocolVersion,
                                             buffer,
                                             new ProtocolTypeSerializerOptions((SerializerOptions.SERIALIZE_WITNESS, transaction.HasWitness())));

            return(HashGenerator.DoubleSha256AsUInt256(buffer.WrittenSpan));

            throw new NotImplementedException();
        }