Ejemplo n.º 1
0
        private void SubscribeToChannels()
        {
            foreach (var channelInfo in typeof(Constants.PUBSUB_CHANNEL).GetProperties())
            {
                _ILogger.LogInformation($"Subscribing to Channel : {channelInfo.Name}");

                this.Subscriber.Subscribe(string.Concat("*", "-", channelInfo.Name)).OnMessage(channelMessage =>
                {
                    //Note: exceptions are caught and discarded by StackExchange.Redis here, to prevent cascading failures. To handle failures, use a try/catch inside your handler to do as you wish with any exceptions.

                    //Discard Self published message
                    if (((string)channelMessage.Channel).StartsWith(Constants.PUBSUB_CHANNEL_PPREFIX))
                    {
                        _ILogger.LogInformation($"Discarded Self Published Message from Channel : {(string)channelMessage.Channel}");
                        return;
                    }
                    _ILogger.LogInformation($"Channel : {(string)channelMessage.Channel} => {(string)channelMessage.Message} ");

                    switch (((string)channelMessage.Channel).Substring(37, ((string)channelMessage.Channel).Length - 37))
                    {
                    case "BLOCKCHAIN":
                        try
                        {
                            var newChain = JsonConvert.DeserializeObject <ReadOnlyCollection <Block> >(channelMessage.Message);
                            if (this._IBlockChain.ReplaceChain(newChain))
                            {
                                _ITransactionPool.ClearBlockchainTransaction(this._IBlockChain.LocalChain);
                            }
                        }
                        catch (Exception ex)
                        {
                            _ILogger.LogError(ex, $"Error while processing a message from Channel : BLOCKCHAIN");
                        }
                        break;

                    case "TRANSACTION":
                        try
                        {
                            var newTransaction = JsonConvert.DeserializeObject <Transaction>(channelMessage.Message);
                            this._ITransactionPool.SetTransaction(newTransaction);
                        }
                        catch (Exception ex)
                        {
                            _ILogger.LogError(ex, $"Error while processing a message from Channel : TRANSACTION");
                        }
                        break;

                    default:
                        _ILogger.LogInformation($"Discarded a message from Channel : {(string)channelMessage.Channel}");
                        break;
                    }
                });
            }
        }
Ejemplo n.º 2
0
        public async Task MineTransaction()
        {
            //ToDo
            //get transaction pool's valid transactions
            //generate the miner reward
            //add a block consisting of these transactions to the blockchain
            //broadcast the updated  blockchain
            //clear the pool

            var validTransactions = _ITransactionPool.ValidTransactions();
            var minerReward       = Transaction.RewardTransaction(this._IWallet);

            validTransactions.Add(minerReward);
            _IBlockChain.AddBlock(validTransactions);
            await _IRedis.BroadcastChain();

            _ITransactionPool.ClearBlockchainTransaction(_IBlockChain.LocalChain);
        }