Beispiel #1
0
        public IActionResult GetBlocks([FromBody] ViewModel <string> viewModel)
        {
            if (viewModel != null && viewModel.Data != null)
            {
                var blockHash = viewModel.Data;

                lock (GateKeeper.ChainManagerLock) lock (GateKeeper.BalanceLedgerLock)
                    {
                        var startBlock = _chainManager.GetBlock(HexConverter.ToBytes(blockHash));

                        if (startBlock != null)
                        {
                            var topHeight = _chainManager.Height;

                            if (startBlock.Height < topHeight)
                            {
                                var blockCount = topHeight - startBlock.Height;

                                var blocks = _chainManager.GetBlocks(startBlock.Height + 1, blockCount > 100 ? 100 : blockCount);

                                var serializedBlocks = new List <string>();

                                foreach (var block in blocks)
                                {
                                    serializedBlocks.Add(HexConverter.ToPrefixString(block.Serialize()));
                                }

                                return(Ok(new SyncDTO()
                                {
                                    Blocks = serializedBlocks,
                                    Synced = serializedBlocks.Count == 100 ? false : true
                                }));
                            }
                            else
                            {
                                return(Ok(new SyncDTO()
                                {
                                    Synced = true,
                                    Blocks = null
                                }));
                            }
                        }
                        else
                        {
                            var blockCount = _chainManager.Height < 100 ? _chainManager.Height : 100;
                            var blocks     = _chainManager.GetBlocks(_chainManager.Height - blockCount, blockCount);

                            var serializedBlocks = new List <string>();

                            foreach (var block in blocks)
                            {
                                serializedBlocks.Add(HexConverter.ToPrefixString(block.Serialize()));
                            }

                            return(Ok(new SyncDTO()
                            {
                                Blocks = serializedBlocks,
                                Synced = true
                            }));
                        }
                    }
            }
            else
            {
                return(BadRequest());
            }
        }