123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616 |
- mod fixtures;
- mod hiring_workflow;
- mod mock;
- pub use mock::{build_test_externalities, Test, DEFAULT_WORKER_ACCOUNT_ID};
- use frame_system::RawOrigin;
- use crate::tests::fixtures::{
- CancelOpeningFixture, DecreaseWorkerStakeFixture, IncreaseWorkerStakeFixture, SetBudgetFixture,
- SetStatusTextFixture, SlashWorkerStakeFixture, SpendFromBudgetFixture,
- UpdateRewardAccountFixture, UpdateRewardAmountFixture, UpdateWorkerStorageFixture,
- WithdrawApplicationFixture,
- };
- use crate::tests::hiring_workflow::HiringWorkflow;
- use crate::tests::mock::{
- STAKING_ACCOUNT_ID_FOR_CONFLICTING_STAKES, STAKING_ACCOUNT_ID_NOT_BOUND_TO_MEMBER,
- };
- use crate::types::StakeParameters;
- use crate::{
- default_storage_size_constraint, DefaultInstance, Error, OpeningType, RawEvent,
- RewardPaymentType, StakePolicy, Trait, Worker,
- };
- use common::working_group::WorkingGroupAuthenticator;
- use fixtures::{
- increase_total_balance_issuance_using_account_id, AddOpeningFixture, ApplyOnOpeningFixture,
- EventFixture, FillOpeningFixture, HireLeadFixture, HireRegularWorkerFixture,
- LeaveWorkerRoleFixture, TerminateWorkerRoleFixture, UpdateWorkerRoleAccountFixture,
- };
- use frame_support::dispatch::DispatchError;
- use frame_support::StorageMap;
- use mock::{run_to_block, Balances, RewardPeriod, TestWorkingGroup, ACTOR_ORIGIN_ERROR};
- use sp_runtime::traits::Hash;
- use sp_std::collections::btree_map::BTreeMap;
- #[test]
- fn add_opening_succeeded() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let starting_block = 1;
- run_to_block(starting_block);
- let add_opening_fixture = AddOpeningFixture::default()
- .with_starting_block(starting_block)
- .with_stake_policy(StakePolicy {
- stake_amount: <Test as Trait>::MinimumApplicationStake::get(),
- leaving_unstaking_period: 100,
- })
- .with_reward_per_block(Some(10));
- let opening_id = add_opening_fixture.call_and_assert(Ok(()));
- EventFixture::assert_last_crate_event(RawEvent::OpeningAdded(
- opening_id,
- add_opening_fixture.description,
- add_opening_fixture.opening_type,
- add_opening_fixture.stake_policy,
- add_opening_fixture.reward_per_block,
- ));
- });
- }
- #[test]
- fn add_opening_fails_with_bad_origin() {
- build_test_externalities().execute_with(|| {
- let add_opening_fixture = AddOpeningFixture::default()
- .with_opening_type(OpeningType::Leader)
- .with_origin(RawOrigin::None);
- add_opening_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn add_opening_fails_with_less_than_minimum_stake() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let add_opening_fixture = AddOpeningFixture::default().with_stake_policy(StakePolicy {
- stake_amount: 0,
- leaving_unstaking_period: <Test as Trait>::MinUnstakingPeriodLimit::get(),
- });
- add_opening_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::BelowMinimumStakes.into(),
- ));
- let add_opening_fixture = AddOpeningFixture::default().with_stake_policy(StakePolicy {
- stake_amount: <Test as Trait>::MinimumApplicationStake::get() - 1,
- leaving_unstaking_period: <Test as Trait>::MinUnstakingPeriodLimit::get(),
- });
- add_opening_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::BelowMinimumStakes.into(),
- ));
- });
- }
- #[test]
- fn add_opening_fails_with_zero_reward() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let add_opening_fixture = AddOpeningFixture::default().with_reward_per_block(Some(0));
- add_opening_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::CannotRewardWithZero.into(),
- ));
- });
- }
- #[test]
- fn add_opening_fails_with_insufficient_balance() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default()
- .with_initial_balance(<Test as Trait>::MinimumApplicationStake::get())
- .hire_lead();
- let add_opening_fixture = AddOpeningFixture::default().with_stake_policy(StakePolicy {
- stake_amount: <Test as Trait>::MinimumApplicationStake::get(),
- leaving_unstaking_period: <Test as Trait>::MinUnstakingPeriodLimit::get() + 1,
- });
- add_opening_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::InsufficientBalanceToCoverStake.into(),
- ));
- });
- }
- #[test]
- fn add_opening_fails_with_incorrect_unstaking_period() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let account_id = 1;
- let total_balance = 300;
- let stake = 200;
- increase_total_balance_issuance_using_account_id(account_id, total_balance);
- let invalid_unstaking_period = 3;
- let add_opening_fixture = AddOpeningFixture::default().with_stake_policy(StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: invalid_unstaking_period,
- });
- add_opening_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::UnstakingPeriodLessThanMinimum.into(),
- ));
- });
- }
- #[test]
- fn add_leader_opening_fails_with_incorrect_origin_for_opening_type() {
- build_test_externalities().execute_with(|| {
- let add_opening_fixture =
- AddOpeningFixture::default().with_opening_type(OpeningType::Leader);
- add_opening_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn apply_on_opening_succeeded() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let starting_block = 1;
- run_to_block(starting_block);
- let add_opening_fixture = AddOpeningFixture::default().with_starting_block(starting_block);
- let opening_id = add_opening_fixture.call().unwrap();
- let apply_on_opening_fixture = ApplyOnOpeningFixture::default_for_opening_id(opening_id)
- .with_initial_balance(<Test as Trait>::MinimumApplicationStake::get());
- let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
- EventFixture::assert_last_crate_event(RawEvent::AppliedOnOpening(
- apply_on_opening_fixture.get_apply_on_opening_parameters(),
- application_id,
- ));
- });
- }
- #[test]
- fn apply_on_opening_fails_with_invalid_opening_id() {
- build_test_externalities().execute_with(|| {
- let invalid_opening_id = 22;
- let apply_on_opening_fixture =
- ApplyOnOpeningFixture::default_for_opening_id(invalid_opening_id);
- apply_on_opening_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::OpeningDoesNotExist.into(),
- ));
- });
- }
- #[test]
- fn apply_on_opening_fails_with_bad_origin() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let member_id = 11;
- let add_opening_fixture = AddOpeningFixture::default();
- let opening_id = add_opening_fixture.call().unwrap();
- let apply_on_opening_fixture = ApplyOnOpeningFixture::default_for_opening_id(opening_id)
- .with_origin(RawOrigin::None, member_id);
- apply_on_opening_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn apply_on_opening_fails_with_bad_member_id() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let member_id = 27;
- let add_opening_fixture = AddOpeningFixture::default();
- let opening_id = add_opening_fixture.call().unwrap();
- let apply_on_opening_fixture = ApplyOnOpeningFixture::default_for_opening_id(opening_id)
- .with_origin(RawOrigin::Signed(1), member_id);
- apply_on_opening_fixture
- .call_and_assert(Err(DispatchError::Other(ACTOR_ORIGIN_ERROR).into()));
- });
- }
- #[test]
- fn fill_opening_succeeded() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let starting_block = 1;
- run_to_block(starting_block);
- let reward_per_block = 10;
- let add_opening_fixture = AddOpeningFixture::default()
- .with_starting_block(starting_block)
- .with_reward_per_block(Some(reward_per_block.clone()));
- let opening_id = add_opening_fixture.call().unwrap();
- let apply_on_opening_fixture = ApplyOnOpeningFixture::default_for_opening_id(opening_id);
- let application_id = apply_on_opening_fixture.call().unwrap();
- let fill_opening_fixture =
- FillOpeningFixture::default_for_ids(opening_id, vec![application_id])
- .with_reward_per_block(Some(reward_per_block))
- .with_created_at(starting_block);
- let initial_balance = Balances::usable_balance(&1);
- let worker_id = fill_opening_fixture.call_and_assert(Ok(()));
- assert_eq!(
- Balances::usable_balance(&1),
- initial_balance + <Test as Trait>::LeaderOpeningStake::get()
- );
- let mut result_map = BTreeMap::new();
- result_map.insert(application_id, worker_id);
- EventFixture::assert_last_crate_event(RawEvent::OpeningFilled(
- opening_id,
- result_map,
- fill_opening_fixture.successful_application_ids,
- ));
- });
- }
- #[test]
- fn fill_opening_succeeded_with_stake() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let starting_block = 1;
- run_to_block(starting_block);
- let account_id = 2;
- let total_balance = 300;
- let stake = 200;
- let stake_parameters = StakeParameters {
- stake,
- staking_account_id: account_id,
- };
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- };
- let add_opening_fixture = AddOpeningFixture::default()
- .with_starting_block(starting_block)
- .with_stake_policy(stake_policy.clone());
- let opening_id = add_opening_fixture.call().unwrap();
- let apply_on_opening_fixture = ApplyOnOpeningFixture::default_for_opening_id(opening_id)
- .with_initial_balance(total_balance)
- .with_stake_parameters(stake_parameters);
- let application_id = apply_on_opening_fixture.call().unwrap();
- let fill_opening_fixture =
- FillOpeningFixture::default_for_ids(opening_id, vec![application_id])
- .with_stake_policy(stake_policy)
- .with_staking_account_id(account_id)
- .with_created_at(starting_block);
- let worker_id = fill_opening_fixture.call_and_assert(Ok(()));
- let mut result_map = BTreeMap::new();
- result_map.insert(application_id, worker_id);
- EventFixture::assert_last_crate_event(RawEvent::OpeningFilled(
- opening_id,
- result_map,
- fill_opening_fixture.successful_application_ids,
- ));
- });
- }
- #[test]
- fn fill_opening_fails_with_bad_origin() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let add_opening_fixture = AddOpeningFixture::default();
- let opening_id = add_opening_fixture.call_and_assert(Ok(()));
- let apply_on_opening_fixture = ApplyOnOpeningFixture::default_for_opening_id(opening_id);
- let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
- let fill_opening_fixture =
- FillOpeningFixture::default_for_ids(opening_id, vec![application_id])
- .with_origin(RawOrigin::None);
- fill_opening_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn fill_opening_fails_with_application_for_other_opening() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default()
- .with_initial_balance(
- <Test as Trait>::MinimumApplicationStake::get()
- + 3 * <Test as Trait>::LeaderOpeningStake::get()
- + 1,
- )
- .hire_lead();
- let add_opening_fixture = AddOpeningFixture::default();
- let filling_opening_id = add_opening_fixture.call_and_assert(Ok(()));
- let apply_opening_id = add_opening_fixture.call_and_assert(Ok(()));
- let apply_on_opening_fixture =
- ApplyOnOpeningFixture::default_for_opening_id(apply_opening_id);
- let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
- let fill_opening_fixture =
- FillOpeningFixture::default_for_ids(filling_opening_id, vec![application_id]);
- fill_opening_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::ApplicationsNotForOpening.into(),
- ));
- });
- }
- #[test]
- fn fill_opening_fails_with_invalid_active_worker_number() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let add_opening_fixture = AddOpeningFixture::default();
- let opening_id = add_opening_fixture.call().unwrap();
- let application_id1 = ApplyOnOpeningFixture::default_for_opening_id(opening_id)
- .with_origin(RawOrigin::Signed(2), 2)
- .call()
- .unwrap();
- let application_id2 = ApplyOnOpeningFixture::default_for_opening_id(opening_id)
- .with_origin(RawOrigin::Signed(3), 3)
- .call()
- .unwrap();
- let application_id3 = ApplyOnOpeningFixture::default_for_opening_id(opening_id)
- .with_origin(RawOrigin::Signed(4), 4)
- .call()
- .unwrap();
- let application_id4 = ApplyOnOpeningFixture::default_for_opening_id(opening_id)
- .with_origin(RawOrigin::Signed(5), 5)
- .call()
- .unwrap();
- let fill_opening_fixture = FillOpeningFixture::default_for_ids(
- opening_id,
- vec![
- application_id1,
- application_id2,
- application_id3,
- application_id4,
- ],
- );
- fill_opening_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::MaxActiveWorkerNumberExceeded.into(),
- ));
- });
- }
- #[test]
- fn fill_opening_fails_with_invalid_application_id() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let add_opening_fixture = AddOpeningFixture::default();
- let opening_id = add_opening_fixture.call_and_assert(Ok(()));
- let invalid_application_id = 1;
- let fill_opening_fixture =
- FillOpeningFixture::default_for_ids(opening_id, vec![invalid_application_id]);
- fill_opening_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::SuccessfulWorkerApplicationDoesNotExist.into(),
- ));
- });
- }
- #[test]
- fn fill_opening_fails_with_zero_application_ids() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let add_opening_fixture = AddOpeningFixture::default();
- let opening_id = add_opening_fixture.call_and_assert(Ok(()));
- let fill_opening_fixture = FillOpeningFixture::default_for_ids(opening_id, Vec::new());
- fill_opening_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::NoApplicationsProvided.into(),
- ));
- });
- }
- #[test]
- fn cannot_hire_a_lead_twice() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- HireLeadFixture::default()
- .with_setup_environment(false)
- .expect(Error::<Test, DefaultInstance>::ConflictStakesOnAccount.into());
- });
- }
- #[test]
- fn cannot_hire_muptiple_leaders() {
- build_test_externalities().execute_with(|| {
- HiringWorkflow::default()
- .with_setup_environment(true)
- .with_opening_type(OpeningType::Leader)
- .add_default_application()
- .add_application_full(b"leader2".to_vec(), RawOrigin::Signed(3), 3, 3)
- .expect(Err(
- Error::<Test, DefaultInstance>::CannotHireMultipleLeaders.into(),
- ))
- .execute();
- });
- }
- #[test]
- fn update_worker_role_account_succeeds() {
- build_test_externalities().execute_with(|| {
- /*
- Events are not emitted on block 0.
- So any dispatchable calls made during genesis block formation will have no events emitted.
- https://substrate.dev/recipes/2-appetizers/4-events.html
- */
- run_to_block(1);
- let new_account_id = 10;
- let worker_id = HireRegularWorkerFixture::default().hire();
- let update_worker_account_fixture =
- UpdateWorkerRoleAccountFixture::default_with_ids(worker_id, new_account_id);
- update_worker_account_fixture.call_and_assert(Ok(()));
- EventFixture::assert_last_crate_event(RawEvent::WorkerRoleAccountUpdated(
- worker_id,
- new_account_id,
- ));
- });
- }
- #[test]
- fn update_worker_role_account_by_leader_succeeds() {
- build_test_externalities().execute_with(|| {
- let new_account_id = 10;
- let worker_id = HireLeadFixture::default().hire_lead();
- let old_lead = TestWorkingGroup::worker_by_id(worker_id);
- let update_worker_account_fixture =
- UpdateWorkerRoleAccountFixture::default_with_ids(worker_id, new_account_id);
- update_worker_account_fixture.call_and_assert(Ok(()));
- let new_lead = TestWorkingGroup::worker_by_id(worker_id);
- assert_eq!(
- new_lead,
- Worker::<Test> {
- role_account_id: new_account_id,
- ..old_lead
- }
- );
- });
- }
- #[test]
- fn update_worker_role_fails_with_leaving_worker() {
- build_test_externalities().execute_with(|| {
- let total_balance = 300;
- let stake = 200;
- let leaving_unstaking_period = 10;
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period,
- };
- let worker_id = HireRegularWorkerFixture::default()
- .with_initial_balance(total_balance)
- .with_stake_policy(stake_policy.clone())
- .hire();
- let new_account_id = 10;
- let leave_worker_role_fixture = LeaveWorkerRoleFixture::default_for_worker_id(worker_id);
- leave_worker_role_fixture.call_and_assert(Ok(()));
- let update_worker_account_fixture =
- UpdateWorkerRoleAccountFixture::default_with_ids(worker_id, new_account_id);
- update_worker_account_fixture
- .call_and_assert(Err(Error::<Test, DefaultInstance>::WorkerIsLeaving.into()));
- });
- }
- #[test]
- fn update_worker_role_account_fails_with_invalid_origin() {
- build_test_externalities().execute_with(|| {
- let worker_id = HireRegularWorkerFixture::default().hire();
- let update_worker_account_fixture =
- UpdateWorkerRoleAccountFixture::default_with_ids(worker_id, 1)
- .with_origin(RawOrigin::None);
- update_worker_account_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn leave_worker_role_succeeds() {
- build_test_externalities().execute_with(|| {
- /*
- Events are not emitted on block 0.
- So any dispatchable calls made during genesis block formation will have no events emitted.
- https://substrate.dev/recipes/2-appetizers/4-events.html
- */
- run_to_block(1);
- let worker_id = HireRegularWorkerFixture::default().hire();
- let leave_worker_role_fixture = LeaveWorkerRoleFixture::default_for_worker_id(worker_id);
- leave_worker_role_fixture.call_and_assert(Ok(()));
- EventFixture::assert_last_crate_event(RawEvent::WorkerStartedLeaving(worker_id, None));
- let worker = TestWorkingGroup::worker_by_id(worker_id);
- run_to_block(1 + worker.job_unstaking_period);
- EventFixture::assert_last_crate_event(RawEvent::WorkerExited(worker_id));
- });
- }
- #[test]
- fn leave_worker_role_succeeds_with_paying_missed_reward() {
- build_test_externalities().execute_with(|| {
- let account_id = 2;
- let reward_per_block = 10;
- let worker_id = HireRegularWorkerFixture::default()
- .with_reward_per_block(Some(reward_per_block))
- .hire();
- let reward_period: u64 = <Test as Trait>::RewardPeriod::get().into();
- let missed_reward_block_number = reward_period * 2;
- run_to_block(missed_reward_block_number);
- assert_eq!(Balances::usable_balance(&account_id), 0);
- SetBudgetFixture::default().with_budget(1000000).execute();
- let leave_worker_role_fixture = LeaveWorkerRoleFixture::default_for_worker_id(worker_id);
- leave_worker_role_fixture.call_and_assert(Ok(()));
- let worker = TestWorkingGroup::worker_by_id(worker_id);
- let leaving_block = missed_reward_block_number + worker.job_unstaking_period;
- run_to_block(leaving_block);
- let missed_reward = missed_reward_block_number * reward_per_block;
- EventFixture::contains_crate_event(RawEvent::NewMissedRewardLevelReached(
- worker_id,
- Some(missed_reward),
- ));
- EventFixture::contains_crate_event(RawEvent::RewardPaid(
- worker_id,
- account_id,
- missed_reward,
- RewardPaymentType::MissedReward,
- ));
- // Didn't get the last reward period: leaving earlier than rewarding.
- let reward_block_count = leaving_block - reward_period;
- assert_eq!(
- Balances::usable_balance(&account_id),
- reward_block_count * reward_per_block + <Test as Trait>::MinimumApplicationStake::get()
- );
- });
- }
- #[test]
- fn leave_worker_role_succeeds_with_correct_unstaking_period() {
- build_test_externalities().execute_with(|| {
- let starting_block = 10;
- run_to_block(starting_block);
- let worker_id = HireRegularWorkerFixture::default().hire();
- // Assert initial worker existence
- assert!(<crate::WorkerById<Test, DefaultInstance>>::contains_key(
- worker_id
- ));
- let default_unstaking_period =
- TestWorkingGroup::worker_by_id(worker_id).job_unstaking_period;
- let leave_worker_role_fixture = LeaveWorkerRoleFixture::default_for_worker_id(worker_id);
- leave_worker_role_fixture.call_and_assert(Ok(()));
- // Assert worker existence after leave_role
- assert!(<crate::WorkerById<Test, DefaultInstance>>::contains_key(
- worker_id
- ));
- run_to_block(starting_block + default_unstaking_period - 1);
- // Assert worker existence one block before the end of the unstaking period.
- assert!(<crate::WorkerById<Test, DefaultInstance>>::contains_key(
- worker_id
- ));
- run_to_block(starting_block + default_unstaking_period);
- // Assert worker removal after the unstaking period.
- assert!(!<crate::WorkerById<Test, DefaultInstance>>::contains_key(
- worker_id
- ));
- });
- }
- #[test]
- fn leave_worker_role_succeeds_with_partial_payment_of_missed_reward() {
- build_test_externalities().execute_with(|| {
- let account_id = 2;
- let reward_per_block = 10;
- let worker_id = HireRegularWorkerFixture::default()
- .with_reward_per_block(Some(reward_per_block))
- .hire();
- let block_number = 4;
- run_to_block(block_number);
- assert_eq!(Balances::usable_balance(&account_id), 0);
- let budget = 30;
- SetBudgetFixture::default().with_budget(budget).execute();
- let leave_worker_role_fixture = LeaveWorkerRoleFixture::default_for_worker_id(worker_id);
- leave_worker_role_fixture.call_and_assert(Ok(()));
- let worker = TestWorkingGroup::worker_by_id(worker_id);
- run_to_block(block_number + worker.job_unstaking_period);
- assert_eq!(
- Balances::usable_balance(&account_id),
- budget + <Test as Trait>::MinimumApplicationStake::get()
- );
- });
- }
- #[test]
- fn leave_worker_role_by_leader_succeeds() {
- build_test_externalities().execute_with(|| {
- // Ensure that lead is default
- assert_eq!(TestWorkingGroup::current_lead(), None);
- let worker_id = HireLeadFixture::default().hire_lead();
- assert!(TestWorkingGroup::current_lead().is_some());
- let leave_worker_role_fixture = LeaveWorkerRoleFixture::default_for_worker_id(worker_id)
- .with_origin(RawOrigin::Signed(1));
- leave_worker_role_fixture.call_and_assert(Ok(()));
- let current_lead = TestWorkingGroup::current_lead().unwrap();
- let leader = TestWorkingGroup::worker_by_id(current_lead);
- assert!(leader.started_leaving_at.is_some());
- run_to_block(frame_system::Module::<Test>::block_number() + leader.job_unstaking_period);
- assert_eq!(TestWorkingGroup::current_lead(), None);
- });
- }
- #[test]
- fn leave_worker_role_fails_with_invalid_origin() {
- build_test_externalities().execute_with(|| {
- let leave_worker_role_fixture =
- LeaveWorkerRoleFixture::default_for_worker_id(1).with_origin(RawOrigin::None);
- leave_worker_role_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn leave_worker_role_fails_with_invalid_origin_signed_account() {
- build_test_externalities().execute_with(|| {
- let worker_id = HireRegularWorkerFixture::default().hire();
- let leave_worker_role_fixture = LeaveWorkerRoleFixture::default_for_worker_id(worker_id)
- .with_origin(RawOrigin::Signed(3));
- leave_worker_role_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::SignerIsNotWorkerRoleAccount.into(),
- ));
- });
- }
- #[test]
- fn leave_worker_role_fails_already_leaving_worker() {
- build_test_externalities().execute_with(|| {
- let total_balance = 300;
- let stake = 200;
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- };
- let worker_id = HireRegularWorkerFixture::default()
- .with_initial_balance(total_balance)
- .with_stake_policy(stake_policy.clone())
- .hire();
- let leave_worker_role_fixture = LeaveWorkerRoleFixture::default_for_worker_id(worker_id);
- leave_worker_role_fixture.call_and_assert(Ok(()));
- leave_worker_role_fixture
- .call_and_assert(Err(Error::<Test, DefaultInstance>::WorkerIsLeaving.into()));
- });
- }
- #[test]
- fn leave_worker_role_fails_with_invalid_worker_id() {
- build_test_externalities().execute_with(|| {
- let invalid_worker_id = 10;
- HireRegularWorkerFixture::default().hire();
- let leave_worker_role_fixture =
- LeaveWorkerRoleFixture::default_for_worker_id(invalid_worker_id);
- leave_worker_role_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::WorkerDoesNotExist.into(),
- ));
- });
- }
- #[test]
- fn terminate_worker_role_succeeds() {
- build_test_externalities().execute_with(|| {
- /*
- Events are not emitted on block 0.
- So any dispatchable calls made during genesis block formation will have no events emitted.
- https://substrate.dev/recipes/2-appetizers/4-events.html
- */
- run_to_block(1);
- let worker_id = HireRegularWorkerFixture::default().hire();
- let terminate_worker_role_fixture =
- TerminateWorkerRoleFixture::default_for_worker_id(worker_id);
- terminate_worker_role_fixture.call_and_assert(Ok(()));
- EventFixture::assert_last_crate_event(RawEvent::TerminatedWorker(
- worker_id,
- terminate_worker_role_fixture.penalty,
- terminate_worker_role_fixture.rationale,
- ));
- });
- }
- #[test]
- fn terminate_worker_role_succeeds_with_paying_missed_reward() {
- build_test_externalities().execute_with(|| {
- let account_id = 2;
- let reward_per_block = 10;
- let worker_id = HireRegularWorkerFixture::default()
- .with_reward_per_block(Some(reward_per_block))
- .hire();
- let block_number = 4;
- run_to_block(block_number);
- assert_eq!(Balances::usable_balance(&account_id), 0);
- SetBudgetFixture::default().with_budget(1000000).execute();
- let terminate_worker_role_fixture =
- TerminateWorkerRoleFixture::default_for_worker_id(worker_id);
- terminate_worker_role_fixture.call_and_assert(Ok(()));
- assert_eq!(
- Balances::usable_balance(&account_id),
- block_number * reward_per_block + <Test as Trait>::MinimumApplicationStake::get()
- );
- });
- }
- #[test]
- fn terminate_leader_succeeds() {
- build_test_externalities().execute_with(|| {
- /*
- Events are not emitted on block 0.
- So any dispatchable calls made during genesis block formation will have no events emitted.
- https://substrate.dev/recipes/2-appetizers/4-events.html
- */
- run_to_block(1);
- let worker_id = HireLeadFixture::default().hire_lead();
- let terminate_worker_role_fixture =
- TerminateWorkerRoleFixture::default_for_worker_id(worker_id)
- .with_origin(RawOrigin::Root);
- terminate_worker_role_fixture.call_and_assert(Ok(()));
- EventFixture::assert_last_crate_event(RawEvent::TerminatedLeader(
- worker_id,
- terminate_worker_role_fixture.penalty,
- terminate_worker_role_fixture.rationale,
- ));
- assert_eq!(TestWorkingGroup::current_lead(), None);
- });
- }
- #[test]
- fn terminate_worker_role_fails_with_unset_lead() {
- build_test_externalities().execute_with(|| {
- let worker_id = HireRegularWorkerFixture::default().hire();
- // Remove the leader from the storage.
- TestWorkingGroup::unset_lead();
- let terminate_worker_role_fixture =
- TerminateWorkerRoleFixture::default_for_worker_id(worker_id);
- terminate_worker_role_fixture
- .call_and_assert(Err(Error::<Test, DefaultInstance>::CurrentLeadNotSet.into()));
- });
- }
- #[test]
- fn terminate_worker_role_fails_with_invalid_origin() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let worker_id = HiringWorkflow::default()
- .with_setup_environment(false)
- .add_application_full(b"worker_handle".to_vec(), RawOrigin::Signed(2), 2, 2)
- .execute()
- .unwrap();
- let terminate_worker_role_fixture =
- TerminateWorkerRoleFixture::default_for_worker_id(worker_id)
- .with_origin(RawOrigin::None);
- terminate_worker_role_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn terminate_leader_fails_with_invalid_origin() {
- build_test_externalities().execute_with(|| {
- let worker_id = HireLeadFixture::default().hire_lead();
- let terminate_worker_role_fixture =
- TerminateWorkerRoleFixture::default_for_worker_id(worker_id)
- .with_origin(RawOrigin::None);
- terminate_worker_role_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn unset_lead_event_emitted() {
- build_test_externalities().execute_with(|| {
- /*
- Events are not emitted on block 0.
- So any dispatchable calls made during genesis block formation will have no events emitted.
- https://substrate.dev/recipes/2-appetizers/4-events.html
- */
- run_to_block(1);
- HireRegularWorkerFixture::default().hire();
- // Remove the leader from the storage.
- TestWorkingGroup::unset_lead();
- EventFixture::assert_last_crate_event(RawEvent::LeaderUnset());
- });
- }
- #[test]
- fn set_lead_event_emitted() {
- build_test_externalities().execute_with(|| {
- /*
- Events are not emitted on block 0.
- So any dispatchable calls made during genesis block formation will have no events emitted.
- https://substrate.dev/recipes/2-appetizers/4-events.html
- */
- run_to_block(1);
- let worker_id = 10;
- // Add the leader to the storage.
- TestWorkingGroup::set_lead(worker_id);
- EventFixture::assert_last_crate_event(RawEvent::LeaderSet(worker_id));
- });
- }
- #[test]
- fn apply_on_opening_fails_with_stake_inconsistent_with_opening_stake() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let account_id = 1;
- let stake_parameters = StakeParameters {
- stake: 100,
- staking_account_id: account_id,
- };
- let add_opening_fixture = AddOpeningFixture::default().with_stake_policy(StakePolicy {
- stake_amount: 200,
- leaving_unstaking_period: 10,
- });
- let opening_id = add_opening_fixture.call().unwrap();
- let apply_on_opening_fixture = ApplyOnOpeningFixture::default_for_opening_id(opening_id)
- .with_initial_balance(300)
- .with_stake_parameters(stake_parameters);
- apply_on_opening_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::ApplicationStakeDoesntMatchOpening.into(),
- ));
- });
- }
- #[test]
- fn apply_on_opening_locks_the_stake() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let account_id = 2;
- let total_balance = <Test as Trait>::MinimumApplicationStake::get() + 100;
- let stake = <Test as Trait>::MinimumApplicationStake::get();
- let stake_parameters = StakeParameters {
- stake,
- staking_account_id: account_id,
- };
- let add_opening_fixture = AddOpeningFixture::default().with_stake_policy(StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- });
- let opening_id = add_opening_fixture.call().unwrap();
- let apply_on_opening_fixture = ApplyOnOpeningFixture::default_for_opening_id(opening_id)
- .with_stake_parameters(stake_parameters)
- .with_initial_balance(total_balance);
- apply_on_opening_fixture.call_and_assert(Ok(()));
- assert_eq!(Balances::usable_balance(&account_id), total_balance - stake);
- });
- }
- #[test]
- fn apply_on_opening_fails_stake_amount_check() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let account_id = 2;
- let total_balance = 100;
- let stake = 200;
- let stake_parameters = StakeParameters {
- stake,
- staking_account_id: account_id,
- };
- let add_opening_fixture = AddOpeningFixture::default().with_stake_policy(StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- });
- let opening_id = add_opening_fixture.call().unwrap();
- let apply_on_opening_fixture = ApplyOnOpeningFixture::default_for_opening_id(opening_id)
- .with_initial_balance(total_balance)
- .with_stake_parameters(stake_parameters);
- apply_on_opening_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::InsufficientBalanceToCoverStake.into(),
- ));
- });
- }
- #[test]
- fn apply_on_opening_fails_invalid_staking_check() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let total_balance = 300;
- let stake = 200;
- increase_total_balance_issuance_using_account_id(
- STAKING_ACCOUNT_ID_NOT_BOUND_TO_MEMBER,
- total_balance,
- );
- let stake_parameters = StakeParameters {
- stake,
- staking_account_id: STAKING_ACCOUNT_ID_NOT_BOUND_TO_MEMBER,
- };
- let add_opening_fixture = AddOpeningFixture::default().with_stake_policy(StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- });
- let opening_id = add_opening_fixture.call().unwrap();
- let apply_on_opening_fixture = ApplyOnOpeningFixture::default_for_opening_id(opening_id)
- .with_initial_balance(total_balance)
- .with_stake_parameters(stake_parameters);
- apply_on_opening_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::InvalidStakingAccountForMember.into(),
- ));
- });
- }
- #[test]
- fn apply_on_opening_fails_with_conflicting_stakes() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let account_id = 1;
- let total_balance = 300;
- let stake = 200;
- let stake_parameters = StakeParameters {
- stake,
- staking_account_id: STAKING_ACCOUNT_ID_FOR_CONFLICTING_STAKES,
- };
- increase_total_balance_issuance_using_account_id(account_id, total_balance);
- increase_total_balance_issuance_using_account_id(
- STAKING_ACCOUNT_ID_FOR_CONFLICTING_STAKES,
- total_balance,
- );
- let add_opening_fixture = AddOpeningFixture::default().with_stake_policy(StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- });
- let opening_id = add_opening_fixture.call().unwrap();
- let apply_on_opening_fixture = ApplyOnOpeningFixture::default_for_opening_id(opening_id)
- .with_stake_parameters(stake_parameters.clone())
- .with_initial_balance(stake_parameters.stake);
- apply_on_opening_fixture.call_and_assert(Ok(()));
- let apply_on_opening_fixture = ApplyOnOpeningFixture::default_for_opening_id(opening_id)
- .with_stake_parameters(stake_parameters);
- apply_on_opening_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::ConflictStakesOnAccount.into(),
- ));
- });
- }
- #[test]
- fn terminate_worker_unlocks_the_stake() {
- build_test_externalities().execute_with(|| {
- let account_id = 2;
- let total_balance = 300;
- let stake = 200;
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- };
- let worker_id = HireRegularWorkerFixture::default()
- .with_initial_balance(total_balance)
- .with_stake_policy(stake_policy)
- .hire();
- assert_eq!(Balances::usable_balance(&account_id), total_balance - stake);
- let terminate_worker_role_fixture =
- TerminateWorkerRoleFixture::default_for_worker_id(worker_id);
- terminate_worker_role_fixture.call_and_assert(Ok(()));
- assert_eq!(Balances::usable_balance(&account_id), total_balance);
- });
- }
- #[test]
- fn leave_worker_unlocks_the_stake() {
- build_test_externalities().execute_with(|| {
- let account_id = 2;
- let total_balance = 300;
- let stake = 200;
- let leaving_unstaking_period = 10;
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period,
- };
- let worker_id = HireRegularWorkerFixture::default()
- .with_initial_balance(total_balance)
- .with_stake_policy(stake_policy.clone())
- .hire();
- assert_eq!(Balances::usable_balance(&account_id), total_balance - stake);
- let leave_worker_role_fixture = LeaveWorkerRoleFixture::default_for_worker_id(worker_id);
- leave_worker_role_fixture.call_and_assert(Ok(()));
- run_to_block(leaving_unstaking_period);
- assert_eq!(Balances::usable_balance(&account_id), total_balance);
- });
- }
- #[test]
- fn leave_worker_unlocks_the_stake_with_unstaking_period() {
- build_test_externalities().execute_with(|| {
- let account_id = 2;
- let total_balance = 300;
- let stake = 200;
- let leaving_unstaking_period = 10;
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period,
- };
- let worker_id = HireRegularWorkerFixture::default()
- .with_initial_balance(total_balance)
- .with_stake_policy(stake_policy.clone())
- .hire();
- assert_eq!(Balances::usable_balance(&account_id), total_balance - stake);
- let leave_worker_role_fixture = LeaveWorkerRoleFixture::default_for_worker_id(worker_id);
- leave_worker_role_fixture.call_and_assert(Ok(()));
- run_to_block(leaving_unstaking_period);
- assert!(!<crate::WorkerById<Test, DefaultInstance>>::contains_key(
- worker_id
- ));
- assert_eq!(Balances::usable_balance(&account_id), total_balance);
- EventFixture::assert_last_crate_event(RawEvent::WorkerExited(worker_id));
- });
- }
- #[test]
- fn terminate_worker_with_slashing_succeeds() {
- build_test_externalities().execute_with(|| {
- let account_id = 2;
- let total_balance = 300;
- let stake = 200;
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- };
- let worker_id = HireRegularWorkerFixture::default()
- .with_initial_balance(total_balance)
- .with_stake_policy(stake_policy)
- .hire();
- assert_eq!(Balances::usable_balance(&account_id), total_balance - stake);
- let terminate_worker_role_fixture =
- TerminateWorkerRoleFixture::default_for_worker_id(worker_id).with_penalty(Some(stake));
- terminate_worker_role_fixture.call_and_assert(Ok(()));
- assert_eq!(Balances::usable_balance(&account_id), total_balance - stake);
- });
- }
- #[test]
- fn slash_worker_stake_succeeds() {
- build_test_externalities().execute_with(|| {
- /*
- Events are not emitted on block 0.
- So any dispatchable calls made during genesis block formation will have no events emitted.
- https://substrate.dev/recipes/2-appetizers/4-events.html
- */
- run_to_block(1);
- let total_balance = 300;
- let stake = 200;
- let penalty = 100;
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- };
- let worker_id = HireRegularWorkerFixture::default()
- .with_initial_balance(total_balance)
- .with_stake_policy(stake_policy)
- .hire();
- let slash_stake_fixture =
- SlashWorkerStakeFixture::default_for_worker_id(worker_id).with_penalty(penalty);
- slash_stake_fixture.call_and_assert(Ok(()));
- EventFixture::assert_last_crate_event(RawEvent::StakeSlashed(
- worker_id, penalty, penalty, None,
- ));
- });
- }
- #[test]
- fn slash_leader_stake_succeeds() {
- build_test_externalities().execute_with(|| {
- /*
- Events are not emitted on block 0.
- So any dispatchable calls made during genesis block formation will have no events emitted.
- https://substrate.dev/recipes/2-appetizers/4-events.html
- */
- run_to_block(1);
- let total_balance = 300;
- let penalty = 200;
- let stake_policy = StakePolicy {
- stake_amount: penalty,
- leaving_unstaking_period: 10,
- };
- let leader_worker_id = HireLeadFixture::default()
- .with_initial_balance(total_balance)
- .with_stake_policy(stake_policy)
- .hire_lead();
- let slash_stake_fixture = SlashWorkerStakeFixture::default_for_worker_id(leader_worker_id)
- .with_penalty(penalty)
- .with_account_id(1)
- .with_origin(RawOrigin::Root);
- slash_stake_fixture.call_and_assert(Ok(()));
- EventFixture::assert_last_crate_event(RawEvent::StakeSlashed(
- leader_worker_id,
- penalty,
- penalty,
- None,
- ));
- });
- }
- #[test]
- fn slash_worker_stake_fails_with_invalid_origin() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let invalid_worker_id = 22;
- let slash_stake_fixture = SlashWorkerStakeFixture::default_for_worker_id(invalid_worker_id)
- .with_origin(RawOrigin::None);
- slash_stake_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn slash_leader_stake_fails_with_invalid_origin() {
- build_test_externalities().execute_with(|| {
- let worker_id = HireLeadFixture::default().hire_lead();
- let slash_stake_fixture =
- SlashWorkerStakeFixture::default_for_worker_id(worker_id).with_origin(RawOrigin::None);
- slash_stake_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn slash_worker_stake_fails_with_zero_balance() {
- build_test_externalities().execute_with(|| {
- let total_balance = 300;
- let stake = 200;
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- };
- let worker_id = HireRegularWorkerFixture::default()
- .with_initial_balance(total_balance)
- .with_stake_policy(stake_policy)
- .hire();
- let slash_stake_fixture =
- SlashWorkerStakeFixture::default_for_worker_id(worker_id).with_penalty(0);
- slash_stake_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::StakeBalanceCannotBeZero.into(),
- ));
- });
- }
- #[test]
- fn slash_worker_stake_fails_with_invalid_worker_id() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let invalid_worker_id = 11;
- let slash_stake_fixture = SlashWorkerStakeFixture::default_for_worker_id(invalid_worker_id);
- slash_stake_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::WorkerDoesNotExist.into(),
- ));
- });
- }
- #[test]
- fn slash_worker_stake_fails_with_not_set_lead() {
- build_test_externalities().execute_with(|| {
- let invalid_worker_id = 11;
- let slash_stake_fixture = SlashWorkerStakeFixture::default_for_worker_id(invalid_worker_id);
- slash_stake_fixture
- .call_and_assert(Err(Error::<Test, DefaultInstance>::CurrentLeadNotSet.into()));
- });
- }
- #[test]
- fn decrease_worker_stake_succeeds() {
- build_test_externalities().execute_with(|| {
- /*
- Events are not emitted on block 0.
- So any dispatchable calls made during genesis block formation will have no events emitted.
- https://substrate.dev/recipes/2-appetizers/4-events.html
- */
- run_to_block(1);
- let total_balance = 300;
- let stake = 200;
- let new_stake_balance = 100;
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- };
- let worker_id = HireRegularWorkerFixture::default()
- .with_initial_balance(total_balance)
- .with_stake_policy(stake_policy)
- .hire();
- let decrease_stake_fixture = DecreaseWorkerStakeFixture::default_for_worker_id(worker_id)
- .with_balance(new_stake_balance);
- decrease_stake_fixture.call_and_assert(Ok(()));
- EventFixture::assert_last_crate_event(RawEvent::StakeDecreased(
- worker_id,
- new_stake_balance,
- ));
- });
- }
- #[test]
- fn decrease_worker_stake_succeeds_for_leader() {
- build_test_externalities().execute_with(|| {
- let total_balance = 300;
- let stake = 200;
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- };
- let worker_id = HireLeadFixture::default()
- .with_initial_balance(total_balance)
- .with_stake_policy(stake_policy)
- .hire_lead();
- let new_stake = 100;
- let decrease_stake_fixture = DecreaseWorkerStakeFixture::default_for_worker_id(worker_id)
- .with_account_id(1)
- .with_origin(RawOrigin::Root)
- .with_balance(new_stake);
- decrease_stake_fixture.call_and_assert(Ok(()));
- });
- }
- #[test]
- fn decrease_worker_stake_fails_with_invalid_origin() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let worker_id = 22; // random worker id
- let decrease_stake_fixture = DecreaseWorkerStakeFixture::default_for_worker_id(worker_id)
- .with_origin(RawOrigin::None);
- decrease_stake_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn decrease_worker_stake_fails_with_invalid_origin_for_leader() {
- build_test_externalities().execute_with(|| {
- let worker_id = HireLeadFixture::default().hire_lead();
- let decrease_stake_fixture = DecreaseWorkerStakeFixture::default_for_worker_id(worker_id)
- .with_origin(RawOrigin::None);
- decrease_stake_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn decrease_worker_stake_fails_with_zero_balance() {
- build_test_externalities().execute_with(|| {
- let total_balance = 300;
- let stake = 200;
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- };
- let worker_id = HireRegularWorkerFixture::default()
- .with_stake_policy(stake_policy)
- .with_initial_balance(total_balance)
- .hire();
- let decrease_stake_fixture =
- DecreaseWorkerStakeFixture::default_for_worker_id(worker_id).with_balance(0);
- decrease_stake_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::StakeBalanceCannotBeZero.into(),
- ));
- });
- }
- #[test]
- fn decrease_worker_stake_fails_with_invalid_worker_id() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let invalid_worker_id = 11;
- let decrease_stake_fixture =
- DecreaseWorkerStakeFixture::default_for_worker_id(invalid_worker_id);
- decrease_stake_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::WorkerDoesNotExist.into(),
- ));
- });
- }
- #[test]
- fn decrease_worker_stake_fails_with_not_set_lead() {
- build_test_externalities().execute_with(|| {
- let invalid_worker_id = 11;
- let decrease_stake_fixture =
- DecreaseWorkerStakeFixture::default_for_worker_id(invalid_worker_id);
- decrease_stake_fixture
- .call_and_assert(Err(Error::<Test, DefaultInstance>::CurrentLeadNotSet.into()));
- });
- }
- #[test]
- fn increase_worker_stake_succeeds() {
- build_test_externalities().execute_with(|| {
- /*
- Events are not emitted on block 0.
- So any dispatchable calls made during genesis block formation will have no events emitted.
- https://substrate.dev/recipes/2-appetizers/4-events.html
- */
- run_to_block(1);
- let total_balance = 300;
- let stake = 200;
- let stake_balance_delta = 100;
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- };
- let worker_id = HireRegularWorkerFixture::default()
- .with_initial_balance(total_balance)
- .with_stake_policy(stake_policy)
- .hire();
- let increase_stake_fixture = IncreaseWorkerStakeFixture::default_for_worker_id(worker_id)
- .with_balance(stake_balance_delta);
- increase_stake_fixture.call_and_assert(Ok(()));
- EventFixture::assert_last_crate_event(RawEvent::StakeIncreased(
- worker_id,
- stake_balance_delta,
- ));
- });
- }
- #[test]
- fn increase_worker_stake_succeeds_for_leader() {
- build_test_externalities().execute_with(|| {
- let total_balance = 400;
- let stake = 200;
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- };
- let worker_id = HireLeadFixture::default()
- .with_initial_balance(total_balance)
- .with_stake_policy(stake_policy)
- .hire_lead();
- let increase_stake_fixture = IncreaseWorkerStakeFixture::default_for_worker_id(worker_id)
- .with_balance(stake)
- .with_account_id(1)
- .with_origin(RawOrigin::Signed(1));
- increase_stake_fixture.call_and_assert(Ok(()));
- });
- }
- #[test]
- fn increase_worker_stake_fails_with_invalid_origin() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let worker_id = 0;
- let increase_stake_fixture = IncreaseWorkerStakeFixture::default_for_worker_id(worker_id)
- .with_origin(RawOrigin::None);
- increase_stake_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn increase_worker_stake_fails_with_zero_balance() {
- build_test_externalities().execute_with(|| {
- let total_balance = 300;
- let stake = 200;
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- };
- let worker_id = HireRegularWorkerFixture::default()
- .with_stake_policy(stake_policy)
- .with_initial_balance(total_balance)
- .hire();
- let increase_stake_fixture =
- IncreaseWorkerStakeFixture::default_for_worker_id(worker_id).with_balance(0);
- increase_stake_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::StakeBalanceCannotBeZero.into(),
- ));
- });
- }
- #[test]
- fn increase_worker_stake_fails_with_invalid_worker_id() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let invalid_worker_id = 11;
- let increase_stake_fixture =
- IncreaseWorkerStakeFixture::default_for_worker_id(invalid_worker_id);
- increase_stake_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::WorkerDoesNotExist.into(),
- ));
- });
- }
- #[test]
- fn increase_worker_stake_fails_external_check() {
- build_test_externalities().execute_with(|| {
- let total_balance = 300;
- let stake = 200;
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- };
- let worker_id = HireRegularWorkerFixture::default()
- .with_initial_balance(total_balance)
- .with_stake_policy(stake_policy)
- .hire();
- let invalid_new_stake = 2000;
- let decrease_stake_fixture = IncreaseWorkerStakeFixture::default_for_worker_id(worker_id)
- .with_balance(invalid_new_stake);
- decrease_stake_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::InsufficientBalanceToCoverStake.into(),
- ));
- });
- }
- #[test]
- fn withdraw_application_succeeds() {
- build_test_externalities().execute_with(|| {
- /*
- Events are not emitted on block 0.
- So any dispatchable calls made during genesis block formation will have no events emitted.
- https://substrate.dev/recipes/2-appetizers/4-events.html
- */
- let starting_block = 1;
- run_to_block(starting_block);
- let account_id = 2;
- let total_balance = 300;
- let stake = 200;
- let stake_parameters = StakeParameters {
- stake,
- staking_account_id: account_id,
- };
- HireLeadFixture::default().hire_lead();
- let add_opening_fixture = AddOpeningFixture::default()
- .with_starting_block(starting_block)
- .with_stake_policy(StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- });
- let opening_id = add_opening_fixture.call_and_assert(Ok(()));
- let apply_on_opening_fixture = ApplyOnOpeningFixture::default_for_opening_id(opening_id)
- .with_initial_balance(total_balance)
- .with_stake_parameters(stake_parameters);
- let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
- let withdraw_application_fixture =
- WithdrawApplicationFixture::default_for_application_id(application_id).with_stake();
- withdraw_application_fixture.call_and_assert(Ok(()));
- EventFixture::assert_last_crate_event(RawEvent::ApplicationWithdrawn(application_id));
- });
- }
- #[test]
- fn withdraw_application_fails_invalid_application_id() {
- build_test_externalities().execute_with(|| {
- let invalid_application_id = 6;
- let withdraw_application_fixture =
- WithdrawApplicationFixture::default_for_application_id(invalid_application_id);
- withdraw_application_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::WorkerApplicationDoesNotExist.into(),
- ));
- });
- }
- #[test]
- fn withdraw_application_fails_invalid_origin() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let add_opening_fixture = AddOpeningFixture::default();
- let opening_id = add_opening_fixture.call_and_assert(Ok(()));
- let apply_on_opening_fixture = ApplyOnOpeningFixture::default_for_opening_id(opening_id);
- let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
- let withdraw_application_fixture =
- WithdrawApplicationFixture::default_for_application_id(application_id)
- .with_origin(RawOrigin::None);
- withdraw_application_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn withdraw_worker_application_fails_with_invalid_application_author() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let add_opening_fixture = AddOpeningFixture::default();
- let opening_id = add_opening_fixture.call_and_assert(Ok(()));
- let apply_on_opening_fixture = ApplyOnOpeningFixture::default_for_opening_id(opening_id)
- .with_initial_balance(<Test as Trait>::MinimumApplicationStake::get() + 1);
- let application_id = apply_on_opening_fixture.call_and_assert(Ok(()));
- let invalid_author_account_id = 55;
- let withdraw_application_fixture =
- WithdrawApplicationFixture::default_for_application_id(application_id)
- .with_signer(invalid_author_account_id);
- withdraw_application_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::OriginIsNotApplicant.into(),
- ));
- });
- }
- #[test]
- fn cancel_opening_succeeds() {
- build_test_externalities().execute_with(|| {
- /*
- Events are not emitted on block 0.
- So any dispatchable calls made during genesis block formation will have no events emitted.
- https://substrate.dev/recipes/2-appetizers/4-events.html
- */
- let starting_block = 1;
- run_to_block(starting_block);
- HireLeadFixture::default().hire_lead();
- let add_opening_fixture = AddOpeningFixture::default().with_starting_block(starting_block);
- let opening_id = add_opening_fixture.call_and_assert(Ok(()));
- let initial_balance = Balances::usable_balance(&1);
- let cancel_opening_fixture = CancelOpeningFixture::default_for_opening_id(opening_id);
- cancel_opening_fixture.call_and_assert(Ok(()));
- assert_eq!(
- Balances::usable_balance(&1),
- initial_balance + <Test as Trait>::LeaderOpeningStake::get()
- );
- EventFixture::assert_last_crate_event(RawEvent::OpeningCanceled(opening_id));
- });
- }
- #[test]
- fn cancel_opening_fails_invalid_origin() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let add_opening_fixture = AddOpeningFixture::default();
- let opening_id = add_opening_fixture.call_and_assert(Ok(()));
- let cancel_opening_fixture =
- CancelOpeningFixture::default_for_opening_id(opening_id).with_origin(RawOrigin::None);
- cancel_opening_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn cancel_opening_fails_invalid_opening_id() {
- build_test_externalities().execute_with(|| {
- let invalid_opening_id = 11;
- let cancel_opening_fixture =
- CancelOpeningFixture::default_for_opening_id(invalid_opening_id);
- cancel_opening_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::OpeningDoesNotExist.into(),
- ));
- });
- }
- #[test]
- fn decrease_worker_stake_fails_with_leaving_worker() {
- build_test_externalities().execute_with(|| {
- let total_balance = 300;
- let stake = 200;
- let new_stake_balance = 100;
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- };
- let worker_id = HireRegularWorkerFixture::default()
- .with_initial_balance(total_balance)
- .with_stake_policy(stake_policy.clone())
- .hire();
- let leave_worker_role_fixture = LeaveWorkerRoleFixture::default_for_worker_id(worker_id);
- leave_worker_role_fixture.call_and_assert(Ok(()));
- let decrease_stake_fixture = DecreaseWorkerStakeFixture::default_for_worker_id(worker_id)
- .with_balance(new_stake_balance);
- decrease_stake_fixture
- .call_and_assert(Err(Error::<Test, DefaultInstance>::WorkerIsLeaving.into()));
- });
- }
- #[test]
- fn increase_worker_stake_fails_with_leaving_worker() {
- build_test_externalities().execute_with(|| {
- let total_balance = 300;
- let stake = 200;
- let new_stake_balance = 100;
- let stake_policy = StakePolicy {
- stake_amount: stake,
- leaving_unstaking_period: 10,
- };
- let worker_id = HireRegularWorkerFixture::default()
- .with_stake_policy(stake_policy.clone())
- .with_initial_balance(total_balance)
- .hire();
- let leave_worker_role_fixture = LeaveWorkerRoleFixture::default_for_worker_id(worker_id);
- leave_worker_role_fixture.call_and_assert(Ok(()));
- let increase_stake_fixture = IncreaseWorkerStakeFixture::default_for_worker_id(worker_id)
- .with_balance(new_stake_balance);
- increase_stake_fixture
- .call_and_assert(Err(Error::<Test, DefaultInstance>::WorkerIsLeaving.into()));
- });
- }
- #[test]
- fn rewards_payments_are_successful() {
- build_test_externalities().execute_with(|| {
- let reward_per_block = 10;
- let worker_id = HireRegularWorkerFixture::default()
- .with_reward_per_block(Some(reward_per_block))
- .hire();
- let worker = TestWorkingGroup::worker_by_id(worker_id);
- let account_id = worker.role_account_id;
- SetBudgetFixture::default().execute();
- assert_eq!(Balances::usable_balance(&account_id), 0);
- let block_number = 10;
- run_to_block(block_number);
- assert_eq!(
- Balances::usable_balance(&account_id),
- block_number * reward_per_block
- );
- let reward_period: u64 = <Test as Trait>::RewardPeriod::get().into();
- EventFixture::assert_last_crate_event(RawEvent::RewardPaid(
- worker_id,
- account_id,
- reward_per_block * reward_period,
- RewardPaymentType::RegularReward,
- ));
- });
- }
- #[test]
- fn rewards_payments_with_no_budget() {
- build_test_externalities().execute_with(|| {
- let reward_per_block = 10;
- let worker_id = HireRegularWorkerFixture::default()
- .with_reward_per_block(Some(reward_per_block))
- .hire();
- let worker = TestWorkingGroup::worker_by_id(worker_id);
- let account_id = worker.role_account_id;
- assert_eq!(Balances::usable_balance(&account_id), 0);
- let block_number = 10;
- run_to_block(block_number);
- assert_eq!(Balances::usable_balance(&account_id), 0);
- let worker = TestWorkingGroup::worker_by_id(worker_id);
- assert_eq!(
- worker.missed_reward.unwrap(),
- block_number * reward_per_block
- );
- });
- }
- #[test]
- fn rewards_payments_with_insufficient_budget_and_restored_budget() {
- build_test_externalities().execute_with(|| {
- let reward_per_block = 10;
- let worker_id = HireRegularWorkerFixture::default()
- .with_reward_per_block(Some(reward_per_block))
- .hire();
- let worker = TestWorkingGroup::worker_by_id(worker_id);
- let account_id = worker.reward_account_id;
- assert_eq!(Balances::usable_balance(&account_id), 0);
- let paid_blocks = 3;
- let first_budget = paid_blocks * reward_per_block;
- SetBudgetFixture::default()
- .with_budget(first_budget)
- .execute();
- let block_number = 10;
- run_to_block(block_number);
- assert_eq!(Balances::usable_balance(&account_id), first_budget);
- let worker = TestWorkingGroup::worker_by_id(worker_id);
- let effective_missed_reward: u64 = block_number * reward_per_block - first_budget;
- assert_eq!(worker.missed_reward.unwrap(), effective_missed_reward);
- SetBudgetFixture::default().with_budget(1000000).execute();
- // Checkpoint with restored budget.
- let block_number2 = 20;
- run_to_block(block_number2);
- assert_eq!(
- Balances::usable_balance(&account_id),
- block_number2 * reward_per_block
- );
- });
- }
- #[test]
- fn rewards_payments_with_starting_block() {
- build_test_externalities().execute_with(|| {
- let starting_block = 3;
- run_to_block(starting_block);
- let reward_per_block = 10;
- let reward_period: u64 = RewardPeriod::get().into();
- let worker_id = HireRegularWorkerFixture::default()
- .with_reward_per_block(Some(reward_per_block))
- .hire();
- let worker = TestWorkingGroup::worker_by_id(worker_id);
- let account_id = worker.reward_account_id;
- SetBudgetFixture::default().with_budget(100000).execute();
- let block_number = 11;
- run_to_block(block_number);
- let effective_paid_blocks =
- (block_number - starting_block) - (block_number % reward_period);
- assert_eq!(
- Balances::usable_balance(&account_id),
- effective_paid_blocks * reward_per_block
- );
- });
- }
- #[test]
- fn set_budget_succeeded() {
- build_test_externalities().execute_with(|| {
- run_to_block(1);
- let new_budget = 10000;
- SetBudgetFixture::default()
- .with_budget(new_budget)
- .call_and_assert(Ok(()));
- EventFixture::assert_last_crate_event(RawEvent::BudgetSet(new_budget));
- });
- }
- #[test]
- fn set_budget_fails_with_bad_origin() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let leader_account_id = 1;
- SetBudgetFixture::default()
- .with_origin(RawOrigin::Signed(leader_account_id))
- .call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn update_reward_account_succeeds() {
- build_test_externalities().execute_with(|| {
- /*
- Events are not emitted on block 0.
- So any dispatchable calls made during genesis block formation will have no events emitted.
- https://substrate.dev/recipes/2-appetizers/4-events.html
- */
- run_to_block(1);
- let reward_per_block = 10;
- let worker_id = HireRegularWorkerFixture::default()
- .with_reward_per_block(Some(reward_per_block))
- .hire();
- let new_reward_account = 22;
- let update_account_fixture =
- UpdateRewardAccountFixture::default_with_ids(worker_id, new_reward_account);
- update_account_fixture.call_and_assert(Ok(()));
- EventFixture::assert_last_crate_event(RawEvent::WorkerRewardAccountUpdated(
- worker_id,
- new_reward_account,
- ));
- });
- }
- #[test]
- fn update_reward_account_succeeds_for_leader() {
- build_test_externalities().execute_with(|| {
- let reward_per_block = 10;
- let worker_id = HireLeadFixture::default()
- .with_reward_per_block(Some(reward_per_block))
- .hire_lead();
- let new_reward_account = 22;
- let update_account_fixture =
- UpdateRewardAccountFixture::default_with_ids(worker_id, new_reward_account)
- .with_origin(RawOrigin::Signed(1));
- update_account_fixture.call_and_assert(Ok(()));
- });
- }
- #[test]
- fn update_reward_account_fails_with_invalid_origin() {
- build_test_externalities().execute_with(|| {
- let update_account_fixture =
- UpdateRewardAccountFixture::default_with_ids(1, 1).with_origin(RawOrigin::None);
- update_account_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn update_reward_account_fails_with_invalid_origin_signed_account() {
- build_test_externalities().execute_with(|| {
- let reward_per_block = 10;
- let worker_id = HireLeadFixture::default()
- .with_reward_per_block(Some(reward_per_block))
- .hire_lead();
- let invalid_role_account = 23333;
- let new_reward_account = 22;
- let update_account_fixture =
- UpdateRewardAccountFixture::default_with_ids(worker_id, new_reward_account)
- .with_origin(RawOrigin::Signed(invalid_role_account));
- update_account_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::SignerIsNotWorkerRoleAccount.into(),
- ));
- });
- }
- #[test]
- fn update_reward_account_fails_with_invalid_worker_id() {
- build_test_externalities().execute_with(|| {
- let reward_per_block = 10;
- HireRegularWorkerFixture::default()
- .with_reward_per_block(Some(reward_per_block))
- .hire();
- let invalid_worker_id = 11;
- let new_reward_account = 2;
- let update_account_fixture =
- UpdateRewardAccountFixture::default_with_ids(invalid_worker_id, new_reward_account);
- update_account_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::WorkerDoesNotExist.into(),
- ));
- });
- }
- #[test]
- fn update_reward_account_fails_with_no_recurring_reward() {
- build_test_externalities().execute_with(|| {
- let worker_id = HireRegularWorkerFixture::default().hire();
- let new_reward_account = 343;
- let update_account_fixture =
- UpdateRewardAccountFixture::default_with_ids(worker_id, new_reward_account);
- update_account_fixture
- .call_and_assert(Err(Error::<Test, DefaultInstance>::WorkerHasNoReward.into()));
- });
- }
- #[test]
- fn update_reward_amount_succeeds() {
- build_test_externalities().execute_with(|| {
- /*
- Events are not emitted on block 0.
- So any dispatchable calls made during genesis block formation will have no events emitted.
- https://substrate.dev/recipes/2-appetizers/4-events.html
- */
- run_to_block(1);
- let worker_id = HireRegularWorkerFixture::default().hire();
- let reward_per_block = 120;
- let update_amount_fixture = UpdateRewardAmountFixture::default_for_worker_id(worker_id)
- .with_reward_per_block(Some(reward_per_block));
- update_amount_fixture.call_and_assert(Ok(()));
- EventFixture::assert_last_crate_event(RawEvent::WorkerRewardAmountUpdated(
- worker_id,
- Some(reward_per_block),
- ));
- });
- }
- #[test]
- fn update_reward_amount_succeeds_for_leader() {
- build_test_externalities().execute_with(|| {
- let worker_id = HireLeadFixture::default()
- .with_reward_per_block(Some(1000))
- .hire_lead();
- let update_amount_fixture = UpdateRewardAmountFixture::default_for_worker_id(worker_id)
- .with_origin(RawOrigin::Root);
- update_amount_fixture.call_and_assert(Ok(()));
- });
- }
- #[test]
- fn update_reward_amount_fails_with_invalid_origin() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let worker_id = 22; // random worker id
- let update_amount_fixture = UpdateRewardAmountFixture::default_for_worker_id(worker_id)
- .with_origin(RawOrigin::None);
- update_amount_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn update_reward_amount_fails_with_invalid_origin_for_leader() {
- build_test_externalities().execute_with(|| {
- let worker_id = HireLeadFixture::default().hire_lead();
- let update_amount_fixture = UpdateRewardAmountFixture::default_for_worker_id(worker_id)
- .with_origin(RawOrigin::None);
- update_amount_fixture.call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn update_reward_amount_fails_with_invalid_origin_signed_account() {
- build_test_externalities().execute_with(|| {
- let worker_id = HireRegularWorkerFixture::default().hire();
- let update_amount_fixture = UpdateRewardAmountFixture::default_for_worker_id(worker_id)
- .with_origin(RawOrigin::Signed(2));
- update_amount_fixture
- .call_and_assert(Err(Error::<Test, DefaultInstance>::IsNotLeadAccount.into()));
- });
- }
- #[test]
- fn update_reward_amount_fails_with_invalid_worker_id() {
- build_test_externalities().execute_with(|| {
- HireRegularWorkerFixture::default().hire();
- let invalid_worker_id = 12;
- let update_amount_fixture =
- UpdateRewardAmountFixture::default_for_worker_id(invalid_worker_id);
- update_amount_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::WorkerDoesNotExist.into(),
- ));
- });
- }
- #[test]
- fn set_status_text_succeeded() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- run_to_block(1);
- let status_text = b"some".to_vec();
- SetStatusTextFixture::default()
- .with_status_text(Some(status_text.clone()))
- .call_and_assert(Ok(()));
- let expected_hash = <Test as frame_system::Trait>::Hashing::hash(&status_text);
- EventFixture::assert_last_crate_event(RawEvent::StatusTextChanged(
- expected_hash.as_ref().to_vec(),
- Some(status_text),
- ));
- });
- }
- #[test]
- fn set_status_text_fails_with_bad_origin() {
- build_test_externalities().execute_with(|| {
- HireLeadFixture::default().hire_lead();
- let leader_account_id = 10;
- SetStatusTextFixture::default()
- .with_origin(RawOrigin::Signed(leader_account_id))
- .call_and_assert(Err(Error::<Test, DefaultInstance>::IsNotLeadAccount.into()));
- });
- }
- #[test]
- fn spend_from_budget_succeeded() {
- build_test_externalities().execute_with(|| {
- let account_id = 2;
- let amount = 100;
- HireLeadFixture::default().hire_lead();
- run_to_block(1);
- let set_budget_fixture = SetBudgetFixture::default().with_budget(1000);
- assert_eq!(set_budget_fixture.call(), Ok(()));
- SpendFromBudgetFixture::default()
- .with_account_id(account_id)
- .with_amount(amount)
- .call_and_assert(Ok(()));
- EventFixture::assert_last_crate_event(RawEvent::BudgetSpending(account_id, amount, None));
- });
- }
- #[test]
- fn spend_from_budget_failed_with_invalid_origin() {
- build_test_externalities().execute_with(|| {
- SpendFromBudgetFixture::default()
- .with_origin(RawOrigin::None.into())
- .call_and_assert(Err(DispatchError::BadOrigin));
- });
- }
- #[test]
- fn spend_from_budget_fails_with_empty_budget() {
- build_test_externalities().execute_with(|| {
- let account_id = 2;
- let amount = 100;
- HireLeadFixture::default().hire_lead();
- SpendFromBudgetFixture::default()
- .with_account_id(account_id)
- .with_amount(amount)
- .call_and_assert(Err(
- Error::<Test, DefaultInstance>::InsufficientBudgetForSpending.into(),
- ));
- });
- }
- #[test]
- fn spend_from_budget_fails_with_zero_amount() {
- build_test_externalities().execute_with(|| {
- let account_id = 2;
- let amount = 0;
- HireLeadFixture::default().hire_lead();
- SpendFromBudgetFixture::default()
- .with_account_id(account_id)
- .with_amount(amount)
- .call_and_assert(Err(Error::<Test, DefaultInstance>::CannotSpendZero.into()));
- });
- }
- #[test]
- fn ensure_worker_origin_works_correctly() {
- build_test_externalities().execute_with(|| {
- let invalid_worker_id = 2;
- assert_eq!(
- TestWorkingGroup::ensure_worker_origin(RawOrigin::None.into(), &invalid_worker_id),
- Err(DispatchError::BadOrigin)
- );
- let account_id = 2;
- assert_eq!(
- TestWorkingGroup::ensure_worker_origin(
- RawOrigin::Signed(account_id).into(),
- &invalid_worker_id
- ),
- Err(Error::<Test, DefaultInstance>::WorkerDoesNotExist.into())
- );
- let worker_id = HireRegularWorkerFixture::default().hire();
- let invalid_account = 3;
- assert_eq!(
- TestWorkingGroup::ensure_worker_origin(
- RawOrigin::Signed(invalid_account).into(),
- &worker_id
- ),
- Err(Error::<Test, DefaultInstance>::SignerIsNotWorkerRoleAccount.into())
- );
- assert_eq!(
- TestWorkingGroup::ensure_worker_origin(
- RawOrigin::Signed(account_id).into(),
- &worker_id
- ),
- Ok(())
- );
- });
- }
- #[test]
- fn ensure_leader_origin_works_correctly() {
- build_test_externalities().execute_with(|| {
- assert_eq!(
- TestWorkingGroup::ensure_leader_origin(RawOrigin::None.into()),
- Err(DispatchError::BadOrigin)
- );
- let account_id = 1;
- assert_eq!(
- TestWorkingGroup::ensure_leader_origin(RawOrigin::Signed(account_id).into()),
- Err(Error::<Test, DefaultInstance>::CurrentLeadNotSet.into())
- );
- HireLeadFixture::default().hire_lead();
- let invalid_account = 2;
- assert_eq!(
- TestWorkingGroup::ensure_leader_origin(RawOrigin::Signed(invalid_account).into()),
- Err(Error::<Test, DefaultInstance>::IsNotLeadAccount.into())
- );
- assert_eq!(
- TestWorkingGroup::ensure_leader_origin(RawOrigin::Signed(account_id).into()),
- Ok(())
- );
- });
- }
- #[test]
- fn get_leader_member_id_works_correctly() {
- build_test_externalities().execute_with(|| {
- assert_eq!(TestWorkingGroup::get_leader_member_id(), None);
- HireLeadFixture::default().hire_lead();
- let leader_member_id = 1;
- assert_eq!(
- TestWorkingGroup::get_leader_member_id(),
- Some(leader_member_id)
- );
- });
- }
- #[test]
- fn is_leader_account_id_works_correctly() {
- build_test_externalities().execute_with(|| {
- let invalid_account_id = 2u64;
- // No leader set
- assert_eq!(
- TestWorkingGroup::is_leader_account_id(&invalid_account_id),
- false
- );
- HireLeadFixture::default().hire_lead();
- assert_eq!(
- TestWorkingGroup::is_leader_account_id(&invalid_account_id),
- false
- );
- let account_id = 1u64;
- assert_eq!(TestWorkingGroup::is_leader_account_id(&account_id), true);
- });
- }
- #[test]
- fn is_worker_account_id_works_correctly() {
- build_test_externalities().execute_with(|| {
- let invalid_account_id = 3u64;
- let invalid_worker_id = 3u64;
- // Not hired
- assert_eq!(
- TestWorkingGroup::is_worker_account_id(&invalid_account_id, &invalid_worker_id),
- false
- );
- let worker_id = HireRegularWorkerFixture::default().hire();
- assert_eq!(
- TestWorkingGroup::is_worker_account_id(&invalid_account_id, &worker_id),
- false
- );
- let account_id = 2u64;
- assert_eq!(
- TestWorkingGroup::is_worker_account_id(&account_id, &invalid_worker_id),
- false
- );
- assert_eq!(
- TestWorkingGroup::is_worker_account_id(&account_id, &worker_id),
- true
- );
- });
- }
- #[test]
- fn update_worker_storage_succeeds() {
- build_test_externalities().execute_with(|| {
- /*
- Events are not emitted on block 0.
- So any dispatchable calls made during genesis block formation will have no events emitted.
- https://substrate.dev/recipes/2-appetizers/4-events.html
- */
- run_to_block(1);
- let storage_field = vec![0u8].repeat(10);
- let worker_id = HireRegularWorkerFixture::default().hire();
- let update_storage_fixture = UpdateWorkerStorageFixture::default_with_storage_field(
- worker_id,
- storage_field.clone(),
- );
- update_storage_fixture.call_and_assert(Ok(()));
- EventFixture::assert_last_crate_event(RawEvent::WorkerStorageUpdated(
- worker_id,
- storage_field,
- ));
- });
- }
- #[test]
- fn update_worker_storage_by_leader_succeeds() {
- build_test_externalities().execute_with(|| {
- let storage_field = vec![0u8].repeat(10);
- let leader_account_id = 1;
- let worker_id = HireLeadFixture::default().hire_lead();
- let update_storage_fixture = UpdateWorkerStorageFixture::default_with_storage_field(
- worker_id,
- storage_field.clone(),
- )
- .with_origin(RawOrigin::Signed(leader_account_id));
- update_storage_fixture.call_and_assert(Ok(()));
- let worker_storage = TestWorkingGroup::worker_storage(worker_id);
- assert_eq!(storage_field, worker_storage);
- });
- }
- #[test]
- fn update_worker_storage_fails_with_invalid_origin_signed_account() {
- build_test_externalities().execute_with(|| {
- let worker_id = HireRegularWorkerFixture::default().hire();
- let invalid_account_id = 44;
- let storage_field = vec![0u8].repeat(10);
- let update_storage_fixture =
- UpdateWorkerStorageFixture::default_with_storage_field(worker_id, storage_field)
- .with_origin(RawOrigin::Signed(invalid_account_id));
- update_storage_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::SignerIsNotWorkerRoleAccount.into(),
- ));
- });
- }
- #[test]
- fn update_worker_storage_fails_with_invalid_worker_id() {
- build_test_externalities().execute_with(|| {
- let storage_field = vec![0u8].repeat(10);
- HireRegularWorkerFixture::default().hire();
- let invalid_worker_id = 111;
- let update_storage_fixture = UpdateWorkerStorageFixture::default_with_storage_field(
- invalid_worker_id,
- storage_field.clone(),
- );
- update_storage_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::WorkerDoesNotExist.into(),
- ));
- });
- }
- #[test]
- fn update_worker_storage_fails_with_too_long_text() {
- build_test_externalities().execute_with(|| {
- let storage_field = vec![0u8].repeat(default_storage_size_constraint() as usize + 1);
- let worker_id = HireRegularWorkerFixture::default().hire();
- let update_storage_fixture = UpdateWorkerStorageFixture::default_with_storage_field(
- worker_id,
- storage_field.clone(),
- );
- update_storage_fixture.call_and_assert(Err(
- Error::<Test, DefaultInstance>::WorkerStorageValueTooLong.into(),
- ));
- });
- }
|