Exemplo n.º 1
0
 public TxMQ(MQSetting setting)
 {
     _setting = setting;
     _factory = new ConnectionFactory();
     if (!string.IsNullOrEmpty(_setting.UserName))
     {
         _factory.UserName = _setting.UserName;
     }
     if (!string.IsNullOrEmpty(_setting.UserName))
     {
         _factory.UserName = _setting.UserName;
     }
     if (!string.IsNullOrEmpty(_setting.Password))
     {
         _factory.Password = _setting.Password;
     }
     if (!string.IsNullOrEmpty(_setting.VirtualHost))
     {
         _factory.VirtualHost = _setting.VirtualHost;
     }
     if (_setting.Port != 0)
     {
         _factory.Port = _setting.Port;
     }
     if (!string.IsNullOrEmpty(_setting.Host))
     {
         _factory.HostName = _setting.Host;
     }
 }
Exemplo n.º 2
0
        public Node(
            string channelId,
            ILoggerFactory loggerFactory,
            IAssemblyProvider assemblyProvider,
            IIdentityProvider identityProvider,
            DataManager dataManager,
            MQSetting mQSetting,
            IHttpClientFactory clientFactory,
            IMemoryCache memoryCache
            )
        {
            _memoryCache   = memoryCache;
            _mq            = mQSetting;
            _channelId     = channelId;
            _dataManager   = dataManager;
            _loggerFactory = loggerFactory;
            _logger        = _loggerFactory.CreateLogger <Node>();

            //链码执行器
            _chainCodeExecutor = new ChainCodeExecutor(assemblyProvider, identityProvider, _dataManager);
            //节点通讯提供者
            //var peerProvider = new GrpcPeerProvider(loggerFactory, dataManager);
            var peerProvider = new PeersProvider(loggerFactory, dataManager, clientFactory);
            //背书策略提供者
            var policeProvider = new PolicyProvider(loggerFactory, identityProvider, peerProvider, dataManager);

            //配置提供
            _configProvider = new ConfigProvider(assemblyProvider, policeProvider, identityProvider, peerProvider, _mq);
            //交易池
            TxPool = new TxPool(loggerFactory, _configProvider, _dataManager, this, _memoryCache);
            //节点状态
            _stateprovider = new StateProvider(_configProvider, this, _loggerFactory, _dataManager);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 初始化ActiveMQ消息服务器
        /// </summary>
        /// <param name="setting"></param>
        private void InitActiveMQ(MQSetting setting)
        {
            XTrace.WriteLine($"初始化消息服务 {Enum.GetName(typeof(MQTypeEnum), MQType)} ……");
            ServerUri = setting.ServerUri.Trim();
            UserName  = setting.UserName;
            Password  = setting.Password;
            if (connectionFactory == null)
            {
                connectionFactory = new ConnectionFactory(ServerUri);
            }

            // 初始化到消息服务器的连接
            try
            {
                if (Connection == null)
                {
                    Connection = connectionFactory.CreateConnection(UserName, Password);
                }
                if (!Connection.IsStarted)
                {
                    Connection.Start();
                }
            }
            catch (Exception ex)
            {
                exceptionCount++;
                XTrace.WriteException(ex);
                throw ex;
            }
            XTrace.WriteLine($"初始化消息服务 {Enum.GetName(typeof(MQTypeEnum), MQType)} 完成。");
        }
Exemplo n.º 4
0
 /// <summary>
 /// 构造函数
 /// </summary>
 public RabbitMQConsumerApplication(
     IOptions <MQSetting> mqSetting,
     IOptions <List <ConsumerSetting> > consumerSettings,
     IConfiguration configuration,
     IRedisServices redis
     )
 {
     _redis            = redis;
     _mqSetting        = mqSetting.Value;
     _consumerSettings = consumerSettings.Value;
     _configuration    = configuration;
 }
Exemplo n.º 5
0
 public ConfigProvider(
     IAssemblyProvider assemblyProvider,
     IPolicyProvider policyProvider,
     IIdentityProvider identityProvider,
     IPeersProvider peersProvider,
     MQSetting mQSetting)
 {
     _settings         = new InMemorySettings();
     _assemblyProvider = assemblyProvider;
     _identityProvider = identityProvider;
     _policyProvider   = policyProvider;
     _peersProvider    = peersProvider;
     _mQSetting        = mQSetting;
 }
Exemplo n.º 6
0
 public NodePeer(
     ILoggerFactory loggerFactory,
     IMemoryCache memoryCache,
     IBlockDatabaseSettings blockSetting,
     IHistoryDatabaseSettings historySetting,
     IStatusDatabaseSettings statusSetting,
     IAssemblyProvider assemblyProvider,
     IIdentityProvider identityProvider,
     MQSetting mq,
     IHttpClientFactory httpClientFactory
     )
 {
     _clientFactory           = httpClientFactory;
     _mq                      = mq;
     _identityProvider        = identityProvider;
     _assemblyProvider        = assemblyProvider;
     _blockDatabaseSetting    = blockSetting;
     _historyDatabaseSettings = historySetting;
     _statusDatabaseSettings  = statusSetting;
     _memoryCache             = memoryCache;
     _loggerFactory           = loggerFactory;
     _logger                  = loggerFactory.CreateLogger <NodePeer>();
 }
Exemplo n.º 7
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddGrpc();

            #region EF数据库配置 (临时)

            //services.AddDbContext<BlockContext>(options =>
            //options.UseSqlServer(Configuration.GetConnectionString("BlockContext")), ServiceLifetime.Transient);

            #endregion

            #region raft网络相关配置
            services.AddMemoryCache();

            services.AddScoped <IInvokeBLL, InvokeBLL>();
            services.AddScoped <IQueryBLL, QueryBLL>();
            services.AddScoped <IIdentityProvider, IdentityProvider>();
            //services.AddScoped<IPeersProvider, GrpcPeerProvider>();
            services.AddScoped <IAssemblyProvider, AssemblyProvider>();
            //services.AddScoped<IChainCodeExecutor, ChainCodeExecutor>();
            //services.AddScoped<IPolicyProvider, PolicyProvider>();
            //services.AddScoped<IConfigProvider, ConfigProvider>();
            services.AddScoped <IRaftNetService, RaftNetService>();
            services.AddScoped <QMBlockServer.Service.Intetface.ITxService, QMBlockServer.Service.Imp.TxService>();
            #endregion

            #region mongoDB配置
            //区块数据
            services.Configure <BlockDatabaseSettings>(
                Configuration.GetSection(nameof(BlockDatabaseSettings)));
            services.AddSingleton <IBlockDatabaseSettings>(sp =>
                                                           sp.GetRequiredService <IOptions <BlockDatabaseSettings> >().Value);
            //历史数据库
            services.Configure <HistoryDatabaseSettings>(
                Configuration.GetSection(nameof(HistoryDatabaseSettings)));
            services.AddSingleton <IHistoryDatabaseSettings>(sp =>
                                                             sp.GetRequiredService <IOptions <HistoryDatabaseSettings> >().Value);
            //状态数据库
            services.Configure <StatusDatabaseSettings>(
                Configuration.GetSection(nameof(StatusDatabaseSettings)));
            services.AddSingleton <IStatusDatabaseSettings>(sp =>
                                                            sp.GetRequiredService <IOptions <StatusDatabaseSettings> >().Value);

            #endregion

            #region CA账号与节点账号
            var mspFile  = Configuration.GetSection("peerIdentity:MspFile").Value;
            var basepath = AppContext.BaseDirectory;
            var account  = new UserAccount();
            //如果文件存在
            var path = Path.Combine(basepath, mspFile);
            if (File.Exists(Path.Combine(basepath, mspFile)))
            {
                var mspdata = File.ReadAllText(mspFile).Trim();
                account = Newtonsoft.Json.JsonConvert.DeserializeObject <UserAccount>(mspdata);
            }

            var caAccount = new CaAccount()
            {
                Username = Configuration.GetSection("peerIdentity:ca:username").Value,
                Password = Configuration.GetSection("peerIdentity:ca:password").Value
            };
            PeerIdentity peerIdentity = new PeerIdentity()
            {
                Address     = Configuration.GetSection("peerIdentity:peerUrl").Value,
                OrgId       = Configuration.GetSection("peerIdentity:OrgId").Value,
                Certificate = account.Certificate,
                PrivateKey  = account.PrivateKey,
                Name        = account.Certificate?.TBSCertificate.Subject
            };

            services.AddSingleton <UserAccount>(account);
            services.AddSingleton <CaAccount>(caAccount);
            services.AddSingleton <PeerIdentity>(peerIdentity);

            #endregion

            #region auth身份验证与授权
            var    securityKey = string.IsNullOrEmpty(account.PrivateKey) ? "securityKey" : account.PrivateKey;     //configuration.GetSection("peerIdentity:privateKey").ToString();
            string name        = account.Certificate == null ? "name" : account.Certificate.TBSCertificate.Subject; //configuration.GetSection("peerIdentity:name").ToString();
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer = true,     //是否验证Issuer
                    //ValidateAudience = true,//是否验证Audience
                    ValidateLifetime = true,   //是否验证失效时间
                    //验证码有效时间
                    ClockSkew = TimeSpan.FromSeconds(30),
                    ValidateIssuerSigningKey = true,                                                 //是否验证SecurityKey
                    //ValidAudience = Const.Domain,//Audience
                    ValidIssuer      = name,                                                         //Issuer,这两项和前面签发jwt的设置一致
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(securityKey)) //拿到SecurityKey
                };
            });
            services.AddAuthorization();
            #endregion

            #region RabbitMQ消息队列配置
            var mq = new MQSetting();
            Configuration.GetSection("RabbitMQ").Bind(mq);
            //var mqsetting = Configuration.Get<MQSetting>( "RabbitMQ"); .Bind<MQSetting>("RabbitMQ");
            services.AddSingleton(mq);
            #endregion

            services.AddScoped <NodePeer>();
        }