resolc 1.1.0

Solidity frontend for the revive compiler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! The tests for running resolc with standard JSON option.

use revive_solc_json_interface::{
    PolkaVMDefaultHeapMemorySize, PolkaVMDefaultStackMemorySize,
    SolcStandardJsonInputSettingsSelectionFileFlag, SolcStandardJsonOutput,
};

use crate::cli_utils::{
    assert_command_success, assert_equal_exit_codes, execute_resolc_with_stdin_input,
    execute_solc_with_stdin_input, STANDARD_JSON_ALL_OUTPUTS_PATH, STANDARD_JSON_CONTRACTS_PATH,
    STANDARD_JSON_NO_EVM_CODEGEN_COMPLEX_PATH, STANDARD_JSON_NO_EVM_CODEGEN_PATH,
    STANDARD_JSON_NO_PVM_CODEGEN_PER_FILE_PATH, STANDARD_JSON_PVM_CODEGEN_ALL_WILDCARD_PATH,
    STANDARD_JSON_PVM_CODEGEN_ONE_FILE_PATH, STANDARD_JSON_PVM_CODEGEN_PER_FILE_PATH,
    STANDARD_JSON_YUL_NO_PVM_CODEGEN_PATH, STANDARD_JSON_YUL_PVM_CODEGEN_PATH,
};

const JSON_OPTION: &str = "--standard-json";

/// A subset of contracts and sources expected to exist in the JSON output.
struct ExpectedOutput {
    contracts: Vec<ExpectedContract>,
    sources: Vec<ExpectedSource>,
}

/// A contract expected to exist in the JSON output.
struct ExpectedContract {
    /// The file path.
    path: &'static str,
    /// The contract name.
    name: &'static str,
    /// All contract-level fields of the JSON output selection expected to exist,
    /// such as `metadata`, `irOptimized`, etc. If `evm.bytecode` was requested,
    /// both `evm` and `evm.bytecode` should be expected.
    fields: Vec<&'static str>,
}

/// A source expected to exist in the JSON output.
struct ExpectedSource {
    /// The file path.
    path: &'static str,
    /// All file-level fields of the JSON output selection expected to exist,
    /// such as `ast`, as well as the `id` field.
    fields: Vec<&'static str>,
}

/// Asserts that the `expected` subset of contracts and sources match the ones in the `actual` output.
/// If expected sources or contracts are empty, asserts that the respective actual output is also empty.
fn assert_output_matches(actual: &SolcStandardJsonOutput, expected: &ExpectedOutput) {
    assert_sources_output_matches(actual, expected);
    assert_contracts_output_matches(actual, expected);
}

/// Asserts that the `expected` subset of sources match the ones in the `actual` output.
/// If expected sources is empty, asserts that the actual output is also empty.
fn assert_sources_output_matches(actual: &SolcStandardJsonOutput, expected: &ExpectedOutput) {
    if expected.sources.is_empty() {
        assert!(actual.sources.is_empty(), "sources should not be populated");
        return;
    }

    assert!(
        actual.sources.len() >= expected.sources.len(),
        "at least {} sources should be populated",
        expected.sources.len()
    );

    for expected_source in &expected.sources {
        let actual_source = actual
            .sources
            .get(expected_source.path)
            .unwrap_or_else(|| panic!("the file `{}` should exist", expected_source.path));
        let actual_source_json = serde_json::to_value(actual_source).unwrap();

        // Verify that every expected output exists.
        for field in &expected_source.fields {
            assert!(
                actual_source_json.get(field).is_some(),
                "the `{field}` for the file `{}` should be generated",
                expected_source.path
            );
        }

        // Verify that every unexpected output is omitted.
        let all_fields = &["id", "ast"];
        for field in all_fields {
            if !expected_source.fields.contains(field) {
                assert!(
                    actual_source_json.get(field).is_none(),
                    "the `{field}` for the file `{}` should not be generated",
                    expected_source.path
                );
            }
        }
    }
}

/// Asserts that the `expected` subset of contracts match the ones in the `actual` output.
/// If expected contracts is empty, asserts that the actual output is also empty.
fn assert_contracts_output_matches(actual: &SolcStandardJsonOutput, expected: &ExpectedOutput) {
    if expected.contracts.is_empty() {
        assert!(
            actual.contracts.is_empty(),
            "contracts should not be populated"
        );
        return;
    }

    assert!(
        actual.contracts.len() >= expected.contracts.len(),
        "at least {} contracts should be populated",
        expected.contracts.len()
    );

    for expected_contract in &expected.contracts {
        let actual_contract = actual
            .contracts
            .get(expected_contract.path)
            .unwrap_or_else(|| panic!("the file `{}` should exist", expected_contract.path))
            .get(expected_contract.name)
            .unwrap_or_else(|| {
                panic!(
                    "the contract `{}` in file `{}` should exist",
                    expected_contract.name, expected_contract.path
                )
            });
        let actual_contract_json = serde_json::to_value(actual_contract).unwrap();

        // Verify that every expected output exists (e.g. `evm.bytecode`).
        for field in &expected_contract.fields {
            let mut parts = field.split('.');
            let (parent_field, child_field) = (parts.next().unwrap(), parts.next());
            let parent_output = actual_contract_json.get(parent_field);

            assert!(
                parent_output.is_some(),
                "the `{parent_field}` for the contract `{}` should be generated",
                expected_contract.name,
            );
            if let Some(child_field) = child_field {
                assert!(
                    parent_output.unwrap().get(child_field).is_some(),
                    "the `{field}` for the contract `{}` should be generated",
                    expected_contract.name,
                );
            }
        }

        // Verify that every unexpected output is omitted.
        for flag in SolcStandardJsonInputSettingsSelectionFileFlag::all() {
            let field = serde_json::to_string(flag)
                .unwrap()
                .trim_matches('"')
                .to_owned();
            if !expected_contract.fields.contains(&field.as_str()) {
                let mut parts = field.split('.');
                let (parent_field, child_field) = (parts.next().unwrap(), parts.next());
                let parent_output = actual_contract_json.get(parent_field);

                if let Some(child_field) = child_field {
                    assert!(
                        parent_output.is_none_or(|p| p.get(child_field).is_none()),
                        "the `{field}` for the contract `{}` should not be generated",
                        expected_contract.name,
                    );
                } else {
                    assert!(
                        parent_output.is_none(),
                        "the `{parent_field}` for the contract `{}` should not be generated",
                        expected_contract.name,
                    );
                }
            }
        }
    }
}

/// Asserts that the standard JSON output has at least one error with the given `error_message`.
fn assert_standard_json_errors_contain(output: &SolcStandardJsonOutput, error_message: &str) {
    assert!(
        output
            .errors
            .iter()
            .any(|error| error.is_error() && error.message.contains(error_message)),
        "the standard JSON output should contain the error message `{error_message}`"
    );
}

/// Asserts that the standard JSON output has no errors with severity `error`.
fn assert_no_errors(output: &SolcStandardJsonOutput) {
    assert!(!output.errors.iter().any(|error| error.is_error()));
}

/// Converts valid JSON text to `SolcStandardJsonOutput`.
fn to_solc_standard_json_output(json_text: &str) -> SolcStandardJsonOutput {
    serde_json::from_str(json_text).unwrap()
}

#[test]
fn runs_with_valid_input_file() {
    let arguments = &[JSON_OPTION];
    let resolc_result = execute_resolc_with_stdin_input(arguments, STANDARD_JSON_CONTRACTS_PATH);
    assert_command_success(&resolc_result, "Providing a valid input file to stdin");

    let resolc_output = to_solc_standard_json_output(&resolc_result.stdout);
    assert_no_errors(&resolc_output);

    assert!(
        resolc_result.stdout.contains("contracts"),
        "Expected the output to contain a `contracts` field."
    );

    let solc_result = execute_solc_with_stdin_input(arguments, STANDARD_JSON_CONTRACTS_PATH);
    assert_equal_exit_codes(&solc_result, &resolc_result);
}

#[test]
fn no_evm_codegen_requested() {
    let result = execute_resolc_with_stdin_input(&[JSON_OPTION], STANDARD_JSON_NO_EVM_CODEGEN_PATH);
    assert_command_success(&result, "EVM codegen std json input fixture should build");

    let output = to_solc_standard_json_output(&result.stdout);
    assert_no_errors(&output);
}

/// A variant with more complex contracts.
///
/// The fixture is from a real project known to trigger the "Stack too deep" error.
///
/// The test ensures we set the right flags when requesting the Yul IR from solc:
/// no EVM codegen should be involved.
#[test]
fn no_evm_codegen_requested_complex() {
    let result =
        execute_resolc_with_stdin_input(&[JSON_OPTION], STANDARD_JSON_NO_EVM_CODEGEN_COMPLEX_PATH);
    assert_command_success(
        &result,
        "the EVM codegen std json complext input fixture should build fine",
    );

    let output = to_solc_standard_json_output(&result.stdout);
    assert_no_errors(&output);
}

#[test]
fn all_outputs_requested() {
    let result = execute_resolc_with_stdin_input(&[JSON_OPTION], STANDARD_JSON_ALL_OUTPUTS_PATH);
    assert_command_success(&result, "the all output std JSON input fixture");

    let output = to_solc_standard_json_output(&result.stdout);
    assert_no_errors(&output);

    let expected_contract_fields = &[
        "abi",
        "metadata",
        "devdoc",
        "userdoc",
        "storageLayout",
        "irOptimized",
        "ir",
        "evm",
        "evm.methodIdentifiers",
        "evm.bytecode",
        "evm.deployedBytecode",
        "evm.assembly",
    ];
    let expected = ExpectedOutput {
        contracts: vec![
            ExpectedContract {
                path: "src/Counter.sol",
                name: "Counter",
                fields: expected_contract_fields.into(),
            },
            ExpectedContract {
                path: "script/Counter.s.sol",
                name: "CounterScript",
                fields: expected_contract_fields.into(),
            },
            ExpectedContract {
                path: "lib/forge-std/src/mocks/MockERC20.sol",
                name: "MockERC20",
                fields: expected_contract_fields.into(),
            },
        ],
        sources: vec![
            ExpectedSource {
                path: "src/Counter.sol",
                fields: vec!["id", "ast"],
            },
            ExpectedSource {
                path: "script/Counter.s.sol",
                fields: vec!["id", "ast"],
            },
            ExpectedSource {
                path: "lib/forge-std/src/mocks/MockERC20.sol",
                fields: vec!["id", "ast"],
            },
        ],
    };
    assert_output_matches(&output, &expected);
}

#[test]
fn pvm_codegen_requested() {
    let files = &[
        STANDARD_JSON_PVM_CODEGEN_ALL_WILDCARD_PATH,
        STANDARD_JSON_PVM_CODEGEN_PER_FILE_PATH,
        STANDARD_JSON_PVM_CODEGEN_ONE_FILE_PATH,
    ];

    for file in files {
        let result = execute_resolc_with_stdin_input(&[JSON_OPTION], file);
        assert_command_success(&result, &format!("the `{file}` input fixture"));

        let output = to_solc_standard_json_output(&result.stdout);
        assert_no_errors(&output);

        let requests_codegen_for_one_file = *file == STANDARD_JSON_PVM_CODEGEN_ONE_FILE_PATH;
        let expected_contract_fields_with_codegen = &[
            "abi",
            "metadata",
            "evm",
            "evm.bytecode",
            "evm.methodIdentifiers",
        ];
        let expected_contract_fields_without_codegen =
            &["abi", "metadata", "evm", "evm.methodIdentifiers"];

        let expected = ExpectedOutput {
            contracts: vec![
                ExpectedContract {
                    path: "src/common/GasService.sol",
                    name: "GasService",
                    // This is the contract expected to have codegen for all files tested here.
                    fields: expected_contract_fields_with_codegen.into(),
                },
                ExpectedContract {
                    path: "src/common/Gateway.sol",
                    name: "Gateway",
                    fields: if requests_codegen_for_one_file {
                        expected_contract_fields_without_codegen.into()
                    } else {
                        expected_contract_fields_with_codegen.into()
                    },
                },
                ExpectedContract {
                    path: "src/common/MessageDispatcher.sol",
                    name: "MessageDispatcher",
                    fields: if requests_codegen_for_one_file {
                        expected_contract_fields_without_codegen.into()
                    } else {
                        expected_contract_fields_with_codegen.into()
                    },
                },
            ],
            sources: vec![
                ExpectedSource {
                    path: "src/common/GasService.sol",
                    fields: vec!["id"],
                },
                ExpectedSource {
                    path: "src/common/Gateway.sol",
                    fields: vec!["id"],
                },
                ExpectedSource {
                    path: "src/common/MessageDispatcher.sol",
                    fields: vec!["id"],
                },
            ],
        };
        assert_output_matches(&output, &expected);
    }
}

#[test]
fn no_pvm_codegen_requested() {
    let result =
        execute_resolc_with_stdin_input(&[JSON_OPTION], STANDARD_JSON_NO_PVM_CODEGEN_PER_FILE_PATH);
    assert_command_success(
        &result,
        "the no PVM codegen std JSON per file input fixture",
    );

    let output = to_solc_standard_json_output(&result.stdout);
    assert_no_errors(&output);

    let expected = ExpectedOutput {
        contracts: vec![
            ExpectedContract {
                path: "src/common/GasService.sol",
                name: "GasService",
                fields: vec!["abi", "evm", "evm.methodIdentifiers", "metadata"],
            },
            ExpectedContract {
                path: "src/common/Gateway.sol",
                name: "Gateway",
                fields: vec!["abi", "evm", "evm.methodIdentifiers", "metadata"],
            },
            ExpectedContract {
                path: "src/common/MessageDispatcher.sol",
                name: "MessageDispatcher",
                fields: vec!["abi", "evm", "evm.methodIdentifiers", "metadata"],
            },
        ],
        sources: vec![
            ExpectedSource {
                path: "src/common/GasService.sol",
                fields: vec!["id"],
            },
            ExpectedSource {
                path: "src/common/Gateway.sol",
                fields: vec!["id"],
            },
            ExpectedSource {
                path: "src/common/MessageDispatcher.sol",
                fields: vec!["id"],
            },
        ],
    };
    assert_output_matches(&output, &expected);
}

#[test]
fn yul_pvm_codegen_requested() {
    let result =
        execute_resolc_with_stdin_input(&[JSON_OPTION], STANDARD_JSON_YUL_PVM_CODEGEN_PATH);
    assert_command_success(&result, "the PVM codegen from Yul std JSON input fixture");

    let output = to_solc_standard_json_output(&result.stdout);
    assert_no_errors(&output);

    let expected = ExpectedOutput {
        contracts: vec![ExpectedContract {
            path: "Test",
            name: "Return",
            fields: vec![
                "evm",
                "evm.bytecode",
                "evm.deployedBytecode",
                "evm.assembly",
            ],
        }],
        sources: vec![],
    };
    assert_output_matches(&output, &expected);
}

#[test]
fn yul_no_pvm_codegen_requested() {
    let result =
        execute_resolc_with_stdin_input(&[JSON_OPTION], STANDARD_JSON_YUL_NO_PVM_CODEGEN_PATH);
    assert_command_success(
        &result,
        "the no PVM codegen from Yul std JSON input fixture",
    );

    let output = to_solc_standard_json_output(&result.stdout);
    assert_no_errors(&output);

    let expected = ExpectedOutput {
        contracts: vec![],
        sources: vec![],
    };
    assert_output_matches(&output, &expected);
}

#[test]
fn invalid_extra_arguments() {
    struct TestCase<'a> {
        arguments: Vec<&'a str>,
        error_message: &'static str,
    }

    let default_heap_size_string = PolkaVMDefaultHeapMemorySize.to_string();
    let default_stack_size_string = PolkaVMDefaultStackMemorySize.to_string();

    let cases = vec![
        TestCase {
            arguments: vec![JSON_OPTION, "--heap-size", &default_heap_size_string],
            error_message:
                "Heap size must be specified in standard JSON input polkavm memory settings",
        },
        TestCase {
            arguments: vec![JSON_OPTION, "--stack-size", &default_stack_size_string],
            error_message:
                "Stack size must be specified in standard JSON input polkavm memory settings",
        },
        TestCase {
            arguments: vec![JSON_OPTION, "--optimization", "z"],
            error_message: "LLVM optimizations must be specified in standard JSON input settings",
        },
        TestCase {
            arguments: vec![JSON_OPTION, "-Oz"],
            error_message: "LLVM optimizations must be specified in standard JSON input settings",
        },
        TestCase {
            arguments: vec![JSON_OPTION, "--metadata-hash", "ipfs"],
            error_message:
                "`ipfs` metadata hash type is not supported. Please use `keccak256` instead",
        },
        TestCase {
            arguments: vec![JSON_OPTION, "--metadata-hash", "keccak256"],
            error_message: "Metadata hash mode must be specified in standard JSON input settings",
        },
        TestCase {
            arguments: vec![JSON_OPTION, "--asm"],
            error_message: "Cannot output assembly or binary outside of JSON in standard JSON mode",
        },
        TestCase {
            arguments: vec![JSON_OPTION, "--bin"],
            error_message: "Cannot output assembly or binary outside of JSON in standard JSON mode",
        },
        TestCase {
            arguments: vec![JSON_OPTION, "--libraries", "myLib=0x0000"],
            error_message: "Libraries must be passed via standard JSON input",
        },
        TestCase {
            arguments: vec![JSON_OPTION, "--evm-version", "osaka"],
            error_message: "EVM version must be passed via standard JSON input",
        },
        TestCase {
            arguments: vec![JSON_OPTION, "--output-dir", "tmp"],
            error_message: "Output directory cannot be used in standard JSON mode",
        },
        TestCase {
            arguments: vec![JSON_OPTION, "--overwrite"],
            error_message: "Overwriting flag cannot be used in standard JSON mode",
        },
        TestCase {
            arguments: vec![JSON_OPTION, "--disable-solc-optimizer"],
            error_message:
                "Disabling the solc optimizer must be specified in standard JSON input settings",
        },
        TestCase {
            arguments: vec![JSON_OPTION, "-g"],
            error_message: "Debug info must be requested in standard JSON input polkavm settings",
        },
        TestCase {
            arguments: vec![JSON_OPTION, "--llvm-arg=-riscv"],
            error_message:
                "LLVM arguments must be configured in standard JSON input polkavm settings",
        },
    ];

    for case in cases {
        let result = execute_resolc_with_stdin_input(&case.arguments, STANDARD_JSON_CONTRACTS_PATH);
        let output = to_solc_standard_json_output(&result.stdout);
        assert_standard_json_errors_contain(&output, case.error_message);
    }
}