public void Add(byte[] key, byte[] value) { LinearDictionaryCoder.Add(key, ByteManipulator.GetBytes((uint)serialCoder.FileLength)); reverseLookupLinearDictionaryCoder.Add(ByteManipulator.GetBytes((uint)serialCoder.FileLength), key); serialCoder.Append(value); Count++; }
public static byte[] Encode(IEnumerable <byte[]> input) { var items = new List <byte[]>(); var totalLength = 0; /*if (bytes != null && bytes.Length != 0) * { * var truncatedBytes = ByteManipulator.TruncateMostSignificatZeroBytes(bytes); * * if (truncatedBytes[0] != 0) * { * items.Add(truncatedBytes); * totalLength += bytes.Length; * } * else * { * items.Add(new byte[] { 0x80 }); //empty byte[] * totalLength += 1; * } * }*/ foreach (var bytes in input.Select(Encode)) { if (bytes != null && bytes.Length != 0 && bytes[0] != 0) { items.Add(bytes); totalLength += bytes.Length; } else { items.Add(new byte[] { 0x80 }); //empty byte[] totalLength += 1; } } if (totalLength == 0x80) { items.Insert(0, new byte[] { 0x80 }); } else if (totalLength <= SizeThreshold) { items.Insert(0, new[] { Convert.ToByte(ShortCollectionOffset + totalLength) }); } else { var lengthBytes = ByteManipulator.GetBytes((uint)totalLength); var lengthBytesCount = GetLength(totalLength); items.Insert(0, new[] { Convert.ToByte(LargeCollectionOffset + lengthBytesCount) }); for (int i = lengthBytes.Length - 1; i >= lengthBytes.Length - lengthBytesCount; i--) { items.Insert(1, new[] { lengthBytes[i] }); } } return(items.SelectMany(x => x).ToArray()); }
public void RemoveTopItem() { var topItemOffset = serialCoder.GetIndexOffset(serialCoder.Count - 1); serialCoder.RemoveByOffset(topItemOffset); var reverseKey = ByteManipulator.GetBytes((uint)topItemOffset); var key = reverseLookupLinearDictionaryCoder.Get(reverseKey); LinearDictionaryCoder.Remove(key); reverseLookupLinearDictionaryCoder.Remove(reverseKey); }
public static byte[] Encode(byte[] bytes) { if (bytes == null) { return(null); } else { bytes = ByteManipulator.TruncateMostSignificatZeroBytes(bytes); } if (bytes.Length == 1 && bytes[0] < 128) { return(new byte[] { bytes[0] }); } if (bytes.Length == 0) { return(new byte[0]); } if (bytes[0] == 0) { return(new byte[0]); } if (bytes.Length <= SizeThreshold) { var newBytes = new byte[bytes.Length + 1]; newBytes[0] = Convert.ToByte(ShortItemOffset + bytes.Length); Array.Copy(bytes, 0, newBytes, 1, bytes.Length); return(newBytes); } else { var lengthBytesCount = GetLength(bytes.Length); var lengthBytes = ByteManipulator.GetBytes((uint)bytes.Length); var newBytes = new byte[bytes.Length + lengthBytesCount + 1]; newBytes[0] = Convert.ToByte(LargeItemOffset + lengthBytesCount); for (int i = lengthBytes.Length - 1; i >= lengthBytes.Length - lengthBytesCount; i--) { newBytes[i - 3 + lengthBytesCount] = lengthBytes[i]; } Array.Copy(bytes, 0, newBytes, 1 + lengthBytesCount, bytes.Length); return(newBytes); } }
public bool ApplyDiff(Dictionary <byte[], Tuple <long, LargeInteger> > diff) { var ret = true; foreach (var kvp in diff) { if (kvp.Value.Item2 < 0) { ret = false; break; } } if (ret) { foreach (var kvp in diff) { //update balances if (balanceCoder.ContainsKey(kvp.Key)) { balanceCoder.Replace(kvp.Key, kvp.Value.Item2.GetBytes()); } else { balanceCoder.Add(kvp.Key, kvp.Value.Item2.GetBytes()); } //update nonces if (nonceCoder.ContainsKey(kvp.Key)) { if (kvp.Value.Item1 >= 0) { nonceCoder.Replace(kvp.Key, ByteManipulator.GetBytes((uint)kvp.Value.Item1)); } else { nonceCoder.Remove(kvp.Key); } } else { if (kvp.Value.Item1 >= 0) { nonceCoder.Add(kvp.Key, ByteManipulator.GetBytes((uint)kvp.Value.Item1)); } } } } return(ret); }
public byte[] Serialize() { var tx = new byte[][] { ByteManipulator.GetBytes(Nonce), Ammount.GetBytes(), Fee.GetBytes(), Source, Destination, Signature, Network }; return(RLP.Encode(tx)); }
public async void AddContact(string url) { if (url != null) { var hostnameBytes = ToBytes(url); if (hostnameBytes != null) { if (!coder.ContainsKey(ByteManipulator.BigEndianTruncate(hostnameBytes, 128))) { if (await Handshake(url)) { coder.Add(hostnameBytes, ByteManipulator.GetBytes((uint)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds)); } } } } }
public async Task <bool> AddContact(string url) { var ret = false; if (url != null) { var hostnameBytes = ToBytes(url); if ((hostnameBytes != null) && (url != Program.Settings.url)) { if (!coder.ContainsKey(ByteManipulator.BigEndianTruncate(hostnameBytes, 128))) { if (await Handshake(url)) { coder.Add(hostnameBytes, ByteManipulator.GetBytes((uint)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds)); ret = true; } } } } return(ret); }
public void Append(byte[] data) { var bytes = new byte[LengthBytes + data.Length]; var lengthData = ByteManipulator.GetBytes((uint)data.Length); for (int i = 0; i < LengthBytes; i++) { bytes[i] = lengthData[i + 4 - LengthBytes]; } for (int i = 0; i < data.Length; i++) { bytes[i + LengthBytes] = data[i]; } fileStream.Position = FileLength; fileStream.Write(bytes); fileStream.Flush(); FileLength = fileStream.Length; Count++; }
public byte[] Serialize() { var transactionByteList = new List <byte[]>(); foreach (var transaction in Transactions) { transactionByteList.Add(transaction.Serialize()); } var serializedTransactions = RLP.Encode(transactionByteList); var block = new byte[][] { ByteManipulator.GetBytes((uint)Height), ByteManipulator.GetBytes(Timestamp), Difficulty.GetBytes(), Nonce, MinerAddress, PreviousBlockHash, serializedTransactions }; return(RLP.Encode(block)); }