Exemplo n.º 1
0
        public bool SignAndSubmit(Transaction transaction)
        {
            var signed = new SignedTransaction(transaction);

            SignTransaction(signed, LedgerService.LedgerManager.Network);
            var command   = LiveService.AddCommand(new SendTransactionCommand(signed, (response, code) => {}));
            var output    = command.GetOutput();
            var isSuccess = command.ResultCode == ResultCode.Success;

            if (isSuccess)
            {
                TransactionSubmissionListener.OnSubmitted(signed);
            }

            return(isSuccess);
        }
Exemplo n.º 2
0
        private void DispatchRequest(Request request, Action <Response, ResultCode> sendResponse)
        {
            if (request is GetAccountRequest)
            {
                var message = (GetAccountRequest)request;

                var command = new GetAccountCommand(message.Address, (acc, rc) => sendResponse(ResponseHelper.CreateGetAccountResponse(acc), rc));
                LiveService.AddCommand(command);
            }
            else if (request is GetTransactionsRequest)
            {
                var message = (GetTransactionsRequest)request;

                if (!RequestHelper.TryReadTransactionHashes(message, out var hashes))
                {
                    sendResponse.Call(ResponseHelper.CreateGetTransactionResponse(), ResultCode.InvalidInputParam);
                    return;
                }

                LiveService.AddCommand(new GetTransactionsCommand(hashes, sendResponse));
            }
            else if (request is SendTransactionRequest)
            {
                var message = (SendTransactionRequest)request;
                if (!RequestHelper.TryReadSignedTransaction(message, out var signed))
                {
                    sendResponse.Call(new SendTransactionResponse(), ResultCode.CannotReadSignedTransaction);
                    return;
                }

                LiveService.AddCommand(new SendTransactionCommand(signed, sendResponse));
            }
            else if (request is GetSignedLedgerRequest)
            {
                var message = (GetSignedLedgerRequest)request;

                if (message.Height < 0)
                {
                    sendResponse.Call(ResponseHelper.CreateGetSignedLedgerResponseFromZip(null), ResultCode.InvalidInputParam);
                    return;
                }

                var height = LedgerService.LedgerManager.GetSignedLedger().GetHeight();

                if (message.Height > height)
                {
                    sendResponse.Call(ResponseHelper.CreateGetSignedLedgerResponseFromZip(null), ResultCode.LedgerDoesnotExist);
                    return;
                }

                if (message.Height == height)
                {
                    var ledger = LedgerService.LedgerManager.GetSignedLedger();
                    var zipped = LedgerCompressionEngine.ZipSignedLedger(ledger);
                    sendResponse.Call(ResponseHelper.CreateGetSignedLedgerResponseFromZip(zipped), ResultCode.Success);
                    return;
                }

                var signed = DatabaseService.ReadDatabaseManager.GetRawLedger(message.Height);
                if (signed == null)
                {
                    sendResponse.Call(ResponseHelper.CreateGetSignedLedgerResponseFromZip(null), ResultCode.LedgerDoesnotExist);
                }

                sendResponse.Call(ResponseHelper.CreateGetSignedLedgerResponseFromZip(signed), ResultCode.Success);
            }
            else if (request is GetCurrentLedgerHeightRequest)
            {
                var height = LedgerService.LedgerManager.GetSignedLedger().GetHeight();
                sendResponse.Call(ResponseHelper.CreateGetCurrentLedgerHeightResponse(height), ResultCode.Success);
            }

            else
            {
                sendResponse.Call(new Response(), ResultCode.UnknownMessage);
            }
        }
Exemplo n.º 3
0
        private void DispatchRequest(ISession session, Request request, Action <Response, ResultCode> sendResponse)
        {
            if (request is SendTransactionRequest)
            {
                var message = (SendTransactionRequest)request;
                if (!RequestHelper.TryReadSignedTransaction(message, out var signed))
                {
                    sendResponse.Call(ResponseHelper.CreateSendTransactionResponse(), ResultCode.CannotReadSignedTransaction);
                    return;
                }

                LiveService.AddCommand(new SendTransactionCommand(signed, (r, rc) =>
                {
                    if (rc == ResultCode.Success)
                    {
                        ExplorerConnectionService.SubscriptionManager.ListenTo(session, new TransactionTopic(signed.Hash));
                    }
                    sendResponse.Call(ResponseHelper.CreateSendTransactionResponse(signed.Hash), rc);
                }));
            }
            else if (request is GetSignedLedgerRequest)
            {
                var message = (GetSignedLedgerRequest)request;

                if (!message.Height.HasValue) // Get current ledger
                {
                    var ledger = LedgerService.LedgerManager.GetSignedLedger();
                    sendResponse.Call(ResponseHelper.CreateGetSignedLedgerResponse(ledger), ResultCode.Success);
                    return;
                }

                var height = message.Height.Value;

                if (height < 0)
                {
                    sendResponse.Call(ResponseHelper.CreateGetSignedLedgerResponse(), ResultCode.InvalidInputParam);
                    return;
                }

                var currentHeight = LedgerService.LedgerManager.GetSignedLedger().GetHeight();

                if (height > currentHeight)
                {
                    sendResponse.Call(ResponseHelper.CreateGetSignedLedgerResponse(), ResultCode.LedgerDoesnotExist);
                    return;
                }

                if (height == currentHeight)
                {
                    var ledger = LedgerService.LedgerManager.GetSignedLedger();
                    sendResponse.Call(ResponseHelper.CreateGetSignedLedgerResponse(ledger), ResultCode.Success);
                    return;
                }

                var signed = DatabaseService.ReadDatabaseManager.GetLedgerFromRaw(height);
                if (signed == null)
                {
                    sendResponse.Call(ResponseHelper.CreateGetSignedLedgerResponse(), ResultCode.LedgerDoesnotExist);
                    return;
                }

                sendResponse.Call(ResponseHelper.CreateGetSignedLedgerResponse(signed), ResultCode.Success);
            }
            else if (request is GetBalanceRequest)
            {
                var message = (GetBalanceRequest)request;

                var command = new GetAccountCommand(message.Address, (acc, rc) => sendResponse(ResponseHelper.CreateGetBalanceResponse(acc), rc));
                LiveService.AddCommand(command);
            }
            else if (request is GetAccountRequest)
            {
                var message = (GetAccountRequest)request;

                var command = new GetAccountCommand(message.Address, (acc, rc) => sendResponse(ResponseHelper.CreateGetAccountResponse(acc), rc));
                LiveService.AddCommand(command);
            }
            else if (request is GetTransactionRequest)
            {
                var    message = (GetTransactionRequest)request;
                byte[] hash;
                try
                {
                    hash = Convert.FromBase64String(message.Hash);
                }
                catch (Exception)
                {
                    sendResponse.Call(ResponseHelper.CreateGetTransactionResponse(), ResultCode.NotABase64);
                    return;
                }

                if (hash.Length != TransactionHash.SIZE)
                {
                    sendResponse.Call(ResponseHelper.CreateGetTransactionResponse(), ResultCode.NotAHash256);
                    return;
                }

                var transaction = ExplorerDatabaseService.ReadDatabaseManager.GetTransaction(new TransactionHash(hash));

                sendResponse.Call(ResponseHelper.CreateGetTransactionResponse(TransactionConverter.GetTransaction(transaction)), ResultCode.Success);
            }
            else if (request is GetTransactionHistoryRequest)
            {
                var     message = (GetTransactionHistoryRequest)request;
                Address address;
                try
                {
                    address = new Address(message.Address);
                }
                catch (Exception)
                {
                    sendResponse.Call(ResponseHelper.CreateGetTransactionHistoryResponse(), ResultCode.ReadAddressFailure);
                    return;
                }

                if (message.Count <= 0)
                {
                    sendResponse.Call(ResponseHelper.CreateGetTransactionHistoryResponse(), ResultCode.InvalidInputParam);
                    return;
                }

                if (message.Page <= 0)
                {
                    sendResponse.Call(ResponseHelper.CreateGetTransactionHistoryResponse(), ResultCode.InvalidInputParam);
                    return;
                }

                // TODO this is a temporary hack
                {
                    var raw   = ExplorerDatabaseService.ReadDatabaseManager.GetTransactionHistory(address, message.Height).OrderByDescending(_ => _.LedgerHeight).ToList();
                    var total = raw.Count;

                    raw = raw.Skip(message.Count * (message.Page - 1)).Take(message.Count).ToList();

                    var results = new List <HistoricalTransaction>();

                    foreach (var transaction in raw)
                    {
                        var ledger = DatabaseService.ReadDatabaseManager.GetLedgerFromRaw(transaction.LedgerHeight);
                        results.Add(new HistoricalTransaction(transaction.LedgerHeight, transaction.Transaction, ledger.GetTimestamp()));
                    }

                    var transactions = results.Select(TransactionConverter.GetHistoricalTransaction).ToList();
                    sendResponse.Call(ResponseHelper.CreateGetTransactionHistoryResponse(transactions, total), ResultCode.Success);
                }
            }
            else if (request is GetLedgerRequest)
            {
                var message = (GetLedgerRequest)request;

                if (message.Height != null && message.Height >= 0)
                {
                    var currentHeight = LedgerService.LedgerManager.GetSignedLedger().GetHeight();

                    if (message.Height > currentHeight)
                    {
                        sendResponse.Call(ResponseHelper.CreateGetLedgerResponse(), ResultCode.LedgerDoesnotExist);
                        return;
                    }

                    if (message.Height == currentHeight)
                    {
                        sendResponse.Call(ResponseHelper.CreateGetLedgerResponse(LedgerConverter.GetLedger(LedgerService.LedgerManager.GetSignedLedger())), ResultCode.Success);
                        return;
                    }

                    var signed = DatabaseService.ReadDatabaseManager.GetLedgerFromRaw(message.Height.Value);
                    if (signed == null)
                    {
                        sendResponse.Call(ResponseHelper.CreateGetLedgerResponse(), ResultCode.LedgerDoesnotExist);
                        return;
                    }

                    var ledger = LedgerConverter.GetLedger(signed);
                    sendResponse.Call(ResponseHelper.CreateGetLedgerResponse(ledger), ResultCode.Success);
                    return;
                }

                if (!string.IsNullOrEmpty(message.Hash))
                {
                    try
                    {
                        var bytes  = Convert.FromBase64String(message.Hash);
                        var ledger = DatabaseService.ReadDatabaseManager.GetLedgerByHash(new LedgerHash(bytes));
                        sendResponse.Call(ResponseHelper.CreateGetLedgerResponse(LedgerConverter.GetLedger(ledger)), ResultCode.Success);
                        return;
                    }
                    catch (Exception e)
                    {
                        sendResponse.Call(ResponseHelper.CreateGetLedgerResponse(), ResultCode.InvalidInputParam);
                        return;
                    }
                }
                sendResponse.Call(ResponseHelper.CreateGetLedgerResponse(), ResultCode.InvalidInputParam);
            }
            else if (request is GetOrderBookRequest)
            {
                var message = (GetOrderBookRequest)request;

                if (message.Symbol.IsNullOrEmpty() || !OrderBookService.GetSymbols().Contains(message.Symbol))
                {
                    sendResponse.Call(ResponseHelper.CreateGetOrderBookResponse(null, message.Symbol), ResultCode.InvalidInputParam);
                    return;
                }

                OrderBookService.GetOrderBook(message.Symbol, (ob) => sendResponse(ResponseHelper.CreateGetOrderBookResponse(OrderConverter.GetOrders(ob), message.Symbol), ResultCode.Success));
            }
            else if (request is SubscribeRequest)
            {
                var message = (SubscribeRequest)request;

                if (TopicsConverter.TryGetTopic(message.Topic, OrderBookService.GetSymbols(), out var topic))
                {
                    ExplorerConnectionService.SubscriptionManager.ListenTo(session, topic);
                    sendResponse(ResponseHelper.CreateSubscribeResponse(), ResultCode.Success);
                    return;
                }

                sendResponse(ResponseHelper.CreateSubscribeResponse(), ResultCode.InvalidInputParam);
            }
            else if (request is GetLatestLedgersRequest)
            {
                var height = LedgerService.LedgerManager.GetSignedLedger().GetHeight();

                var ledgers = DatabaseService.ReadDatabaseManager.GetLedgersFromHeight(height - 9);

                var results = ledgers.Select(LedgerConverter.GetLedger).ToList();
                sendResponse.Call(ResponseHelper.CreateGetLatestLedgersResponse(results), ResultCode.Success);
            }

            else
            {
                sendResponse.Call(new Response(), ResultCode.UnknownMessage);
            }
        }