Skip to content

Commit

Permalink
chore(leaser): addressed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Gancho Manev authored and Gancho Manev committed May 19, 2023
1 parent a114bb4 commit c35630a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 28 deletions.
6 changes: 3 additions & 3 deletions contracts/leaser/src/leaser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
cmd::Quote,
error::ContractError,
migrate::{self},
msg::{ConfigResponse, NbInstances, QuoteResponse},
msg::{ConfigResponse, MaxLeases, QuoteResponse},
result::ContractResult,
state::{config::Config, leases::Leases},
};
Expand Down Expand Up @@ -92,7 +92,7 @@ pub(super) fn try_configure(
pub(super) fn try_migrate_leases(
storage: &mut dyn Storage,
new_code_id: u64,
max_leases: NbInstances,
max_leases: MaxLeases,
) -> ContractResult<MessageResponse> {
Config::update_lease_code(storage, new_code_id)?;

Expand All @@ -107,7 +107,7 @@ pub(super) fn try_migrate_leases(
pub(super) fn try_migrate_leases_cont(
storage: &mut dyn Storage,
next_customer: Addr,
max_leases: NbInstances,
max_leases: MaxLeases,
) -> ContractResult<MessageResponse> {
let lease_code_id = Config::load(storage)?.lease_code_id;

Expand Down
41 changes: 23 additions & 18 deletions contracts/leaser/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use lease::api::MigrateMsg;
use platform::batch::Batch;
use sdk::cosmwasm_std::Addr;

use crate::{msg::NbInstances, result::ContractResult};
use crate::{msg::MaxLeases, result::ContractResult};

pub struct Customer<LeaseIter> {
customer: Addr,
Expand All @@ -16,21 +16,23 @@ pub struct MigrationResult {
pub next_customer: Option<Addr>,
}

pub type MayCustomer<LI> = ContractResult<Customer<LI>>;

/// Builds a batch of messages for the migration of up to `max_leases`
///
/// Consumes the customers iterator to the next customer or error.
pub fn migrate_leases<I, LI>(
mut customers: I,
lease_code_id: u64,
max_leases: NbInstances,
max_leases: MaxLeases,
) -> ContractResult<MigrationResult>
where
I: Iterator<Item = ContractResult<Customer<LI>>>,
I: Iterator<Item = MayCustomer<LI>>,
LI: ExactSizeIterator<Item = Addr>,
{
let mut msgs = MigrateBatch::new(lease_code_id, max_leases);
customers
.find_map(|may_customer| match may_customer {
.find_map(|maybe_customer| match maybe_customer {
Ok(customer) => msgs.migrate_or_be_next(customer),
Err(err) => Some(Err(err)),
})
Expand Down Expand Up @@ -61,11 +63,11 @@ impl MigrationResult {

struct MigrateBatch {
new_code_id: u64,
leases_left: NbInstances,
leases_left: MaxLeases,
msgs: Batch,
}
impl MigrateBatch {
fn new(new_code_id: u64, max_leases: NbInstances) -> Self {
fn new(new_code_id: u64, max_leases: MaxLeases) -> Self {
Self {
new_code_id,
leases_left: max_leases,
Expand All @@ -78,8 +80,8 @@ impl MigrateBatch {
where
LI: ExactSizeIterator<Item = Addr>,
{
let may_leases_nb: Result<NbInstances, _> = customer.leases.len().try_into();
match may_leases_nb {
let maybe_leases_nb: Result<MaxLeases, _> = customer.leases.len().try_into();
match maybe_leases_nb {
Err(err) => Some(Err(err.into())),
Ok(leases_nb) => {
if leases_nb <= self.leases_left {
Expand Down Expand Up @@ -133,11 +135,11 @@ mod test {
#[test]
fn no_leases() {
use std::array::IntoIter;
let new_code = 242;
let new_code: u64 = 242;
let no_leases: Vec<Customer<IntoIter<Addr, 0>>> = vec![];
assert_eq!(
Ok(MigrationResult::default()),
super::migrate_leases(no_leases.into_iter().map(Ok), 2, new_code)
super::migrate_leases(no_leases.into_iter().map(Ok), new_code, 2)
);
}

Expand Down Expand Up @@ -286,21 +288,24 @@ mod test {

fn test_customers() -> impl Iterator<Item = ContractResult<Customer<IntoIter<Addr>>>> {
let lease1 = Addr::unchecked(LEASE1);
let customer_addr1 = Addr::unchecked(CUSTOMER_ADDR1);
let cust1 = Customer::from(customer_addr1, vec![lease1].into_iter());

let lease21 = Addr::unchecked(LEASE21);
let lease22 = Addr::unchecked(LEASE22);
let customer_addr2 = Addr::unchecked(CUSTOMER_ADDR2);
let cust2 = Customer::from(customer_addr2, vec![lease21, lease22].into_iter());

let lease3 = Addr::unchecked(LEASE3);
let customer_addr3 = Addr::unchecked(CUSTOMER_ADDR3);
let cust3 = Customer::from(customer_addr3, vec![lease3].into_iter());

let lease41 = Addr::unchecked(LEASE41);
let lease42 = Addr::unchecked(LEASE42);
let lease43 = Addr::unchecked(LEASE43);
let customer_addr1 = Addr::unchecked(CUSTOMER_ADDR1);
let customer_addr2 = Addr::unchecked(CUSTOMER_ADDR2);
let customer_addr3 = Addr::unchecked(CUSTOMER_ADDR3);
let customer_addr4 = Addr::unchecked(CUSTOMER_ADDR4);
let cust1 = Customer::from(customer_addr1, vec![lease1].into_iter());
let cust2 = Customer::from(customer_addr2, vec![lease21, lease22].into_iter());
let cust3 = Customer::from(customer_addr3, vec![lease3].into_iter());
let cust4 = Customer::from(customer_addr4, vec![lease41, lease42, lease43].into_iter());

let cust4 = Customer::from(customer_addr4, vec![lease41, lease42, lease43].into_iter());

vec![Ok(cust1), Ok(cust2), Ok(cust3), Ok(cust4)].into_iter()
}
}
6 changes: 3 additions & 3 deletions contracts/leaser/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct InstantiateMsg {
#[derive(Serialize, Deserialize)]
pub struct MigrateMsg {}

pub type NbInstances = u32;
pub type MaxLeases = u32;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
Expand All @@ -47,13 +47,13 @@ pub enum ExecuteMsg {
/// no key is provided and 'wasm-migrate-leases.status=done'.
MigrateLeases {
new_code_id: Uint64,
max_leases: NbInstances,
max_leases: MaxLeases,
},
/// Continue a Lease migration
///
/// It migrates the next batch of up to `max_leases` number of Lease instances
/// and emits the status as specified in `MigrateLeases`.
MigrateLeasesCont { key: Addr, max_leases: NbInstances },
MigrateLeasesCont { key: Addr, max_leases: MaxLeases },
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
Expand Down
12 changes: 8 additions & 4 deletions contracts/leaser/src/state/leases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use sdk::{
cw_storage_plus::{Bound, Item, Map},
};

use crate::{error::ContractError, migrate::Customer, result::ContractResult};
use crate::{
error::ContractError,
migrate::{Customer, MayCustomer},
};

const IDS: InstantiateReplyIdSeq<'static> = InstantiateReplyIdSeq::new("instantiate_reply_ids");
const PENDING: Map<'static, InstantiateReplyId, Addr> = Map::new("pending_instance_creations");
Expand Down Expand Up @@ -77,13 +80,14 @@ impl Leases {
pub fn iter(
storage: &dyn Storage,
next_customer: Option<Addr>,
) -> impl Iterator<Item = ContractResult<Customer<IntoIter<Addr>>>> + '_ {
) -> impl Iterator<Item = MayCustomer<IntoIter<Addr>>> + '_ {
let start_bound = next_customer.map(Bound::<Addr>::inclusive);
Self::STORAGE
.prefix(())
.range(storage, start_bound, None, cosmwasm_std::Order::Ascending)
.map(|e| {
e.map(|(customer, leases)| Customer::from(customer, leases.into_iter()))
.map(|record| {
record
.map(|(customer, leases)| Customer::from(customer, leases.into_iter()))
.map_err(Into::into)
})
}
Expand Down

0 comments on commit c35630a

Please sign in to comment.