fix(solc): make StorageLayout json parsing lossless (#1515)

This commit is contained in:
Matthias Seitz 2022-07-26 17:26:19 +02:00 committed by GitHub
parent c12033f436
commit 1d40d4e049
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 0 deletions

View File

@ -1619,6 +1619,7 @@ pub struct Ewasm {
pub wasm: String, pub wasm: String,
} }
/// Represents the `storage-layout` section of the `CompilerOutput` if selected.
#[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)] #[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)]
pub struct StorageLayout { pub struct StorageLayout {
pub storage: Vec<Storage>, pub storage: Vec<Storage>,
@ -1647,9 +1648,16 @@ pub struct Storage {
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct StorageType { pub struct StorageType {
pub encoding: String, pub encoding: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub key: Option<String>,
pub label: String, pub label: String,
#[serde(rename = "numberOfBytes")] #[serde(rename = "numberOfBytes")]
pub number_of_bytes: String, pub number_of_bytes: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub value: Option<String>,
/// additional fields
#[serde(flatten)]
pub other: BTreeMap<String, serde_json::Value>,
} }
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Hash)] #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Hash)]
@ -2120,4 +2128,11 @@ mod tests {
let value = serde_json::to_string(&c).unwrap(); let value = serde_json::to_string(&c).unwrap();
pretty_assertions::assert_eq!(s, value); pretty_assertions::assert_eq!(s, value);
} }
#[test]
fn test_lossless_storage_layout() {
let input = include_str!("../../test-data/foundryissue2462.json");
let layout: StorageLayout = serde_json::from_str(input).unwrap();
pretty_assertions::assert_eq!(input, &serde_json::to_string_pretty(&layout).unwrap());
}
} }

View File

@ -0,0 +1,54 @@
{
"storage": [
{
"astId": 3,
"contract": "test-data/foundryissue2462.sol:Token",
"label": "x",
"offset": 0,
"slot": "0",
"type": "t_uint256"
},
{
"astId": 7,
"contract": "test-data/foundryissue2462.sol:Token",
"label": "balances",
"offset": 0,
"slot": "1",
"type": "t_mapping(t_address,t_uint256)"
},
{
"astId": 13,
"contract": "test-data/foundryissue2462.sol:Token",
"label": "allowances",
"offset": 0,
"slot": "2",
"type": "t_mapping(t_address,t_mapping(t_address,t_uint256))"
}
],
"types": {
"t_address": {
"encoding": "inplace",
"label": "address",
"numberOfBytes": "20"
},
"t_mapping(t_address,t_mapping(t_address,t_uint256))": {
"encoding": "mapping",
"key": "t_address",
"label": "mapping(address => mapping(address => uint256))",
"numberOfBytes": "32",
"value": "t_mapping(t_address,t_uint256)"
},
"t_mapping(t_address,t_uint256)": {
"encoding": "mapping",
"key": "t_address",
"label": "mapping(address => uint256)",
"numberOfBytes": "32",
"value": "t_uint256"
},
"t_uint256": {
"encoding": "inplace",
"label": "uint256",
"numberOfBytes": "32"
}
}
}