public void GenerateNextblock(string senderAddress, Node.Callback callback) { List <Transaction> blockData = new List <Transaction>(); Transaction coinBaseTx = Transaction.CreateCoinbaseTransaction(senderAddress, blocks.Last().index + 1); blockData.Add(coinBaseTx); blockData.AddRange(transactionPool.GetTransactions()); GenerateRawNextblock(blockData, callback); }
public void GenerateRawNextblock(IEnumerable <Transaction> data, Node.Callback callback) { Block prev = blocks.Last(); int difficulty = GetUpdatedDifficulty(); int nextIndex = prev.index + 1; DateTime nextTimestamp = DateTime.Now; currentlyMining = Instantiate(blockPrefab); currentlyMining.SetColor(chainColor); currentlyMining.Init(nextIndex, nextTimestamp, prev.hash, data.ToArray(), difficulty); PlaceBlock(currentlyMining); miningCoroutine = currentlyMining.MineBlock(() => { AddBlock(currentlyMining); currentlyMining = null; callback(); }); StartCoroutine(miningCoroutine); }
/// <summary> /// Mine the block with unity animations /// </summary> /// <param name="onMined">callback upon block successfuly found</param> /// <returns></returns> public IEnumerator MineBlock(Node.Callback onMined) { Canvas animatedHash; Color plainColor = blockSprite.color; Color fadedColor = new Color(plainColor.r, plainColor.g, plainColor.b, 0.2f); do { yield return(new WaitForSeconds(1 / 20f)); // limit the mining speed to make the animation more readable nonce++; hash = ComputeHash(); blockSprite.color = Color.Lerp(plainColor, fadedColor, Time.time % 1); animatedHash = Instantiate(animatedHashPrefab, transform, false); animatedHash.GetComponentInChildren <Text>().text = hash; } while (!Cryptography.HashMatchesDifficulty(hash, difficulty)); animatedHash.GetComponent <Animator>().SetTrigger("MatchDifficulty"); animatedHash.GetComponentInChildren <Text>().color = Color.green; blockSprite.color = plainColor; onMined(); }