Пример #1
0
        public bool AddForeignBlock(Block block)
        {
            if (block.GetPreviousHash().Equals(blocks[blocks.Count - 1].GetHash()))
            {
                blocks.Add(block);
                BlockChainSerializer.serialize(blocks, blockChainName);
                return(true);
            }

            return(false);
        }
Пример #2
0
        public void InternalAdd()
        {
            Prescription prescription = prescriptionToAdd;
            Block        block        = null;

            do
            {
                UpdateBlockChain();

                Block lastBlock = blocks[blocks.Count - 1];
                block = new Block(lastBlock.GetHash(), prescription);
            } while (!CheckAddedBlock(block));

            blocks.Add(block);
            BlockChainSerializer.serialize(blocks, blockChainName);
            prescriptionToAdd = null;
        }
Пример #3
0
        public BlockChain(BlockChainClient blockChainClient, string blockChainName)
        {
            this.blockChainClient = blockChainClient;
            this.blockChainName   = blockChainName;
            this.updateThread     = new Thread(new ThreadStart(InternalUpdateBlockChain));
            this.addThread        = new Thread(new ThreadStart(InternalAdd));

            blocks = BlockChainSerializer.deserialize(blockChainName);

            //Creating Genesis Block
            if (blocks == null)
            {
                blocks = new List <Block>();
                blocks.Add(new Block(null, null));
            }


            validator = new BlockChainValidator(blockChainName);
        }
Пример #4
0
        public void InternalUpdateBlockChain()
        {
            blockChainClient.sendNewChainMethod = GiveNewChain;
            while (!CheckCurrentChain())
            {
                foreach (KeyValuePair <string, bool> keyValue in validator.verificationAnswers)
                {
                    if (keyValue.Value == false)
                    {
                        ChainRequestSent = true;
                        blockChainClient.SendRequestOfChain(keyValue.Key);
                        break;
                    }
                }
                while (!ChainRequestSent)
                {
                    //wait for answer
                }
            }

            BlockChainSerializer.serialize(blocks, blockChainName);
        }