Skip to content

Commit

Permalink
Handle cargo registry index lookup requests
Browse files Browse the repository at this point in the history
  • Loading branch information
pgarg66 committed Oct 13, 2023
1 parent 47540af commit 290eca2
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 105 deletions.
82 changes: 1 addition & 81 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ gag = "1.0.0"
generic-array = { version = "0.14.7", default-features = false }
gethostname = "0.2.3"
getrandom = "0.2.10"
git2 = "0.18.1"
goauth = "0.13.1"
hex = "0.4.3"
hidapi = { version = "2.4.1", default-features = false }
Expand All @@ -216,7 +215,6 @@ http = "0.2.9"
humantime = "2.0.1"
hyper = "0.14.27"
hyper-proxy = "0.9.1"
hyper-staticfile = "0.9.5"
im = "15.1.0"
index_list = "0.2.7"
indexmap = "2.0.2"
Expand Down
3 changes: 1 addition & 2 deletions cargo-registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ edition = { workspace = true }
[dependencies]
clap = { workspace = true }
flate2 = { workspace = true }
git2 = { workspace = true }
hyper = { workspace = true, features = ["full"] }
hyper-staticfile = { workspace = true }
log = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sha2 = { workspace = true }
solana-clap-utils = { workspace = true }
solana-cli = { workspace = true }
solana-cli-config = { workspace = true }
Expand Down
20 changes: 13 additions & 7 deletions cargo-registry/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use {
crate::{
client::Client,
publisher::{Error, Publisher},
sparse_index::RegistryIndex,
},
hyper::{
body,
Expand Down Expand Up @@ -30,15 +31,17 @@ impl CargoRegistryService {
async fn handle_publish_request(
request: hyper::Request<hyper::Body>,
client: Arc<Client>,
index: Arc<RegistryIndex>,
) -> hyper::Response<hyper::Body> {
info!("Handling request to publish the crate");
let bytes = body::to_bytes(request.into_body()).await;

match bytes {
Ok(data) => {
let Ok(result) =
tokio::task::spawn_blocking(move || Publisher::publish_crate(data, client))
.await
let Ok(result) = tokio::task::spawn_blocking(move || {
Publisher::publish_crate(data, client, index)
})
.await
else {
return response_builder::error_response(
hyper::StatusCode::INTERNAL_SERVER_ERROR,
Expand Down Expand Up @@ -218,7 +221,7 @@ impl CargoRegistryService {
}

async fn handler(
index: sparse_index::RegistryIndex,
index: Arc<sparse_index::RegistryIndex>,
request: hyper::Request<hyper::Body>,
client: Arc<Client>,
) -> Result<hyper::Response<hyper::Body>, Error> {
Expand Down Expand Up @@ -257,7 +260,7 @@ impl CargoRegistryService {
"Invalid length of the request.",
)
} else {
Self::handle_publish_request(request, client.clone()).await
Self::handle_publish_request(request, client.clone(), index.clone()).await
}
}
"unyank" => Self::handle_unyank_request(path, &request),
Expand Down Expand Up @@ -297,7 +300,10 @@ async fn main() {
let client = Arc::new(Client::new().expect("Failed to get RPC Client instance"));

let bind_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), client.port);
let index = sparse_index::RegistryIndex::new("/index", &client.server_url);
let index = Arc::new(sparse_index::RegistryIndex::new(
"/index",
&client.server_url,
));

let registry_service = make_service_fn(move |_| {
let client_inner = client.clone();
Expand All @@ -310,7 +316,7 @@ async fn main() {
});

let server = Server::bind(&bind_addr).serve(registry_service);
info!("Server running on on http://{}", bind_addr);
info!("Server running on http://{}", bind_addr);

let _ = server.await;
}
49 changes: 46 additions & 3 deletions cargo-registry/src/publisher.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use {
crate::client::{Client, ClientConfig},
crate::{
client::{Client, ClientConfig},
sparse_index::{IndexEntry, IndexEntryDep, RegistryIndex},
},
flate2::read::GzDecoder,
hyper::body::Bytes,
log::*,
serde::{Deserialize, Serialize},
serde_json::from_slice,
sha2::{Digest, Sha256},
solana_cli::program_v4::{process_deploy_program, read_and_verify_elf},
solana_sdk::{
signature::{Keypair, Signer},
Expand Down Expand Up @@ -46,6 +50,22 @@ struct Dependency {
explicit_name_in_toml: Option<String>,
}

impl Into<IndexEntryDep> for Dependency {
fn into(self) -> IndexEntryDep {
IndexEntryDep {
name: self.name,
req: self.version_req,
features: self.features,
optional: self.optional,
default_features: self.default_features,
target: self.target,
kind: serde_json::to_string(&self.kind).expect("Failed to stringify dep kind"),
registry: self.registry,
package: None,
}
}
}

#[derive(Debug, Deserialize)]
#[allow(unused)]
struct PackageMetaData {
Expand All @@ -69,6 +89,20 @@ struct PackageMetaData {
rust_version: Option<String>,
}

impl Into<IndexEntry> for PackageMetaData {
fn into(self) -> IndexEntry {
IndexEntry {
name: self.name,
vers: self.vers,
deps: self.deps.into_iter().map(|v| v.into()).collect(),
cksum: String::new(),
features: self.features,
yanked: false,
links: self.links,
}
}
}

impl PackageMetaData {
fn new(bytes: &Bytes) -> serde_json::Result<(PackageMetaData, usize)> {
let (json_length, sizeof_length) = Self::read_u32_length(bytes)?;
Expand All @@ -86,7 +120,7 @@ impl PackageMetaData {
}
}

pub struct Publisher {}
pub(crate) struct Publisher {}

impl Publisher {
fn make_path<P: AsRef<Path>>(tempdir: &TempDir, meta: &PackageMetaData, append: P) -> PathBuf {
Expand All @@ -107,12 +141,17 @@ impl Publisher {
Ok(library_name.to_string())
}

pub(crate) fn publish_crate(bytes: Bytes, client: Arc<Client>) -> Result<(), Error> {
pub(crate) fn publish_crate(
bytes: Bytes,
client: Arc<Client>,
index: Arc<RegistryIndex>,
) -> Result<(), Error> {
let (meta_data, offset) = PackageMetaData::new(&bytes)?;

let (_crate_file_length, length_size) =
PackageMetaData::read_u32_length(&bytes.slice(offset..))?;
let crate_bytes = bytes.slice(offset.saturating_add(length_size)..);
let crate_cksum = format!("{:x}", Sha256::digest(&crate_bytes));

let decoder = GzDecoder::new(crate_bytes.as_ref());
let mut archive = Archive::new(decoder);
Expand Down Expand Up @@ -154,6 +193,10 @@ impl Publisher {
format!("Failed to deploy the program: {}", e)
})?;

let mut entry: IndexEntry = meta_data.into();
entry.cksum = crate_cksum;
index.insert_entry(entry)?;

info!("Successfully deployed the program");
Ok(())
}
Expand Down
Loading

0 comments on commit 290eca2

Please sign in to comment.