private void HandleTransaction(CommandMessage message)
        {
            var tx = new Transaction().DeSerialize(message.Data);

            logger.Debug("New Transaction recieved : " + HexadecimalEncoding.ToHexString(tx.Id));

            //add to pool
            TransactionPool.Add(HexadecimalEncoding.ToHexString(tx.Id), tx);
            TransactionPoolChanged?.Invoke(this, new TransactionPoolEventArgs(new List <string>(TransactionPool.Keys)));

            if (!miningInProgress)
            {
                if (TransactionPool.Count >= numberOfTransactionsToStartMining)
                {
                    //Mine transaction
                    var thread = new Thread(MineTransactions);
                    thread.Start();
                }
            }
        }
        public void Send(string from, string to, int amount)
        {
            var utxoSet = new UTXOSet(chain);

            var tx = Transaction.NewTransaction(from, to, amount, utxoSet);

            if (tx == null)
            {
                logger.Debug("Couldn't create new TX to send");
                InsufficientFund?.Invoke(this, EventArgs.Empty);
                return;
            }
            //old ver
            //var block = chain.MineBlock(new List<Transaction>() { tx });
            //utxoSet.Update(block);
            //WholeChainDownloaded?.Invoke(this, EventArgs.Empty);
            //chain.ReindexUTXO();


            //check if not referencing same output, TODO BUG FIX
            //var referencingSameOutput = false;
            //foreach (var transaction in TransactionPool)
            //{
            //    var txOutputs = tx.Outputs;
            //    var poolOutputs = transaction.Value.Outputs;

            //    foreach (var outp in poolOutputs)
            //    {
            //        foreach (var txOutp in txOutputs)
            //        {
            //            if (outp.Equals(txOutp))
            //            {
            //                referencingSameOutput = true;
            //                break;
            //            }
            //        }
            //        if (referencingSameOutput) break;
            //    }
            //    if (referencingSameOutput) break;
            //}

            //if (referencingSameOutput)
            //{
            //    logger.Debug("Referencing same output in tx. Tx not added to TPool");
            //    return;
            //}

            //add to pool
            TransactionPool.Add(HexadecimalEncoding.ToHexString(tx.Id), tx);
            TransactionPoolChanged?.Invoke(this, new TransactionPoolEventArgs(new List <string>(TransactionPool.Keys)));

            //send to all except me and the one who send the tx
            var addressesToExclude = new string[] { _blockchainPeer.ClientDetails().ToString() };
            var msg = new CommandMessage();

            msg.Command = CommandType.Transaction;
            msg.Client  = _blockchainPeer.ClientDetails();
            msg.Data    = tx.Serialize();

            _blockchainPeer.BroadcastMessageAsyncExceptAddress(addressesToExclude, msg);
            logger.Debug("Sending tx over nettwork");

            if (!miningInProgress)
            {
                if (TransactionPool.Count >= numberOfTransactionsToStartMining)
                {
                    var thread = new Thread(MineTransactions);
                    thread.Start();
                }
            }
        }
Esempio n. 3
0
 private void OnTransactionPoolChanged()
 {
     TransactionPoolChanged?.Invoke(this, new EventArgs());
 }