Add option to disable CBOR metadata in bytecode. (#1782)

This commit is contained in:
Leo 2022-10-13 23:57:03 +02:00 committed by GitHub
parent 96e195d2d7
commit 2c28fa47e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 1 deletions

View File

@ -106,6 +106,8 @@ impl CompilerInput {
once_cell::sync::Lazy::new(|| VersionReq::parse("<0.8.10").unwrap()); once_cell::sync::Lazy::new(|| VersionReq::parse("<0.8.10").unwrap());
static PRE_V0_7_5: once_cell::sync::Lazy<VersionReq> = static PRE_V0_7_5: once_cell::sync::Lazy<VersionReq> =
once_cell::sync::Lazy::new(|| VersionReq::parse("<0.7.5").unwrap()); once_cell::sync::Lazy::new(|| VersionReq::parse("<0.7.5").unwrap());
static PRE_V0_8_18: once_cell::sync::Lazy<VersionReq> =
once_cell::sync::Lazy::new(|| VersionReq::parse("<0.8.18").unwrap());
if PRE_V0_6_0.matches(version) { if PRE_V0_6_0.matches(version) {
if let Some(ref mut meta) = self.settings.metadata { if let Some(ref mut meta) = self.settings.metadata {
@ -133,6 +135,13 @@ impl CompilerInput {
self.settings.via_ir.take(); self.settings.via_ir.take();
} }
if PRE_V0_8_18.matches(version) {
// introduced in 0.8.18 <https://github.com/ethereum/solidity/releases/tag/v0.8.18>
if let Some(ref mut meta) = self.settings.metadata {
meta.cbor_metadata = None;
}
}
self self
} }
@ -838,11 +847,19 @@ pub struct SettingsMetadata {
with = "serde_helpers::display_from_str_opt" with = "serde_helpers::display_from_str_opt"
)] )]
pub bytecode_hash: Option<BytecodeHash>, pub bytecode_hash: Option<BytecodeHash>,
#[serde(default, rename = "appendCBOR", skip_serializing_if = "Option::is_none")]
pub cbor_metadata: Option<bool>,
}
impl SettingsMetadata {
pub fn new(hash: BytecodeHash, cbor: bool) -> Self {
Self { use_literal_content: None, bytecode_hash: Some(hash), cbor_metadata: Some(cbor) }
}
} }
impl From<BytecodeHash> for SettingsMetadata { impl From<BytecodeHash> for SettingsMetadata {
fn from(hash: BytecodeHash) -> Self { fn from(hash: BytecodeHash) -> Self {
Self { use_literal_content: None, bytecode_hash: Some(hash) } Self { use_literal_content: None, bytecode_hash: Some(hash), cbor_metadata: None }
} }
} }
@ -2069,6 +2086,29 @@ mod tests {
assert!(i.settings.metadata.unwrap().bytecode_hash.is_none()); assert!(i.settings.metadata.unwrap().bytecode_hash.is_none());
} }
#[test]
fn can_sanitize_cbor_metadata() {
let version: Version = "0.8.18".parse().unwrap();
let settings = Settings {
metadata: Some(SettingsMetadata::new(BytecodeHash::Ipfs, true)),
..Default::default()
};
let input = CompilerInput {
language: "Solidity".to_string(),
sources: Default::default(),
settings,
};
let i = input.clone().sanitized(&version);
assert_eq!(i.settings.metadata.unwrap().cbor_metadata, Some(true));
let version: Version = "0.8.0".parse().unwrap();
let i = input.sanitized(&version);
assert!(i.settings.metadata.unwrap().cbor_metadata.is_none());
}
#[test] #[test]
fn can_parse_libraries() { fn can_parse_libraries() {
let libraries = ["./src/lib/LibraryContract.sol:Library:0xaddress".to_string()]; let libraries = ["./src/lib/LibraryContract.sol:Library:0xaddress".to_string()];