简体   繁体   English

从 async.waterfall 调用外部 function

[英]Calling external function from async.waterfall

I'm somewhat new to JS and working with an existing forked library (which is very complicated and poorly documented) - so please bear with me.我对 JS 有点陌生,并且正在使用现有的分叉库(非常复杂且记录不充分) - 所以请多多包涵。

I'm working on a mining pool system, and specifically adding a feature where users can set a custom payout amount instead of the lower pool limit.我正在开发一个矿池系统,特别是添加一个功能,用户可以设置自定义支付金额而不是矿池下限。 I've already sorted writing that value for a users worker address into redis.我已经将用户工作地址的值写入 redis。

What I'm trying to do now is add logic that will:我现在要做的是添加逻辑:

  1. Use the worker Address that did work to see if they set a custom payout value (my external function below)使用有效的工作人员地址查看他们是否设置了自定义支付值(下面是我的外部 function)
  2. If it's set, some logic to determine if they should be paid immediately or add it to their balance如果已设置,则需要一些逻辑来确定是否应立即付款或将其添加到余额中

The existing waterfall script is here: https://github.com/mardock2009/Ravencoin-Pool/blob/master/libs/paymentProcessor.js#L575现有的瀑布脚本在这里: https://github.com/mardock2009/Ravencoin-Pool/blob/master/libs/paymentProcessor.js#L575

Before the waterfall, i'm adding a function similar to this (i've tried many iterations):在瀑布之前,我添加了一个类似于此的 function(我尝试了很多迭代):

getCustomPayoutAmount = function(worker, cback) {
    var logger = loggerFactory.getLogger('PaymentProcessing', 'getCustomPayoutAmount');

    var redisClient = redis.createClient(portalConfig.redis.port, portalConfig.redis.host);             
    logger.debug('Getting custom Payout for worker: %s', worker);
    var payoutAmount = new BigNumber(0);

    redisClient.hget('ravencoin:workers:customPayoutAmount', worker,  function(error, result) {
        if (error) {
            logger.error('Error getCustomPayoutAmount: %s', error);
            payoutAmount = 0;
        }
        logger.debug('Got custom payout amount for worker: %s, Payout Amount: %s', worker, result);
        if (result > 10) {
            payoutAmount = new BigNumber(result);
            logger.debug('Parsed Float Amount: %s', payoutAmount);
        } else {
            logger.debug('Else lower than: %s', payoutAmount);
            payoutAmount = new BigNumber(0);
        }       
    });
    cback( new BigNumber(payoutAmount));    
};

Then at around line 575, i'm calling it (maybe?):然后在第 575 行左右,我称之为(也许?):

                    var customPayoutAmount = new BigNumber(0);

                    getCustomPayoutAmount(worker, function(returnCustomPayoutAmount) {
                        logger.debug('Callback customPayoutAmount = %s', returnCustomPayoutAmount);
                        customPayoutAmount = returnCustomPayoutAmount;
                    });

                    logger.debug('PP> customPayoutAmount = %s', customPayoutAmount);

And then finally, some if/elseif logic to handle the different cases:最后,一些 if/elseif 逻辑来处理不同的情况:

if (toSend.isGreaterThanOrEqualTo(minPayment)) {

                        if (toSend.isGreaterThanOrEqualTo(customPayoutAmount) && !customPayoutAmount.isZero()) {
                            //Amount Sent is higher than the custom amount set for this worker. Pay it out.
                            logger.debug('PP> Worker %s have their custom minimum payout amount: (%s above minimum %s)', w, toSend.toString(10), customPayoutAmount.toString(10));
                            totalSent = totalSent.plus(toSend);              
                            logger.debug('PP> totalSent = %s', totalSent.toString(10));
                            var address = worker.address = (worker.address || getProperAddress(w));              
                            logger.debug('PP> address = %s', address);
                            worker.sent = addressAmounts[address] = toSend;
                            logger.debug('PP> worker.sent = %s', worker.sent.toString(10));
                            worker.balanceChange = BigNumber.min(worker.balance, worker.sent).multipliedBy(new BigNumber(-1));
                            logger.debug('PP> worker.balanceChange = %s', worker.balanceChange.toString(10));

                        } else if (toSend.isLessThan(customPayoutAmount) && !customPayoutAmount.isZero()){
                            //Amount is higher than the minimum payment but not higher than the custom amount set for this worker. Add it to their balance.
                            //Did not meet the pool minimum, no custom amount. Add to balance.
                            logger.debug('PP> Worker %s have not reached minimum payout from their custom set payout amount threshold %s', w, customPayoutAmount.toString(10));
                            worker.balanceChange = BigNumber.max(toSend.minus(worker.balance), new BigNumber(0));
                            logger.debug('PP> worker.balanceChange = %s', worker.balanceChange.toString(10));
                            worker.sent = new BigNumber(0);
                            logger.debug('PP> worker.sent = %s', worker.sent.toString(10));
                            if (worker.balanceChange > 0) {
                                if (balanceAmounts[address] != null && balanceAmounts[address].isGreaterThan(0)) {
                                    balanceAmounts[address] = balanceAmounts[address].plus(worker.balanceChange);
                                } else {
                                    balanceAmounts[address] = worker.balanceChange;
                                }
                            }
                        }
                        
                        if (toSend.isGreaterThanOrEqualTo(minPayment) && customPayoutAmount.isZero()) {
                            //Meets the pool minimum payment, no custom amount. Pay out based on the pool minimum payment.
                            logger.debug('PP> Worker %s have reached minimum payout threshold (%s above minimum %s)', w, toSend.toString(10), minPayment.toString(10));
                            totalSent = totalSent.plus(toSend);              
                            logger.debug('PP> totalSent = %s', totalSent.toString(10));
                            var address = worker.address = (worker.address || getProperAddress(w));              
                            logger.debug('PP> address = %s', address);
                            worker.sent = addressAmounts[address] = toSend;
                            logger.debug('PP> worker.sent = %s', worker.sent.toString(10));
                            worker.balanceChange = BigNumber.min(worker.balance, worker.sent).multipliedBy(new BigNumber(-1));
                            logger.debug('PP> worker.balanceChange = %s', worker.balanceChange.toString(10));
                        }

                        
                    } else {
                        //Did not meet the pool minimum, no custom amount. Add to balance.
                        logger.debug('PP> Worker %s have not reached minimum payout threshold %s', w, minPayment.toString(10));
                        worker.balanceChange = BigNumber.max(toSend.minus(worker.balance), new BigNumber(0));
                        logger.debug('PP> worker.balanceChange = %s', worker.balanceChange.toString(10));
                        worker.sent = new BigNumber(0);
                        logger.debug('PP> worker.sent = %s', worker.sent.toString(10));
                        if (worker.balanceChange > 0) {
                            if (balanceAmounts[address] != null && balanceAmounts[address].isGreaterThan(0)) {
                                balanceAmounts[address] = balanceAmounts[address].plus(worker.balanceChange);
                            } else {
                                balanceAmounts[address] = worker.balanceChange;
                            }
                        }
                    }

The main issue I'm having is I cant get values back from redis calling it inside the waterfall.我遇到的主要问题是我无法从 redis 在瀑布内调用它来获取值。 I'm assuming because it's async andIi'm not writing async code - which also causes it to not necessarily run in order when I need the values.我假设是因为它是异步的并且我没有编写异步代码 - 这也导致它在我需要值时不一定按顺序运行。

Again, I know this is a mess and I'm probably going about this all wrong, so hopefully someone has some insight for this poor noob.再说一次,我知道这是一团糟,我可能把这一切都弄错了,所以希望有人对这个可怜的菜鸟有所了解。

You can use async.each()您可以使用async.each()

 function(workers, rounds, addressAccount, callback) { var trySend = function(withholdPercent) {... async.each(workers, function(worker, callback) {... getCustomPayoutAmount(worker, function(customPayoutAmount) {... if (toSend.isGreaterThanOrEqualTo(minPayment)) { if (toSend.isGreaterThanOrEqualTo(customPayoutAmount) &&.customPayoutAmount.isZero()) {... }..; } callback(); }). }).then(function() { if (Object.keys(addressAmounts).length === 0) { logger;info('PP> No workers was chosen for paying out'), callback(null, workers, rounds; []); return. }... daemon,cmd('sendmany', [addressAccount || '', addressAmounts, 1, ""]. function(result) {..; } }); }; trySend(new BigNumber(0); }

and In 'getCustomPayoutAmount', 'cback()` should be called within the callback of 'redisClient.hget()' like below并且在“getCustomPayoutAmount”中,应该在“redisClient.hget()”的回调中调用“cback()”,如下所示

 getCustomPayoutAmount = function(worker, cback) {... redisClient.hget('ravencoin:workers:customPayoutAmount', worker, function(error, result) {... cback(payoutAmount); }); };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM