Beispiel #1
0
        public bool verifyBlock(TinyBlock previousBlock)
        {
            byte[] POWcheck = hash.ComputeHash(Encoding.UTF8.GetBytes(thisHash.ToString() + POW));
            //if(POWcheck[0] == 0 && POWcheck[1] == 0) // Difficuly hardcoded :)
            if (POWcheck[0] == 0 && POWcheck[1] == 0 && POWcheck[2] == 0) // Difficuly hardcoded :)
            {
                if (previousHash.SequenceEqual(previousBlock.thisHash) || index == 0)
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #2
0
        public void startFindPOW()
        {
            Console.WriteLine("Starting POW working thread");
            TinyBlock genesisblock = new TinyBlock();

            string json = JMessage.Serialize(JMessage.FromValue(genesisblock));

            Console.WriteLine(json);
            Console.WriteLine(genesisblock.Serialize());
            blockchain.Add(genesisblock);

            ThreadStart ts = new ThreadStart(FindPOW);

            POWsearchThread = new Thread(ts);
            POWsearchThread.Start();
        }
Beispiel #3
0
        public void FindPOW()
        {
            byte[] thisHash = blockchain[blockchain.Count - 1].thisHash;
            SHA256 hash     = SHA256.Create();

            byte[] POWcheck;
            int    POW          = 0;
            int    workingIndex = blockchain.Count;
            Random rnd          = new Random();

            do
            {
                hashMre.WaitOne();

                if (blockchain.Count != workingIndex)
                {
                    thisHash     = blockchain[blockchain.Count - 1].thisHash;
                    workingIndex = blockchain.Count;
                    POW          = rnd.Next();
                }

                POWcheck = hash.ComputeHash(Encoding.UTF8.GetBytes(thisHash.ToString() + POW));

                //if(POWcheck[0] == 0 && POWcheck[1] == 0) // Difficuly hardcoded :)
                if (POWcheck[0] == 0 && POWcheck[1] == 0 && POWcheck[2] == 0) // Difficuly hardcoded :)
                {
                    Console.WriteLine("Found block");
                    TinyBlock newBlock = new TinyBlock(workingIndex, thisHash, newBlockData, POW);
                    //Console.WriteLine(json);
                    sendDataToAll(newBlock);
                    blockchain.Add(newBlock);
                }
                else
                {
                    POW++;
                }
            } while(true);
        }
Beispiel #4
0
        private void clientListenThread(object obj)
        {
            TcpClient     client      = (TcpClient)obj;
            StringBuilder sb          = new StringBuilder();
            int           bytesRead   = 0;
            int           bytesToRead = 0;

            while (true)
            {
                try
                {
                    byte[] data       = new byte[1024 * 8];
                    int    dataLenght = client.Client.Receive(data);
                    //Console.WriteLine("total size: " + dataLenght);
                    if (dataLenght == 0)
                    {
                        Console.WriteLine("Disconnected");
                        clients.Remove(client);
                        break;
                    }

                    int offset = 0;
                    //Split data
                    while (offset < dataLenght)
                    {
                        if (bytesRead >= bytesToRead)
                        {
                            sb.Clear();
                            bytesRead = 0;

                            //There is a super small chance that the size bytes does not come at the same time, that has not been accounted for
                            byte[] byteSize = { data[offset + 0], data[offset + 1], data[offset + 2], data[offset + 3] };
                            int    size     = BitConverter.ToInt32(byteSize, 0);
                            bytesToRead = size;
                            //Console.WriteLine("data size: " + size);
                            offset += 4;

                            if (offset >= dataLenght)
                            {
                                break;
                            }
                        }

                        for (int i = offset; i < offset + bytesToRead; i++)
                        {
                            if (i >= dataLenght)
                            {
                                break;
                            }
                            sb.Append(Convert.ToChar(data[i]));
                            bytesRead++;
                        }

                        if (bytesRead < bytesToRead)
                        {
                            break;
                        }

                        offset += bytesRead;

                        Console.WriteLine("From " + ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString() + ": " + sb.ToString());

                        //Must be of json data or it will crash
                        JMessage message = JMessage.Deserialize(sb.ToString());

                        if (message.Type == typeof(Command))
                        {
                            Command c = message.Value.ToObject <Command>();
                            if (c.command == "getBlockHeight")
                            {
                                sendDataTo(client, new BlockHeight(blockchain.Count));
                            }
                            else if (c.command == "sync")
                            {
                                /*foreach(TinyBlock tb in blockchain)
                                 * {
                                 *  sendDataTo(client, tb);
                                 * }*/
                                sendSyncList(client);
                            }
                        }
                        else if (message.Type == typeof(TinyBlock))
                        {
                            hashMre.Reset();
                            TinyBlock tb = message.Value.ToObject <TinyBlock>();
                            if (tb.verifyBlock(blockchain.Last()))
                            {
                                blockchain.Add(tb);
                            }
                            hashMre.Set();
                        }
                        else if (message.Type == typeof(SyncList))
                        {
                            SyncList syncList = message.Value.ToObject <SyncList>();
                            startSyncing(syncList);
                        }
                        else if (message.Type == typeof(BlockHeight))
                        {
                            BlockHeight c = message.Value.ToObject <BlockHeight>();
                            syncCount++;

                            if (c.value > blockchain.Count && syncBlockSize < c.value)
                            {
                                syncBlockSize = c.value;
                                syncClient    = client;
                            }
                            if (syncCount == clients.Count && syncClient != null)
                            {
                                syncCount = 0;
                                sendDataTo(syncClient, new Command("sync"));
                                //blockchain.Clear();
                            }
                        }
                    }
                }
                catch (SocketException e)
                {
                    Console.WriteLine(e.Message);
                    clients.Remove(client);
                    break;
                }
            }
        }