#14121 implemented blockfilterindex
in a way that it is extensible to different filter types, however there is only one single filter type so far, which is the default one, and combinations of these filter types aren’t possible. In this proposal I describe how it is possible to add other filter types in a way that it is (1) backwards compatible and (2) all possible combinations of different filter types are possible.
Having Different Filters In Parallel
As far as I understand the code it is already prepared and adding a new filter type does not require refactoring to achieve this. So one would be able to define
0blockfilterindex=basic
1blockfilterindex=p2wpkh
and the software will create both of those filters in parallel.
Combinations of Filters
blockfilterindex=basic
’s value maps to uint8_t
values: 0 and 1. We can also represent uint8_t
with a byte array that holds 8 octets. In case of basic
it would be 00000000
(0) or 00000001
(1).
The idea is to use the bytes to denote the next filter types. For example we can assign 00000010
(2) to p2wpkh
and 00000100
(4) to p2wsh
. Further as the need arises we can use a larger type uint32_t
or something like that to be able to define larger number of types.
But why not just go 1,2,3,4,5...
instead? Because this would enable us to also specify filter combinations. For example if I specify 6 (00000110
) then I see that the p2wpkh
and p2wsh
octets are both flipped to true, so the software would add both to the filter:
0// If blockfilterindex is turned on.
1foreach (var script in scripts) {
2
3 if (no higher octets are flipped to true other than the basic filter type's) {
4 filter.Add(script);
5 continue;
6 }
7 // However for backwards compatibility if a higher octet is true then disregard the basic filter type's octet since currently both 0 and 1 are defined to be the basic octet type. This will result two numbers evaluating to the same combination (Alternatively config parsing can throw an error or whatever to disable one of the number.)
8
9 if (p2wpkh octet is true AND script is p2wpkh) {
10 filter.Add(script);
11 continue;
12 }
13
14 if (p2wsh octet is true AND script is p2wsh) {
15 filter.Add(script);
16 continue;
17 }
18
19 // Add other types
20}
Not In a Hurry
Luckily whoever would add the next filter type wouldn’t need to consider this proposal at all, because the next filter type would be 2
anyway, which lets us delay the decision until the 3rd filter type is added, of which according to this proposal should hold the value of 4
instead of 3
in order to facilitate the combinations of filter types.