public async Task <Block> MineRound(Wallet wallet) { minedBlock.MinerAddress = wallet.PublicKey; var difficultyBytes = minedBlock.Difficulty.GetBytes(); for (int i = 0; i < hashesPerRound; i++) { if (stop) { break; } minedBlock.Nonce = nonce.GetBytes(); numberOfAttempts = numberOfAttempts + 1; if (ArrayManipulator.IsGreater(ByteManipulator.BigEndianTruncate(difficultyBytes, 32), minedBlock.GetHash(), difficultyBytes.Length)) //new block found { stopWatch.Stop(); return(minedBlock); } nonce = nonce + 1; } return(null); }
public void Add(byte[] key, byte[] value) { value = ByteManipulator.BigEndianTruncate(value, DataLength); key = ByteManipulator.BigEndianTruncate(key, KeyLength); if (LinearCoder.Count == 0) { LinearCoder.Push(CreateEntry(key, value)); } else { int minimum = 0; int maximum = LinearCoder.Count - 1; while (minimum <= maximum) { int middle = (minimum + maximum) / 2; var middleValue = LinearCoder.Read(middle); if (ArrayManipulator.Compare(key, middleValue.Take(key.Length).ToArray())) { throw new Exception("Key already exists"); } else if (ArrayManipulator.IsGreater(key, middleValue, KeyLength)) { minimum = middle + 1; } else { maximum = middle - 1; } } var popCount = LinearCoder.Count - minimum; if ((maximum >= 0) && ArrayManipulator.IsGreater(LinearCoder.Read(maximum), key, KeyLength)) { popCount++; } List <byte[]> popedItems; if (popCount != 0) { popedItems = LinearCoder.BulkPop(popCount); } else { popedItems = new List <byte[]>(); } popedItems.Insert(0, CreateEntry(key, value)); LinearCoder.BulkPush(popedItems); } }
public void LittleEndianByteCompareUnitTest() { var inputA = new byte[] { 127, 56, 212, 36 }; var inputB = new byte[] { 127, 56, 14, 36 }; var actual = ArrayManipulator.IsGreater(inputA, inputB, inputA.Length); Assert.IsTrue(actual); }
public async Task <Block> MineRound() { var block = new Block() { Difficulty = difficulty, Height = height, MinerAddress = minerAddress, PreviousBlockHash = previousBlockHash, Timestamp = timestamp, Transactions = transactions }; var difficultyBytes = difficulty.GetBytes(); for (int i = 0; i < hashesPerRound; i++) { block.Nonce = nonce.GetBytes(); numberOfAttempts = numberOfAttempts + 1; if (ArrayManipulator.IsGreater(ByteManipulator.BigEndianTruncate(difficultyBytes, 32), block.GetHash(), difficultyBytes.Length)) //new block found { lock (GateKeeper.ChainManagerLock) { _chainManager.ProcessBlocks(new List <Block>() { block }); } lock (GateKeeper.TransactionPoolLock) { lock (GateKeeper.BalanceLedgerLock) { _transactionPool.Clean(); } } BlockTime = block.Timestamp - lastBlockTimeStamp; return(block); } nonce = nonce + 1; } return(null); }
private int GetIndex(byte[] key) { if (LinearCoder.Count == 1) { var value = LinearCoder.Read(0); if (!ArrayManipulator.Compare(key, value)) { return(-1); } return(0); } else if (key != null && key.Length == KeyLength) { int minimum = 0; int maximum = LinearCoder.Count - 1; while (minimum <= maximum) { int middle = (minimum + maximum) / 2; var middleValue = LinearCoder.Read(middle); if (ArrayManipulator.Compare(key, middleValue.Take(key.Length).ToArray())) { return(middle); } else if (ArrayManipulator.IsGreater(key, middleValue, KeyLength)) { minimum = middle + 1; } else { maximum = middle - 1; } } return(-1); } else { throw new Exception("Invalid key format"); } }