Skip to content

Commit efb1177

Browse files
Developed get_afloat_asset_id method in the functions module of the afloat pallet, and updated all clients using it. Added AfloatAssetNotSet error to the afloat pallet. Developed TestExternalitiesBuilder in the mock module of the afloat pallet to enable the configuration of the test setup, and updated all tests to use it. Developed test in afloat pallet to verify that an error is returned when calling an extrinsic and the afloat asset is not set. Developed set_xpub method of the bitcoin vaults pallet to ensure that the xpub is valid. Developed test in bitcoin vaults pallet to assert that an error is returned when an invalid xpub is set.
1 parent 198569a commit efb1177

File tree

6 files changed

+106
-20
lines changed

6 files changed

+106
-20
lines changed

pallets/afloat/src/functions.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,19 @@ use sp_runtime::{
1717
};
1818

1919
impl<T: Config> Pallet<T> {
20+
fn get_afloat_asset_id() -> Result<T::AssetId, Error<T>> {
21+
let asset_id = AfloatAssetId::<T>::get();
22+
match asset_id {
23+
Some(id) => Ok(id),
24+
None => Err(Error::<T>::AfloatAssetNotSet),
25+
}
26+
}
2027
// ! Setup pallet
2128
pub fn do_create_afloat_marketplace(
2229
creator: T::AccountId,
2330
admin: T::AccountId,
2431
) -> DispatchResult {
25-
let asset_id = AfloatAssetId::<T>::get().expect("AfloatAssetId should be set");
32+
let asset_id = Self::get_afloat_asset_id()?;
2633

2734
let label: BoundedVec<u8, T::LabelMaxLen> =
2835
BoundedVec::try_from(b"Afloat".to_vec()).map_err(|_| Error::<T>::LabelTooLong)?;
@@ -301,10 +308,10 @@ impl<T: Config> Pallet<T> {
301308
amount: T::Balance,
302309
) -> DispatchResult {
303310
let authority = ensure_signed(origin.clone())?;
304-
let asset_id = AfloatAssetId::<T>::get().expect("AfloatAssetId should be set");
311+
let asset_id = Self::get_afloat_asset_id()?;
305312
ensure!(UserInfo::<T>::contains_key(user_address.clone()), Error::<T>::UserNotFound);
306313

307-
let current_balance = Self::do_get_afloat_balance(user_address.clone());
314+
let current_balance = Self::do_get_afloat_balance(user_address.clone())?;
308315
if current_balance > amount {
309316
let diff = current_balance - amount;
310317
pallet_mapped_assets::Pallet::<T>::afloat_do_burn(
@@ -327,9 +334,9 @@ impl<T: Config> Pallet<T> {
327334
Ok(())
328335
}
329336

330-
pub fn do_get_afloat_balance(user_address: T::AccountId) -> T::Balance {
331-
let asset_id = AfloatAssetId::<T>::get().expect("AfloatAssetId should be set");
332-
pallet_mapped_assets::Pallet::<T>::balance(asset_id.into(), user_address)
337+
pub fn do_get_afloat_balance(user_address: T::AccountId) -> Result<T::Balance, Error<T>> {
338+
let asset_id = Self::get_afloat_asset_id()?;
339+
Ok(pallet_mapped_assets::Pallet::<T>::balance(asset_id.into(), user_address))
333340
}
334341

335342
pub fn do_create_sell_order(
@@ -468,7 +475,7 @@ impl<T: Config> Pallet<T> {
468475
);
469476
//ensure user has enough afloat balance
470477
ensure!(
471-
Self::do_get_afloat_balance(who.clone()) >=
478+
Self::do_get_afloat_balance(who.clone())? >=
472479
offer.price_per_credit * tax_credit_amount.into(),
473480
Error::<T>::NotEnoughAfloatBalanceAvailable
474481
);

pallets/afloat/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ pub mod pallet {
148148
Underflow,
149149
// Afloat marketplace label too long
150150
LabelTooLong,
151+
// Afloat asset has not been set
152+
AfloatAssetNotSet,
151153
}
152154

153155
#[pallet::storage]

pallets/afloat/src/mock.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,56 @@ impl pallet_mapped_assets::Config for Test {
239239
type BenchmarkHelper = ();
240240
}
241241

242+
pub struct TestExternalitiesBuilder {
243+
test_externalities: sp_io::TestExternalities,
244+
}
245+
246+
impl TestExternalitiesBuilder {
247+
pub fn new() -> Self {
248+
TestExternalitiesBuilder {
249+
test_externalities: frame_system::GenesisConfig::<Test>::default()
250+
.build_storage()
251+
.unwrap()
252+
.into(),
253+
}
254+
}
255+
256+
pub fn setup_balances(mut self) -> Self {
257+
self.test_externalities.execute_with(|| {
258+
Balances::make_free_balance_be(&1, 100);
259+
Balances::make_free_balance_be(&2, 100);
260+
});
261+
self
262+
}
263+
264+
pub fn initialize_all(self) -> Self {
265+
let mut builder = self.setup_balances();
266+
builder.test_externalities.execute_with(|| {
267+
let args = InitialSetupArgs::All {
268+
creator: 1,
269+
admin: 2,
270+
asset: CreateAsset::New { asset_id: 0, min_balance: 1 },
271+
};
272+
Afloat::initial_setup(RawOrigin::Root.into(), args)
273+
.expect("Error configuring initial setup");
274+
});
275+
builder
276+
}
277+
278+
pub fn initialize_roles(self) -> Self {
279+
let mut builder = self.setup_balances();
280+
builder.test_externalities.execute_with(|| {
281+
let args = InitialSetupArgs::Roles { creator: 1, admin: 2 };
282+
Afloat::initial_setup(RawOrigin::Root.into(), args).expect("Error configuring roles");
283+
});
284+
builder
285+
}
286+
287+
pub fn build(self) -> sp_io::TestExternalities {
288+
return self.test_externalities
289+
}
290+
}
291+
242292
// Build genesis storage according to the mock runtime.
243293
pub fn new_test_ext() -> sp_io::TestExternalities {
244294
// TODO: get initial conf?

pallets/afloat/src/tests.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn dummy_description() -> BoundedVec<u8, StringLimit> {
1818

1919
#[test]
2020
fn sign_up_works() {
21-
new_test_ext().execute_with(|| {
21+
TestExternalitiesBuilder::new().initialize_all().build().execute_with(|| {
2222
let user = new_account(3);
2323
Balances::make_free_balance_be(&user, 100);
2424
let args = SignUpArgs::BuyerOrSeller {
@@ -35,7 +35,7 @@ fn sign_up_works() {
3535

3636
#[test]
3737
fn update_user_info_edit_works() {
38-
new_test_ext().execute_with(|| {
38+
TestExternalitiesBuilder::new().initialize_all().build().execute_with(|| {
3939
let user = new_account(3);
4040
Balances::make_free_balance_be(&user, 100);
4141

@@ -66,7 +66,7 @@ fn update_user_info_edit_works() {
6666

6767
#[test]
6868
fn update_other_user_info_by_not_admin_fails() {
69-
new_test_ext().execute_with(|| {
69+
TestExternalitiesBuilder::new().initialize_all().build().execute_with(|| {
7070
let user = new_account(3);
7171
let other_user = new_account(4);
7272

@@ -99,7 +99,7 @@ fn update_other_user_info_by_not_admin_fails() {
9999

100100
#[test]
101101
fn update_other_user_info_by_admin_works() {
102-
new_test_ext().execute_with(|| {
102+
TestExternalitiesBuilder::new().initialize_all().build().execute_with(|| {
103103
let owner = new_account(1);
104104
let admin = new_account(2);
105105
let user = new_account(3);
@@ -137,7 +137,7 @@ fn update_other_user_info_by_admin_works() {
137137

138138
#[test]
139139
fn update_user_info_delete_works() {
140-
new_test_ext().execute_with(|| {
140+
TestExternalitiesBuilder::new().initialize_all().build().execute_with(|| {
141141
let user = new_account(3);
142142
Balances::make_free_balance_be(&user, 100);
143143

@@ -161,7 +161,7 @@ fn update_user_info_delete_works() {
161161

162162
#[test]
163163
fn set_afloat_balance_works() {
164-
new_test_ext().execute_with(|| {
164+
TestExternalitiesBuilder::new().initialize_all().build().execute_with(|| {
165165
let user = new_account(3);
166166
let other_user = new_account(4);
167167

@@ -177,15 +177,15 @@ fn set_afloat_balance_works() {
177177
assert_ok!(Afloat::sign_up(RawOrigin::Signed(user.clone()).into(), args.clone()));
178178

179179
assert_ok!(Afloat::set_afloat_balance(RawOrigin::Signed(1).into(), user.clone(), 10000));
180-
assert_eq!(Afloat::do_get_afloat_balance(user.clone()), 10000);
180+
assert_eq!(Afloat::do_get_afloat_balance(user.clone()).unwrap(), 10000);
181181
assert_ok!(Afloat::set_afloat_balance(RawOrigin::Signed(1).into(), user.clone(), 1000));
182-
assert_eq!(Afloat::do_get_afloat_balance(user.clone()), 1000);
182+
assert_eq!(Afloat::do_get_afloat_balance(user.clone()).unwrap(), 1000);
183183
});
184184
}
185185

186186
#[test]
187187
fn set_balance_by_other_than_owner_fails() {
188-
new_test_ext().execute_with(|| {
188+
TestExternalitiesBuilder::new().initialize_all().build().execute_with(|| {
189189
let user = new_account(3);
190190
let other_user = new_account(4);
191191

@@ -211,9 +211,20 @@ fn set_balance_by_other_than_owner_fails() {
211211
});
212212
}
213213

214+
#[test]
215+
fn set_balance_without_initializing_afloat_asset_fails() {
216+
TestExternalitiesBuilder::new().initialize_roles().build().execute_with(|| {
217+
let other_user = new_account(4);
218+
assert_noop!(
219+
Afloat::set_afloat_balance(RawOrigin::Signed(1).into(), other_user.clone(), 10000),
220+
Error::<Test>::AfloatAssetNotSet
221+
);
222+
});
223+
}
224+
214225
#[test]
215226
fn create_tax_credit_works() {
216-
new_test_ext().execute_with(|| {
227+
TestExternalitiesBuilder::new().initialize_all().build().execute_with(|| {
217228
let user = new_account(3);
218229
let other_user = new_account(4);
219230

@@ -240,7 +251,7 @@ fn create_tax_credit_works() {
240251

241252
#[test]
242253
fn create_sell_order_works() {
243-
new_test_ext().execute_with(|| {
254+
TestExternalitiesBuilder::new().initialize_all().build().execute_with(|| {
244255
let user = new_account(3);
245256
let other_user = new_account(4);
246257
let item_id = 0;
@@ -277,7 +288,7 @@ fn create_sell_order_works() {
277288

278289
#[test]
279290
fn create_buy_order_works() {
280-
new_test_ext().execute_with(|| {
291+
TestExternalitiesBuilder::new().initialize_all().build().execute_with(|| {
281292
let user = new_account(3);
282293
let other_user = new_account(4);
283294
let item_id = 0;
@@ -317,7 +328,7 @@ fn create_buy_order_works() {
317328

318329
// #[test]
319330
// fn take_buy_order_works() {
320-
// new_test_ext().execute_with(|| {
331+
// TestExternalitiesBuilder::new().initialize_all().build().execute_with(|| {
321332
// let user = new_account(3);
322333
// let other_user = new_account(4);
323334
// let item_id = 0;

pallets/bitcoin-vaults/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ pub mod pallet {
184184
ProofNotFound,
185185
/// The proposal/proof of reserve was already previously broadcasted
186186
AlreadyBroadcasted,
187+
/// The provided xpub is invalid
188+
InvalidXpub,
187189
}
188190

189191
/* --- Onchain storage section --- */
@@ -352,6 +354,7 @@ pub mod pallet {
352354
// Check that the extrinsic was signed and get the signer.
353355
let who = ensure_signed(origin.clone())?;
354356
ensure!(xpub.len() > 0, Error::<T>::NoneValue);
357+
ensure!(str::from_utf8(&xpub).is_ok(), Error::<T>::InvalidXpub);
355358
ensure!(!<XpubsByOwner<T>>::contains_key(who.clone()), Error::<T>::UserAlreadyHasXpub);
356359
let manual_hash = xpub.clone().using_encoded(blake2_256);
357360
// Assert if the input xpub is free to take (or if the user owns it)

pallets/bitcoin-vaults/src/tests.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ fn inserting_same_xpub_should_fail() {
9696
});
9797
}
9898

99+
#[test]
100+
fn inserting_invalid_xpub_should_fail() {
101+
new_test_ext().execute_with(|| {
102+
assert_noop!(
103+
BitcoinVaults::set_xpub(
104+
RuntimeOrigin::signed(test_pub(2)),
105+
BoundedVec::try_from(vec![192, 175]).unwrap()
106+
),
107+
Error::<Test>::InvalidXpub
108+
);
109+
});
110+
}
111+
99112
#[test]
100113
fn inserting_without_removing_xpub_should_fail() {
101114
new_test_ext().execute_with(|| {

0 commit comments

Comments
 (0)