public void TruncateZeroBytesUnitTest() { var input = new byte[] { 0, 0, 0, 123, 45, 0, 11 }; var expected = new byte[] { 123, 45, 0, 11 }; var actual = ByteManipulator.TruncateMostSignificatZeroBytes(input); for (int i = 0; i < expected.Length; i++) { Assert.AreEqual(expected[i], actual[i]); } }
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 string GetRandomUrl() { if (coder.LinearCoder.Count != 0) { var bytes = coder.LinearCoder.Read(random.Next(0, coder.LinearCoder.Count - 1)); bytes = ByteManipulator.TruncateMostSignificatZeroBytes(bytes.Take(bytes.Length - 4).ToArray()); return(Encoding.UTF8.GetString(bytes)); } else { return(null); } }
async Task StartMiner(IServiceProvider serviceProvider) { try { var miner = (Miner)serviceProvider.GetService(typeof(Miner)); var contactLedger = (ContactLedger)serviceProvider.GetService(typeof(ContactLedger)); var httpProvider = (HttpProvider)serviceProvider.GetService(typeof(HttpProvider)); while (Program.Settings.mine) { miner.UpdateParameters(); var block = await miner.MineRound(); if (block != null) //relay new block { var difficultyBytes = ByteManipulator.TruncateMostSignificatZeroBytes(block.Difficulty.GetBytes()); var firstDifficultyBytes = new byte[4]; for (int i = 0; i < firstDifficultyBytes.Length; i++) { firstDifficultyBytes[i] = difficultyBytes[i]; } Console.WriteLine($"[Miner] Block found! Height: {block.Height}, Tx count: {block.Transactions.Count}, Difficulty: {HexConverter.ToPrefixString(firstDifficultyBytes)} {difficultyBytes.Length - firstDifficultyBytes.Length} B, Block time: {miner.BlockTime} s, Hashrate: {miner.NumberOfAttempts / ((miner.BlockTime == 0 ? 1 : miner.BlockTime) * 1000)} kH/s"); List <string> contacts = null; lock (GateKeeper.ContactLock) { contacts = contactLedger.GetAll(); } foreach (var contact in contacts) { httpProvider.PeerPost <string, object>(HexConverter.ToPrefixString(block.Serialize()), contact + "block/new"); } } } } catch (Exception e) { Console.WriteLine($"[Miner] Exception occured"); Console.WriteLine($"[Miner] Stack trace: {e.StackTrace}"); Console.WriteLine($"[Miner] Message: {e.Message}"); } }
private Tuple <string, DateTime> DecodeEntry(byte[] entry) { return(new Tuple <string, DateTime>(Encoding.UTF8.GetString(ByteManipulator.TruncateMostSignificatZeroBytes(entry.Take(entry.Length - 4).ToArray())), DateTimeOffset.FromUnixTimeSeconds(ByteManipulator.GetUInt32(entry.Skip(128).ToArray())).DateTime)); }
private void buttonConnect_Click(object sender, RoutedEventArgs e) { var destination = HexConverter.ToBytes(textBoxDestination.Text); if (destination != null) { double ammountDouble; if (double.TryParse(textBoxAmmount.Text, out ammountDouble)) { double feeDouble; if (double.TryParse(textBoxFee.Text, out feeDouble)) { var balance = ConnectionJob.GetSmallUnitsBalance(httpProvider, SenderWallet.PublicKey); if (balance != null) { var ammount = GetSmallestUnits(ammountDouble); var fee = GetSmallestUnits(feeDouble); if (_nonce == null) { _nonce = httpProvider.PeerPost <string, uint>(HexConverter.ToPrefixString(SenderWallet.PublicKey), @"http://127.0.0.1:8125/transaction/count").Result; } else { _nonce = _nonce + 1; } if ((balance + fee - 1) > ammount) { var tx = new Transaction() { Ammount = ammount, Destination = ByteManipulator.TruncateMostSignificatZeroBytes(destination), Fee = fee, Network = new byte[] { 1 }, Nonce = _nonce.Value, Source = ByteManipulator.TruncateMostSignificatZeroBytes(SenderWallet.PublicKey) }; tx.Sign(SenderWallet.PrivateKey); var response = httpProvider.PeerPost <string, string>(HexConverter.ToPrefixString(tx.Serialize()), @"http://127.0.0.1:8125/transaction/relay").Result; if (response != "ok") { MessageBox.Show("Sending transaction failed", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } else //insufficient balance { MessageBox.Show("Insufficient balance", "Error", MessageBoxButton.OK, MessageBoxImage.Information); } } else //could not fetch balance { MessageBox.Show("Could not fetch nonce", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } else //invalid fee { MessageBox.Show("Invalid fee", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } else //invalid ammount { MessageBox.Show("Invalid invalid ammount", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } else //invalid destination { MessageBox.Show("Invalid invalid destination", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }