Example #1
0
        public async Task Execute(IJobExecutionContext context)
        {
            tempBlockChains = await blockChainsDbContext.AllBlockChains.SingleOrDefaultAsync(m => m.ID == 1);

            //First time scheduler is running
            if (tempBlockChains == null)
            {
                //Initialize all block chains with genesis block
                var newBlockChains = new StoreBlockChains();
                newBlockChains.PureMedBlockChain    = JsonConvert.SerializeObject(PureMedBlockChain.Chain);
                newBlockChains.Verifier_1BlockChain = JsonConvert.SerializeObject(PureMedBlockChain.Chain);
                newBlockChains.Verifier_2BlockChain = JsonConvert.SerializeObject(PureMedBlockChain.Chain);
                newBlockChains.Verifier_3BlockChain = JsonConvert.SerializeObject(PureMedBlockChain.Chain);
                newBlockChains.Difficulty           = PureMedBlockChain.Difficulty;

                //Store Initialized blockChains in database
                await blockChainsDbContext.AllBlockChains.AddAsync(newBlockChains);

                await blockChainsDbContext.SaveChangesAsync();

                //Initialize local blockchains to genesis block
                tempBlockChains = new StoreBlockChains();
                tempBlockChains.PureMedBlockChain    = JsonConvert.SerializeObject(PureMedBlockChain.Chain);
                tempBlockChains.Verifier_1BlockChain = JsonConvert.SerializeObject(PureMedBlockChain.Chain);
                tempBlockChains.Verifier_2BlockChain = JsonConvert.SerializeObject(PureMedBlockChain.Chain);
                tempBlockChains.Verifier_3BlockChain = JsonConvert.SerializeObject(PureMedBlockChain.Chain);
                tempBlockChains.Difficulty           = PureMedBlockChain.Difficulty;
            }

            //Get mainBlockChain from database
            PureMedBlockChain.Chain      = JsonConvert.DeserializeObject <List <Block> >(tempBlockChains.PureMedBlockChain);
            PureMedBlockChain.Difficulty = tempBlockChains.Difficulty;

            //Get all pending transactions till now and store them in mining transactions for mining
            PureMedBlockChain.MiningTransactions  = PureMedBlockChain.PendingTransactions;
            PureMedBlockChain.PendingTransactions = new List <Transaction>();

            //Create and mine new block
            Block tempBlock = new Block(PureMedBlockChain.GetLatestBlock().CurrentHash, PureMedBlockChain.MiningTransactions, PureMedBlockChain.Difficulty);
            await tempBlock.MineBlock(PureMedBlockChain.Difficulty);

            //Fire up first verifier to verify if new block is okay
            if (await Verifier1(tempBlock) == true)
            {
                logger.LogInformation($"Block mined with hash : {tempBlock.CurrentHash}. Log Timestamp : {DateTime.Now.ToLongTimeString()}.");
            }
            else
            {
                //In Case block is not okay, restore all transactions to pending transactions
                PureMedBlockChain.PendingTransactions.AddRange(PureMedBlockChain.MiningTransactions);
                PureMedBlockChain.MiningTransactions = new List <Transaction>();
                logger.LogError($"Block can't be mined or verified. Log Timestamp : {DateTime.Now.ToLongDateString()}.");
            }
        }
Example #2
0
 public ScheduledMiner(IConfiguration cfg, IOptions <BlockChain> blockChain, BlockChainsDbContext sdb, ILogger <ScheduledMiner> log)
 {
     configuration        = cfg;
     PureMedBlockChain    = blockChain.Value;
     blockChainsDbContext = sdb;
     Verifier1Chain       = new Verifier_1BlockChain();
     Verifier2Chain       = new Verifier_2BlockChain();
     Verifier3Chain       = new Verifier_3BlockChain();
     tempBlockChains      = new StoreBlockChains();
     logger = log;
 }