1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
crate::ix!();

/**
  | Get the human-readable name for a filter
  | type. Returns empty string for unknown
  | types.
  |
  */
pub fn block_filter_type_name<'a>(filter_type: &BlockFilterType) -> &'a str {
    
    lazy_static!{
        static ref UNKNOWN_RETVAL: String = "".to_string();
    }

    match G_FILTER_TYPES.get(filter_type) {
        Some(val) => val,
        None      => &UNKNOWN_RETVAL
    }
}

/**
  | Find a filter type by its human-readable
  | name.
  |
  */
pub fn block_filter_type_by_name(
        name:        &String,
        filter_type: &mut BlockFilterType) -> bool {

    for entry in G_FILTER_TYPES.iter() {

        if entry.1 == name {
            *filter_type = entry.0.clone();
            return true;
        }
    }

    false
}

/**
  | Get a list of known filter types.
  |
  */
pub fn all_block_filter_types<'a>() -> &'a HashSet<BlockFilterType> {

    lazy_static!{
        static ref TYPES: HashSet::<BlockFilterType> = {

            let mut x = HashSet::<BlockFilterType>::default();

            for entry in G_FILTER_TYPES.iter() {
                x.insert(entry.0.clone());
            }

            x
        };
    }
    
    &TYPES
}

/**
  | Get a comma-separated list of known
  | filter type names.
  |
  */
pub fn list_block_filter_types<'a>() -> &'a str {

    lazy_static!{
        static ref TYPE_LIST: String = {

            let mut x = String::default();

            let mut first: bool = true;

            for entry in G_FILTER_TYPES.iter() {

                if !first {
                    x.push_str(", ");
                }

                x.push_str(entry.1);

                first = false;
            }

            x
        };
    }

    &TYPE_LIST
}

pub fn basic_filter_elements(
        block:      &Block,
        block_undo: &BlockUndo) -> gcs_filter::ElementSet {
    
    let mut elements = gcs_filter::ElementSet::default();

    for tx in block.vtx.iter() {

        for txout in (*tx).get().vout.iter() {

            let script: &Script = &txout.script_pub_key;

            if script.is_empty() || script[0] == ScriptError::OP_RETURN as u8 {
                continue;
            }

            elements.insert(script.base.to_vec()); //elements.emplace(script.begin(), script.end());
        }
    }

    for tx_undo in block_undo.vtxundo.iter() {

        for prevout in tx_undo.vprevout.iter() {

            let script: &Script = &prevout.out.script_pub_key;

            if script.is_empty() {
                continue;
            }

            elements.insert(script.base.to_vec()); //elements.emplace(script.begin(), script.end());
        }
    }

    elements
}