-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
492 additions
and
92 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
smart-contract-verifier/smart-contract-verifier-server/src/settings.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
smart-contract-verifier/smart-contract-verifier-server/tests/lookup_methods.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use blockscout_service_launcher::{ | ||
launcher::ConfigSettings, | ||
test_server::{get_test_server_settings, init_server, send_post_request}, | ||
}; | ||
use ethers_solc::{CompilerInput, CompilerOutput, EvmVersion, Solc}; | ||
use rstest::rstest; | ||
use serde::Deserialize; | ||
use smart_contract_verifier_proto::blockscout::smart_contract_verifier::v2::{ | ||
LookupMethodsRequest, LookupMethodsResponse, | ||
}; | ||
use smart_contract_verifier_server::Settings; | ||
use std::{ | ||
collections::{BTreeMap, BTreeSet}, | ||
path::PathBuf, | ||
}; | ||
|
||
const ROUTE: &str = "/api/v2/verifier/solidity/methods:lookup"; | ||
|
||
fn process_compiler_output( | ||
output: &CompilerOutput, | ||
contract_name: &str, | ||
) -> anyhow::Result<(LookupMethodsRequest, BTreeMap<String, String>)> { | ||
let (_, contract) = output | ||
.contracts_iter() | ||
.find(|(name, _)| *name == contract_name) | ||
.ok_or_else(|| anyhow::anyhow!("contract not found"))?; | ||
let evm = contract.evm.as_ref().expect("evm included"); | ||
let deployed_bytecode = evm | ||
.deployed_bytecode | ||
.as_ref() | ||
.expect("deployed bytecode included") | ||
.bytecode | ||
.as_ref() | ||
.expect("bytecode included"); | ||
let methods = evm.method_identifiers.clone(); | ||
|
||
let bytecode = deployed_bytecode | ||
.object | ||
.clone() | ||
.into_bytes() | ||
.unwrap() | ||
.to_string(); | ||
let abi = serde_json::to_string(&contract.abi.clone().expect("abi included"))?; | ||
let source_map = deployed_bytecode | ||
.source_map | ||
.as_ref() | ||
.expect("srcmap included") | ||
.clone(); | ||
let file_ids = output | ||
.sources | ||
.iter() | ||
.map(|(name, file)| (file.id, name.clone())) | ||
.collect(); | ||
|
||
let request = LookupMethodsRequest { | ||
abi, | ||
bytecode, | ||
file_ids, | ||
source_map, | ||
}; | ||
Ok((request, methods)) | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct TestCase { | ||
version: String, | ||
contract_name: String, | ||
} | ||
|
||
#[rstest] | ||
#[tokio::test(flavor = "multi_thread", worker_threads = 1)] | ||
async fn test_lookup_methods(#[files("tests/test_cases_lookup_methods/*")] test_dir: PathBuf) { | ||
let mut settings = Settings::build().expect("Failed to build settings"); | ||
let (server_settings, base) = get_test_server_settings(); | ||
settings.server = server_settings; | ||
settings.vyper.enabled = false; | ||
settings.solidity.enabled = true; | ||
settings.sourcify.enabled = false; | ||
settings.jaeger.enabled = false; | ||
settings.tracing.enabled = false; | ||
|
||
init_server(|| smart_contract_verifier_server::run(settings), &base).await; | ||
|
||
let test_case: TestCase = serde_json::from_str( | ||
std::fs::read_to_string(test_dir.join("config.json")) | ||
.unwrap() | ||
.as_str(), | ||
) | ||
.expect("Failed to parse test case"); | ||
|
||
let solc = | ||
Solc::find_or_install_svm_version(test_case.version).expect("failed to install version"); | ||
|
||
let inputs = CompilerInput::new(test_dir).expect("failed to read dir"); | ||
let input = inputs[0].clone().evm_version(EvmVersion::London); | ||
let output = solc.compile(&input).expect("failed to compile"); | ||
|
||
let (request, methods) = process_compiler_output(&output, &test_case.contract_name).unwrap(); | ||
let res: LookupMethodsResponse = send_post_request(&base, ROUTE, &request).await; | ||
|
||
// Make sure we extracted all methods | ||
assert_eq!( | ||
methods.values().collect::<BTreeSet<&String>>(), | ||
res.methods.keys().collect::<BTreeSet<&String>>() | ||
); | ||
} |
10 changes: 10 additions & 0 deletions
10
...mart-contract-verifier-server/tests/test_cases_lookup_methods/import/AbstractContract.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
pragma solidity ^0.8.0; | ||
|
||
abstract contract AbstractContract { | ||
// Declaring functions | ||
function getStr( | ||
string memory _strIn) public view virtual returns( | ||
string memory); | ||
function setValue(uint _in1, uint _in2) public virtual; | ||
function add() public virtual returns(uint); | ||
} |
21 changes: 21 additions & 0 deletions
21
...smart-contract-verifier-server/tests/test_cases_lookup_methods/import/DerivedContract.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
pragma solidity ^0.8.0; | ||
|
||
import "./AbstractContract.sol"; | ||
|
||
contract DerivedContract is AbstractContract{ | ||
uint private num1; | ||
uint private num2; | ||
|
||
function getStr(string memory _strIn) public pure override returns(string memory) { | ||
return _strIn; | ||
} | ||
|
||
function setValue(uint _in1, uint _in2) public override { | ||
num1 = _in1; | ||
num2 = _in2; | ||
} | ||
|
||
function add() public view override returns(uint) { | ||
return (num2 + num1); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...erifier/smart-contract-verifier-server/tests/test_cases_lookup_methods/import/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"version": "0.8.17", | ||
"contract_name": "DerivedContract" | ||
} |
18 changes: 18 additions & 0 deletions
18
...erifier/smart-contract-verifier-server/tests/test_cases_lookup_methods/simple/Greeter.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//SPDX-License-Identifier: Unlicense | ||
pragma solidity ^0.8.0; | ||
|
||
contract Greeter { | ||
string private greeting; | ||
|
||
constructor(string memory _greeting) { | ||
greeting = _greeting; | ||
} | ||
|
||
function greet() public view returns (string memory) { | ||
return greeting; | ||
} | ||
|
||
function setGreeting(string memory _greeting) public { | ||
greeting = _greeting; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...erifier/smart-contract-verifier-server/tests/test_cases_lookup_methods/simple/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"version": "0.8.17", | ||
"contract_name": "Greeter" | ||
} |
2 changes: 1 addition & 1 deletion
2
smart-contract-verifier/smart-contract-verifier/src/lookup_methods/disassemble.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters