1+ package org .tron .core .jsonrpc ;
2+
3+ import java .lang .reflect .Method ;
4+ import java .util .BitSet ;
5+ import java .util .concurrent .ExecutorService ;
6+ import java .util .concurrent .Executors ;
7+ import javax .annotation .Resource ;
8+ import org .junit .Assert ;
9+ import org .junit .Before ;
10+ import org .junit .Test ;
11+ import org .tron .common .BaseTest ;
12+ import org .tron .core .Constant ;
13+ import org .tron .core .config .args .Args ;
14+ import org .tron .core .services .jsonrpc .TronJsonRpc .FilterRequest ;
15+ import org .tron .core .services .jsonrpc .filters .LogBlockQuery ;
16+ import org .tron .core .services .jsonrpc .filters .LogFilterWrapper ;
17+ import org .tron .core .store .SectionBloomStore ;
18+
19+ public class LogBlockQueryTest extends BaseTest {
20+
21+ @ Resource
22+ SectionBloomStore sectionBloomStore ;
23+ private ExecutorService sectionExecutor ;
24+ private Method partialMatchMethod ;
25+ private static final long CURRENT_MAX_BLOCK_NUM = 50000L ;
26+
27+ static {
28+ Args .setParam (new String [] {"--output-directory" , dbPath ()}, Constant .TEST_CONF );
29+ }
30+
31+ @ Before
32+ public void setup () throws Exception {
33+ sectionExecutor = Executors .newFixedThreadPool (5 );
34+
35+ // Get private method through reflection
36+ partialMatchMethod = LogBlockQuery .class .getDeclaredMethod ("partialMatch" ,
37+ int [][].class , int .class );
38+ partialMatchMethod .setAccessible (true );
39+
40+ BitSet bitSet = new BitSet (SectionBloomStore .BLOCK_PER_SECTION );
41+ bitSet .set (0 , SectionBloomStore .BLOCK_PER_SECTION );
42+ sectionBloomStore .put (0 , 1 , bitSet );
43+ sectionBloomStore .put (0 , 2 , bitSet );
44+ sectionBloomStore .put (0 , 3 , bitSet );
45+ BitSet bitSet2 = new BitSet (SectionBloomStore .BLOCK_PER_SECTION );
46+ bitSet2 .set (0 );
47+ sectionBloomStore .put (1 , 1 , bitSet2 );
48+ sectionBloomStore .put (1 , 2 , bitSet2 );
49+ sectionBloomStore .put (1 , 3 , bitSet2 );
50+ }
51+
52+ @ Test
53+ public void testPartialMatch () throws Exception {
54+ // Create a basic LogFilterWrapper
55+ LogFilterWrapper logFilterWrapper = new LogFilterWrapper (
56+ new FilterRequest ("0x0" , "0x1" , null , null , null ),
57+ CURRENT_MAX_BLOCK_NUM , null , false );
58+
59+ LogBlockQuery logBlockQuery = new LogBlockQuery (logFilterWrapper , sectionBloomStore ,
60+ CURRENT_MAX_BLOCK_NUM , sectionExecutor );
61+
62+ int section = 0 ;
63+
64+ // Create a hit condition
65+ int [][] bitIndexes = new int [][] {
66+ {1 , 2 , 3 }, // topic0
67+ {4 , 5 , 6 } // topic1
68+ };
69+
70+ // topic0 hit section 0
71+ BitSet result = (BitSet ) partialMatchMethod .invoke (logBlockQuery , bitIndexes , section );
72+ Assert .assertNotNull (result );
73+ Assert .assertEquals (SectionBloomStore .BLOCK_PER_SECTION , result .cardinality ());
74+
75+ // topic0 hit section 1
76+ result = (BitSet ) partialMatchMethod .invoke (logBlockQuery , bitIndexes , 1 );
77+ Assert .assertNotNull (result );
78+ Assert .assertEquals (1 , result .cardinality ());
79+
80+ // not exist section 2
81+ result = (BitSet ) partialMatchMethod .invoke (logBlockQuery , bitIndexes , 2 );
82+ Assert .assertNotNull (result );
83+ Assert .assertTrue (result .isEmpty ());
84+
85+ //not hit
86+ bitIndexes = new int [][] {
87+ {1 , 2 , 4 }, // topic0
88+ {3 , 5 , 6 } // topic1
89+ };
90+ result = (BitSet ) partialMatchMethod .invoke (logBlockQuery , bitIndexes , section );
91+ Assert .assertNotNull (result );
92+ Assert .assertTrue (result .isEmpty ());
93+
94+ // null condition
95+ bitIndexes = new int [0 ][];
96+ result = (BitSet ) partialMatchMethod .invoke (logBlockQuery , bitIndexes , section );
97+ Assert .assertNotNull (result );
98+ Assert .assertTrue (result .isEmpty ());
99+ }
100+ }
0 commit comments