Exemplo n.º 1
0
        private void createParticle(int height)
        {
            Particle         p           = new Particle();
            ExtendedParticle extParticle = new ExtendedParticle();

            for (int i = 0; i < TransactionPool.Count; i++)
            {
                Transaction trans = TransactionPool[i];

                if (verifyTransaction(trans, trans.proofOfOwnership))
                {
                    //converting now to segwit transaction
                    extParticle.proof.Add(trans.proofOfOwnership);
                    Transaction temp = new Transaction(trans);
                    temp.proofOfOwnership = "_";

                    p.allTransactions.Add(temp);
                }
                else
                {
                    Console.WriteLine("TRANSACTION NOT CORRECT!");
                    TransactionPool.Remove(trans);
                }
            }

            if (transactionPoolWindow != null)
            {
                transactionPoolWindow.updateTransactionPool();
            }

            //add hash to block before
            p.hashToBlock = Utility.ComputeHash(globalChainPath + (height - 1));

            Utility.storeFile(extParticle, globalChainPath + "E" + height + ".blk");
            Utility.storeFile(p, globalChainPath + "P" + height + ".blk");
        }
Exemplo n.º 2
0
        public void analyseChain()
        {
            if (dontAnalyseYetFlag)
            {
                return;
            }

            if (!File.Exists(globalChainPath + "0.blk"))
            {
                createGenesisBlock();
            }

            Block       b                      = Utility.loadFile <Block>(globalChainPath + "0.blk");
            int         i                      = 1;
            bool        errorFlag              = false;
            int         difficulty             = b.difficulty;
            List <bool> listOfMissingExtBlocks = new List <bool>();

            userTransactionHistory.Clear();

            //get genesis block too
            foreach (Account acc in accountList.Values)
            {
                acc.coinCounter = 0;
                if (acc.publicKey.Equals(b.minerAddress))
                {
                    acc.coinCounter += 3;
                }
            }


            while (File.Exists(globalChainPath + i + ".blk"))
            {
                b = Utility.loadFile <Block>(globalChainPath + i + ".blk");

                if (b == null)
                {
                    errorFlag = true;
                    i++;
                    break;
                }

                string hashOfThisBlock   = Utility.ComputeHash(globalChainPath + i);
                string hashOfBlockBefore = Utility.ComputeHash(globalChainPath + (i - 1));
                string proofHash         = Utility.getHashSha256(b.nonce + hashOfBlockBefore + b.hashOfParticle);

                //checking nonce
                if (!Utility.verifyHashDifficulty(proofHash, difficulty))
                {
                    Console.WriteLine("WRONG SHIT! HASH DIFFICULTY WRONG!");
                    break;
                }

                if (File.Exists(globalChainPath + "P" + i + ".blk"))
                {
                    //particle exists
                    Particle         p   = Utility.loadFile <Particle>(globalChainPath + "P" + i + ".blk");
                    ExtendedParticle ext = null;

                    if (File.Exists(globalChainPath + "E" + i + ".blk"))
                    {
                        ext = Utility.loadFile <ExtendedParticle>(globalChainPath + "E" + i + ".blk");
                    }

                    //block points to particle
                    if (!Utility.ComputeHash(globalChainPath + "P" + i).Equals(b.hashOfParticle))
                    {
                        break;
                    }

                    //block points to block before
                    if (!hashOfBlockBefore.Equals(b.hashOfBlockBefore))
                    {
                        break;
                    }

                    //particle points to block before
                    if (!hashOfBlockBefore.Equals(p.hashToBlock))
                    {
                        break;
                    }

                    Dictionary <string, int> listOfAllOwners = new Dictionary <string, int>();
                    listOfAllOwners.Add(b.minerAddress, 0);
                    listOfMissingExtBlocks.Add(ext != null ? true : false);

                    for (int index = 0; index < p.allTransactions.Count; index++)
                    {
                        Transaction trans = p.allTransactions[index];

                        //verify each transaction
                        if (ext != null)
                        {
                            if (!verifyTransaction(trans, ext.proof[index]))
                            {
                                break;
                            }
                        }

                        //add transaction to history for user
                        if (trans.owner.Equals(accountList[mainAccount].publicKey) || trans.receiver.Equals(accountList[mainAccount].publicKey))
                        {
                            userTransactionHistory.Add(trans);
                        }

                        //add transactions to a list
                        if (listOfAllOwners.ContainsKey(trans.owner))
                        {
                            listOfAllOwners[trans.owner] += trans.amount;
                        }
                        else
                        {
                            listOfAllOwners.Add(trans.owner, trans.amount);
                        }

                        // check if each transaction is possible
                        foreach (string owner in listOfAllOwners.Keys)
                        {
                            if (returnCoinBalance(owner, i - 1) < listOfAllOwners[owner])
                            {
                                errorFlag = true;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    break;
                }

                i++;
            }

            i--;

            if (errorFlag)
            {
                Console.WriteLine("ERROR BLOCK<this?");

                File.Delete(globalChainPath + i + ".blk");
                File.Delete(globalChainPath + "P" + i + ".blk");
                File.Delete(globalChainPath + "E" + i + ".blk");

                i--;
            }

            latestBlock = i;

            //check if the last 2 blocks have extended blocks
            if (i > 3 && !downloadingFlag)
            {
                for (int index = listOfMissingExtBlocks.Count - 1; index > 1; index--)
                {
                    if (listOfMissingExtBlocks[index] && listOfMissingExtBlocks[index - 1])
                    {
                        break;
                    }
                    else
                    {
                        try {
                            File.Delete(globalChainPath + "P" + (index + 1) + ".blk");
                            File.Delete(globalChainPath + (index + 1) + ".blk");
                        } catch (Exception e) {
                            Console.WriteLine("Error: " + e.ToString());
                        }

                        latestBlock = index;
                    }
                }
            }

            int counter = latestBlock - 2;

            while (true)
            {
                if (File.Exists(globalChainPath + "E" + counter + ".blk"))
                {
                    File.Delete(globalChainPath + "E" + counter + ".blk");
                }

                counter--;

                if (counter <= 0)
                {
                    break;
                }
            }


            if (InvokeRequired)
            {
                Invoke(new Action(() => {
                    updateCoinBalanceGUI();
                }));
            }
            else
            {
                updateCoinBalanceGUI();
            }
        }