Exemplo n.º 1
0
            public Blockchain(string Data, string Blocktype = "root", int complexity = 0)
            {
                _complexity       = complexity;
                block._complexity = complexity;

                this.Chain = new List <block>()
                {
                    new block
                    {
                        index         = 0,
                        timestamp     = DateTime.Now.Ticks,
                        previous_hash = new byte[0], //block.GetHash("genesis"),
                        data          = Data,        //!! Data is not stored !!
                        blocktype     = Blocktype
                    }
                };


                block oGenesis = Chain.First();

                if (string.IsNullOrEmpty(Data))
                {
                    oGenesis.data = "";
                }
                oGenesis.nonce = Mine(0, Blocktype, new byte[0]);
                oGenesis.calc_hash();

                //File.WriteAllText(@"c:\temp\data\" + Base32Encoding.ToString(oGenesis.data) + ".json", Data);
            }
Exemplo n.º 2
0
            /*public block NewBlock(string Data, block ParentBlock, string BlockType = "")
             * {
             *  var oNew = new block()
             *  {
             *      index = ParentBlock.index + 1,
             *      timestamp = DateTime.Now.Ticks,
             *      previous_hash = ParentBlock.hash,
             *      data = block.GetHash(Data),
             *      blocktype = BlockType
             *  };
             *  //oNew.hash = block.GetHash(oNew.index.ToString() + oNew.timestamp.ToString() + oNew.proof.ToString() + oNew.previous_hash.ToString() + oNew.data.ToString());
             *  oNew.calc_hash();
             *  Chain.Add(oNew);
             *
             *  return oNew;
             * }*/

            public block UseBlock(string Data, block FreeBlock)
            {
                /*if(!validateChain()) //Chain not Valid
                 *  return FreeBlock;*/

                //Check if FreeBlock is valid..
                var oParent = Chain.FirstOrDefault(t => t.hash == FreeBlock.previous_hash);

                if (oParent != null)
                {
                    /*if(!FreeBlock.validate(oParent.nonce))
                     * {
                     *  Console.WriteLine("Invalid Block: \n" + JsonConvert.SerializeObject(FreeBlock));
                     *  return FreeBlock;
                     * }*/
                }
                else
                {
                    Console.WriteLine("Invalid Block: \n" + JsonConvert.SerializeObject(FreeBlock));
                    return(FreeBlock);
                }


                if (FreeBlock.data != null | FreeBlock.hash != null | FreeBlock.signature != null) //it's not a free Block
                {
                    return(FreeBlock);
                }

                //FreeBlock.index = oParent.index + 1;
                FreeBlock.data      = Data;
                FreeBlock.timestamp = DateTime.Now.Ticks;
                FreeBlock.calc_hash();

                return(FreeBlock);
            }