Beispiel #1
0
        public bool IsValid()
        {
            Block previousBlock = Chain.First();

            if (previousBlock.Hash.Substring(0, _lead.Length) != _lead)
            {
                return(false);
            }
            if (previousBlock.Hash != previousBlock.CalculateHash())
            {
                return(false);
            }

            foreach (var block in Chain.Skip(1))
            {
                if (block.Hash.Substring(0, _lead.Length) != _lead)
                {
                    return(false);
                }
                if (block.Hash != block.CalculateHash())
                {
                    return(false);
                }
                if (block.PreviousHash != previousBlock.Hash)
                {
                    return(false);
                }
                previousBlock = block;
            }

            return(true);
        }
Beispiel #2
0
 public Boolean isValid(List <Block> nodo, int chain)
 {
     for (int i = 0; i < nodo.Count; i++)
     {
         Block currentBlock  = nodo[i];
         Block previousBlock = new Block();
         if (i > 0)
         {
             previousBlock = nodo[i - 1];
         }
         if (currentBlock.Hash != currentBlock.CalculateHash())
         {
             log1.Debug(String.Format("Block # {0} from block chain {1} is corrupted", i + 1, chain));
             log2.Debug(String.Format("Block # {0} from block chain {1} is corrupted", i + 1, chain));
             log3.Debug(String.Format("Block # {0} from block chain {1} is corrupted", i + 1, chain));
             return(false);
         }
         if (i > 0)
         {
             if (currentBlock.PreviousHash != previousBlock.Hash)
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Beispiel #3
0
        public void AddBlock(Block block)
        {
            Block latestBlock = GetLatestBlock();

            block.Index        = latestBlock.Index + 1;
            block.PreviousHash = latestBlock.Hash;
            block.Hash         = block.CalculateHash();
            Chain.Add(block);
        }
Beispiel #4
0
        public void AddBlock(Block block)
        {
            Block latestBlock = GetLatestBlock();

            block.SetIndex(latestBlock.GetIndex() + 1);
            block.SetPreviousHash(latestBlock.GetHash());
            block.SetHash(block.CalculateHash());
            chain.Add(block);
        }
Beispiel #5
0
        public bool IsValid()
        {
            for (int i = 1; i < chain.Count; i++)
            {
                Block currentBlock  = chain[i];
                Block previousBlock = chain[i - 1];

                if (currentBlock.GetHash() != currentBlock.CalculateHash())
                {
                    return(false);
                }
                else if (currentBlock.GetPreviousHash() != previousBlock.GetHash())
                {
                    return(false);
                }
            }
            return(true);
        }
Beispiel #6
0
        public bool IsValid()
        {
            for (int i = 1; i < Chain.Count; i++)
            {
                Block currentBlock  = Chain[i];
                Block previousBlock = Chain[i - 1];

                if (currentBlock.Hash != currentBlock.CalculateHash())
                {
                    return(false);
                }

                if (currentBlock.PreviousHash != previousBlock.Hash)
                {
                    return(false);
                }
            }
            return(true);
        }
Beispiel #7
0
 /// <summary>
 /// Double check the data integrity by checking if the hash has changed in the current block both on itself and the next one (in that case checking previous hash)
 /// </summary>
 /// <returns></returns>
 public bool IsValid()
 {
     for (int i = 1; i < Chain.Count; i++)
     {
         Block currentBlock  = Chain[i];
         Block previousBlock = Chain[i - 1];
         //it calculates the hash with the data if the data has been altered the hash will be different
         if (currentBlock.Hash != currentBlock.CalculateHash())
         {
             Console.WriteLine($"Corrupted block id " + currentBlock.Index);
             return(false);
         }
         //When it iterates over the next block it checks if the hash of the altered block is the same
         //This is needed in case of hash had been recalculated and changed on the altered block (the previous one)
         if (currentBlock.PreviousHash != previousBlock.Hash)
         {
             Console.WriteLine($"Corrupted block id " + (currentBlock.Index - 1));
             return(false);
         }
     }
     return(true);
 }
Beispiel #8
0
        public bool IsChainValid()
        {
            for (int i = 1; i < chain.Count; i++)
            {
                Block currentBlock  = chain[i];
                Block previousBlock = chain[i - 1];

                if (!currentBlock.Hash.Equals(currentBlock.CalculateHash()))
                {
                    Console.WriteLine("Current Hashes not equal");
                    return(false);
                }

                if (!previousBlock.Hash.Equals(currentBlock.PreviousHash))
                {
                    Console.WriteLine("Previous Hashes not equal");
                    return(false);
                }
            }

            return(true);
        }
        private static void Interpreter(string message, string ip)
        {
            message = message.Replace("SupplyChain", "Blockchain");
            // received miners list
            if (message.StartsWith("minersList"))
            {
                message = message.Substring(10);
                object ret = JsonDeserialize(message);
                var    obj = Cast(ret, new { list = new List <string>() });
                Miners.minerIPs = obj.list;
                Console.WriteLine("Current miner count: " + Miners.minerIPs.Count);

                for (int a = 0; a < Miners.minerIPs.Count; a++)
                {
                    Miners.miners.Add(new List <KeyValuePair <Block, bool> >());
                }

                if (Miners.minerIPs.Count == 0)
                {
                    BlockChain.GetChain();
                    SendWebServer("addMeNow");
                }

                return;
            }

            // received a new block to add
            if (message.StartsWith("addBlock"))
            {
                Data data = (Data)JsonDeserialize(message.Substring(8));
                BlockChain.ReceiveNewBlock(data);
                return;
            }

            if (message.StartsWith("checkNonce"))
            {
                message = message.Substring(10);
                string[] checkNonceArray = message.Split('$');
                Miners.SetMyMinerTrue(DateTime.Parse(checkNonceArray[0]), long.Parse(checkNonceArray[1]), Int32.Parse(checkNonceArray[2]), ip);
                return;
            }

            if (message.StartsWith("nonceIsTrue"))
            {
                message = message.Substring(11);
                Miners.SetMinersTrue(ip, long.Parse(message));
                return;
            }

            if (message.StartsWith("getChain"))
            {
                Send(ip, "receiveChain" + JsonSerialize(new { chain = BlockChain.GetChain() }));
                return;
            }

            if (message.StartsWith("receiveChain"))
            {
                object ret = JsonDeserialize(message.Substring(12));
                var    obj = Cast(ret, new { chain = new List <Block>() });
                BlockChain.ReceiveChain(obj.chain);
                return;
            }

            if (message.StartsWith("newMinerJoined"))
            {
                message = message.Substring(14);
                Miners.minerIPs.Add(message);
                Miners.miners.Add(new List <KeyValuePair <Block, bool> >());
                if (message == myIP)
                {
                    Console.WriteLine("\n-------Connected to network!-------\n");
                }
                else
                {
                    Console.WriteLine("New miner joined to network -> " + message);
                }
                Console.WriteLine("Current miner count: " + Miners.minerIPs.Count);
                return;
            }

            if (message.StartsWith("verify"))
            {
                message = message.Substring(6);
                Product product = BlockChain.GetProductInfo(long.Parse(message));
                SendWebServer("verifyReturn" + JsonSerialize(product));
                return;
            }

            if (message.StartsWith("block"))
            {
                message = message.Substring(5);
                Block takenBlock = (Block)JsonDeserialize(message);

                // wait until the block is inserted into chain
                bool wait = true;
                while (wait)
                {
                    List <Block> chain = BlockChain.GetChain();

                    try {
                        wait = !chain.Exists(b => b.BlockID.Equals(takenBlock.BlockID));
                    }
                    catch { }
                }

                Block currentBlock = BlockChain.GetBlock(takenBlock.BlockID);

                // change time earlier if taken one is earlier
                if (currentBlock.Time.CompareTo(takenBlock.Time) > 0)
                {
                    currentBlock.Time  = takenBlock.Time;
                    currentBlock.Nonce = takenBlock.Nonce;
                    currentBlock.Hash  = currentBlock.CalculateHash();
                }

                // change smaller nonce if the found dates equal
                if (currentBlock.Time.CompareTo(takenBlock.Time) == 0)
                {
                    currentBlock.Nonce = Math.Min(takenBlock.Nonce, currentBlock.Nonce);
                }
            }
        }