1
1
use crate :: from_json;
2
2
use blockscout_display_bytes:: decode_hex;
3
3
use blockscout_service_launcher:: test_database:: database;
4
- use sea_orm:: { prelude:: Uuid , DatabaseConnection } ;
4
+ use pretty_assertions:: assert_eq;
5
+ use sea_orm:: DatabaseConnection ;
5
6
use std:: collections:: BTreeMap ;
6
7
use verification_common:: verifier_alliance:: {
7
8
CompilationArtifacts , CreationCodeArtifacts , Match , MatchTransformation , MatchValues ,
8
9
RuntimeCodeArtifacts ,
9
10
} ;
10
11
use verifier_alliance_database:: {
11
- CompiledContract , CompiledContractCompiler , CompiledContractLanguage , InsertContractDeployment ,
12
- VerifiedContract , VerifiedContractMatches ,
12
+ CompiledContract , CompiledContractCompiler , CompiledContractLanguage , ContractDeployment ,
13
+ InsertContractDeployment , VerifiedContract , VerifiedContractMatches ,
13
14
} ;
14
15
use verifier_alliance_migration:: Migrator ;
15
16
16
17
#[ tokio:: test]
17
18
async fn insert_verified_contract_with_complete_matches_work ( ) {
18
19
let db_guard = database ! ( Migrator ) ;
19
20
20
- let contract_deployment_id = insert_contract_deployment ( db_guard. client ( ) . as_ref ( ) ) . await ;
21
+ let contract_deployment_id = insert_contract_deployment ( db_guard. client ( ) . as_ref ( ) )
22
+ . await
23
+ . id ;
21
24
let compiled_contract = complete_compiled_contract ( ) ;
22
25
let verified_contract = VerifiedContract {
23
26
contract_deployment_id,
@@ -48,7 +51,9 @@ async fn insert_verified_contract_with_complete_matches_work() {
48
51
async fn insert_verified_contract_with_runtime_only_matches_work ( ) {
49
52
let db_guard = database ! ( Migrator ) ;
50
53
51
- let contract_deployment_id = insert_contract_deployment ( db_guard. client ( ) . as_ref ( ) ) . await ;
54
+ let contract_deployment_id = insert_contract_deployment ( db_guard. client ( ) . as_ref ( ) )
55
+ . await
56
+ . id ;
52
57
let compiled_contract = complete_compiled_contract ( ) ;
53
58
let verified_contract = VerifiedContract {
54
59
contract_deployment_id,
@@ -74,7 +79,9 @@ async fn insert_verified_contract_with_runtime_only_matches_work() {
74
79
async fn insert_verified_contract_with_creation_only_matches_work ( ) {
75
80
let db_guard = database ! ( Migrator ) ;
76
81
77
- let contract_deployment_id = insert_contract_deployment ( db_guard. client ( ) . as_ref ( ) ) . await ;
82
+ let contract_deployment_id = insert_contract_deployment ( db_guard. client ( ) . as_ref ( ) )
83
+ . await
84
+ . id ;
78
85
let compiled_contract = complete_compiled_contract ( ) ;
79
86
let verified_contract = VerifiedContract {
80
87
contract_deployment_id,
@@ -100,7 +107,9 @@ async fn insert_verified_contract_with_creation_only_matches_work() {
100
107
async fn insert_verified_contract_with_filled_matches ( ) {
101
108
let db_guard = database ! ( Migrator ) ;
102
109
103
- let contract_deployment_id = insert_contract_deployment ( db_guard. client ( ) . as_ref ( ) ) . await ;
110
+ let contract_deployment_id = insert_contract_deployment ( db_guard. client ( ) . as_ref ( ) )
111
+ . await
112
+ . id ;
104
113
let compiled_contract = complete_compiled_contract ( ) ;
105
114
106
115
let ( runtime_match_values, runtime_match_transformations) = {
@@ -174,6 +183,157 @@ async fn insert_verified_contract_with_filled_matches() {
174
183
. expect ( "error while inserting" ) ;
175
184
}
176
185
186
+ #[ tokio:: test]
187
+ async fn inserted_verified_contract_can_be_retrieved ( ) {
188
+ let db_guard = database ! ( Migrator ) ;
189
+ let database_connection = db_guard. client ( ) ;
190
+ let database_connection = database_connection. as_ref ( ) ;
191
+
192
+ let contract_deployment = insert_contract_deployment ( database_connection) . await ;
193
+ let compiled_contract = complete_compiled_contract ( ) ;
194
+ let verified_contract = VerifiedContract {
195
+ contract_deployment_id : contract_deployment. id ,
196
+ compiled_contract,
197
+ matches : VerifiedContractMatches :: Complete {
198
+ runtime_match : Match {
199
+ metadata_match : true ,
200
+ transformations : vec ! [ ] ,
201
+ values : Default :: default ( ) ,
202
+ } ,
203
+ creation_match : Match {
204
+ metadata_match : true ,
205
+ transformations : vec ! [ ] ,
206
+ values : Default :: default ( ) ,
207
+ } ,
208
+ } ,
209
+ } ;
210
+
211
+ verifier_alliance_database:: insert_verified_contract (
212
+ database_connection,
213
+ verified_contract. clone ( ) ,
214
+ )
215
+ . await
216
+ . expect ( "error while inserting" ) ;
217
+
218
+ let retrieved_contracts = verifier_alliance_database:: find_verified_contracts (
219
+ database_connection,
220
+ contract_deployment. chain_id ,
221
+ contract_deployment. address ,
222
+ )
223
+ . await
224
+ . expect ( "error while retrieving" ) ;
225
+ let retrieved_verified_contracts: Vec < _ > = retrieved_contracts
226
+ . into_iter ( )
227
+ . map ( |value| value. verified_contract )
228
+ . collect ( ) ;
229
+ assert_eq ! (
230
+ retrieved_verified_contracts,
231
+ vec![ verified_contract] ,
232
+ "invalid retrieved values"
233
+ ) ;
234
+ }
235
+
236
+ #[ tokio:: test]
237
+ async fn not_override_partial_matches ( ) {
238
+ let db_guard = database ! ( Migrator ) ;
239
+ let database_connection = db_guard. client ( ) ;
240
+ let database_connection = database_connection. as_ref ( ) ;
241
+
242
+ let contract_deployment = insert_contract_deployment ( database_connection) . await ;
243
+
244
+ let partially_verified_contract = VerifiedContract {
245
+ contract_deployment_id : contract_deployment. id ,
246
+ compiled_contract : complete_compiled_contract ( ) ,
247
+ matches : VerifiedContractMatches :: Complete {
248
+ runtime_match : Match {
249
+ metadata_match : false ,
250
+ transformations : vec ! [ ] ,
251
+ values : Default :: default ( ) ,
252
+ } ,
253
+ creation_match : Match {
254
+ metadata_match : false ,
255
+ transformations : vec ! [ ] ,
256
+ values : Default :: default ( ) ,
257
+ } ,
258
+ } ,
259
+ } ;
260
+ verifier_alliance_database:: insert_verified_contract (
261
+ database_connection,
262
+ partially_verified_contract. clone ( ) ,
263
+ )
264
+ . await
265
+ . expect ( "error while inserting partially verified contract" ) ;
266
+
267
+ let mut another_partially_verified_contract = partially_verified_contract. clone ( ) ;
268
+ another_partially_verified_contract
269
+ . compiled_contract
270
+ . creation_code
271
+ . extend_from_slice ( & [ 0x10 ] ) ;
272
+ another_partially_verified_contract
273
+ . compiled_contract
274
+ . runtime_code
275
+ . extend_from_slice ( & [ 0x10 ] ) ;
276
+ verifier_alliance_database:: insert_verified_contract (
277
+ database_connection,
278
+ another_partially_verified_contract. clone ( ) ,
279
+ )
280
+ . await
281
+ . map_err ( |err| {
282
+ assert ! (
283
+ err. to_string( ) . contains( "is not better than existing" ) ,
284
+ "unexpected error: {}" ,
285
+ err
286
+ )
287
+ } )
288
+ . expect_err ( "error expected while inserting another partially verified contract" ) ;
289
+
290
+ let mut fully_verified_contract = partially_verified_contract. clone ( ) ;
291
+ fully_verified_contract
292
+ . compiled_contract
293
+ . creation_code
294
+ . extend_from_slice ( & [ 0xff ] ) ;
295
+ fully_verified_contract
296
+ . compiled_contract
297
+ . runtime_code
298
+ . extend_from_slice ( & [ 0xff ] ) ;
299
+ fully_verified_contract. matches = VerifiedContractMatches :: Complete {
300
+ creation_match : Match {
301
+ metadata_match : true ,
302
+ transformations : vec ! [ ] ,
303
+ values : Default :: default ( ) ,
304
+ } ,
305
+ runtime_match : Match {
306
+ metadata_match : true ,
307
+ transformations : vec ! [ ] ,
308
+ values : Default :: default ( ) ,
309
+ } ,
310
+ } ;
311
+ verifier_alliance_database:: insert_verified_contract (
312
+ database_connection,
313
+ fully_verified_contract. clone ( ) ,
314
+ )
315
+ . await
316
+ . expect ( "error while inserting fully verified contract" ) ;
317
+
318
+ let mut retrieved_contracts = verifier_alliance_database:: find_verified_contracts (
319
+ database_connection,
320
+ contract_deployment. chain_id ,
321
+ contract_deployment. address ,
322
+ )
323
+ . await
324
+ . expect ( "error while retrieving" ) ;
325
+ retrieved_contracts. sort_by_key ( |value| value. created_at ) ;
326
+ let retrieved_verified_contracts: Vec < _ > = retrieved_contracts
327
+ . into_iter ( )
328
+ . map ( |value| value. verified_contract )
329
+ . collect ( ) ;
330
+
331
+ assert_eq ! (
332
+ retrieved_verified_contracts,
333
+ vec![ partially_verified_contract, fully_verified_contract]
334
+ ) ;
335
+ }
336
+
177
337
fn complete_compiled_contract ( ) -> CompiledContract {
178
338
CompiledContract {
179
339
compiler : CompiledContractCompiler :: Solc ,
@@ -209,7 +369,9 @@ fn complete_compiled_contract() -> CompiledContract {
209
369
}
210
370
}
211
371
212
- async fn insert_contract_deployment ( database_connection : & DatabaseConnection ) -> Uuid {
372
+ async fn insert_contract_deployment (
373
+ database_connection : & DatabaseConnection ,
374
+ ) -> ContractDeployment {
213
375
let contract_deployment = InsertContractDeployment :: Regular {
214
376
chain_id : 10 ,
215
377
address : decode_hex ( "0x8FbB39A5a79aeCE03c8f13ccEE0b96C128ec1a67" ) . unwrap ( ) ,
@@ -227,5 +389,4 @@ async fn insert_contract_deployment(database_connection: &DatabaseConnection) ->
227
389
verifier_alliance_database:: insert_contract_deployment ( database_connection, contract_deployment)
228
390
. await
229
391
. expect ( "error while inserting contract deployment" )
230
- . id
231
392
}
0 commit comments