multiple stream filters question

+1 vote
Hi guys,

If I have multiple stream filters activated on a stream which validate different item data, is there a fixed order in which the streams checks will be performed?

e.g. filter 1 checks:

var oItem = getfilterstreamitem();
    if (!oItem && !oItem.myData) {
        return 'no valid stream item';
    }

filter 2 wants to check deeper item properties e.g.

var oItem = getfilterstreamitem();
    if (!oItem.myData.myProperty) {
        return 'no valid stream item';
    }

filter 2 relies on filter 1. So I want filter 1 to be performed first and then filter2. Because if filter 2 is performed first and there is no oItem.myData it will crash with a JS error. To prevent this I have to copy the code from filter 1 also to filter2 which I don't want as that would mean that every filter has to have the same "precondition filter code"

Or do I have to deactivate filter 1 first and then copy filter 1 code to filter 2 and activate only one "big" filter?

Another question: what is with performance of many small filters vs one big filter? What is better?
asked Jan 22, 2020 by ironmanager

1 Answer

+1 vote

First, you don't have to worry about a filter crashing. If it encounters a Javascript error (as could happen if filter2 runs before filter1), then the item would not pass the filter, and you would see the Javascript error gives as the "reason".

Second, there is some performance cost in using multiple filters, mainly because of the getfilterstreamitem() callback having to be called and build its response each time. So it is worth considering combining filters together.

Finally, as for the order in which filters run, I'll check and revert.

answered Jan 22, 2020 by MultiChain
thanks!
Yes from a users point a meaningful error message would be nicer instead of a JS error which can mean that the filter code is wrong...
Filters are applied in the order they were created (create streamfilter transactions).
Since transaction order is finalized only in block, you cannot be sure which one is first until they are confirmed. And even then, this order may change in the unlikely event of block reorganization.

To improve significantly chances of filters being created in the right order, you may want to wait couple of blocks after filter1 confirmation.

To be 100% sure filter1 is created before filter2, you can use createrawtransaction mechanism and spend "change" of filter1 creation transaction for creating filter2
...