cargo-build-bpf: Create a random -keypair.json file alongside the program deploy artifact for easy upgrades (bp #14162) (#14164)

* Use AsRef

(cherry picked from commit 9993d2c623)

* Create a random `-keypair.json` file alongside the program deploy artifact for easy upgrades

(cherry picked from commit 636a455790)

* Update Cargo.toml

Co-authored-by: Michael Vines <mvines@gmail.com>
This commit is contained in:
mergify[bot]
2020-12-16 21:21:22 +00:00
committed by GitHub
parent cb145b9dd5
commit 238b596773
4 changed files with 34 additions and 17 deletions

1
Cargo.lock generated
View File

@ -3792,6 +3792,7 @@ version = "1.4.18"
dependencies = [
"cargo_metadata",
"clap",
"solana-sdk",
]
[[package]]

View File

@ -12,6 +12,7 @@ publish = false
[dependencies]
clap = "2.33.3"
cargo_metadata = "0.12.0"
solana-sdk = { path = "..", version = "1.4.18" }
[features]
program = []

View File

@ -1,13 +1,16 @@
use clap::{
crate_description, crate_name, crate_version, value_t, value_t_or_exit, values_t, App, Arg,
};
use std::{
env,
ffi::OsStr,
fs,
path::{Path, PathBuf},
process::exit,
process::Command,
use {
clap::{
crate_description, crate_name, crate_version, value_t, value_t_or_exit, values_t, App, Arg,
},
solana_sdk::signature::{write_keypair_file, Keypair},
std::{
env,
ffi::OsStr,
fs,
path::{Path, PathBuf},
process::exit,
process::Command,
},
};
struct Config {
@ -166,6 +169,7 @@ fn build_bpf_package(
let program_unstripped_so = target_build_directory.join(&format!("{}.so", program_name));
let program_dump = bpf_out_dir.join(&format!("{}-dump.txt", program_name));
let program_so = bpf_out_dir.join(&format!("{}.so", program_name));
let program_keypair = bpf_out_dir.join(&format!("{}-keypair.json", program_name));
fn file_older_or_missing(prerequisite_file: &Path, target_file: &Path) -> bool {
let prerequisite_metadata = fs::metadata(prerequisite_file).unwrap_or_else(|err| {
@ -186,6 +190,17 @@ fn build_bpf_package(
}
}
if !program_keypair.exists() {
write_keypair_file(&Keypair::new(), &program_keypair).unwrap_or_else(|err| {
eprintln!(
"Unable to get create {}: {}",
program_keypair.display(),
err
);
exit(1);
});
}
if file_older_or_missing(&program_unstripped_so, &program_so) {
spawn(
&config.bpf_sdk.join("scripts/strip.sh"),

View File

@ -333,9 +333,8 @@ pub fn read_keypair<R: Read>(reader: &mut R) -> Result<Keypair, Box<dyn error::E
Ok(Keypair(dalek_keypair))
}
pub fn read_keypair_file(path: &str) -> Result<Keypair, Box<dyn error::Error>> {
assert!(path != "-");
let mut file = File::open(path.to_string())?;
pub fn read_keypair_file<F: AsRef<Path>>(path: F) -> Result<Keypair, Box<dyn error::Error>> {
let mut file = File::open(path.as_ref())?;
read_keypair(&mut file)
}
@ -349,12 +348,13 @@ pub fn write_keypair<W: Write>(
Ok(serialized)
}
pub fn write_keypair_file(
pub fn write_keypair_file<F: AsRef<Path>>(
keypair: &Keypair,
outfile: &str,
outfile: F,
) -> Result<String, Box<dyn error::Error>> {
assert!(outfile != "-");
if let Some(outdir) = Path::new(outfile).parent() {
let outfile = outfile.as_ref();
if let Some(outdir) = outfile.parent() {
fs::create_dir_all(outdir)?;
}