Beispiel #1
0
 public TxPool(ILoggerFactory logfactory, IConfigProvider configProvider,
               IBlockDataManager blockDataManager, INode node, IMemoryCache memoryCache)
 {
     _memoryCache      = memoryCache;
     _log              = logfactory.CreateLogger <TxPool>();
     _configProvider   = configProvider;
     _txList           = new List <Envelope>();
     _timer            = new Timer(ResetElectionTimer, null, _configProvider.GetBatchTimeout(), _configProvider.GetBatchTimeout());
     _blockDataManager = blockDataManager;
     _node             = node;
 }
Beispiel #2
0
        /// <summary>
        /// 打包
        /// </summary>
        private void BlockPacking()
        {
            try
            {
                _timer.Change(Timeout.Infinite, Timeout.Infinite);
                lock (packing)
                {
                    //对交易进行排序
                    List <Envelope> data = null;
                    lock (obj)
                    {
                        if (_txList.Count == 0)
                        {
                            return;
                        }
                        data = _txList.Where(p => true).ToList();
                        _txList.Clear();
                    }

                    #region 组装区块数据
                    //var txmq = new Msg.Imp.TxMQ(_configProvider.GetMQSetting());

                    var currentBlock = _blockDataManager.GetLastBlock(_node.GetChannelId());
                    var block        = new Block();
                    block.Signer.Identity = _configProvider.GetPeerIdentity().GetPublic();
                    //交易验证
                    data = Validate(data);
                    //错误交易发送
                    //txmq.PublishTxResponse(errorTx, "交易失败");

                    block.Data.Envelopes   = data;
                    block.Header.Timestamp = DateTime.Now.Ticks;
                    block.Header.Number    = currentBlock.Header.Number + 1;
                    block.Header.ChannelId = _node.GetChannelId();
                    //Term
                    block.Header.PreviousHash = currentBlock.Header.DataHash;
                    block.Header.DataHash     = RSAHelper.GenerateMD5(Newtonsoft.Json.JsonConvert.SerializeObject(block));
                    block.Signer.Signature    = RSAHelper.SignData(_configProvider.GetPrivateKey(), block);

                    #endregion

                    #region 区块分发

                    Concensus.Messages.HandOutResponse ts = null;
                    try
                    {
                        _log.LogWarning("区块分发开始");
                        ts = _node.BlockHandOut(block).Result;
                        _log.LogWarning("区块分发结束");
                    }
                    catch (Exception ex)
                    {
                        ts = new Concensus.Messages.HandOutResponse
                        {
                            Message = ex.Message,
                            Success = false
                        };
                        _log.LogWarning("区块分发错误");
                        _log.LogError(ex, ex.Message);
                    }

                    #endregion

                    #region  链结果消息发送

                    foreach (var item in block.Data.Envelopes)
                    {
                        _memoryCache.Set("tx_" + item.TxReqeust.Data.TxId, ts.Success ? "1" : "2", TimeSpan.FromMinutes(10));
                    }

                    //if (ts.Success)
                    //{
                    //    //txmq.PublishTxResponse(block, null);
                    //}
                    //else
                    //{
                    //    //txmq.PublishTxResponse(block, ts.Message);
                    //}

                    /*
                     * if (ts.Success)
                     * {
                     *  foreach (var item in block.Data.Envelopes)
                     *  {
                     *      //
                     *      // _txStatus.Add(item.TxReqeust.Data.TxId, true);
                     *  }
                     * }
                     * else
                     * {
                     *  //通知  block中的交易失败
                     *  foreach (var item in block.Data.Envelopes)
                     *  {
                     *      _txStatus.Add(item.TxReqeust.Data.TxId, false);
                     *  }
                     * }
                     */
                    #endregion
                }
            }
            catch (Exception ex)
            {
                _log.LogError(ex, ex.Message);
            }
            finally
            {
                _timer.Change(_configProvider.GetBatchTimeout(), _configProvider.GetBatchTimeout());
            }
        }