コード例 #1
0
        private async void syncBlockchains(List <NodeConnection> notSyncNodeConnections, Block lastBlock)
        {
            var tasks = new List <Task <List <Transaction> > >();

            foreach (var connection in notSyncNodeConnections)
            {
                var lastBlockHeight = await connection.LastBlockHeight();

                if (lastBlockHeight > lastBlock.Height)
                {
                    var conBlockHashes = await connection.BlockHashes();

                    if (conBlockHashes != null)
                    {
                        var localBlockHashes = _blockChain.BlockHashes();
                        var smallerListEnd   = conBlockHashes.Count < localBlockHashes.Count ? conBlockHashes.Count : localBlockHashes.Count;
                        var indexNotSame     = 0;
                        for (int i = 0; i < smallerListEnd; i++)
                        {
                            if (conBlockHashes[i] != localBlockHashes[i])
                            {
                                indexNotSame = i + 1;
                                break;
                            }
                        }
                        if (indexNotSame > 0)
                        {
                            try
                            {
                                if (conBlockHashes.Count - indexNotSame < 50)
                                {
                                    var alternateBlockChain = await connection.GetBlocks(Enumerable.Range(indexNotSame, conBlockHashes.Count + 1).ToList());

                                    using (BlockchainDbContext db = new BlockchainDbContext())
                                    {
                                        var blocksToDelete = db.Blocks.Where(b => b.Height >= indexNotSame).ToList();
                                        blocksToDelete.ForEach(b => db.Blocks.Remove(b));
                                        db.SaveChanges();
                                        alternateBlockChain.ForEach(b => db.Blocks.Add(b));
                                        db.SaveChanges();
                                    }
                                }
                                else
                                {
                                    if (_blockChain.NextBlock != null)
                                    {
                                        _blockChain.NextBlock.StopMining();
                                        await Task.Delay(5000);
                                    }
                                    using (BlockchainDbContext db = new BlockchainDbContext())
                                    {
                                        var blocksToDelete = db.Blocks.ToList();
                                        blocksToDelete.ForEach(b => db.Blocks.Remove(b));
                                        db.SaveChanges();
                                    }
                                }
                            }
                            catch (Exception exc)
                            {
                            }
                        }
                    }
                }
            }
        }