Exemplo n.º 1
0
        public bool validate(CBlock <string> prevB, CBlock <string> newB)
        {
            bool valid = true;

            string newHash = getSHA(newB.index.ToString() + newB.previousHash + newB.data + newB.date.ToString());

            if (prevB.index + 1 != newB.index)
            {
                valid = false;
                throw new Exception("index");
            }

            if (prevB.hash != newB.previousHash)
            {
                valid = false;
                throw new Exception("index");
            }

            if (newHash != newB.hash)
            {
                valid = false;
                throw new Exception("index");
            }

            /*if (prevB.index + 1 != newB.index || prevB.hash != newB.previousHash || newHash != newB.hash)
             * {
             *  valid = false;
             * }*/

            return(valid);
        }
Exemplo n.º 2
0
        public CBlockChain(string prevHash, string data, DateTime date)
        {
            current = 0;
            string hash = getHash(current, prevHash, data, date);

            firstHash = hash;
            CBlock <string> b = new CBlock <string>(current, prevHash, hash, data, date);

            Chain.Add(b);
            current++;
        }
Exemplo n.º 3
0
        static void list()
        {
            Console.Clear();
            foreach (CBlock <string> c in chain.Chain)
            {
                CBlock <string> ch = c;
                Console.WriteLine($"Block { ch.index }: { ch.data }");
            }

            Console.WriteLine("");
        }
Exemplo n.º 4
0
        static void add()
        {
            Console.Clear();
            Console.Write("Data: ");
            string data = Console.ReadLine();

            Console.Clear();

            CBlock <string> c = chain.AddBlock(currHash, data, DateTime.Now);

            Console.WriteLine($"Added Block({ c.data }, { c.date.ToString() }, { c.hash })\n");

            currHash = c.hash;
        }
Exemplo n.º 5
0
        public CBlock <string> AddBlock(string prevHash, string data, DateTime date)
        {
            string          hash = getHash(current, prevHash, data, date);
            CBlock <string> b    = new CBlock <string>(current, prevHash, hash, data, date);

            CBlock <string> o = (CBlock <string>)Chain[current - 1];

            if (validate(o, b))
            {
                Chain.Add(b);
                current++;
            }
            else
            {
                throw new Exception("Chain not valid");
            }
            return(b);
        }