Example #1
0
        /*Called from the server thread, adds the first block to the blockchain*/
        public void InitialiseChain()
        {
            APIClasses.Block b = new APIClasses.Block("", blockID, "");
            blockID++;
            GenerateHash(b);
            Messenger.blockChain.Add(b);

            GetMostPopular(b);
            //makes sure there aren't clients who already have a block chain going
        }
Example #2
0
 public static void ValidateBlock(APIClasses.Block newBlock)
 {
     try
     {
         CheckBlockID(newBlock.blockID);
         CheckBlockOffset(newBlock.blockOffset);
         CheckPrevHash(newBlock.prevHash);
         GenerateHash(newBlock);
         CheckHash(newBlock.hash);
     }
     catch (InvalidBlockException e)
     {
         logger.LogFunc("ERROR: Invalid Block " + e.Message);
     }
 }
Example #3
0
 public string GetPrevHash()
 {
     try
     {
         APIClasses.Block b = Messenger.blockChain.ElementAt(Messenger.blockChain.Count - 1);
         return(b.hash);
     }
     catch (ArgumentOutOfRangeException ex)
     {
         if (Messenger.blockChain.Count > 0)
         {
             throw new InvalidBlockException("Argument out of range in getting previous hash", ex);
         }
         else
         {
             return("");
         }
     }
 }
Example #4
0
        /*-------------------------------BlockChain Thread--------------------------------------*/

        public async void MinerThread()
        {
            await Task.Run(() =>
            {
                try
                {
                    while (true)
                    {
                        string[] script;
                        if (Messenger.scriptQueue.TryDequeue(out script))
                        {
                            localScripts.Add(script);
                            if (localScripts.Count == 5)                                                          //When 5 scripts have been submitted thats enough for a block
                            {
                                List <string[]> scriptList = localScripts.OrderBy(orderby => script[0]).ToList(); //ordering the scripts

                                RunScripts();                                                                     //executeing the scripts

                                string jsonScripts = JsonConvert.SerializeObject(scriptList);


                                string prevHash           = GetPrevHash();
                                APIClasses.Block newBlock = new APIClasses.Block(jsonScripts, blockID, prevHash);
                                blockID++;
                                ValidateBlock(newBlock);                                //validate the block create the has and validate that
                                Messenger.blockChain.Add(newBlock);                     //add to this clients block chain
                                GetMostPopular(newBlock);                               //Check my block chain against everyother clients block chain for the most popular
                                DownloadJobTest();
                                myJobs.Clear();
                            }
                        }
                    }
                }
                catch (InvalidBlockException e)
                {
                    logger.LogFunc("ERROR: Invalid Block");
                    MessageBox.Show("Invalid Transaction");
                }
            });
        }
Example #5
0
        /*Generating the hash*/
        public static APIClasses.Block GenerateHash(APIClasses.Block newBlock)
        {
            string final   = "";
            string newHash = "";

            bool hashed = true;

            do
            {
                //Hash string
                final = newBlock.blockID.ToString() + newBlock.jsonTransactions.ToString() + newBlock.blockOffset.ToString() + newBlock.prevHash.ToString();


                //Generating a valid Hash
                SHA256 sha256 = SHA256.Create();

                byte[] hashBytes = System.Text.Encoding.UTF8.GetBytes(final);    //encoding
                byte[] finalHash = sha256.ComputeHash(hashBytes);                //hashing


                newHash = ConvertHashToString(finalHash);
                string checkHash = newHash.Substring(0, 5);

                if (checkHash.Equals("12345"))                //Does it start with 12345
                {
                    hashed = false;
                }
                else
                {
                    newBlock.blockOffset += 2;
                }
            } while (hashed);


            newBlock.hash = newHash;
            return(newBlock);
        }
Example #6
0
 public static void AddBlock(APIClasses.Block newBlock)
 {
     Messenger.blockChain.Add(newBlock);
 }
Example #7
0
        public static void GetMostPopular(APIClasses.Block block)
        {
            bool          isPopular = false;
            RestRequest   request   = new RestRequest("api/client/getclientlist/");
            IRestResponse resp      = RestClient.Get(request);

            List <Client> clients = JsonConvert.DeserializeObject <List <Client> >(resp.Content);

            Dictionary <string, List <Client> > clientMap = new Dictionary <string, List <Client> >(); //<hash, <list<ports who have that hash>>

            Client mostPopular = new Client();                                                         //This is the client's whose block chain we will take
            int    popularity  = 0;
            string popularHash = block.hash;

            foreach (Client c in clients)
            {
                ChannelFactory <PeerServerInterface> channelFactory;
                NetTcpBinding tcp = new NetTcpBinding();
                tcp.MaxReceivedMessageSize = 2147483647;
                string clientURL = String.Format("net.tcp://{0}:{1}/DataService", c.ipaddress, c.port);
                channelFactory = new ChannelFactory <PeerServerInterface>(tcp, clientURL);
                PeerServerInterface channel = channelFactory.CreateChannel();
                APIClasses.Block    b       = channel.GetCurrentBlock();

                //Hash is the key and then if someone else has that key increase the value at that key
                if (clientMap.ContainsKey(b.hash))
                {
                    clientMap[b.hash].Add(c);
                }
                else
                {
                    List <Client> ports = new List <Client>();
                    ports.Add(c);
                    clientMap.Add(b.hash, ports);
                }
            }

            foreach (KeyValuePair <string, List <Client> > entry in clientMap)
            {
                if (entry.Value.Count > popularity)
                {
                    mostPopular = entry.Value.ElementAt(0);
                    popularHash = entry.Key;
                    popularity  = entry.Value.Count;
                }
            }
            if (!popularHash.Equals(block.hash))   //If my hash is not the most popular
            {
                Thread.Sleep(1000);                //Sleep long enough so you can get the updated version of the others
                ChannelFactory <PeerServerInterface> channelFactory;
                NetTcpBinding tcp = new NetTcpBinding();
                tcp.MaxReceivedMessageSize = 2147483647;
                string clientURL = String.Format("net.tcp://{0}:{1}/DataService", mostPopular.ipaddress, mostPopular.port);
                channelFactory = new ChannelFactory <PeerServerInterface>(tcp, clientURL);
                PeerServerInterface channel = channelFactory.CreateChannel();
                Messenger.blockChain = channel.GetBlockChain();
            }
            else
            {
                //you've got the best chain
            }
        }