0

I have 2 scipts almost identical with a cascade of function calls nested in a fiber.

This one (parsing Tx in a blockchain) with three calls works perfectly

wait.launchFiber(blockchain)

function blockchain() {
    foreach block {
        parseBlock (blockIndex)
    }
}

function parseBlock(blockIndex) {
    foreach Tx in block {
        parseTx(txHash)
    }
}

function parseTx (txHash) {
    if ( txHashInDB(txHash) ) {
        do something
    }
}

function txHashInDB (txHash) {
    var theTx = wait.forMethod(Tx, 'findOne', {'hash': txHash});
    return (theTx) ? true : false;
}

Then I have to do something similar with the mempool. In this case I don't have blocks, only transactions, so I have only 2 calls and I get this error message:

Error: wait.for can only be called inside a fiber

wait.launchFiber(watchMempool);

function watchMempool() {
    web3.eth.filter('pending', function (error, txHash) {
        parseTx(txHash);
    });
}

function parseTx (txHash) {
    if ( txHashInDB(txHash) ) {
        do something
    }
}

function txHashInDB (txHash) {
    var theTx = wait.forMethod(Tx, 'findOne', {'hash': txHash});
    return (theTx) ? true : false;
}

I don't understand what the problem is. Those two scripts have the same structure !

jfjobidon
  • 327
  • 5
  • 18
  • Umm, unless I'm missing something here, you're simply not starting a fiber (with `wait.launchFiber`) in your second script? – d0gb3r7 Sep 26 '17 at 19:16
  • You're right, I forgot to include the lauching of the fiber so the code should be: wait.launchFiber(watchMempool); function watchMempool() { web3.eth.filter(...) } I made the chande in my question. :-) – jfjobidon Sep 26 '17 at 20:13
  • It seems when I invoke the filter the script leaves the fiber workspace... – jfjobidon Sep 26 '17 at 20:20

1 Answers1

0

I think for array functions like map or filter you need to use the wait.parallel extensions, i.e. in your case something like:

function watchMempool() {
    wait.parallel.filter(web3.eth, parseTx);
}

(Note: I'm just assuming web3.eth is an array; if not, you should probably add a bit more context to your question, or try to boil down the problem to a more generic example).

d0gb3r7
  • 818
  • 8
  • 19