@@ -8,11 +8,28 @@ use crate::{
8
8
use async_trait:: async_trait;
9
9
use dashmap:: { mapref:: entry:: Entry , DashMap } ;
10
10
11
- use primitives:: { Address , Channel , ChannelId , ToETHChecksum , ValidatorId } ;
11
+ use once_cell:: sync:: Lazy ;
12
+ use primitives:: {
13
+ Address , Chain , ChainId , ChainOf , Channel , ChannelId , ToETHChecksum , ValidatorId ,
14
+ } ;
12
15
use std:: { collections:: HashMap , sync:: Arc } ;
13
16
14
17
pub type Adapter < S > = crate :: Adapter < Dummy , S > ;
15
18
19
+ /// The Dummy Chain to be used with this adapter
20
+ /// The Chain is not applicable to the adapter, however, it is required for
21
+ /// applications because of the `authentication` & [`Channel`] interactions.
22
+ pub static DUMMY_CHAIN : Lazy < Chain > = Lazy :: new ( || Chain {
23
+ chain_id : ChainId :: new ( 1 ) ,
24
+ rpc : "http://dummy.com" . parse ( ) . expect ( "Should parse ApiUrl" ) ,
25
+ outpace : "0x0000000000000000000000000000000000000000"
26
+ . parse ( )
27
+ . unwrap ( ) ,
28
+ sweeper : "0x0000000000000000000000000000000000000000"
29
+ . parse ( )
30
+ . unwrap ( ) ,
31
+ } ) ;
32
+
16
33
/// Dummy adapter implementation intended for testing.
17
34
#[ derive( Debug , Clone ) ]
18
35
pub struct Dummy {
@@ -86,6 +103,9 @@ impl Locked for Dummy {
86
103
}
87
104
88
105
/// Verify, based on the signature & state_root, that the signer is the same
106
+ ///
107
+ /// Splits the signature by `" "` (whitespace) and takes
108
+ /// the last part of it which contains the signer [`Address`].
89
109
fn verify (
90
110
& self ,
91
111
signer : ValidatorId ,
@@ -102,8 +122,8 @@ impl Locked for Dummy {
102
122
Ok ( is_same)
103
123
}
104
124
105
- /// Creates a `Session` from a provided Token by calling the Contract.
106
- /// Does **not** cache the (`Token`, `Session`) pair .
125
+ /// Finds the authorization token from the configured values
126
+ /// and creates a [`Session`] out of it using a [`ChainId`] of `1` .
107
127
async fn session_from_token ( & self , token : & str ) -> Result < Session , crate :: Error > {
108
128
let identity = self
109
129
. authorization_tokens
@@ -114,6 +134,7 @@ impl Locked for Dummy {
114
134
Some ( ( address, _token) ) => Ok ( Session {
115
135
uid : * address,
116
136
era : 0 ,
137
+ chain : DUMMY_CHAIN . clone ( ) ,
117
138
} ) ,
118
139
None => Err ( Error :: authentication ( format ! (
119
140
"No identity found that matches authentication token: {}" ,
@@ -124,11 +145,11 @@ impl Locked for Dummy {
124
145
125
146
async fn get_deposit (
126
147
& self ,
127
- channel : & Channel ,
148
+ channel_context : & ChainOf < Channel > ,
128
149
depositor_address : Address ,
129
150
) -> Result < Deposit , crate :: Error > {
130
151
self . deposits
131
- . get_next_deposit ( channel . id ( ) , depositor_address)
152
+ . get_next_deposit ( channel_context . context . id ( ) , depositor_address)
132
153
. ok_or_else ( || {
133
154
Error :: adapter ( format ! (
134
155
"No more mocked deposits found for depositor {:?}" ,
@@ -151,7 +172,7 @@ impl Unlocked for Dummy {
151
172
}
152
173
153
174
// requires Unlocked
154
- fn get_auth ( & self , _intended_for : ValidatorId ) -> Result < String , Error > {
175
+ fn get_auth ( & self , _for_chain : ChainId , _intended_for : ValidatorId ) -> Result < String , Error > {
155
176
self . authorization_tokens
156
177
. get ( & self . identity . to_address ( ) )
157
178
. cloned ( )
@@ -174,16 +195,31 @@ impl Unlockable for Dummy {
174
195
175
196
#[ cfg( test) ]
176
197
mod test {
198
+ use std:: num:: NonZeroU8 ;
199
+
177
200
use primitives:: {
201
+ config:: TokenInfo ,
178
202
util:: tests:: prep_db:: { ADDRESSES , DUMMY_CAMPAIGN , IDS } ,
179
- BigNum ,
203
+ BigNum , ChainOf , UnifiedNum ,
180
204
} ;
181
205
182
206
use super :: * ;
183
207
184
208
#[ tokio:: test]
185
209
async fn test_deposits_calls ( ) {
186
210
let channel = DUMMY_CAMPAIGN . channel ;
211
+
212
+ let channel_context = ChainOf {
213
+ context : channel,
214
+ token : TokenInfo {
215
+ min_token_units_for_deposit : 1_u64 . into ( ) ,
216
+ min_validator_fee : 1_u64 . into ( ) ,
217
+ precision : NonZeroU8 :: new ( UnifiedNum :: PRECISION ) . expect ( "Non zero u8" ) ,
218
+ address : channel. token ,
219
+ } ,
220
+ chain : DUMMY_CHAIN . clone ( ) ,
221
+ } ;
222
+
187
223
let dummy_client = Dummy :: init ( Options {
188
224
dummy_identity : IDS [ "leader" ] ,
189
225
dummy_auth_tokens : Default :: default ( ) ,
@@ -193,7 +229,7 @@ mod test {
193
229
194
230
// no mocked deposit calls should cause an Error
195
231
{
196
- let result = dummy_client. get_deposit ( & channel , address) . await ;
232
+ let result = dummy_client. get_deposit ( & channel_context , address) . await ;
197
233
198
234
assert ! ( result. is_err( ) ) ;
199
235
}
@@ -211,25 +247,25 @@ mod test {
211
247
dummy_client. add_deposit_call ( channel. id ( ) , address, deposits[ 1 ] . clone ( ) ) ;
212
248
213
249
let first_call = dummy_client
214
- . get_deposit ( & channel , address)
250
+ . get_deposit ( & channel_context , address)
215
251
. await
216
252
. expect ( "Should get first mocked deposit" ) ;
217
253
assert_eq ! ( & deposits[ 0 ] , & first_call) ;
218
254
219
255
// should not affect the Mocked deposit calls and should cause an error
220
256
let different_address_call = dummy_client
221
- . get_deposit ( & channel , ADDRESSES [ "leader" ] )
257
+ . get_deposit ( & channel_context , ADDRESSES [ "leader" ] )
222
258
. await ;
223
259
assert ! ( different_address_call. is_err( ) ) ;
224
260
225
261
let second_call = dummy_client
226
- . get_deposit ( & channel , address)
262
+ . get_deposit ( & channel_context , address)
227
263
. await
228
264
. expect ( "Should get second mocked deposit" ) ;
229
265
assert_eq ! ( & deposits[ 1 ] , & second_call) ;
230
266
231
267
// Third call should error, we've only mocked 2 calls!
232
- let third_call = dummy_client. get_deposit ( & channel , address) . await ;
268
+ let third_call = dummy_client. get_deposit ( & channel_context , address) . await ;
233
269
assert ! ( third_call. is_err( ) ) ;
234
270
}
235
271
}
0 commit comments