Esempio n. 1
0
        private static void Main(string[] args)
        {
            var txn1  = new Transaction("ABC123", 1000.00m, DateTime.Now, "QWE123", 10000, ClaimType.TotalLoss);
            var txn2  = new Transaction("VBG354", 2000.00m, DateTime.Now, "JKH567", 20000, ClaimType.TotalLoss);
            var txn3  = new Transaction("XCF234", 3000.00m, DateTime.Now, "DH23ED", 30000, ClaimType.TotalLoss);
            var txn4  = new Transaction("CBHD45", 4000.00m, DateTime.Now, "DH34K6", 40000, ClaimType.TotalLoss);
            var txn5  = new Transaction("AJD345", 5000.00m, DateTime.Now, "28FNF4", 50000, ClaimType.TotalLoss);
            var txn6  = new Transaction("QAX367", 6000.00m, DateTime.Now, "FJK676", 60000, ClaimType.TotalLoss);
            var txn7  = new Transaction("CGO444", 7000.00m, DateTime.Now, "LKU234", 70000, ClaimType.TotalLoss);
            var txn8  = new Transaction("PLO254", 8000.00m, DateTime.Now, "VBN456", 80000, ClaimType.TotalLoss);
            var txn9  = new Transaction("ABC123", 1000.00m, DateTime.Now, "QWE123", 10000, ClaimType.TotalLoss);
            var txn10 = new Transaction("VBG354", 2000.00m, DateTime.Now, "JKH567", 20000, ClaimType.TotalLoss);
            var txn11 = new Transaction("XCF234", 3000.00m, DateTime.Now, "DH23ED", 30000, ClaimType.TotalLoss);
            var txn12 = new Transaction("CBHD45", 4000.00m, DateTime.Now, "DH34K6", 40000, ClaimType.TotalLoss);
            var txn13 = new Transaction("AJD345", 5000.00m, DateTime.Now, "28FNF4", 50000, ClaimType.TotalLoss);
            var txn14 = new Transaction("QAX367", 6000.00m, DateTime.Now, "FJK676", 60000, ClaimType.TotalLoss);
            var txn15 = new Transaction("CGO444", 7000.00m, DateTime.Now, "LKU234", 70000, ClaimType.TotalLoss);
            var txn16 = new Transaction("PLO254", 8000.00m, DateTime.Now, "VBN456", 80000, ClaimType.TotalLoss);


            var block1 = new Block(0);
            var block2 = new Block(1);
            var block3 = new Block(2);
            var block4 = new Block(3);

            block1.AddTransaction(txn1);
            block1.AddTransaction(txn2);
            block1.AddTransaction(txn3);
            block1.AddTransaction(txn4);

            block2.AddTransaction(txn5);
            block2.AddTransaction(txn6);
            block2.AddTransaction(txn7);
            block2.AddTransaction(txn8);

            block3.AddTransaction(txn9);
            block3.AddTransaction(txn10);
            block3.AddTransaction(txn11);
            block3.AddTransaction(txn12);

            block4.AddTransaction(txn13);
            block4.AddTransaction(txn14);
            block4.AddTransaction(txn15);
            block4.AddTransaction(txn16);

            block1.SetBlockHash(null);
            block2.SetBlockHash(block1);
            block3.SetBlockHash(block2);
            block4.SetBlockHash(block3);

            var chain = new BlockChain();

            chain.AcceptBlock(block1);
            chain.AcceptBlock(block2);
            chain.AcceptBlock(block3);
            chain.AcceptBlock(block4);

            chain.VerifyChain();

            Console.WriteLine("");
            Console.WriteLine("");

            txn5.ClaimNumber = "weqwewe";
            chain.VerifyChain();

            Console.WriteLine();
            Console.Read();
        }
Esempio n. 2
0
        private void btn_addTransactions_Click(object sender, EventArgs e)
        {
            string EmployerID = tb_id.Text.ToString().Trim();
            string Name       = tb_name.Text.ToString().Trim();

            DateTime timestamp = DateTime.Now;

            if (EmployerID != "" && Name != "")
            {
                bool type = false;

                for (int i = 0; i < chain.Blocks.Count; i++)
                {
                    for (int j = 0; j < chain.Blocks[i].Transaction.Count; j++)
                    {
                        if (chain.Blocks[i].Transaction[j].ID == EmployerID)
                        {
                            type = !chain.Blocks[i].Transaction[j].Type;
                        }
                    }
                }

                for (int i = 0; i < cacheBlock.Transaction.Count; i++)
                {
                    if (cacheBlock.Transaction[i].ID == EmployerID)
                    {
                        type = !cacheBlock.Transaction[i].Type;
                    }
                }

                if (dgv_outTransactions.RowCount == 5)
                {
                    dgv_outTransactions.Rows.Clear();
                }
                if (cacheBlock.Transaction.Count == 0)
                {
                    cb_blockList.Items.Add("Block " + (nBlock + 1).ToString());
                }

                ITransaction txn = new Transaction(EmployerID, Name, type, timestamp);
                txn.SetSignature(rsa);
                cacheBlock.AddTransaction(txn);

                if (cacheBlock.Transaction.Count == MAX_TRANSACTION)
                {
                    cacheBlock.SetBlockHash(nBlock == 0 ? null : chain.Blocks[nBlock - 1]);
                    chain.AcceptBlock(cacheBlock);
                    tb_previousBlockHash.Text = chain.Blocks[chain.Blocks.Count - 1].PreviousBlockHash != null
                        ? chain.Blocks[chain.Blocks.Count - 1].PreviousBlockHash : "N/A";
                    lb_createdDate.Text = chain.Blocks[chain.Blocks.Count - 1].CreatedDate.ToString();
                    tb_blockHash.Text   = chain.Blocks[chain.Blocks.Count - 1].BlockHash;
                    tb_merkleRoot.Text  = chain.Blocks[chain.Blocks.Count - 1].getHashMerkleRoot();

                    nBlock    += 1;
                    cacheBlock = new Block(nBlock);

                    lb_blockName.Text = "Block " + (nBlock + 1).ToString();
                }
                dgv_outTransactions.Rows.Add(EmployerID, Name, type ? "check-out" : "check-in", timestamp.ToString("HH:mm:ss"), timestamp.ToString("dd-MM-yyyy"), "unverified");

                cb_blockList.SelectedIndex = cb_blockList.Items.Count - 1;
                tb_name.Text = "";
                tb_id.Text   = "";
                AlertOK(type ? "Checked-out!" : "Checked-in!");
            }
            else
            {
                AlertError("Enter Employer ID and Name to add.");
            }
        }
Esempio n. 3
0
        private void btn_addTransactions_Click(object sender, EventArgs e)
        {
            if (dtg_inTransactions.RowCount > 1)
            {
                int numAdded                = cacheBlock.Transaction.Count;
                int numInTransactions       = dtg_inTransactions.RowCount - 1;
                int numTransactions         = numAdded == 0 ? numInTransactions : numInTransactions - (5 - numAdded);
                int numAddCacheTransactions = numAdded == 0 ? 0 : (numInTransactions <= 5 - numAdded ? numInTransactions : 5 - numAdded);

                for (int i = 0; i < numAddCacheTransactions; i++)
                {
                    string   claimNumber      = dtg_inTransactions.Rows[i].Cells[0].Value.ToString().Trim();
                    string   settlementAmount = dtg_inTransactions.Rows[i].Cells[1].Value.ToString().Trim();
                    string   carRegistration  = dtg_inTransactions.Rows[i].Cells[2].Value.ToString().Trim();
                    string   mileage          = dtg_inTransactions.Rows[i].Cells[3].Value.ToString().Trim();
                    DateTime timestamp        = DateTime.Now;

                    ITransaction txn = new Transaction(claimNumber, Convert.ToDecimal(settlementAmount), timestamp, carRegistration, Convert.ToInt32(mileage), ClaimType.TotalLoss);
                    if (chb_signTransaction.Checked)
                    {
                        txn.SetSignature(rsa);
                    }
                    cacheBlock.AddTransaction(txn);
                    dtg_outTransactions.Rows.Add(claimNumber, Convert.ToDecimal(settlementAmount), timestamp, carRegistration, Convert.ToInt32(mileage), ClaimType.TotalLoss);
                }
                //dtg_outTransactions.DataSource = cacheBlock.Transaction;
                //dtg_outTransactions.Refresh();

                if (cacheBlock.Transaction.Count == 5)
                {
                    cacheBlock.SetBlockHash(nBlock == 0 ? null : chain.Blocks[nBlock - 1]);
                    chain.AcceptBlock(cacheBlock);
                    nBlock            += 1;
                    txt_nameBlock.Text = "Block " + (nBlock + 1).ToString();
                    if (numTransactions == 0)
                    {
                        dtg_inTransactions.Rows.Clear();
                    }
                    cacheBlock = new Block(nBlock);
                }
                int numBlocks = numTransactions > 0 ? numTransactions / 5 + (numTransactions % 5 == 0 ? 0 : 1) : 0;

                for (int i = 0; i < numBlocks; i++)
                {
                    IBlock block           = new Block(nBlock);
                    int    iMaxTransaction = (i == numBlocks - 1 && numTransactions != 5) ? numTransactions % 5 : 5;
                    dtg_outTransactions.Rows.Clear();
                    for (int j = 0; j < iMaxTransaction; j++)
                    {
                        string       claimNumber      = dtg_inTransactions.Rows[numAddCacheTransactions + i * 5 + j].Cells[0].Value.ToString().Trim();
                        string       settlementAmount = dtg_inTransactions.Rows[numAddCacheTransactions + i * 5 + j].Cells[1].Value.ToString().Trim();
                        string       carRegistration  = dtg_inTransactions.Rows[numAddCacheTransactions + i * 5 + j].Cells[2].Value.ToString().Trim();
                        string       mileage          = dtg_inTransactions.Rows[numAddCacheTransactions + i * 5 + j].Cells[3].Value.ToString().Trim();
                        DateTime     timestamp        = DateTime.Now;
                        ITransaction txn = new Transaction(claimNumber, Convert.ToDecimal(settlementAmount), timestamp, carRegistration, Convert.ToInt32(mileage), ClaimType.TotalLoss);
                        if (chb_signTransaction.Checked)
                        {
                            txn.SetSignature(rsa);
                        }

                        if (iMaxTransaction < 5)
                        {
                            cacheBlock.AddTransaction(txn);
                        }
                        else
                        {
                            block.AddTransaction(txn);
                        }
                        dtg_outTransactions.Rows.Add(claimNumber, Convert.ToDecimal(settlementAmount), timestamp, carRegistration, Convert.ToInt32(mileage), ClaimType.TotalLoss);
                    }
                    //dtg_outTransactions.DataSource = null;
                    cb_blockList.Items.Add("Block " + (nBlock + 1).ToString());
                    if (iMaxTransaction < 5)
                    {
                    }
                    //dtg_outTransactions.DataSource = cacheBlock.Transaction;
                    else
                    {
                        //dtg_outTransactions.DataSource = block.Transaction;
                        block.SetBlockHash(nBlock == 0 ? null : chain.Blocks[nBlock - 1]);
                        chain.AcceptBlock(block);
                        nBlock            += 1;
                        txt_nameBlock.Text = "Block " + (nBlock + 1).ToString();
                    }
                    cb_blockList.SelectedIndex = cb_blockList.Items.Count - 1;
                    if (i == numBlocks - 1)
                    {
                        dtg_inTransactions.Rows.Clear();
                    }
                }
                if (cacheBlock.Transaction.Count > 0)
                {
                    dtg_inTransactions.Rows.Clear();
                }
            }
        }