Ejemplo n.º 1
0
        private static void SendToChain(string address, RestIotaRepository repository, string message)
        {
            Seed     seed     = new Seed(address);
            Transfer transfer = new Transfer
            {
                Address         = new Address(address),
                Message         = TryteString.FromUtf8String(message),
                ValueToTransfer = 0
            };

            Bundle bundle = new Bundle();

            bundle.AddTransfer(transfer);


            int depth = 3;
            int minWeightMagnitude = 9;

            try
            {
                var sendTransfer = repository.SendTransfer(seed, bundle, SecurityLevel.Medium, depth, minWeightMagnitude);

                //TransactionHashList bundleTransactions = repository.FindTransactionsByBundles(new List<Hash> {bundle.Hash});
                //foreach (Hash transactionsHash in bundleTransactions.Hashes)
                //{
                //    Hash hash = new Hash(transactionsHash.Value);
                //    var transactionsTrytes = repository.GetTrytes(bundleTransactions.Hashes);
                //    var transaction = Transaction.FromTrytes(transactionsTrytes[0], hash);
                //}

                TransactionHashList transactionsByAdress =
                    repository.FindTransactionsByAddresses(new List <Address> {
                    new Address(address)
                });

                var  transactionsTrytesAddress = repository.GetTrytes(transactionsByAdress.Hashes).First();
                Hash hashAddress = new Hash(transactionsByAdress.Hashes[0].Value);

                Transaction       transactionOne    = Transaction.FromTrytes(transactionsTrytesAddress, hashAddress);
                TransactionTrytes transactionTrytes = new TransactionTrytes("");
                TransactionTrytes test = transactionOne.ToTrytes();
                var text = test.Value;
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
                //throw;
            }
        }
        private void DoProcessPipelineTasks(object state)
        {
            const int maxMessageChars = 2187;

            if (this.ProcessingTasksInProgress)
            {
                return;                            // task is already running, exiting
            }
            this.ProcessingTasksInProgress = true; // semaphore

            _logger.LogInformation("Background Task: Processing Tasks... Starting");

            var tasks = _db.GetDBTaskEntryFromPipelineAsync().Result;

            if (tasks.Count > 0)
            {
                bool ErrorOccurred = false;
                foreach (var item in tasks)
                {
                    if (item.Task == "SENDTX") // has to be all uppercase
                    {
                        var actualnode    = this._nodemanager.SelectNode();
                        var actualPOWnode = this._nodemanager.SelectPOWNode();
                        // TODO: select also POW service and potential failover

                        if (actualPOWnode == null) //if no POW server then using the same node as for TIPS selection, etc.
                        {
                            actualPOWnode = actualnode;
                            if (actualPOWnode != null)
                            {
                                _logger.LogInformation("No POW server. Using the standard one... Actual node: {actualnode}", actualnode);
                            }
                        }

                        if (actualnode != null && actualPOWnode != null)
                        {
                            var IotaRepo = new RestIotaRepository(new RestClient(actualnode)
                            {
                                Timeout = 15000
                            }, new POWService(actualPOWnode));
                            var    bundle    = new Bundle();
                            Bundle RetBundle = null;
                            var    guid      = item.GlobalId;
                            var    seed      = Seed.Random();
                            var    input     = item.Input;

                            var MessageinTrytes = TryteString.FromUtf8String(input.Message).Value; // converting to trytes

                            while (MessageinTrytes.Length > 0)                                     // now let's split message to several transactions in case it is too long. Max is 2187 trytes
                            {
                                bundle.AddTransfer(
                                    new Transfer
                                {
                                    Address         = new Address(input.Address),
                                    Tag             = new Tag(input.Tag),
                                    ValueToTransfer = 0,
                                    Timestamp       = Timestamp.UnixSecondsTimestamp,
                                    Message         = new TryteString(MessageinTrytes.Length > maxMessageChars ? MessageinTrytes.Substring(0, maxMessageChars) : MessageinTrytes)
                                });
                                MessageinTrytes = MessageinTrytes.Length > maxMessageChars?MessageinTrytes.Substring(maxMessageChars - 1) : "";
                            }

                            try
                            {
                                _logger.LogInformation("Performing external API call SendTransfer via {actualnode}", actualnode);
                                RetBundle = IotaRepo.SendTransfer(seed, bundle, 2, depth: 3);
                                _logger.LogInformation("Background Task: Processing Tasks... Transaction sent, Bundle Hash: {RetBundle.Hash.Value}", RetBundle.Hash.Value);
                            }
                            catch (Exception e)
                            {
                                // swallowing exception
                                ErrorOccurred = true;
                                _logger.LogInformation("Background Task: Processing Tasks... Error occured, Error: {e}", e);
                            }

                            if (!ErrorOccurred)
                            {
                                _db.UpdateDBTaskEntryInPipeline(guid, 200, RetBundle.Hash.Value).Wait();
                            }
                        }
                        else
                        {
                            _logger.LogInformation("No nodes via which to send TX. Skipping it for the current cycle...");
                        }
                    }
                }
            }
            else
            { // no other tasks in pipeline
                StopProcessingPipeline();
            }
            this.ProcessingTasksInProgress = false; // semaphore
            _logger.LogInformation("Background Task: Processing Tasks... Ending");
        }