Ejemplo n.º 1
0
        public void SetBlockChain()
        {
            BlockChain bc = Repository.BlockChainMaster.BlockChain;

            Block latestBlock = Repository.BlockChainMaster.GetLatestBlock();

            Account senderAccount = HandleGetCase.GetAccount(transaction.SenderAccount.ID);

            if (transaction.SenderAccount.ID != transaction.ReceiverAccount.ID && senderAccount.TotalAmount < transaction.Amount)
            {
                throw new Exception("Invalid Transaction");
            }
            //Determine the size of encrypt transaction + Size of Existing block,
            if ((GetSize(transaction) + GetSize(latestBlock)) < latestBlock.Threshhold)
            {
                //Add in existing
                latestBlock.EncryptedTransactions.Add(transaction);
            }
            else
            {
                //create new
                Block newBlock = CreateNewBlock(latestBlock);
                bc.Blocks.Add(newBlock.ProofOfWork, (newBlock));
            }
            if (transaction.SenderAccount.ID != transaction.ReceiverAccount.ID)
            {
                //Thread t = new Thread(Sync);
                //.Start();
                Thread.Sleep(1000);
                Sync();
            }
        }
        public void SettleTransactions()
        {
            List <Block> blocksToProcess = new List <Block>(BlockChainMaster.BlockChain.Blocks.Values.OfType <Block>());

            if (_lastProcessedDate > DateTimeOffset.MinValue)
            {
                blocksToProcess = blocksToProcess.Where(b => b.TimeStamp > _lastProcessedDate).OrderBy(b => b.TimeStamp).ToList();
            }
            _lastProcessedDate = DateTimeOffset.Now;
            foreach (Block block in blocksToProcess)
            {
                foreach (var trans in block.EncryptedTransactions)
                {
                    if (trans.SenderAccount.ID == trans.ReceiverAccount.ID)
                    {
                        continue;
                    }
                    Account senderAccount     = HandleGetCase.GetAccount(trans.SenderAccount.ID);
                    Process.HandleSetCase obj = new HandleSetCase(trans.SenderAccount, trans.SenderAccount, senderAccount.TotalAmount - trans.Amount);
                    obj.SetBlockChain();
                    Thread.Sleep(1000);
                    Account receiverAccount = HandleGetCase.GetAccount(trans.ReceiverAccount.ID);
                    obj = new HandleSetCase(trans.ReceiverAccount, trans.ReceiverAccount, receiverAccount.TotalAmount + trans.Amount);
                    obj.SetBlockChain();
                }
            }
        }