public void UpdateBlock(IPersistedBlock block)
        {
            try
            {
                if (!IsEnabled)
                {
                    return;
                }

                using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
                {
                    connection.Execute(
                        @"UPDATE Block SET Orphaned = @orphaned, Confirmed = @confirmed, Accounted = @accounted, Reward = @reward WHERE Height = @height",
                        new
                    {
                        orphaned  = block.Status == BlockStatus.Orphaned,
                        confirmed = block.Status == BlockStatus.Confirmed,
                        accounted = block.Accounted,
                        reward    = block.Reward,
                        height    = block.Height
                    });
                }
            }
            catch (Exception e)
            {
                _logger.Error("An exception occured while updating block; {0:l}", e.Message);
            }
        }
        public void UpdateBlock(IPersistedBlock block)
        {
            try
            {
                if (!IsEnabled)
                    return;

                using (var connection = new MySqlConnection(_mySqlProvider.ConnectionString))
                {
                    connection.Execute(
                        @"UPDATE Block SET Orphaned = @orphaned, Confirmed = @confirmed, Accounted = @accounted, Reward = @reward WHERE Height = @height",
                        new
                        {
                            orphaned = block.Status == BlockStatus.Orphaned,
                            confirmed = block.Status == BlockStatus.Confirmed,
                            accounted = block.Accounted,
                            reward = block.Reward,
                            height = block.Height
                        });
                }
            }
            catch (Exception e)
            {
                _logger.Error("An exception occured while updating block; {0:l}", e.Message);
            }
        }
Ejemplo n.º 3
0
 public Payment(IPersistedBlock block, int accountId, decimal amount)
 {
     BlockId   = (int)block.Height;
     AccountId = accountId;
     Amount    = amount;
     Completed = false;
 }
Ejemplo n.º 4
0
 public PaymentRound(IPersistedBlock block, IRewardsConfig rewardsConfig)
 {
     Block         = block;
     RewardsConfig = rewardsConfig;
     Payouts       = new Dictionary <string, decimal>();
     Shares        = new Dictionary <string, double>();
 }
Ejemplo n.º 5
0
 public Payment(IPersistedBlock block, int accountId, decimal amount)
 {
     BlockId = (int)block.Height;
     AccountId = accountId;
     Amount = amount;
     Completed = false;
 }
        public void MoveOrphanedShares(IPersistedBlock block)
        {
            try
            {
                if (!IsEnabled || !_redisProvider.IsConnected)
                {
                    return;
                }

                if (block.Status == BlockStatus.Confirmed || block.Status == BlockStatus.Pending)
                {
                    return;
                }

                var round   = string.Format("{0}:shares:round:{1}", _coin, block.Height); // rounds shares key.
                var current = string.Format("{0}:shares:round:current", _coin);           // current round key.

                //_client.StartPipeTransaction(); // batch the commands as atomic.

                // add shares to current round again.
                foreach (var entry in _redisProvider.Client.HGetAll(round))
                {
                    _redisProvider.Client.HIncrByFloat(current, entry.Key, double.Parse(entry.Value, CultureInfo.InvariantCulture));
                }

                _redisProvider.Client.Del(round); // delete the round shares.

                //_client.EndPipe(); // execute the batch commands.
            }
            catch (Exception e)
            {
                _logger.Error("An exception occurred while moving shares: {0:l}", e.Message);
            }
        }
        public Dictionary <string, double> GetShares(IPersistedBlock block)
        {
            var shares = new Dictionary <string, double>();

            try
            {
                if (!IsEnabled || !_redisProvider.IsConnected)
                {
                    return(shares);
                }

                var key    = string.Format("{0}:shares:round:{1}", _coin, block.Height);
                var hashes = _redisProvider.Client.HGetAll(key);

                foreach (var hash in hashes)
                {
                    shares.Add(hash.Key, double.Parse(hash.Value, CultureInfo.InvariantCulture));
                }
            }
            catch (Exception e)
            {
                _logger.Error("An exception occurred while getting shares for round; {0:l}", e.Message);
            }

            return(shares);
        }
Ejemplo n.º 8
0
        private void QueryBlock(ref IPersistedBlock block)
        {
            var blockInfo = _blockProcessor.GetBlock(block.BlockHash); // query the block.

            if (blockInfo == null || blockInfo.Confirmations == -1)    // make sure the block exists and is accepted.
            {
                block.Status = BlockStatus.Orphaned;
                return;
            }

            // calculate our expected generation transactions's hash
            var expectedTxHash = block.TransactionHash; // expected transaction hash
            var genTxHash      = blockInfo.Tx.First();  // read the hash of very first (generation transaction) of the block

            // make sure our calculated and reported generation tx hashes match.
            if (expectedTxHash != genTxHash)
            {
                block.Status = BlockStatus.Orphaned;
                return;
            }

            var genTx = _blockProcessor.GetGenerationTransaction(blockInfo); // get the generation transaction.

            // get the output transaction that targets pools central wallet.
            var poolOutput = _blockProcessor.GetPoolOutput(genTx);

            // make sure we have a valid reference to poolOutput
            if (poolOutput == null)
            {
                block.Status = BlockStatus.Orphaned;
                return;
            }

            block.SetReward((decimal)poolOutput.Amount); // set the reward of the block to miners.

            switch (poolOutput.Category)
            {
            case "immature":
                block.Status = BlockStatus.Pending;
                break;

            case "orphan":
                block.Status = BlockStatus.Orphaned;
                break;

            case "generate":
                block.Status = BlockStatus.Confirmed;
                break;
            }


            // TODO: add back these.
            // total amount of coins contained in the block.
            // candidate.Amount = transaction.Details.Sum(output => (decimal)output.Amount);
            // candidate.Reward = (decimal) poolOutput.Amount;
        }
Ejemplo n.º 9
0
        public PaymentRound(IPersistedBlock block, IStorageLayer storageLayer, IAccountManager accountManager)
        {
            Block           = block;
            _storageLayer   = storageLayer;
            _accountManager = accountManager;

            Payments = new List <IPayment>();
            _shares  = _storageLayer.GetShares(Block); // load the shares for the round.
            CalculatePayments();                       // calculate the per-user payments.
        }
Ejemplo n.º 10
0
        public PaymentRound(IPersistedBlock block, IStorageLayer storageLayer, IAccountManager accountManager)
        {
            Block = block;
            _storageLayer = storageLayer;
            _accountManager = accountManager;

            Payments = new List<IPayment>();
            _shares = _storageLayer.GetShares(Block); // load the shares for the round.
            CalculatePayments(); // calculate the per-user payments.
        }
Ejemplo n.º 11
0
        public IPaymentRound GetPaymentRound(IPersistedBlock block, IStorageLayer storageLayer, IAccountManager accountManager)
        {
            var @params = new NamedParameterOverloads
            {
                { "block", block },
                { "storageLayer", storageLayer },
                { "accountManager", accountManager }
            };

            return(_applicationContext.Container.Resolve <IPaymentRound>(@params));
        }
Ejemplo n.º 12
0
        private Block GetBlockInfo(IPersistedBlock block)
        {
            try
            {
                return(_daemonClient.GetBlock(block.BlockHash)); // query the block.
            }
            catch (RpcTimeoutException e)
            {
                _logger.Error(e, "RpcTimeoutException while getting block from daemon");
                // on rpc-timeout exception, let block stay in pending status so we can query it again later.
                block.Status = BlockStatus.Pending;
                return(null);
            }
            catch (RpcConnectionException e)
            {
                _logger.Error(e, "RpcConnectionException while getting block from daemon");
                // on rpc-connection exception, let block stay in pending status so we can query it again later.
                block.Status = BlockStatus.Pending;
                return(null);
            }
            catch (RpcErrorException e)
            {
                _logger.Error(e, "RpcErrorException while getting block from daemon");
                // block not found
                if (e.Code == (int)RpcErrorCode.RPC_INVALID_ADDRESS_OR_KEY)
                {
                    block.Status = BlockStatus.Orphaned; // orphan the block if block does not exist.
                }
                // if we got an error that we do not handle
                else
                {
                    block.Status = BlockStatus.Pending; // let the block in pending status so we can query it again later.
                    _logger.Error("Unhandled rpc-error: {0:l}", e.Message);
                }

                return(null);
            }
            catch (RpcException e)
            {
                block.Status = BlockStatus.Pending; // and let block stay in pending status so we can query it again later.
                _logger.Error("Unhandled rpc-exception: {0:l}", e.Message);
                return(null);
            }
        }
Ejemplo n.º 13
0
        private Transaction GetGenerationTx(IPersistedBlock block)
        {
            try
            {
                return(_daemonClient.GetTransaction(block.TransactionHash)); // query the transaction
            }
            catch (RpcTimeoutException e)
            {
                _logger.Error(e, "RpcTimeoutException while GetGenerationTx");
                // on rpc-timeout exception, let block stay in pending status so we can query it again later.
                block.Status = BlockStatus.Pending;
                return(null);
            }
            catch (RpcConnectionException e)
            {
                _logger.Error(e, "RpcConnectionException while GetGenerationTx");
                // on rpc-connection exception, let block stay in pending status so we can query it again later.
                block.Status = BlockStatus.Pending;
                return(null);
            }
            catch (RpcErrorException e)
            {
                _logger.Error(e, "RpcErrorException while GetGenerationTx");
                // Invalid or non-wallet transaction id
                if (e.Code == (int)RpcErrorCode.RPC_INVALID_ADDRESS_OR_KEY)
                {
                    block.Status = BlockStatus.Orphaned; // orphan the block if block does not exist.
                }
                // if we got an error that we do not handle
                else
                {
                    block.Status = BlockStatus.Pending; // let the block in pending status so we can query it again later.
                    _logger.Error("Unhandled rpc-error: {0:l}", e.Message);
                }

                return(null);
            }
            catch (RpcException e)
            {
                block.Status = BlockStatus.Pending; // and let block stay in pending status so we can query it again later.
                _logger.Error("Unhandled rpc-exception: {0:l}", e.Message);
                return(null);
            }
        }
Ejemplo n.º 14
0
 public void MoveOrphanedShares(IPersistedBlock block)
 {
     return;
 }
Ejemplo n.º 15
0
 public void MoveOrphanedShares(IPersistedBlock block)
 {
     // this function is not supported as this functionality is handled by mpos itself.
     throw new NotSupportedException();
 }
Ejemplo n.º 16
0
 public Dictionary <string, double> GetShares(IPersistedBlock block)
 {
     // this function is not supported as this functionality is only required by payment processors which mpos itself is already one so and handles itself.
     throw new NotSupportedException();
 }
Ejemplo n.º 17
0
        private void QueryBlock(IPersistedBlock block)
        {
            var blockInfo = GetBlockInfo(block); // query the block.

            if (blockInfo == null) // make sure we got a valid blockInfo response before continuing on 
                return; // in case we have a null, status will be already decided by GetBlockInfo() function.

            if (blockInfo.Confirmations == -1) // check if block is orphaned already.
            {
                block.Status = BlockStatus.Orphaned;
                return;
            }

            // calculate our expected generation transactions's hash
            var expectedTxHash = block.TransactionHash; // expected transaction hash
            var genTxHash = blockInfo.Tx.First(); // read the hash of very first (generation transaction) of the block

            // make sure our calculated and reported generation tx hashes match.
            if (expectedTxHash != genTxHash)
            {
                block.Status = BlockStatus.Orphaned;
                return;
            }

            // get the generation transaction.
            var genTx = GetGenerationTx(block);

            if (genTx == null) // make sure we were able to read the generation transaction
                return; // in case we have a null, status will be already decided by GetGenerationTx() function.

            // get the output transaction that targets pools central wallet.
            var poolOutput = genTx.GetPoolOutput(_poolConfig.Wallet.Adress, _poolAccount);

            // make sure we have a valid reference to poolOutput
            if (poolOutput == null)
            {
                block.Status = BlockStatus.Orphaned;
                return;
            }

            // set the reward of the block to miners.
            block.Reward = (decimal) poolOutput.Amount;

            // find the block status
            switch (poolOutput.Category)
            {
                case "immature":
                    block.Status = BlockStatus.Pending;
                    break;
                case "orphan":
                    block.Status = BlockStatus.Orphaned;
                    break;
                case "generate":
                    block.Status = BlockStatus.Confirmed;
                    break;
            }
        }
Ejemplo n.º 18
0
 public void UpdateBlock(IPersistedBlock block)
 {
     return;
 }
Ejemplo n.º 19
0
        private void QueryBlock(IPersistedBlock block)
        {
            var blockInfo = GetBlockInfo(block); // query the block.

            if (blockInfo == null)               // make sure we got a valid blockInfo response before continuing on
            {
                return;                          // in case we have a null, status will be already decided by GetBlockInfo() function.
            }
            if (blockInfo.Confirmations == -1)   // check if block is orphaned already.
            {
                block.Status = BlockStatus.Orphaned;
                return;
            }

            // calculate our expected generation transactions's hash
            var expectedTxHash = block.TransactionHash; // expected transaction hash
            var genTxHash      = blockInfo.Tx.First();  // read the hash of very first (generation transaction) of the block

            // make sure our calculated and reported generation tx hashes match.
            if (expectedTxHash != genTxHash)
            {
                block.Status = BlockStatus.Orphaned;
                return;
            }

            // get the generation transaction.
            var genTx = GetGenerationTx(block);

            if (genTx == null) // make sure we were able to read the generation transaction
            {
                return;        // in case we have a null, status will be already decided by GetGenerationTx() function.
            }
            // get the output transaction that targets pools central wallet.
            var poolOutput = genTx.GetPoolOutput(_poolConfig.Wallet.Address, _poolAccount);

            // make sure we have a valid reference to poolOutput
            if (poolOutput == null)
            {
                block.Status = BlockStatus.Orphaned;
                return;
            }

            // set the reward of the block to miners.
            block.Reward = (decimal)poolOutput.Amount;

            // find the block status
            switch (poolOutput.Category)
            {
            case "immature":
                block.Status = BlockStatus.Pending;
                break;

            case "orphan":
                block.Status = BlockStatus.Orphaned;
                break;

            case "generate":
                block.Status = BlockStatus.Confirmed;
                break;
            }
        }
Ejemplo n.º 20
0
 public void UpdateBlock(IPersistedBlock block)
 {
     // this function is not supported as this functionality is handled by mpos itself.
 }
        public void MoveOrphanedShares(IPersistedBlock block)
        {
            try
            {
                if (!IsEnabled || !_redisProvider.IsConnected)
                    return;

                if (block.Status == BlockStatus.Confirmed || block.Status == BlockStatus.Pending)
                    return;

                var round = string.Format("{0}:shares:round:{1}", _coin, block.Height); // rounds shares key.
                var current = string.Format("{0}:shares:round:current", _coin); // current round key.

                //_client.StartPipeTransaction(); // batch the commands as atomic.

                // add shares to current round again.
                foreach (var entry in _redisProvider.Client.HGetAll(round))
                {
                    _redisProvider.Client.HIncrByFloat(current, entry.Key, double.Parse(entry.Value, CultureInfo.InvariantCulture));
                }

                _redisProvider.Client.Del(round); // delete the round shares.

                //_client.EndPipe(); // execute the batch commands.
            }
            catch (Exception e)
            {
                _logger.Error("An exception occured while moving shares: {0:l}", e.Message);
            }
        }
Ejemplo n.º 22
0
 public void MoveOrphanedShares(IPersistedBlock block)
 {
     // this function is not supported as this functionality is handled by mpos itself.
     throw new NotSupportedException();
 }
Ejemplo n.º 23
0
 public Dictionary<string, double> GetShares(IPersistedBlock block)
 {
     // this function is not supported as this functionality is only required by payment processors which mpos itself is already one so and handles itself.
     throw new NotSupportedException();
 }
Ejemplo n.º 24
0
 public void UpdateBlock(IPersistedBlock block)
 {
     // this function is not supported as this functionality is handled by mpos itself.
 }
Ejemplo n.º 25
0
 public void MoveOrphanedShares(IPersistedBlock block)
 {
     return;
 }
Ejemplo n.º 26
0
 public Dictionary<string, double> GetShares(IPersistedBlock block)
 {
     return new Dictionary<string, double>();
 }
Ejemplo n.º 27
0
 public void UpdateBlock(IPersistedBlock block)
 {
     return;
 }
Ejemplo n.º 28
0
        public IPaymentRound GetPaymentRound(IPersistedBlock block, IStorageLayer storageLayer, IAccountManager accountManager)
        {
            var @params = new NamedParameterOverloads
            {
                {"block", block},
                {"storageLayer", storageLayer},
                {"accountManager", accountManager}
            };

            return _applicationContext.Container.Resolve<IPaymentRound>(@params);
        }
Ejemplo n.º 29
0
 public PaymentRound(IPersistedBlock block)
 {
     Block   = block;
     Payouts = new Dictionary <string, decimal>();
     Shares  = new Dictionary <string, double>();
 }
Ejemplo n.º 30
0
        private Transaction GetGenerationTx(IPersistedBlock block)
        {
            try
            {
                return _daemonClient.GetTransaction(block.TransactionHash); // query the transaction
            }
            catch (RpcTimeoutException)
            {
                // on rpc-timeout exception, let block stay in pending status so we can query it again later.
                block.Status = BlockStatus.Pending;
                return null;
            }
            catch (RpcConnectionException)
            {
                // on rpc-connection exception, let block stay in pending status so we can query it again later.
                block.Status = BlockStatus.Pending;
                return null;
            }
            catch (RpcErrorException e)
            {
                // Invalid or non-wallet transaction id
                if (e.Code == (int) RpcErrorCode.RPC_INVALID_ADDRESS_OR_KEY)
                    block.Status = BlockStatus.Orphaned; // orphan the block if block does not exist.

                // if we got an error that we do not handle
                else
                {                    
                    block.Status = BlockStatus.Pending; // let the block in pending status so we can query it again later.
                    _logger.Error("Unhandled rpc-error: {0:l}", e.Message);
                }

                return null;
            }
            catch (RpcException e)
            {
                block.Status = BlockStatus.Pending; // and let block stay in pending status so we can query it again later.
                _logger.Error("Unhandled rpc-exception: {0:l}", e.Message);
                return null;
            }
        }
        public Dictionary<string, double> GetShares(IPersistedBlock block)
        {
            var shares = new Dictionary<string, double>();

            try
            {
                if (!IsEnabled || !_redisProvider.IsConnected)
                    return shares;

                var key = string.Format("{0}:shares:round:{1}", _coin, block.Height);
                var hashes = _redisProvider.Client.HGetAll(key);

                foreach (var hash in hashes)
                {
                    shares.Add(hash.Key, double.Parse(hash.Value, CultureInfo.InvariantCulture));
                }
            }
            catch (Exception e)
            {
                _logger.Error("An exception occured while getting shares for round; {0:l}", e.Message);
            }

            return shares;
        }
Ejemplo n.º 32
0
 public Dictionary <string, double> GetShares(IPersistedBlock block)
 {
     return(new Dictionary <string, double>());
 }