public static void WriteCompactString(this IBufferWriter <byte> output, string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                output.WriteVarInt(1);
                return;
            }

            var textLength = Encoding.UTF8.GetByteCount(text);

            output.WriteVarInt((uint)textLength + 1);

            var textSpan = output.GetSpan(textLength);

            Encoding.UTF8.GetBytes(text, textSpan);
            output.Advance(textLength);
        }
        public static int WriteByteArray(this IBufferWriter <byte> writer, byte[]?value)
        {
            ulong arraySize = (ulong)(value?.Length ?? 0);

            int size = writer.WriteVarInt(arraySize);

            if (arraySize > 0)
            {
                size += writer.WriteBytes(value !);
            }

            return(size);
        }
        public static void WriteCompactNullableString(this IBufferWriter <byte> output, string text)
        {
            if (text == null)
            {
                output.WriteVarInt(0);
                return;
            }
            else if (text.Length == 0)
            {
                output.WriteVarInt(1);
                return;
            }

            var textLength = Encoding.UTF8.GetByteCount(text);

            output.WriteVarInt((uint)textLength + 1);

            var textSpan = output.GetSpan(textLength);

            Encoding.UTF8.GetBytes(text, textSpan);
            output.Advance(textLength);
        }
        public static int WriteArray <TSerializableType>(this IBufferWriter <byte> writer, TSerializableType[]?items, int protocolVersion, IProtocolTypeSerializer <TSerializableType> serializer, ProtocolTypeSerializerOptions?options = null)
        {
            if ((items?.Length ?? 0) == 0)
            {
                return(writer.WriteVarInt(0));
            }

            int size = WriteVarInt(writer, (ulong)items !.Length);

            for (int i = 0; i < items.Length; i++)
            {
                size += serializer.Serialize(items[i], protocolVersion, writer, options);
            }

            return(size);
        }
        public int Serialize(Transaction tx, int protocolVersion, IBufferWriter <byte> writer, ProtocolTypeSerializerOptions?options = null)
        {
            bool allowWitness = options?.Get(SerializerOptions.SERIALIZE_WITNESS, false) ?? false;
            byte flags        = 0;
            int  size         = 0;

            size += writer.WriteInt(tx.Version);

            // Consistency check.
            if (allowWitness)
            {
                // Check whether witnesses need to be serialized.
                if (tx.HasWitness())
                {
                    flags |= 1;
                }
            }

            if (flags != 0)
            {
                // Use extended format in case witnesses are to be serialized.
                size += writer.WriteVarInt(0);
                size += writer.WriteByte(flags);
            }

            size += writer.WriteArray(tx.Inputs, protocolVersion, _transactionInputSerializer);
            size += writer.WriteArray(tx.Outputs, protocolVersion, _transactionOutputSerializer);

            if ((flags & 1) != 0)
            {
                if (tx.Inputs != null)
                {
                    for (int i = 0; i < tx.Inputs.Length; i++)
                    {
                        size += writer.WriteWithSerializer(tx.Inputs[i].ScriptWitness !, protocolVersion, _transactionWitnessSerializer);
                    }
                }
            }

            size += writer.WriteUInt(tx.LockTime);

            return(size);
        }
示例#6
0
        public void WriteRecord(Message <TKey, TValue> message, IBufferWriter <byte> output)
        {
            // Record...
            // length: varint
            // attributes: int8
            //     bit 0~7: unused
            // timestampDelta: varint
            // offsetDelta: varint
            // keyLength: varint
            // key: byte[]
            // valueLen: varint
            // value: byte[]
            // Headers => [Header]

            // Header...
            // headerKeyLength: varint
            // headerKey: String
            // headerValueLength: varint
            // Value: byte[]


            using var buffer = new MemoryBufferWriter();

            buffer.WriteByte(0);         // attributes: int8 - bit 0~7: unused
            buffer.WriteVarInt((uint)0); // timestampDelta: varint
            buffer.WriteVarInt((uint)0); // offsetDelta: varint

            buffer.WritePrefixed(keySerializer, message.Key, BufferWriterExtensions.PrefixType.VarInt);
            buffer.WritePrefixed(valueSerializer, message.Value, BufferWriterExtensions.PrefixType.VarInt);

            // headers
            buffer.WriteVarInt((ulong)message.HeadersCount);
            message.ForEachHeader(h =>
            {
                buffer.WriteCompactString(h.Key);
                buffer.WritePrefixed(h.Value.AsSpan(), BufferWriterExtensions.PrefixType.VarInt);
            });

            output.WriteVarInt(buffer.Length);
            buffer.CopyTo(output);
        }
示例#7
0
        public int Serialize(BlockHeader typeInstance, int protocolVersion, IBufferWriter <byte> writer, ProtocolTypeSerializerOptions?options = null)
        {
            bool headerInBlock = options?.HasOption(SerializerOptions.HEADER_IN_BLOCK) ?? false;

            int size = 0;

            size += writer.WriteInt(typeInstance.Version);
            size += writer.WriteWithSerializer(typeInstance.PreviousBlockHash !, protocolVersion, _uInt256Serializator);
            size += writer.WriteWithSerializer(typeInstance.MerkleRoot !, protocolVersion, _uInt256Serializator);
            size += writer.WriteUInt(typeInstance.TimeStamp);
            size += writer.WriteUInt(typeInstance.Bits);
            size += writer.WriteUInt(typeInstance.Nonce);

            // serialized headers contains this byte, while header serialized as block doesn't because it's serialized as part of the transactions array
            if (!headerInBlock)
            {
                // protocol still expect this value to be set for Headers payload
                size += writer.WriteVarInt(0);
            }

            return(size);
        }
示例#8
0
 public void WriteTo(IBufferWriter <byte> output)
 {
     output.WriteVarInt(buffer.Length);
     buffer.CopyTo(output);
 }
 public static void WriteVarInt(this IBufferWriter <byte> output, uint num) => output.WriteVarInt((ulong)num);
        public static void WriteVarInt(this IBufferWriter <byte> output, long num)
        {
            var n = (ulong)((num << 1) ^ (num >> 63));

            output.WriteVarInt(n);
        }
        public static void WriteVarInt(this IBufferWriter <byte> output, int num)
        {
            var n = (uint)((num << 1) ^ (num >> 31));

            output.WriteVarInt((ulong)n);
        }