Skip to content

Commit

Permalink
add last_job_id to free_accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
simke9445 committed Nov 7, 2023
1 parent 6ac2f51 commit c7a165b
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 15 deletions.
3 changes: 3 additions & 0 deletions contracts/warp-controller/src/execute/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ pub fn delete_job(
config.job_account_tracker_address.to_string(),
job.owner.to_string(),
job_account_addr.to_string(),
job.id,
));
}

Expand Down Expand Up @@ -460,6 +461,7 @@ pub fn execute_job(
config.job_account_tracker_address.to_string(),
job.owner.to_string(),
job_account_addr.to_string(),
job.id,
));
}

Expand Down Expand Up @@ -553,6 +555,7 @@ pub fn evict_job(
config.job_account_tracker_address.to_string(),
job.owner.to_string(),
job_account_addr.to_string(),
job.id,
));
}
}
Expand Down
2 changes: 2 additions & 0 deletions contracts/warp-controller/src/util/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ pub fn build_free_account_msg(
job_account_tracker_addr: String,
account_owner_addr: String,
account_addr: String,
last_job_id: Uint64,
) -> CosmosMsg {
CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: job_account_tracker_addr,
msg: to_binary(&job_account_tracker::ExecuteMsg::FreeAccount(
FreeAccountMsg {
account_owner_addr,
account_addr,
last_job_id,
},
))
.unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion contracts/warp-job-account-tracker/src/execute/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn free_account(deps: DepsMut, data: FreeAccountMsg) -> Result<Response, Con
(account_owner_ref, account_addr_ref),
|s| match s {
// value is a dummy data because there is no built in support for set in cosmwasm
None => Ok(true),
None => Ok(data.last_job_id),
Some(_) => Err(ContractError::AccountAlreadyFreeError {}),
},
)?;
Expand Down
23 changes: 14 additions & 9 deletions contracts/warp-job-account-tracker/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ mod tests {
&ExecuteMsg::FreeAccount(FreeAccountMsg {
account_owner_addr: USER_1.to_string(),
account_addr: DUMMY_WARP_ACCOUNT_1_ADDR.to_string(),
last_job_id: DUMMY_JOB_1_ID,
}),
&[],
);
Expand All @@ -150,6 +151,7 @@ mod tests {
&ExecuteMsg::FreeAccount(FreeAccountMsg {
account_owner_addr: USER_1.to_string(),
account_addr: DUMMY_WARP_ACCOUNT_1_ADDR.to_string(),
last_job_id: DUMMY_JOB_1_ID,
}),
&[],
),
Expand All @@ -163,6 +165,7 @@ mod tests {
&ExecuteMsg::FreeAccount(FreeAccountMsg {
account_owner_addr: USER_1.to_string(),
account_addr: DUMMY_WARP_ACCOUNT_2_ADDR.to_string(),
last_job_id: DUMMY_JOB_2_ID,
}),
&[],
);
Expand All @@ -174,6 +177,7 @@ mod tests {
&ExecuteMsg::FreeAccount(FreeAccountMsg {
account_owner_addr: USER_1.to_string(),
account_addr: DUMMY_WARP_ACCOUNT_3_ADDR.to_string(),
last_job_id: DUMMY_JOB_1_ID,
}),
&[],
);
Expand All @@ -189,7 +193,7 @@ mod tests {
Ok(FirstFreeAccountResponse {
account: Some(Account {
addr: Addr::unchecked(DUMMY_WARP_ACCOUNT_1_ADDR),
taken_by_job_id: None
taken_by_job_id: Some(DUMMY_JOB_1_ID)
})
})
);
Expand All @@ -208,15 +212,15 @@ mod tests {
accounts: vec![
Account {
addr: Addr::unchecked(DUMMY_WARP_ACCOUNT_3_ADDR),
taken_by_job_id: None
taken_by_job_id: Some(DUMMY_JOB_1_ID)
},
Account {
addr: Addr::unchecked(DUMMY_WARP_ACCOUNT_2_ADDR),
taken_by_job_id: None
taken_by_job_id: Some(DUMMY_JOB_2_ID)
},
Account {
addr: Addr::unchecked(DUMMY_WARP_ACCOUNT_1_ADDR),
taken_by_job_id: None
taken_by_job_id: Some(DUMMY_JOB_1_ID)
}
],
total_count: 3
Expand Down Expand Up @@ -280,11 +284,11 @@ mod tests {
accounts: vec![
Account {
addr: Addr::unchecked(DUMMY_WARP_ACCOUNT_3_ADDR),
taken_by_job_id: None
taken_by_job_id: Some(DUMMY_JOB_1_ID)
},
Account {
addr: Addr::unchecked(DUMMY_WARP_ACCOUNT_1_ADDR),
taken_by_job_id: None
taken_by_job_id: Some(DUMMY_JOB_1_ID)
}
],
total_count: 2
Expand Down Expand Up @@ -317,6 +321,7 @@ mod tests {
&ExecuteMsg::FreeAccount(FreeAccountMsg {
account_owner_addr: USER_1.to_string(),
account_addr: DUMMY_WARP_ACCOUNT_2_ADDR.to_string(),
last_job_id: DUMMY_JOB_1_ID,
}),
&[],
);
Expand All @@ -335,15 +340,15 @@ mod tests {
accounts: vec![
Account {
addr: Addr::unchecked(DUMMY_WARP_ACCOUNT_3_ADDR),
taken_by_job_id: None
taken_by_job_id: Some(DUMMY_JOB_1_ID)
},
Account {
addr: Addr::unchecked(DUMMY_WARP_ACCOUNT_2_ADDR),
taken_by_job_id: None
taken_by_job_id: Some(DUMMY_JOB_1_ID)
},
Account {
addr: Addr::unchecked(DUMMY_WARP_ACCOUNT_1_ADDR),
taken_by_job_id: None
taken_by_job_id: Some(DUMMY_JOB_1_ID)
}
],
total_count: 3
Expand Down
8 changes: 4 additions & 4 deletions contracts/warp-job-account-tracker/src/query/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ pub fn query_first_free_account(
)
.next();
let free_account = match maybe_free_account {
Some(Ok((account, _))) => Some(Account {
Some(Ok((account, last_job_id))) => Some(Account {
addr: account.1,
taken_by_job_id: None,
taken_by_job_id: Some(last_job_id),
}),
_ => None,
};
Expand Down Expand Up @@ -105,9 +105,9 @@ pub fn query_free_accounts(deps: Deps, data: QueryFreeAccountsMsg) -> StdResult<
let accounts = iter
.take(data.limit.unwrap_or(QUERY_LIMIT) as usize)
.map(|item| {
item.map(|(account, _)| Account {
item.map(|(account, last_job_id)| Account {
addr: account.1,
taken_by_job_id: None,
taken_by_job_id: Some(last_job_id),
})
})
.collect::<StdResult<Vec<Account>>>()?;
Expand Down
2 changes: 1 addition & 1 deletion contracts/warp-job-account-tracker/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ pub const CONFIG: Item<Config> = Item::new("config");
pub const TAKEN_ACCOUNTS: Map<(&Addr, &Addr), Uint64> = Map::new("taken_accounts");

// Key is the (account owner address, account address), value is id of the last job which reserved it
pub const FREE_ACCOUNTS: Map<(&Addr, &Addr), bool> = Map::new("free_accounts");
pub const FREE_ACCOUNTS: Map<(&Addr, &Addr), Uint64> = Map::new("free_accounts");
1 change: 1 addition & 0 deletions packages/job-account-tracker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct TakeAccountMsg {
pub struct FreeAccountMsg {
pub account_owner_addr: String,
pub account_addr: String,
pub last_job_id: Uint64,
}

#[derive(QueryResponses)]
Expand Down

0 comments on commit c7a165b

Please sign in to comment.