Skip to content

Commit

Permalink
feat: support new deal status (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
justprosh authored Mar 7, 2024
1 parent ab9c97b commit d95fe9d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/aqua/chain/deal_status.aqua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ aqua DealStatus declares *
import Worker from "@fluencelabs/aqua-lib/workers.aqua"

import JoinedDeal from "../decider/deal_storage.aqua"
import DEAL_STATUS_ENDED, DEAL_STATUS_ACTIVE, DEAL_STATUS_INACTIVE from "../decider/consts.aqua"
import DEAL_STATUS_ENDED, DEAL_STATUS_ACTIVE, DEAL_STATUS_INSUFFICIENT_FUNDS, DEAL_STATUS_NOT_ENOUGH_WORKERS from "../decider/consts.aqua"
import spell_log, deal_log from "../fluence/spell.aqua"
import SpellId, DealId from "../types.aqua"
import remove_ended_deal from "../decider/deal_removed.aqua"
Expand Down Expand Up @@ -41,10 +41,10 @@ func poll_deal_statuses_by_ids(spell_id: SpellId, api_endpoint: string, deal_ids
if deal_status.status == DEAL_STATUS_ACTIVE:
activate_deal(spell_id, deal_status.deal_id)
else:
if deal_status.status == DEAL_STATUS_INACTIVE:
if deal_status.status == DEAL_STATUS_NOT_ENOUGH_WORKERS:
deactivate_deal(spell_id, deal_status.deal_id)
else:
if deal_status.status == DEAL_STATUS_ENDED:
if deal_status.status == DEAL_STATUS_ENDED || deal_status.status == DEAL_STATUS_INSUFFICIENT_FUNDS:
remove_ended_deal(spell_id, deal_status.deal_id)
else:
spell_log(spell_id, ["unsupported deal status: ", deal_status])
Expand Down
3 changes: 2 additions & 1 deletion src/aqua/decider/consts.aqua
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const SYNC_INFO = "sync_info"
-- Deal statuses
const DEAL_STATUS_ENDED = "ENDED"
const DEAL_STATUS_ACTIVE = "ACTIVE"
const DEAL_STATUS_INACTIVE = "INACTIVE"
const DEAL_STATUS_INSUFFICIENT_FUNDS = "INSUFFICIENT_FUNDS"
const DEAL_STATUS_NOT_ENOUGH_WORKERS = "NOT_ENOUGH_WORKERS"

-- Keys prefixes
func removed_state_key(deal_id: string) -> string:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::Display;
use crate::chain::chain_data::{parse_chain_data, ChainDataError};
use crate::curl::send_jsonrpc_batch;
use crate::jsonrpc::deal_status::DealStatusError::UnknownStatus;
Expand All @@ -23,29 +24,33 @@ pub enum DealStatusError {

#[derive(Debug)]
pub enum DealStatus {
INACTIVE = 0,
ACTIVE,
ENDED,
InsufficientFunds = 0,
Active,
Ended,
NotEnoughWorkers
}

impl DealStatus {
fn from(num: u8) -> Option<Self> {
match num {
0 => Some(DealStatus::INACTIVE),
1 => Some(DealStatus::ACTIVE),
2 => Some(DealStatus::ENDED),
0 => Some(DealStatus::InsufficientFunds),
1 => Some(DealStatus::Active),
2 => Some(DealStatus::Ended),
3 => Some(DealStatus::NotEnoughWorkers),
_ => None,
}
}
}

impl ToString for DealStatus {
fn to_string(&self) -> String {
match self {
DealStatus::INACTIVE => "INACTIVE".to_string(),
DealStatus::ACTIVE => "ACTIVE".to_string(),
DealStatus::ENDED => "ENDED".to_string(),
}
impl Display for DealStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let str = match self {
DealStatus::InsufficientFunds => "INSUFFICIENT_FUNDS".to_string(),
DealStatus::Active => "ACTIVE".to_string(),
DealStatus::Ended => "ENDED".to_string(),
DealStatus::NotEnoughWorkers => "NOT_ENOUGH_WORKERS".to_string(),
};
write!(f, "{}", str)
}
}

Expand Down Expand Up @@ -202,7 +207,7 @@ mod tests {
const DEAL_ID: &'static str = "0x6328bb918a01603adc91eae689b848a9ecaef26d";
const DEAL_ID_2: &'static str = "0x6328bb918a01603adc91eae689b848a9ecaef26f";

let jsonrpc_inactive = r#"[{"jsonrpc":"2.0","id":0,"result":"0x0000000000000000000000000000000000000000000000000000000000000000"}]"#;
let jsonrpc_INSUFFICIENT_FUNDS = r#"[{"jsonrpc":"2.0","id":0,"result":"0x0000000000000000000000000000000000000000000000000000000000000000"}]"#;
let jsonrpc_active = r#"[{"jsonrpc":"2.0","id":0,"result":"0x0000000000000000000000000000000000000000000000000000000000000001"}]"#;
let jsonrpc_ended = r#"[{"jsonrpc":"2.0","id":0,"result":"0x0000000000000000000000000000000000000000000000000000000000000002"}]"#;
let jsonrpc_unknown = r#"[{"jsonrpc":"2.0","id":0,"result":"0x"}]"#;
Expand All @@ -216,7 +221,7 @@ mod tests {
jsonrpc_unknown,
jsonrpc_ended,
jsonrpc_active,
jsonrpc_inactive,
jsonrpc_INSUFFICIENT_FUNDS,
]));
let mock = server
.mock("POST", "/")
Expand Down Expand Up @@ -264,7 +269,7 @@ mod tests {
);
assert!(result.statuses[0].error.is_empty());

assert_eq!(result.statuses[0].status, "INACTIVE");
assert_eq!(result.statuses[0].status, "INSUFFICIENT_FUNDS");
assert_eq!(result.statuses[0].deal_id, DEAL_ID);

let result = connector.get_status_batch(url.clone(), vec![DEAL_ID.to_string()]);
Expand Down Expand Up @@ -315,7 +320,7 @@ mod tests {

assert!(result.statuses[0].error.is_empty());
assert_eq!(result.statuses[0].deal_id, DEAL_ID);
assert_eq!(result.statuses[0].status, "INACTIVE");
assert_eq!(result.statuses[0].status, "INSUFFICIENT_FUNDS");

assert!(!result.statuses[1].success,);
assert!(!result.statuses[1].error.is_empty());
Expand Down
8 changes: 4 additions & 4 deletions src/tests/decider-distro-tests-rs/tests/test_activate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::utils::chain::{DealStatusReq, LogsReq};
use crate::utils::control::{
update_config, update_decider_script_for_tests, wait_decider_stopped, wait_worker_spell_stopped,
};
use crate::utils::default::{default_receipt, DEAL_IDS, DEAL_STATUS_ACTIVE, DEAL_STATUS_INACTIVE};
use crate::utils::default::{default_receipt, DEAL_IDS, DEAL_STATUS_ACTIVE, DEAL_STATUS_NOT_ENOUGH_WORKERS};
use crate::utils::distro::make_distro_with_api;
use crate::utils::oneshot_config;
use crate::utils::setup::{setup_nox, setup_rpc_empty_run_with_status};
Expand All @@ -20,7 +20,7 @@ pub mod utils;

/// Test plan:
/// - Use standard nox setup.
/// - On the first run, deploy a deal to create a worker. The deal is INACTIVE.
/// - On the first run, deploy a deal to create a worker. The deal is NOT_ENOUGH_WORKERS.
/// - On the second run, the deal is ACTIVE, so decider should activate the worker.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_activate() {
Expand Down Expand Up @@ -63,7 +63,7 @@ async fn test_activate() {
"eth_call" => {
let req = serde_json::from_value::<DealStatusReq>(params[0].clone()).unwrap();
assert_eq!(req.to, deal_address, "request the status of the wrong deal");
json!(DEAL_STATUS_INACTIVE)
json!(DEAL_STATUS_NOT_ENOUGH_WORKERS)
}
_ => panic!("mock http got an unexpected rpc method: {}", method),
};
Expand Down Expand Up @@ -109,7 +109,7 @@ async fn test_activate() {
setup_rpc_empty_run_with_status(
&mut server,
LATEST_BLOCK + 10,
DEAL_STATUS_INACTIVE,
DEAL_STATUS_NOT_ENOUGH_WORKERS,
joined.len(),
)
.await
Expand Down
4 changes: 2 additions & 2 deletions src/tests/decider-distro-tests-rs/tests/utils/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub fn default_receipt() -> Value {

pub const DEAL_STATUS_ACTIVE: &str =
"0x0000000000000000000000000000000000000000000000000000000000000001";
pub const DEAL_STATUS_INACTIVE: &str =
"0x0000000000000000000000000000000000000000000000000000000000000000";
pub const DEAL_STATUS_NOT_ENOUGH_WORKERS: &str =
"0x0000000000000000000000000000000000000000000000000000000000000003";
pub const DEAL_STATUS_ENDED: &str =
"0x0000000000000000000000000000000000000000000000000000000000000002";
pub fn default_status() -> Value {
Expand Down

0 comments on commit d95fe9d

Please sign in to comment.