Ejemplo n.º 1
0
        static List <StreamTransaction> transactions = new List <StreamTransaction>(); // List that stores stream transactions


        // Called when receiving S2 data from clients
        public static void receiveData(byte[] bytes, RemoteEndpoint endpoint)
        {
            string endpoint_wallet_string = Base58Check.Base58CheckEncoding.EncodePlain(endpoint.presence.wallet);

            Logging.info(string.Format("Receiving S2 data from {0}", endpoint_wallet_string));

            StreamMessage message = new StreamMessage(bytes);

            // Don't allow clients to send error stream messages, as it's reserved for S2 nodes only
            if (message.type == StreamMessageCode.error)
            {
                Logging.warn(string.Format("Discarding error message type from {0}", endpoint_wallet_string));
                return;
            }

            // TODO: commented for development purposes ONLY!

            /*if (QuotaManager.exceededQuota(endpoint.presence.wallet))
             * {
             *  Logging.error(string.Format("Exceeded quota of info relay messages for {0}", endpoint_wallet_string));
             *  sendError(endpoint.presence.wallet);
             *  return;
             * }*/

            bool data_message = false;

            if (message.type == StreamMessageCode.data)
            {
                data_message = true;
            }

            QuotaManager.addActivity(endpoint.presence.wallet, data_message);

            // Relay certain messages without transaction
            NetworkServer.forwardMessage(message.recipient, ProtocolMessageCode.s2data, bytes);

            // TODO: commented for development purposes ONLY!

            /*
             *          // Extract the transaction
             *          Transaction transaction = new Transaction(message.transaction);
             *
             *          // Validate transaction sender
             *          if(transaction.from.SequenceEqual(message.sender) == false)
             *          {
             *              Logging.error(string.Format("Relayed message transaction mismatch for {0}", endpoint_wallet_string));
             *              sendError(message.sender);
             *              return;
             *          }
             *
             *          // Validate transaction amount and fee
             *          if(transaction.amount < CoreConfig.relayPriceInitial || transaction.fee < CoreConfig.transactionPrice)
             *          {
             *              Logging.error(string.Format("Relayed message transaction amount too low for {0}", endpoint_wallet_string));
             *              sendError(message.sender);
             *              return;
             *          }
             *
             *          // Validate transaction receiver
             *          if (transaction.toList.Keys.First().SequenceEqual(Node.walletStorage.address) == false)
             *          {
             *              Logging.error("Relayed message transaction receiver is not this S2 node");
             *              sendError(message.sender);
             *              return;
             *          }
             *
             *          // Update the recipient dictionary
             *          if (dataRelays.ContainsKey(message.recipient))
             *          {
             *              dataRelays[message.recipient]++;
             *              if(dataRelays[message.recipient] > Config.relayDataMessageQuota)
             *              {
             *                  Logging.error(string.Format("Exceeded amount of unpaid data relay messages for {0}", endpoint_wallet_string));
             *                  sendError(message.sender);
             *                  return;
             *              }
             *          }
             *          else
             *          {
             *              dataRelays.Add(message.recipient, 1);
             *          }
             *
             *
             *          // Store the transaction
             *          StreamTransaction streamTransaction = new StreamTransaction();
             *          streamTransaction.messageID = message.getID();
             *          streamTransaction.transaction = transaction;
             *          lock (transactions)
             *          {
             *              transactions.Add(streamTransaction);
             *          }
             *
             *          // For testing purposes, allow the S2 node to receive relay data itself
             *          if (message.recipient.SequenceEqual(Node.walletStorage.getWalletAddress()))
             *          {
             *              string test = Encoding.UTF8.GetString(message.data);
             *              Logging.info(test);
             *
             *              return;
             *          }
             *
             *          Logging.info("NET: Forwarding S2 data");
             *          NetworkStreamServer.forwardMessage(message.recipient, DLT.Network.ProtocolMessageCode.s2data, bytes);
             */
        }