@@ -2,6 +2,8 @@ use crate::ckb::config::{UdtArgInfo, UdtCellDep, UdtCfgInfos, UdtDep, UdtScript}
22use crate :: fiber:: gen:: fiber:: UdtCfgInfos as MoleculeUdtCfgInfos ;
33use ckb_jsonrpc_types:: OutPoint ;
44use ckb_types:: core:: { DepType , ScriptHashType } ;
5+ use ckb_types:: packed:: Script ;
6+ use ckb_types:: prelude:: { Builder , Pack } ;
57use ckb_types:: H256 ;
68use molecule:: prelude:: Entity ;
79
@@ -30,3 +32,185 @@ fn test_udt_whitelist() {
3032 UdtCfgInfos :: from ( MoleculeUdtCfgInfos :: from_slice ( & serialized) . expect ( "invalid mol" ) ) ;
3133 assert_eq ! ( udt_whitelist, deserialized) ;
3234}
35+
36+ #[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test:: wasm_bindgen_test) ]
37+ #[ cfg_attr( not( target_arch = "wasm32" ) , test) ]
38+ fn test_find_matching_udt_exact_match ( ) {
39+ let code_hash = H256 :: from ( [ 1u8 ; 32 ] ) ;
40+ let args = "0x1234" . to_string ( ) ;
41+ let udt_whitelist = UdtCfgInfos ( vec ! [ UdtArgInfo {
42+ name: "TestUDT" . to_string( ) ,
43+ script: UdtScript {
44+ code_hash: code_hash. clone( ) ,
45+ hash_type: ScriptHashType :: Data ,
46+ args: args. clone( ) ,
47+ } ,
48+ auto_accept_amount: Some ( 100 ) ,
49+ cell_deps: vec![ ] ,
50+ } ] ) ;
51+
52+ let script = Script :: new_builder ( )
53+ . code_hash ( code_hash. pack ( ) )
54+ . hash_type ( ScriptHashType :: Data . into ( ) )
55+ . args ( args. as_bytes ( ) . pack ( ) )
56+ . build ( ) ;
57+
58+ let found = udt_whitelist. find_matching_udt ( & script) ;
59+ assert ! ( found. is_some( ) ) ;
60+ assert_eq ! ( found. unwrap( ) . name, "TestUDT" ) ;
61+ }
62+
63+ #[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test:: wasm_bindgen_test) ]
64+ #[ cfg_attr( not( target_arch = "wasm32" ) , test) ]
65+ fn test_find_matching_udt_regex_pattern ( ) {
66+ let code_hash = H256 :: from ( [ 2u8 ; 32 ] ) ;
67+ let udt_whitelist = UdtCfgInfos ( vec ! [ UdtArgInfo {
68+ name: "RegexUDT" . to_string( ) ,
69+ script: UdtScript {
70+ code_hash: code_hash. clone( ) ,
71+ hash_type: ScriptHashType :: Data ,
72+ args: "0x[0-9a-f]{4}" . to_string( ) , // Regex pattern matching 4 hex digits
73+ } ,
74+ auto_accept_amount: Some ( 200 ) ,
75+ cell_deps: vec![ ] ,
76+ } ] ) ;
77+
78+ // Test with matching args
79+ let script = Script :: new_builder ( )
80+ . code_hash ( code_hash. pack ( ) )
81+ . hash_type ( ScriptHashType :: Data . into ( ) )
82+ . args ( "0xabcd" . as_bytes ( ) . pack ( ) )
83+ . build ( ) ;
84+
85+ let found = udt_whitelist. find_matching_udt ( & script) ;
86+ assert ! ( found. is_some( ) ) ;
87+ assert_eq ! ( found. unwrap( ) . name, "RegexUDT" ) ;
88+
89+ // Test with non-matching args
90+ let script_no_match = Script :: new_builder ( )
91+ . code_hash ( code_hash. pack ( ) )
92+ . hash_type ( ScriptHashType :: Data . into ( ) )
93+ . args ( "0xabc" . as_bytes ( ) . pack ( ) ) // Only 3 hex digits, doesn't match pattern
94+ . build ( ) ;
95+
96+ let found = udt_whitelist. find_matching_udt ( & script_no_match) ;
97+ assert ! ( found. is_none( ) ) ;
98+ }
99+
100+ #[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test:: wasm_bindgen_test) ]
101+ #[ cfg_attr( not( target_arch = "wasm32" ) , test) ]
102+ fn test_find_matching_udt_wrong_code_hash ( ) {
103+ let code_hash1 = H256 :: from ( [ 3u8 ; 32 ] ) ;
104+ let code_hash2 = H256 :: from ( [ 4u8 ; 32 ] ) ;
105+ let udt_whitelist = UdtCfgInfos ( vec ! [ UdtArgInfo {
106+ name: "TestUDT" . to_string( ) ,
107+ script: UdtScript {
108+ code_hash: code_hash1. clone( ) ,
109+ hash_type: ScriptHashType :: Data ,
110+ args: "0x00" . to_string( ) ,
111+ } ,
112+ auto_accept_amount: Some ( 100 ) ,
113+ cell_deps: vec![ ] ,
114+ } ] ) ;
115+
116+ let script = Script :: new_builder ( )
117+ . code_hash ( code_hash2. pack ( ) ) // Different code_hash
118+ . hash_type ( ScriptHashType :: Data . into ( ) )
119+ . args ( "0x00" . as_bytes ( ) . pack ( ) )
120+ . build ( ) ;
121+
122+ let found = udt_whitelist. find_matching_udt ( & script) ;
123+ assert ! ( found. is_none( ) ) ;
124+ }
125+
126+ #[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test:: wasm_bindgen_test) ]
127+ #[ cfg_attr( not( target_arch = "wasm32" ) , test) ]
128+ fn test_find_matching_udt_wrong_hash_type ( ) {
129+ let code_hash = H256 :: from ( [ 5u8 ; 32 ] ) ;
130+ let udt_whitelist = UdtCfgInfos ( vec ! [ UdtArgInfo {
131+ name: "TestUDT" . to_string( ) ,
132+ script: UdtScript {
133+ code_hash: code_hash. clone( ) ,
134+ hash_type: ScriptHashType :: Data ,
135+ args: "0x00" . to_string( ) ,
136+ } ,
137+ auto_accept_amount: Some ( 100 ) ,
138+ cell_deps: vec![ ] ,
139+ } ] ) ;
140+
141+ let script = Script :: new_builder ( )
142+ . code_hash ( code_hash. pack ( ) )
143+ . hash_type ( ScriptHashType :: Type . into ( ) ) // Different hash_type
144+ . args ( "0x00" . as_bytes ( ) . pack ( ) )
145+ . build ( ) ;
146+
147+ let found = udt_whitelist. find_matching_udt ( & script) ;
148+ assert ! ( found. is_none( ) ) ;
149+ }
150+
151+ #[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test:: wasm_bindgen_test) ]
152+ #[ cfg_attr( not( target_arch = "wasm32" ) , test) ]
153+ fn test_find_matching_udt_multiple_udts ( ) {
154+ let code_hash1 = H256 :: from ( [ 6u8 ; 32 ] ) ;
155+ let code_hash2 = H256 :: from ( [ 7u8 ; 32 ] ) ;
156+ let udt_whitelist = UdtCfgInfos ( vec ! [
157+ UdtArgInfo {
158+ name: "FirstUDT" . to_string( ) ,
159+ script: UdtScript {
160+ code_hash: code_hash1. clone( ) ,
161+ hash_type: ScriptHashType :: Data ,
162+ args: "0x00" . to_string( ) ,
163+ } ,
164+ auto_accept_amount: Some ( 100 ) ,
165+ cell_deps: vec![ ] ,
166+ } ,
167+ UdtArgInfo {
168+ name: "SecondUDT" . to_string( ) ,
169+ script: UdtScript {
170+ code_hash: code_hash2. clone( ) ,
171+ hash_type: ScriptHashType :: Data ,
172+ args: "0x01" . to_string( ) ,
173+ } ,
174+ auto_accept_amount: Some ( 200 ) ,
175+ cell_deps: vec![ ] ,
176+ } ,
177+ ] ) ;
178+
179+ // Test finding first UDT
180+ let script1 = Script :: new_builder ( )
181+ . code_hash ( code_hash1. pack ( ) )
182+ . hash_type ( ScriptHashType :: Data . into ( ) )
183+ . args ( "0x00" . as_bytes ( ) . pack ( ) )
184+ . build ( ) ;
185+
186+ let found = udt_whitelist. find_matching_udt ( & script1) ;
187+ assert ! ( found. is_some( ) ) ;
188+ assert_eq ! ( found. unwrap( ) . name, "FirstUDT" ) ;
189+
190+ // Test finding second UDT
191+ let script2 = Script :: new_builder ( )
192+ . code_hash ( code_hash2. pack ( ) )
193+ . hash_type ( ScriptHashType :: Data . into ( ) )
194+ . args ( "0x01" . as_bytes ( ) . pack ( ) )
195+ . build ( ) ;
196+
197+ let found = udt_whitelist. find_matching_udt ( & script2) ;
198+ assert ! ( found. is_some( ) ) ;
199+ assert_eq ! ( found. unwrap( ) . name, "SecondUDT" ) ;
200+ }
201+
202+ #[ cfg_attr( target_arch = "wasm32" , wasm_bindgen_test:: wasm_bindgen_test) ]
203+ #[ cfg_attr( not( target_arch = "wasm32" ) , test) ]
204+ fn test_find_matching_udt_empty_whitelist ( ) {
205+ let udt_whitelist = UdtCfgInfos ( vec ! [ ] ) ;
206+ let code_hash = H256 :: from ( [ 8u8 ; 32 ] ) ;
207+
208+ let script = Script :: new_builder ( )
209+ . code_hash ( code_hash. pack ( ) )
210+ . hash_type ( ScriptHashType :: Data . into ( ) )
211+ . args ( "0x00" . as_bytes ( ) . pack ( ) )
212+ . build ( ) ;
213+
214+ let found = udt_whitelist. find_matching_udt ( & script) ;
215+ assert ! ( found. is_none( ) ) ;
216+ }
0 commit comments