public IEnumerator CoMiner() { while (true) { var txs = new HashSet <Transaction <PolymorphicAction <ActionBase> > >(); var task = Task.Run(() => { var block = _blocks.MineBlock(Address); _swarm.BroadcastBlocks(new[] { block }); return(block); }); yield return(new WaitUntil(() => task.IsCompleted)); if (!task.IsCanceled && !task.IsFaulted) { var block = task.Result; Debug.Log($"created block index: {block.Index}, difficulty: {block.Difficulty}"); #if BLOCK_LOG_USE FileHelper.AppendAllText("Block.log", task.Result.ToVerboseString()); #endif } else { var invalidTxs = txs; var retryActions = new HashSet <IImmutableList <PolymorphicAction <ActionBase> > >(); if (task.IsFaulted) { foreach (var ex in task.Exception.InnerExceptions) { if (ex is InvalidTxNonceException invalidTxNonceException) { var invalidNonceTx = _blocks.Transactions[invalidTxNonceException.TxId]; if (invalidNonceTx.Signer == Address) { Debug.Log($"Tx[{invalidTxNonceException.TxId}] nonce is invalid. Retry it."); retryActions.Add(invalidNonceTx.Actions); } } if (ex is InvalidTxException invalidTxException) { Debug.Log($"Tx[{invalidTxException.TxId}] is invalid. mark to unstage."); invalidTxs.Add(_blocks.Transactions[invalidTxException.TxId]); } Debug.LogException(ex); } } _blocks.UnstageTransactions(invalidTxs); foreach (var retryAction in retryActions) { MakeTransaction(retryAction, true); } } } }
/// <summary> /// 写入单条日志信息 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="info"></param> public virtual void Write <T>(T info) where T : Model.BaseInfo { if (info == null) { return; } // 注意:取类型名称时,不采用 info.GetType().Name ,因为可能有继承情况 string filePath = GetFilePath(typeof(T)); string xml = ObjectToText(info); string contents = xml + "\r\n\r\n" + s_separateLine + "\r\n\r\n"; FileHelper.AppendAllText(filePath, contents, Encoding.UTF8, s_maxLength); }
protected virtual void WriteLog(LogLevel level, string message, Exception exception) { try { if (Client.Config.Logs.InternalLog.Disable) { return; } if (level < Client.Config.Logs.InternalLog.MinLevel) { return; } string path = GetLogPath(); string dir = Path.GetDirectoryName(path); var encoding = Client.Config.Logs.InternalLog.Encoding; if (exception != null) { message = message + Environment.NewLine + exception; } message = DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss") + " " + level.ToString().PadRight(7) + " " + message + Environment.NewLine; lock (this) { if (string.IsNullOrEmpty(dir) == false) { if (Directory.Exists(dir) == false) { Directory.CreateDirectory(dir); } } FileHelper.AppendAllText(path, message, encoding); } } catch { } }
/// <summary> /// 批量写入日志信息 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list"></param> public virtual void Write <T>(List <T> list) where T : Model.BaseInfo { if (list == null || list.Count == 0) { return; } // 注意:取类型名称时,不采用 info.GetType().Name ,因为可能有继承情况 string filePath = GetFilePath(typeof(T)); StringBuilder sb = new StringBuilder(); foreach (T info in list) { string xml = ObjectToText(info); string contents = xml + "\r\n\r\n" + s_separateLine + "\r\n\r\n"; sb.Append(contents); } if (sb.Length > 0) { FileHelper.AppendAllText(filePath, sb.ToString(), Encoding.UTF8, s_maxLength); } }