Skip to content

Commit

Permalink
Add update deployment (#4) (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucca authored Oct 6, 2023
1 parent e5a7336 commit bc79e83
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 7 deletions.
45 changes: 44 additions & 1 deletion src/api/contract_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use crate::utils::compilation_queue::CompilationRequest;
use crate::utils::sanity_check::check_address_len;
use crate::{
models::{
api_models::{DeployMessage, GetDeploymentsMessage, ServerResponse, WizardMessage},
api_models::{
DeployMessage, GetDeploymentsMessage, ServerResponse, UpdateDeployMessage,
WizardMessage,
},
db_models::{Contract, Deployment},
},
repository::mongodb_repo::MongoRepo,
Expand Down Expand Up @@ -154,6 +157,46 @@ pub fn store_deployment(
}
}

#[patch("/deployments", data = "<update_deploy_message>")]
pub fn update_deployment(
db: &State<MongoRepo>,
update_deploy_message: Json<UpdateDeployMessage>,
) -> Result<Json<ServerResponse<String>>, Custom<Json<ServerResponse<String>>>> {
// Check the address is valid
if check_address_len(&update_deploy_message.user_address).is_err()
|| check_address_len(&update_deploy_message.contract_address).is_err()
{
return Err(Custom(
Status::InternalServerError,
Json(ServerResponse::new_error(String::from(
"Invalid address length",
))),
));
}

// Updating the deployment in db
let deployment_update_result = db.update_deployment(&update_deploy_message);
info!(target: "compiler", "Updating deployment {} for user {} in network {}", &update_deploy_message.contract_address, &update_deploy_message.user_address, &update_deploy_message.network);

// Evaluate the result of the update operation
match deployment_update_result {
Ok(_) => {
info!(target: "compiler", "Deployment {} updated in the database", &update_deploy_message.contract_address);
Ok(Json(ServerResponse::new_valid(String::from("ok"))))
}

Err(_) => {
error!(target: "compiler", "There was an error updating the deployment {}", &update_deploy_message.contract_address);
Err(Custom(
Status::InternalServerError,
Json(ServerResponse::new_error(String::from(
"Error updating deployment.",
))),
))
}
}
}

// /deployments endpoint for fetching a deployment
#[get("/deployments?<user_address>&<network>&<contract_address>")]
pub fn get_contract_deployments(
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern crate rocket;

use api::contract_api::{
fetch_or_compile_contract, get_contract, get_contract_deployments, store_deployment,
update_deployment,
};
use repository::mongodb_repo::MongoRepo;
use rocket::fairing::AdHoc;
Expand All @@ -22,7 +23,6 @@ use utils::compilation_queue::CompilationQueue;
use utils::compiler::Compiler;

use log::{debug, error, info};
use log4rs;

use utils::cors::CORS;

Expand Down Expand Up @@ -70,6 +70,7 @@ fn rocket() -> _ {
routes![
fetch_or_compile_contract,
store_deployment,
update_deployment,
get_contract_deployments,
get_contract
],
Expand Down
9 changes: 9 additions & 0 deletions src/models/api_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ pub struct DeployMessage {
pub external_abi: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct UpdateDeployMessage {
pub contract_address: String,
pub network: String,
pub user_address: String,
pub contract_name: Option<String>,
pub hidden: bool,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct GetDeploymentsMessage {
pub user_address: String,
Expand Down
2 changes: 2 additions & 0 deletions src/models/db_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct Deployment {
pub date: String,
pub contract_type: String,
pub external_abi: Option<String>,
pub hidden: bool,
}

impl Deployment {
Expand All @@ -42,6 +43,7 @@ impl Deployment {
date: deploy_message.date.clone(),
contract_type: deploy_message.contract_type.clone(),
external_abi: deploy_message.external_abi.clone(),
hidden: false,
}
}
}
14 changes: 13 additions & 1 deletion src/repository/mongodb_repo.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::env;

use crate::models::api_models::GetDeploymentsMessage;
use crate::models::api_models::{GetDeploymentsMessage, UpdateDeployMessage};
use crate::models::db_models::{Contract, Deployment};
use mongodb::results::UpdateResult;
use mongodb::{
bson::doc,
results::InsertOneResult,
Expand Down Expand Up @@ -87,6 +88,17 @@ impl MongoRepo {
Ok(deployment)
}

// Update a deployment in the database
pub fn update_deployment(
&self,
update_deployment: &UpdateDeployMessage,
) -> Result<UpdateResult, Box<dyn std::error::Error>> {
let filter = doc! {"contract_address": &update_deployment.contract_address, "network": &update_deployment.network, "user_address": &update_deployment.user_address};
let deployment = self
.deployments.update_one(filter, doc! {"$set": {"contract_name": &update_deployment.contract_name, "hidden": &update_deployment.hidden}}, None)?;
Ok(deployment)
}

// Fetch stored deployments from the db
pub fn get_deployments(
&self,
Expand Down
6 changes: 3 additions & 3 deletions src/tests/main_get_deployments_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ mod get_deployments_test {
assert!(response
.into_string()
.unwrap()
.contains("{\"contract_name\":null,\"contract_address\":\"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutnn\",\"network\":\"some_network\",\"code_id\":\"some_impossible_id\",\"user_address\":\"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY\",\"tx_hash\":null,\"date\":\"2021-03-03T15:00:00.000Z\",\"contract_type\":\"custom\",\"external_abi\":null}"));
.contains("{\"contract_name\":null,\"contract_address\":\"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutnn\",\"network\":\"some_network\",\"code_id\":\"some_impossible_id\",\"user_address\":\"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY\",\"tx_hash\":null,\"date\":\"2021-03-03T15:00:00.000Z\",\"contract_type\":\"custom\",\"external_abi\":null,\"hidden\":false}"));
let db_res = db.deployments.delete_one(
doc! {"contract_address": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutnn","user_address": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"},
doc! {"contract_address": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutnn","network": "some_network", "user_address": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"},
None,
);
assert!(db_res.is_ok());
//client.terminate();
client.terminate();
}
}
26 changes: 25 additions & 1 deletion src/tests/main_post_deployments_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,35 @@ mod post_deployments_test {
assert_eq!(response.status(), Status::Ok);

let db_res = db.deployments.delete_one(
doc! {"contract_address": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY","user_address": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"},
doc! {"contract_address": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY","network": "some_network", "user_address": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"},
None,
);
assert!(db_res.is_ok());
std::mem::drop(response);
client.terminate();
}

#[test]
fn patch_deployments_update_is_ok() {
let client = Client::tracked(rocket()).expect("valid rocket instance");
let db = client.rocket().state::<MongoRepo>().unwrap();

let response = client.post(uri!("/deployments")).body(r#"{ "contract_address": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", "network": "some_network", "code_id": "some_id", "user_address": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", "date":"2021-03-03T15:00:00.000Z", "contract_type":"custom" }"#).dispatch();
// status ok means that the deployment was stored in the database
assert_eq!(response.status(), Status::Ok);
std::mem::drop(response);

let response = client.patch(uri!("/deployments")).body(r#"{ "contract_address": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", "network": "some_network", "user_address": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", "contract_name":"name", "hidden": true}"#).dispatch();
// status ok means that the deployment was updated in the database
assert_eq!(response.status(), Status::Ok);
std::mem::drop(response);

// Cleanup
let db_res = db.deployments.delete_one(
doc! {"contract_address": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY","network": "some_network", "user_address": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"},
None,
);
assert!(db_res.is_ok());
client.terminate();
}
}

0 comments on commit bc79e83

Please sign in to comment.