feat(solc): support emitting bytecode as extra files (#2074)

This commit is contained in:
Matthias Seitz 2023-01-27 19:15:03 +01:00 committed by GitHub
parent ff05a8762b
commit f86bc2483f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 4 deletions

View File

@ -13,15 +13,15 @@ use crate::{
bytecode::{CompactBytecode, CompactDeployedBytecode}, bytecode::{CompactBytecode, CompactDeployedBytecode},
contract::{CompactContract, CompactContractBytecode, Contract}, contract::{CompactContract, CompactContractBytecode, Contract},
output_selection::{ output_selection::{
BytecodeOutputSelection, ContractOutputSelection, EvmOutputSelection, BytecodeOutputSelection, ContractOutputSelection, DeployedBytecodeOutputSelection,
EwasmOutputSelection, EvmOutputSelection, EwasmOutputSelection,
}, },
Ast, CompactContractBytecodeCow, DevDoc, Evm, Ewasm, FunctionDebugData, GasEstimates, Ast, CompactContractBytecodeCow, DevDoc, Evm, Ewasm, FunctionDebugData, GasEstimates,
GeneratedSource, LosslessAbi, LosslessMetadata, Metadata, Offsets, Settings, StorageLayout, GeneratedSource, LosslessAbi, LosslessMetadata, Metadata, Offsets, Settings, StorageLayout,
UserDoc, UserDoc,
}, },
sources::VersionedSourceFile, sources::VersionedSourceFile,
ArtifactOutput, SolcConfig, SolcError, SourceFile, Artifact, ArtifactOutput, SolcConfig, SolcError, SourceFile,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{borrow::Cow, collections::BTreeMap, fs, path::Path}; use std::{borrow::Cow, collections::BTreeMap, fs, path::Path};
@ -509,6 +509,8 @@ pub struct ExtraOutputFiles {
pub assembly: bool, pub assembly: bool,
pub source_map: bool, pub source_map: bool,
pub generated_sources: bool, pub generated_sources: bool,
pub bytecode: bool,
pub deployed_bytecode: bool,
/// PRIVATE: This structure may grow, As such, constructing this structure should /// PRIVATE: This structure may grow, As such, constructing this structure should
/// _always_ be done using a public constructor or update syntax: /// _always_ be done using a public constructor or update syntax:
@ -537,6 +539,8 @@ impl ExtraOutputFiles {
assembly: true, assembly: true,
source_map: true, source_map: true,
generated_sources: true, generated_sources: true,
bytecode: true,
deployed_bytecode: true,
__non_exhaustive: (), __non_exhaustive: (),
} }
} }
@ -565,6 +569,8 @@ impl ExtraOutputFiles {
config.assembly = true; config.assembly = true;
config.generated_sources = true; config.generated_sources = true;
config.source_map = true; config.source_map = true;
config.bytecode = true;
config.deployed_bytecode = true;
} }
EvmOutputSelection::Assembly => { EvmOutputSelection::Assembly => {
config.assembly = true; config.assembly = true;
@ -572,9 +578,18 @@ impl ExtraOutputFiles {
EvmOutputSelection::ByteCode(BytecodeOutputSelection::GeneratedSources) => { EvmOutputSelection::ByteCode(BytecodeOutputSelection::GeneratedSources) => {
config.generated_sources = true; config.generated_sources = true;
} }
EvmOutputSelection::ByteCode(BytecodeOutputSelection::Object) => {
config.bytecode = true;
}
EvmOutputSelection::ByteCode(BytecodeOutputSelection::SourceMap) => { EvmOutputSelection::ByteCode(BytecodeOutputSelection::SourceMap) => {
config.source_map = true; config.source_map = true;
} }
EvmOutputSelection::DeployedByteCode(DeployedBytecodeOutputSelection::All) |
EvmOutputSelection::DeployedByteCode(
DeployedBytecodeOutputSelection::Object,
) => {
config.deployed_bytecode = true;
}
_ => {} _ => {}
}, },
ContractOutputSelection::Ewasm(_) => { ContractOutputSelection::Ewasm(_) => {
@ -656,6 +671,21 @@ impl ExtraOutputFiles {
} }
} }
if self.bytecode {
if let Some(ref code) = contract.get_bytecode_bytes() {
let code = hex::encode(code.as_ref());
let file = file.with_extension("bin");
fs::write(&file, code).map_err(|err| SolcError::io(err, file))?
}
}
if self.deployed_bytecode {
if let Some(ref code) = contract.get_deployed_bytecode_bytes() {
let code = hex::encode(code.as_ref());
let file = file.with_extension("deployed-bin");
fs::write(&file, code).map_err(|err| SolcError::io(err, file))?
}
}
Ok(()) Ok(())
} }
} }

View File

@ -407,7 +407,7 @@ impl FromStr for BytecodeOutputSelection {
match s { match s {
"evm.bytecode" => Ok(BytecodeOutputSelection::All), "evm.bytecode" => Ok(BytecodeOutputSelection::All),
"evm.bytecode.functionDebugData" => Ok(BytecodeOutputSelection::FunctionDebugData), "evm.bytecode.functionDebugData" => Ok(BytecodeOutputSelection::FunctionDebugData),
"evm.bytecode.object" => Ok(BytecodeOutputSelection::Object), "code" | "bin" | "evm.bytecode.object" => Ok(BytecodeOutputSelection::Object),
"evm.bytecode.opcodes" => Ok(BytecodeOutputSelection::Opcodes), "evm.bytecode.opcodes" => Ok(BytecodeOutputSelection::Opcodes),
"evm.bytecode.sourceMap" => Ok(BytecodeOutputSelection::SourceMap), "evm.bytecode.sourceMap" => Ok(BytecodeOutputSelection::SourceMap),
"evm.bytecode.linkReferences" => Ok(BytecodeOutputSelection::LinkReferences), "evm.bytecode.linkReferences" => Ok(BytecodeOutputSelection::LinkReferences),
@ -482,6 +482,10 @@ impl FromStr for DeployedBytecodeOutputSelection {
"evm.deployedBytecode.functionDebugData" => { "evm.deployedBytecode.functionDebugData" => {
Ok(DeployedBytecodeOutputSelection::FunctionDebugData) Ok(DeployedBytecodeOutputSelection::FunctionDebugData)
} }
"deployed-code" |
"deployed-bin" |
"runtime-code" |
"runtime-bin" |
"evm.deployedBytecode.object" => Ok(DeployedBytecodeOutputSelection::Object), "evm.deployedBytecode.object" => Ok(DeployedBytecodeOutputSelection::Object),
"evm.deployedBytecode.opcodes" => Ok(DeployedBytecodeOutputSelection::Opcodes), "evm.deployedBytecode.opcodes" => Ok(DeployedBytecodeOutputSelection::Opcodes),
"evm.deployedBytecode.sourceMap" => Ok(DeployedBytecodeOutputSelection::SourceMap), "evm.deployedBytecode.sourceMap" => Ok(DeployedBytecodeOutputSelection::SourceMap),