Exemplo n.º 1
0
        public List <BlockSyncApiModel> All()
        {
            List <BlockSyncApiModel> blocks = new List <BlockSyncApiModel>();

            foreach (var b in Node.BlockChain)
            {
                blocks.Add(BlockSyncApiModel.FromBlock(b.Value));
            }

            return(blocks);
        }
Exemplo n.º 2
0
        public void AttachBroadcastedBlock(BlockSyncApiModel block, string nodeAddress)
        {
            bool isPastBlock = block.Index <= LastBlock.Index;

            if (isPastBlock)
            {
                return;
            }

            Block minedBlock = Block.ReCreateBlock(block);

            ValidateBlockHash(minedBlock, minedBlock.Nonce, minedBlock.BlockHash);

            // replace blockchain if another blockain is longer
            int nodeDifference = minedBlock.Index - BlockChain.Count;

            if (nodeDifference >= 6)
            {
                int          startIndex   = minedBlock.Index - nodeDifference;
                List <Block> forkedBlocks = NodeSynchornizator.GetBlocksForSync(nodeAddress);

                foreach (var bl in forkedBlocks)
                {
                    RevalidateBlock(bl);
                    BlockChain.AddOrUpdate(bl.Index, bl, (index, curBlock) => { return(bl); });
                }

                List <string> blockTxs = forkedBlocks.SelectMany(b => b.Transactions).Select(t => t.TransactionHash).ToList();

                PendingTransactions = new ConcurrentBag <Transaction>(PendingTransactions.
                                                                      Where(t => !blockTxs.Contains(t.TransactionHash)));
            }
            else
            {
                bool isFutureBlock = LastBlock.BlockHash != minedBlock.PreviousBlockHash;
                if (isFutureBlock)
                {
                    return;
                }

                RevalidateBlock(minedBlock);

                // remove mined transactions from pending transactions
                List <string> minedTxIds = minedBlock.Transactions.Select(t => t.TransactionHash).ToList();
                PendingTransactions = new ConcurrentBag <Transaction>(PendingTransactions.Where(t => !minedTxIds.Contains(t.TransactionHash)).ToList());

                BlockChain.TryAdd(minedBlock.Index, minedBlock);
            }
        }
Exemplo n.º 3
0
        public static BlockSyncApiModel FromBlock(Block block)
        {
            BlockSyncApiModel blockModel = new BlockSyncApiModel();

            blockModel.Index             = block.Index;
            blockModel.Transactions      = block.Transactions.ToList();
            blockModel.Difficulty        = block.Difficulty;
            blockModel.PreviousBlockHash = block.PreviousBlockHash;
            blockModel.MinedBy           = block.MinedBy;
            blockModel.BlockDataHash     = block.BlockDataHash;
            blockModel.BlockHash         = block.BlockHash;
            blockModel.Nonce             = block.Nonce;
            blockModel.CreatedDate       = block.CreatedDate;

            return(blockModel);
        }
Exemplo n.º 4
0
        public static Block ReCreateBlock(BlockSyncApiModel bm)
        {
            Block block = new Block
            {
                Index             = bm.Index,
                Transactions      = bm.Transactions,
                Difficulty        = bm.Difficulty,
                PreviousBlockHash = bm.PreviousBlockHash,
                MinedBy           = bm.MinedBy,
                BlockDataHash     = bm.BlockDataHash,
                BlockHash         = bm.BlockHash,
                Nonce             = bm.Nonce,
                CreatedDate       = bm.CreatedDate
            };

            return(block);
        }
Exemplo n.º 5
0
        public List <BlockSyncApiModel> GetBlocksForSync(int fromIndex, int count)
        {
            List <BlockSyncApiModel> blocks = new List <BlockSyncApiModel>();
            int endIndex = fromIndex + count - 1;

            if (endIndex > Node.BlockChain.Count)
            {
                endIndex = Node.BlockChain.Count;
            }

            for (int i = fromIndex; i < endIndex; i++)
            {
                blocks.Add(BlockSyncApiModel.FromBlock(Node.BlockChain[i]));
            }

            return(blocks);
        }
        public void BroadcastBlock(Block block)
        {
            foreach (Peer p in Peers)
            {
                try
                {
                    RestClient       client   = new RestClient(p.Url);
                    NewBlockApiModel apiModel = new NewBlockApiModel();
                    apiModel.Block       = BlockSyncApiModel.FromBlock(block);
                    apiModel.NodeAddress = Current.Url;

                    var blData = JsonConvert.SerializeObject(apiModel);
                    Console.WriteLine("Block for broadcast: " + blData);
                    Console.WriteLine();
                    Console.WriteLine();

                    client.Post("api/block/new", apiModel);
                }
                catch (Exception ex) { }
            }
        }