1
- // Handles test suite
1
+ // Handles Basic test suite
2
2
import '@frequency-chain/api-augment' ;
3
3
import assert from 'assert' ;
4
- import { createMsa , DOLLARS , getTestHandle } from '../scaffolding/helpers' ;
4
+ import { CENTS , createMsa , DOLLARS , getTestHandle } from '../scaffolding/helpers' ;
5
5
import { KeyringPair } from '@polkadot/keyring/types' ;
6
6
import { MessageSourceId } from '@frequency-chain/api-augment/interfaces' ;
7
7
import { ExtrinsicHelper } from '../scaffolding/extrinsicHelpers' ;
@@ -13,199 +13,54 @@ import { getFundingSource } from '../scaffolding/funding';
13
13
const fundingSource = getFundingSource ( import . meta. url ) ;
14
14
const expirationOffset = hasRelayChain ( ) ? 4 : 100 ;
15
15
16
- describe ( '🤝 Handles' , function ( ) {
17
- describe ( 'Claim and Retire' , function ( ) {
18
- let msa_id : MessageSourceId ;
19
- let msaOwnerKeys : KeyringPair ;
16
+ describe ( 'Handles: Claim and Retire' , function ( ) {
17
+ let msaId : MessageSourceId ;
18
+ let msaOwnerKeys : KeyringPair ;
20
19
21
- before ( async function ( ) {
22
- // Create a MSA for the delegator
23
- [ msa_id , msaOwnerKeys ] = await createMsa ( fundingSource ) ;
24
- assert . notEqual ( msaOwnerKeys , undefined , 'setup should populate delegator_key' ) ;
25
- assert . notEqual ( msa_id , undefined , 'setup should populate msa_id' ) ;
26
- } ) ;
27
-
28
- it ( 'should be able to claim a handle' , async function ( ) {
29
- const handle = getTestHandle ( ) ;
30
- const currentBlock = await getBlockNumber ( ) ;
31
- const handle_vec = new Bytes ( ExtrinsicHelper . api . registry , handle ) ;
32
- const payload = {
33
- baseHandle : handle_vec ,
34
- expiration : currentBlock + expirationOffset ,
35
- } ;
36
- const claimHandlePayload = ExtrinsicHelper . api . registry . createType (
37
- 'CommonPrimitivesHandlesClaimHandlePayload' ,
38
- payload
39
- ) ;
40
- const claimHandle = ExtrinsicHelper . claimHandle ( msaOwnerKeys , claimHandlePayload ) ;
41
- const { target : event } = await claimHandle . fundAndSend ( fundingSource ) ;
42
- assert . notEqual ( event , undefined , 'claimHandle should return an event' ) ;
43
- assert . notEqual ( event ! . data . handle . toString ( ) , '' , 'claimHandle should emit a handle' ) ;
44
- } ) ;
45
-
46
- it ( 'should be able to retire a handle' , async function ( ) {
47
- const handle_response = await ExtrinsicHelper . getHandleForMSA ( msa_id ) ;
48
- if ( ! handle_response . isSome ) {
49
- throw new Error ( 'handle_response should be Some' ) ;
50
- }
51
- const full_handle_state = handle_response . unwrap ( ) ;
52
- const suffix_from_state = full_handle_state . suffix ;
53
- const suffix = suffix_from_state . toNumber ( ) ;
54
- assert . notEqual ( suffix , 0 , 'suffix should not be 0' ) ;
55
- assert . notEqual ( full_handle_state . canonical_base , undefined , 'canonical_base should not be undefined' ) ;
56
- assert . notEqual ( full_handle_state . base_handle , undefined , 'base_handle should not be undefined' ) ;
57
- const currentBlock = await getBlockNumber ( ) ;
58
- await ExtrinsicHelper . runToBlock ( currentBlock + expirationOffset + 1 ) ; // Must be at least 1 > original expiration
59
-
60
- const retireHandle = ExtrinsicHelper . retireHandle ( msaOwnerKeys ) ;
61
- const { target : event } = await retireHandle . signAndSend ( ) ;
62
- assert . notEqual ( event , undefined , 'retireHandle should return an event' ) ;
63
- const handle = event ! . data . handle . toString ( ) ;
64
- assert . notEqual ( handle , '' , 'retireHandle should return the correct handle' ) ;
65
- } ) ;
66
- } ) ;
67
-
68
- describe ( 'Claim and Retire Alt' , function ( ) {
69
- let msa_id : MessageSourceId ;
70
- let msaOwnerKeys : KeyringPair ;
71
-
72
- before ( async function ( ) {
73
- // Create a MSA for the delegator
74
- [ msa_id , msaOwnerKeys ] = await createMsa ( fundingSource ) ;
75
- assert . notEqual ( msaOwnerKeys , undefined , 'setup should populate delegator_key' ) ;
76
- assert . notEqual ( msa_id , undefined , 'setup should populate msa_id' ) ;
77
- } ) ;
78
-
79
- describe ( 'Claim Handle with possible presumptive suffix/RPC test' , function ( ) {
80
- it ( 'should be able to claim a handle and check suffix (=suffix_assumed if available on chain)' , async function ( ) {
81
- const handle = getTestHandle ( ) ;
82
- const handle_bytes = new Bytes ( ExtrinsicHelper . api . registry , handle ) ;
83
- /// Get presumptive suffix from chain (rpc)
84
- const suffixes_response = await ExtrinsicHelper . getNextSuffixesForHandle ( handle , 10 ) ;
85
- const resp_base_handle = suffixes_response . base_handle . toString ( ) ;
86
- assert . equal ( resp_base_handle , handle , 'resp_base_handle should be equal to handle' ) ;
87
- const suffix_assumed = suffixes_response . suffixes [ 0 ] ;
88
- assert . notEqual ( suffix_assumed , 0 , 'suffix_assumed should not be 0' ) ;
89
-
90
- const currentBlock = await getBlockNumber ( ) ;
91
- /// Claim handle (extrinsic)
92
- const payload_ext = {
93
- baseHandle : handle_bytes ,
94
- expiration : currentBlock + expirationOffset ,
95
- } ;
96
- const claimHandlePayload = ExtrinsicHelper . api . registry . createType (
97
- 'CommonPrimitivesHandlesClaimHandlePayload' ,
98
- payload_ext
99
- ) ;
100
- const claimHandle = ExtrinsicHelper . claimHandle ( msaOwnerKeys , claimHandlePayload ) ;
101
- const { target : event } = await claimHandle . fundAndSend ( fundingSource ) ;
102
- assert . notEqual ( event , undefined , 'claimHandle should return an event' ) ;
103
- const displayHandle = event ! . data . handle . toUtf8 ( ) ;
104
- assert . notEqual ( displayHandle , '' , 'claimHandle should emit a handle' ) ;
105
-
106
- // get handle using msa (rpc)
107
- const handle_response = await ExtrinsicHelper . getHandleForMSA ( msa_id ) ;
108
- if ( ! handle_response . isSome ) {
109
- throw new Error ( 'handle_response should be Some' ) ;
110
- }
111
- const full_handle_state = handle_response . unwrap ( ) ;
112
- const suffix_from_state = full_handle_state . suffix ;
113
- const suffix = suffix_from_state . toNumber ( ) ;
114
- assert . notEqual ( suffix , 0 , 'suffix should not be 0' ) ;
115
- assert . equal ( suffix , suffix_assumed , 'suffix should be equal to suffix_assumed' ) ;
116
-
117
- /// Get MSA from full display handle (rpc)
118
- const msaOption = await ExtrinsicHelper . getMsaForHandle ( displayHandle ) ;
119
- assert ( msaOption . isSome , 'msaOption should be Some' ) ;
120
- const msaFromHandle = msaOption . unwrap ( ) ;
121
- assert . equal ( msaFromHandle . toString ( ) , msa_id . toString ( ) , 'msaFromHandle should be equal to msa_id' ) ;
122
-
123
- // Check that the rpc returns the index as > 0
124
- const apiCheck = await ExtrinsicHelper . apiPromise . call . handlesRuntimeApi . checkHandle ( handle ) ;
125
- assert ( apiCheck . suffixIndex . toNumber ( ) > 0 ) ;
126
- } ) ;
127
- } ) ;
128
-
129
- describe ( '👇 Negative Test: Early retire handle' , function ( ) {
130
- it ( 'should not be able to retire a handle before expiration' , async function ( ) {
131
- const handle_response = await ExtrinsicHelper . getHandleForMSA ( msa_id ) ;
132
- if ( ! handle_response . isSome ) {
133
- throw new Error ( 'handle_response should be Some' ) ;
134
- }
135
-
136
- const full_handle_state = handle_response . unwrap ( ) ;
137
- const suffix_from_state = full_handle_state . suffix ;
138
- const suffix = suffix_from_state . toNumber ( ) ;
139
- assert . notEqual ( suffix , 0 , 'suffix should not be 0' ) ;
140
-
141
- const currentBlock = await getBlockNumber ( ) ;
142
- await ExtrinsicHelper . runToBlock ( currentBlock + expirationOffset + 1 ) ;
143
- try {
144
- const retireHandle = ExtrinsicHelper . retireHandle ( msaOwnerKeys ) ;
145
- const { target : event } = await retireHandle . fundAndSend ( fundingSource ) ;
146
- assert . equal ( event , undefined , 'retireHandle should not return an event' ) ;
147
- } catch ( e ) {
148
- assert . notEqual ( e , undefined , 'retireHandle should throw an error' ) ;
149
- }
150
- } ) ;
151
- } ) ;
20
+ before ( async function ( ) {
21
+ // Create a MSA for the delegator
22
+ [ msaId , msaOwnerKeys ] = await createMsa ( fundingSource , 50n * CENTS ) ;
23
+ assert . notEqual ( msaOwnerKeys , undefined , 'setup should populate delegator_key' ) ;
24
+ assert . notEqual ( msaId , undefined , 'setup should populate msaId' ) ;
152
25
} ) ;
153
26
154
- describe ( 'Suffixes Integrity Check' , function ( ) {
155
- it ( 'should return same suffixes for `abcdefg` from chain as hardcoded' , async function ( ) {
156
- const suffixes = await ExtrinsicHelper . getNextSuffixesForHandle ( 'abcdefg' , 10 ) ;
157
- const suffixes_expected = [ 23 , 65 , 16 , 53 , 25 , 75 , 29 , 26 , 10 , 87 ] ;
158
- const resp_suffixes_number = suffixes . suffixes . map ( ( x ) => x . toNumber ( ) ) ;
159
- assert . deepEqual ( resp_suffixes_number , suffixes_expected , 'suffixes should be equal to suffixes_expected' ) ;
160
- } ) ;
27
+ it ( 'should be able to claim a handle' , async function ( ) {
28
+ const handle = getTestHandle ( 'A-test' ) ;
29
+ const currentBlock = await getBlockNumber ( ) ;
30
+ const handle_vec = new Bytes ( ExtrinsicHelper . api . registry , handle ) ;
31
+ const payload = {
32
+ baseHandle : handle_vec ,
33
+ expiration : currentBlock + expirationOffset ,
34
+ } ;
35
+ const claimHandlePayload = ExtrinsicHelper . api . registry . createType (
36
+ 'CommonPrimitivesHandlesClaimHandlePayload' ,
37
+ payload
38
+ ) ;
39
+ const claimHandle = ExtrinsicHelper . claimHandle ( msaOwnerKeys , claimHandlePayload ) ;
40
+ const { target : event } = await claimHandle . fundAndSend ( fundingSource ) ;
41
+ assert . notEqual ( event , undefined , 'claimHandle should return an event' ) ;
42
+ assert . notEqual ( event ! . data . handle . toString ( ) , '' , 'claimHandle should emit a handle' ) ;
161
43
} ) ;
162
44
163
- describe ( 'validateHandle basic test' , function ( ) {
164
- it ( 'returns true for good handle, and false for bad handle' , async function ( ) {
165
- let res = await ExtrinsicHelper . validateHandle ( 'Robert`DROP TABLE STUDENTS;--' ) ;
166
- assert . equal ( res . toHuman ( ) , false ) ;
167
- res = await ExtrinsicHelper . validateHandle ( 'Little Bobby Tables' ) ;
168
- assert . equal ( res . toHuman ( ) , true ) ;
169
- res = await ExtrinsicHelper . validateHandle ( 'Bobbay😀😀' ) ;
170
- assert . equal ( res . toHuman ( ) , false ) ;
171
- } ) ;
172
- } ) ;
173
-
174
- describe ( 'checkHandle basic test' , function ( ) {
175
- it ( 'expected outcome for a good handle' , async function ( ) {
176
- const res = await ExtrinsicHelper . apiPromise . call . handlesRuntimeApi . checkHandle ( 'Little Bobby Tables' ) ;
177
- assert ( ! res . isEmpty , 'Expected a response' ) ;
178
- assert . deepEqual ( res . toHuman ( ) , {
179
- baseHandle : 'Little Bobby Tables' ,
180
- canonicalBase : 'l1tt1eb0bbytab1es' ,
181
- suffixIndex : '0' ,
182
- suffixesAvailable : true ,
183
- valid : true ,
184
- } ) ;
185
- } ) ;
186
-
187
- it ( 'expected outcome for a bad handle' , async function ( ) {
188
- const res = await ExtrinsicHelper . apiPromise . call . handlesRuntimeApi . checkHandle ( 'Robert`DROP TABLE STUDENTS;--' ) ;
189
- assert ( ! res . isEmpty , 'Expected a response' ) ;
190
- assert . deepEqual ( res . toHuman ( ) , {
191
- baseHandle : 'Robert`DROP TABLE STUDENTS;--' ,
192
- canonicalBase : '' ,
193
- suffixIndex : '0' ,
194
- suffixesAvailable : false ,
195
- valid : false ,
196
- } ) ;
197
- } ) ;
198
-
199
- it ( 'expected outcome for a good handle with complex whitespace' , async function ( ) {
200
- const res = await ExtrinsicHelper . apiPromise . call . handlesRuntimeApi . checkHandle ( 'नी हुन्न् ।' ) ;
201
- assert ( ! res . isEmpty , 'Expected a response' ) ;
202
- assert . deepEqual ( res . toHuman ( ) , {
203
- baseHandle : '0xe0a4a8e0a58020e0a4b9e0a581e0a4a8e0a58de2808de0a4a8e0a58d20e0a5a4' ,
204
- canonicalBase : '0xe0a4a8e0a4b9e0a4a8e0a4a8e0a5a4' ,
205
- suffixIndex : '0' ,
206
- suffixesAvailable : true ,
207
- valid : true ,
208
- } ) ;
209
- } ) ;
45
+ it ( 'should be able to retire a handle' , async function ( ) {
46
+ const handle_response = await ExtrinsicHelper . getHandleForMSA ( msaId ) ;
47
+ if ( ! handle_response . isSome ) {
48
+ throw new Error ( 'handle_response should be Some' ) ;
49
+ }
50
+ const full_handle_state = handle_response . unwrap ( ) ;
51
+ const suffix_from_state = full_handle_state . suffix ;
52
+ const suffix = suffix_from_state . toNumber ( ) ;
53
+ assert . notEqual ( suffix , 0 , 'suffix should not be 0' ) ;
54
+ assert . notEqual ( full_handle_state . canonical_base , undefined , 'canonical_base should not be undefined' ) ;
55
+ assert . notEqual ( full_handle_state . base_handle , undefined , 'base_handle should not be undefined' ) ;
56
+ const currentBlock = await getBlockNumber ( ) ;
57
+ // Must be at least 6 > original expiration to make sure we get past the finalization
58
+ await ExtrinsicHelper . runToBlock ( currentBlock + expirationOffset + 6 ) ;
59
+
60
+ const retireHandle = ExtrinsicHelper . retireHandle ( msaOwnerKeys ) ;
61
+ const { target : event } = await retireHandle . signAndSend ( ) ;
62
+ assert . notEqual ( event , undefined , 'retireHandle should return an event' ) ;
63
+ const handle = event ! . data . handle . toString ( ) ;
64
+ assert . notEqual ( handle , '' , 'retireHandle should return the correct handle' ) ;
210
65
} ) ;
211
66
} ) ;
0 commit comments