Пример #1
0
        public IActionResult Relay([FromBody] ViewModel <string> viewModel) //data are RLP encoded TX
        {
            if (viewModel != null && viewModel.Data != null)
            {
                lock (GateKeeper.TransactionPoolLock)
                {
                    lock (GateKeeper.BalanceLedgerLock)
                    {
                        try
                        {
                            var tx = new Transaction(viewModel.Data);
                            if (_transactionPool.AddTransactions(new List <Transaction> {
                                tx
                            }))
                            {
                                List <string> contacts;

                                lock (GateKeeper.ContactLock)
                                {
                                    contacts = _contactLedger.GetAll();
                                }

                                foreach (var contact in contacts)
                                {
                                    _httpProvider.PeerPost <string, string>(viewModel.Data, contact + "transaction/new");
                                }

                                Console.WriteLine($"[TransactionController] Recieved new transaction! Relayed to {contacts.Count} nodes");

                                return(Ok("ok"));
                            }
                            else
                            {
                                return(BadRequest());
                            }
                        }
                        catch
                        {
                            return(BadRequest());
                        }
                    }
                }
            }
            else
            {
                return(BadRequest());
            }
        }
Пример #2
0
        public IActionResult Relay([FromBody] ViewModel <string> viewModel)
        {
            IActionResult ret = null;

            if (viewModel != null && viewModel.Data != null)
            {
                var blockHex = viewModel.Data;

                var currentHeight = _chainManager.Height;

                Block newBlock = null;

                lock (GateKeeper.BalanceLedgerLock)
                {
                    lock (GateKeeper.ChainManagerLock)
                    {
                        newBlock = new Block(HexConverter.ToBytes(blockHex));
                        _chainManager.ProcessBlocks(new List <Block>()
                        {
                            newBlock
                        });
                    }
                }

                if (newBlock != null)
                {
                    if (currentHeight == newBlock.Height - 1)
                    {
                        List <string> contacts;

                        lock (GateKeeper.ContactLock)
                        {
                            contacts = _contactLedger.GetAll();
                        }

                        foreach (var contact in contacts)
                        {
                            _httpProvider.PeerPost <string, string>(blockHex, contact + "block/new");
                        }

                        Console.WriteLine($"[ChainManager] Recieved new block. Height: {newBlock.Height}. Relaying to {contacts.Count} nodes");

                        ret = Ok();
                    }
                    else
                    {
                        ret = BadRequest();
                    }
                }
                else
                {
                    ret = BadRequest();
                }
            }
            else
            {
                ret = BadRequest();
            }

            if (ret == null)
            {
                ret = BadRequest();
            }

            return(ret);
        }