Example #1
0
        public static bool ValidateBlockchain()
        {
            using (var db = new BlockchainExampleEntities())
            {
                try
                {
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    var Blockchain = db.Blockchain.AsNoTracking().OrderBy(b => b.Index).ToArray();
                    for (int i = 1; i < Blockchain.Count(); i++)
                    {
                        if (!(Blockchain[i].PrevHash == Blockchain[i - 1].Hash))
                        {
                            Console.WriteLine("Block no " + i + " is invalid");
                            return(false);
                        }
                    }
                    stopwatch.Stop();
                    Console.WriteLine("Blockchain is valid, checked in " + stopwatch.ElapsedMilliseconds + "ms");
                }
                catch (Exception)
                {
                    return(false);
                }


                Console.ReadKey();
                Environment.Exit(0);
                return(true);
            }
        }
Example #2
0
 public int?AddBlock()
 {
     using (var db = new BlockchainExampleEntities())
     {
         using (
             var dbTran = db.Database.BeginTransaction(IsolationLevel.Serializable))
         {
             try
             {
                 if (ValidateBlock())
                 {
                     db.Blockchain.Add(this);
                     db.SaveChanges();
                     dbTran.Commit();
                     return(this.Index);
                 }
                 dbTran.Rollback();
                 return(null);
             }
             catch (Exception)
             {
                 dbTran.Rollback();
                 return(null);
             }
         }
     }
 }
Example #3
0
 public Blockchain GetLastBlock()
 {
     using (var db = new BlockchainExampleEntities())
     {
         try
         {
             return(db.Blockchain.OrderByDescending(b => b.Index).First());
         }
         catch (Exception)
         {
             return(new Blockchain(0, string.Empty, string.Empty, string.Empty, 0));
         }
     }
 }
Example #4
0
 public Data GetDataByID()
 {
     using (var db = new BlockchainExampleEntities())
     {
         try
         {
             return(db.Data.FirstOrDefault(u => u.ID == this.ID));
         }
         catch (Exception)
         {
             return(null);
         }
     }
 }
Example #5
0
 public int?SaveDataInDatabase()
 {
     using (var db = new BlockchainExampleEntities())
     {
         try
         {
             db.Data.Add(this);
             db.SaveChanges();
             db.Dispose();
             return(this.ID);
         }
         catch (Exception)
         {
             return(null);
         }
     }
 }
Example #6
0
        public bool RemoveData()
        {
            BlockchainExampleEntities db = new BlockchainExampleEntities();

            try
            {
                var DataToRemove = db.Data.FirstOrDefault(u => u.ID == this.ID);
                if (DataToRemove == null)
                {
                    throw new Exception();
                }
                db.Data.Remove(DataToRemove);
                db.SaveChanges();
                db.Dispose();
            }
            catch (Exception)
            {
                return(false);
            }
            return(true);
        }