예제 #1
0
파일: Program.cs 프로젝트: fubrak/Merkle
        static void Main(string[] args)
        {
            NamesTable myTable1 = new NamesTable();
            NamesTable myTable2 = new NamesTable();

            NamesEntry randomEntry;

            Console.WriteLine("Adding random entries...");

            for (int i = 0; i < sizeOfTable; i++)
            {
                randomEntry = GetRandomEntry();

                myTable1.AddNewEntry(randomEntry);
                myTable2.AddNewEntry(randomEntry);
            }

            //Uncomment the line below to get false results
            //myTable1.AddNewEntry(new NamesEntry("single", "value", 666));

            myTable1.CompareTablesAndPrintResults(myTable2);
            myTable2.CompareTablesAndPrintResults(myTable1);

            Console.WriteLine("done");
        }
예제 #2
0
파일: NamesTable.cs 프로젝트: fubrak/Merkle
        /// <summary>
        /// Checks if two NamesTable are equals, if not, prints the values that do not appear in both of them
        /// </summary>
        /// <param name="tableNumberTwo">a table of entries</param>
        /// <returns>true of false if table is identical</returns>
        public bool CompareTablesAndPrintResults(NamesTable tableNumberTwo)
        {
            // Check if tree root identical
            if (HexEncoder.Encode(tableHashTree.MerkleRootHash) ==
                HexEncoder.Encode(tableNumberTwo.tableHashTree.MerkleRootHash))
            {
                Console.WriteLine("Tables are identical");
                return(true);
            }

            // If you're here, the tables aren't identical

            int       currIndex  = tableOfEntries.Count / 2;
            int       lowerLimit = 0;
            int       upperLimit = tableOfEntries.Count - 1;
            string    direction;
            Proof     currProof, myProof;
            ArrayList currProofStr, myProofStr;

            while (true)
            {
                try
                {
                    // If GetProof throws exception, value is not in table
                    currProof = tableNumberTwo.tableHashTree.GetProof(ToBytes(tableOfEntries[currIndex].ToString()));
                }
                catch
                {
                    Console.WriteLine("The entry \"" +
                                      tableOfEntries[currIndex].ToString() +
                                      "\" is not in the other table");
                    return(false);
                }

                // this is a failsafe, should never be true
                if (currIndex == 0)
                {
                    break;
                }

                //Console.WriteLine("Checking index number " + currIndex);

                // Getting proof for my table
                myProof = tableHashTree.GetProof(ToBytes(tableOfEntries[currIndex].ToString()));

                // Parsing proofs from both tables
                currProofStr = ParseProof(currProof);
                myProofStr   = ParseProof(myProof);

                // finding different hashes in the Proof and setting the direction to continue
                for (int i = 0; i < currProofStr.Count; i++)
                {
                    i++;

                    if (!(currProofStr[i].Equals(myProofStr[i])))
                    {
                        direction = myProofStr[i - 1].ToString().Replace(" ", string.Empty);

                        if (direction.Equals("right"))  // Going right
                        {
                            lowerLimit = currIndex;

                            currIndex = ((lowerLimit + upperLimit) / 2);
                            if (((lowerLimit + upperLimit) % 2) == 1)
                            {
                                currIndex++;
                            }
                        }
                        else    // Going left
                        {
                            upperLimit = currIndex;
                            currIndex  = (lowerLimit + upperLimit) / 2;
                        }

                        break;
                    }
                }
            }

            return(false);
        }