public async Task <Exception> AddSelfEvent() { if (TransactionPool.Count == 0) { logger.Debug("Empty TxPool"); return(null); } //create new event with self head and empty other parent //empty transaction pool in its payload var newHead = new Event(TransactionPool.ToArray(), BlockSignaturePool.ToArray(), new[] { Head, "" }, PubKey(), Seq + 1); var err = await SignAndInsertSelfEvent(newHead); if (err != null) { return(new CoreError($"Error inserting new head: {err.Message}", err)); } logger.Debug("Created Self-Event Transactions={TransactionCount}; BlockSignatures={BlockSignatureCount}", TransactionPool.Count, BlockSignaturePool.Count); TransactionPool.Clear(); BlockSignaturePool.Clear(); return(null); }
public async Task FindNonce() { await Task.Run(() => { lock (lockObject_) { ulong nonce = 0; do { NewBlock.Nonce = nonce; var hash = GetHash(NewBlock); if (hash < Target) { BlockChain.Add(NewBlock); NewBlock = null; LastHash = SerializeHash(hash); OnBlockChainChanged(); TransactionPool.Clear(); OnTransactionPoolChanged(); return; } ++nonce; } while (nonce != ulong.MaxValue); throw new Exception("nonce not found."); } }); }
public void CreateBlock() { lock (lockObject_) { NewBlock = new Block { Index = BlockChain.Last().Index + 1, PreviousHash = SerializeHash(GetHash(BlockChain.Last())), TimeStamp = DateTime.UtcNow, Transactions = TransactionPool.ToList(), }; TransactionPool.Clear(); } }
public void AppendBlock(Block block) { // Broadcast to nodes and have them validate the block var validation = 0; foreach (var node in Nodes) { if (node.Validate(block)) { validation++; } } // Append block with 51% consensus if (validation > Nodes.Count / 2) { Chain.Add(block); TransactionPool.Clear(); } }
public async Task <Exception> Sync(WireEvent[] unknownEvents) { logger.Debug("Sync unknownEvents={@unknownEvents}; transactionPool={transactionPoolCount}; blockSignaturePool={blockSignaturePoolCount}", unknownEvents.Length, TransactionPool.Count, BlockSignaturePool.Count); using (var tx = Hg.Store.BeginTx()) { string otherHead = ""; //add unknownEvents events int k = 0; Exception err; foreach (var we in unknownEvents) { //logger.Debug("wev={wev}",we.Body.CreatorId); Event ev; (ev, err) = await Hg.ReadWireInfo(we); if (err != null) { return(err); } //logger.Debug("ev={ev}",ev.Creator()); err = await InsertEvent(ev, false); if (err != null) { return(err); } //assume last event corresponds to other-head if (k == unknownEvents.Length - 1) { otherHead = ev.Hex(); } k++; } //create new event with self head and other head //only if there are pending loaded events or the transaction pool is not empty if (unknownEvents.Length > 0 || TransactionPool.Count > 0 || BlockSignaturePool.Count > 0) { var newHead = new Event(TransactionPool.ToArray(), BlockSignaturePool.ToArray(), new[] { Head, otherHead }, PubKey(), Seq + 1); err = await SignAndInsertSelfEvent(newHead); if (err != null) { return(new CoreError($"Error inserting new head: {err.Message}", err)); } //empty the pool TransactionPool.Clear(); BlockSignaturePool.Clear(); } return(null); } }