Example #1
0
        protected TransactionInfo AddToPoolAndReserve()
        {
            lock (this) {
                TransactionInfo transInfo = null;
                while (transInfo == null)
                {
                    transInfo = BuildTransactionInfo();
                    transInfo.Reserve();

                    //drivers doesn't ensure connection is good so make sure
                    if (transInfo.Transaction.Connection == null || transInfo.Transaction.Connection.State == ConnectionState.Closed)
                    {
                        if (transInfo.Transaction.Connection != null)
                        {
                            transInfo.ReturnConnectionToPool();
                        }
                        LogException(new InvalidTransactionException("Connection in transaction is null."), new StackTrace(), "Releasing connection pools and retrying");
                        transInfo = null; // retry;
                        TransactionService.ReleasePooledConnections("Connection in transaction is null.");
                        Thread.Sleep(TransactionServiceConstants.RETRY_CONNECTION_TIME);
                    }
                }

#if DEBUG_DBCONNECTIONPOOLING
                OSTrace.Info("DBConnectionPooling[TID=" + transInfo.Transaction.GetHashCode() + "] - reserved from pool");
                OSTrace.Error(new StackTrace().ToString());
#endif
                TransactionPool.Add(transInfo.Transaction, transInfo);
                return(transInfo);
            }
        }
Example #2
0
        public async Task Merge(Pool pool)
        {
            await DepositPool.Add(await pool.DepositPool.GetPool());

            await OfferPool.Add(await pool.OfferPool.GetPool());

            await OfferCancelPool.Add(await pool.OfferCancelPool.GetPool());

            await TransactionPool.Add(await pool.TransactionPool.GetPool());

            await WithdrawalPool.Add(await pool.WithdrawalPool.GetPool());
        }
Example #3
0
        public async void AddTransaction(string message)
        {
            var guid        = Guid.NewGuid();
            var transaction = new Transaction {
                Timestamp = DateTime.UtcNow, Message = message
            };

            TransactionPool.Add(transaction);
            OnTransactionPoolChanged();
            await Task.Run(async() => {
                foreach (var node in Nodes)
                {
                    try {
                        (await httpClient_.PostAsync(node, new StringContent(JsonConvert.SerializeObject(
                                                                                 new SendTransaction {
                            PortNumber = PortNumber, Transaction = transaction
                        })))).Dispose();
                    } catch (HttpRequestException) {
                    }
                }
            });
        }
 public bool AddToPool(Transaction tx)
 {
     return(transactionPool.Add(tx, unspentTxOuts));
 }
Example #5
0
        public Node()
        {
            if (File.Exists(ConfigFileName))
            {
                string s;
                using (var sr = new StreamReader(ConfigFileName, Encoding)) {
                    s = sr.ReadToEnd();
                }
                var config = JsonConvert.DeserializeObject <Config>(s);
                PortNumber = config.PortNumber;
                Nodes      = config.Nodes ?? new HashSet <Uri>();
            }

            BlockChain.Add(GetGenesisBlock());
            LastHash = SerializeHash(GetHash(BlockChain.Last()));

            httpListener_.Prefixes.Add($"http://+:{PortNumber}/");
            httpListener_.Start();

            Task.Run(() => {
                while (true)
                {
                    var context = httpListener_.GetContext();
                    void respond()
                    {
                        context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                        context.Response.OutputStream.Close();
                    }
                    string requestString;
                    using (var sr = new StreamReader(context.Request.InputStream)) {
                        requestString = sr.ReadToEnd();
                    }
                    Log += $"request get: {requestString}{Environment.NewLine}";
                    OnLogChanged();
                    JObject requestJObject;
                    try {
                        requestJObject = JObject.Parse(requestString);
                    } catch (JsonReaderException) {
                        respond();
                        continue;
                    }
                    if (!requestJObject.ContainsKey("Matcha") || requestJObject["Matcha"].Type != JTokenType.String ||
                        requestJObject["Matcha"].Value <string>() != "Matcha")
                    {
                        respond();
                        continue;
                    }
                    var client = new Uri($"http://{context.Request.RemoteEndPoint.Address}"
                                         + $":{requestJObject["PortNumber"].Value<int>()}/");
                    Nodes.Add(client);
                    var response        = context.Response;
                    response.StatusCode = 200;
                    using (var sw = new StreamWriter(response.OutputStream, Encoding)) {
                        switch (requestJObject["Type"].Value <string>())
                        {
                        case nameof(Methods.GetNodes): {
                            var nodes = new HashSet <Uri>(Nodes);
                            nodes.Remove(client);
                            sw.Write(JsonConvert.SerializeObject(new GetNodesResponse {
                                    Nodes = nodes
                                }));
                            break;
                        }

                        case nameof(SendTransaction): {
                            var request = requestJObject.ToObject <SendTransaction>();
                            if (!TransactionPool.Contains(request.Transaction))
                            {
                                TransactionPool.Add(request.Transaction);
                                OnTransactionPoolChanged();
                            }
                            break;
                        }

                        case nameof(GetCurrentIndex): {
                            sw.Write(JsonConvert.SerializeObject(new GetCurrentIndexResponse {
                                    CurrentIndex = BlockChain.Last().Index
                                }));
                            break;
                        }

                        case nameof(GetBlocks): {
                            var request = requestJObject.ToObject <GetBlocks>();
                            sw.Write(JsonConvert.SerializeObject(new GetBlocksResponse {
                                    Blocks = BlockChain.Skip(request.Index).Take(request.NumBlocks).ToList()
                                }));
                            break;
                        }
                        }
                    }
                }
            });
        }
Example #6
0
 public async Task Add(IEnumerable <Domain.Transaction> tx) =>
 await TransactionPool.Add(tx);
Example #7
0
 public async Task <bool> Add(Domain.Transaction tx) =>
 await TransactionPool.Add(tx);