exonum-rust-runtime 1.0.0

The runtime is for running Exonum services written in Rust.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
// Copyright 2020 The Exonum Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use exonum::{
    blockchain::{config::InstanceInitParams, Blockchain, BlockchainBuilder, BlockchainMut},
    helpers::Height,
    merkledb::{
        access::{Access, FromAccess},
        BinaryValue, ProofEntry, ProofListIndex,
    },
    runtime::{
        migrations::{InitMigrationError, MigrateData, MigrationScript},
        versioning::Version,
        CommonError, CoreError, ErrorMatch, ExecutionContext, ExecutionError, ExecutionFail,
        InstanceSpec, InstanceStatus, SnapshotExt,
    },
};
use exonum_api::UpdateEndpoints;
use exonum_derive::*;
use futures::channel::mpsc;
use pretty_assertions::assert_eq;

use self::inspected::{
    assert_no_endpoint_update, create_genesis_config_builder, execute_transaction,
    get_endpoint_paths, EventsHandle, Inspected, MigrateService, ResumeService, RuntimeEvent,
    StartService, ToySupervisor, ToySupervisorService,
};
use exonum_rust_runtime::{DefaultInstance, RustRuntimeBuilder, Service, ServiceFactory};

mod inspected;

const INITIAL_BALANCE: u64 = 100_000;

#[derive(Debug, FromAccess, RequireArtifact)]
struct Schema<T: Access> {
    balance: ProofEntry<T::Base, u64>,
    withdrawals: ProofListIndex<T::Base, u64>,
}

impl<T: Access> Schema<T> {
    pub fn new(access: T) -> Self {
        Self::from_root(access).unwrap()
    }
}

#[exonum_interface(auto_ids)]
trait Withdrawal<Ctx> {
    type Output;

    fn withdraw(&self, context: Ctx, arg: u64) -> Self::Output;
}

/// This implementation is incorrect and instead of decrementing the balance of the wallet
/// after withdraw, it increases it.
#[derive(Debug, ServiceFactory, ServiceDispatcher)]
#[service_dispatcher(implements("Withdrawal"))]
#[service_factory(artifact_name = "withdrawal", artifact_version = "0.1.0")]
struct WithdrawalServiceV1;

impl Withdrawal<ExecutionContext<'_>> for WithdrawalServiceV1 {
    type Output = Result<(), ExecutionError>;

    fn withdraw(&self, context: ExecutionContext<'_>, arg: u64) -> Self::Output {
        let mut schema = Schema::new(context.service_data());
        schema.balance.set(schema.balance.get().unwrap() + arg);
        schema.withdrawals.push(arg);
        Ok(())
    }
}

impl Service for WithdrawalServiceV1 {
    fn initialize(
        &self,
        context: ExecutionContext<'_>,
        params: Vec<u8>,
    ) -> Result<(), ExecutionError> {
        let mut schema = Schema::new(context.service_data());
        schema.balance.set(u64::from_bytes(params.into()).unwrap());
        Ok(())
    }
}

impl DefaultInstance for WithdrawalServiceV1 {
    const INSTANCE_ID: u32 = 2;
    const INSTANCE_NAME: &'static str = "withdrawal";

    fn default_instance(&self) -> InstanceInitParams {
        self.artifact_id()
            .into_default_instance(Self::INSTANCE_ID, Self::INSTANCE_NAME)
            .with_constructor(INITIAL_BALANCE)
    }
}

/// This implementation fixes the incorrect behavior of the previous one. After the migration
/// procedure, during the resuming this implementation also recalculates the resulting balance.
#[derive(Debug, ServiceFactory, ServiceDispatcher)]
#[service_dispatcher(implements("Withdrawal"))]
#[service_factory(artifact_name = "withdrawal", artifact_version = "0.2.0")]
struct WithdrawalServiceV2;

impl Withdrawal<ExecutionContext<'_>> for WithdrawalServiceV2 {
    type Output = Result<(), ExecutionError>;

    fn withdraw(&self, context: ExecutionContext<'_>, arg: u64) -> Self::Output {
        let mut schema = Schema::new(context.service_data());
        schema.balance.set(schema.balance.get().unwrap() - arg);
        schema.withdrawals.push(arg);
        Ok(())
    }
}

impl Service for WithdrawalServiceV2 {
    fn initialize(
        &self,
        context: ExecutionContext<'_>,
        params: Vec<u8>,
    ) -> Result<(), ExecutionError> {
        let mut schema = Schema::new(context.service_data());
        schema.balance.set(u64::from_bytes(params.into()).unwrap());
        Ok(())
    }

    fn resume(&self, context: ExecutionContext<'_>, params: Vec<u8>) -> Result<(), ExecutionError> {
        if !params.is_empty() {
            return Err(CommonError::MalformedArguments
                .with_description("Resuming parameters should be empty."));
        }

        // Recalculate the balance taking into account the error of the previous implementation.
        // Despite the simplicity this approach is very fragile and can lead to errors during
        // subsequent migrations.
        // Therefore, this approach should be used only in test environment.
        let mut schema = Schema::new(context.service_data());
        let correct_balance = schema
            .withdrawals
            .iter()
            .fold(schema.balance.get().unwrap(), |balance, value| {
                balance - 2 * value
            });
        schema.balance.set(correct_balance);
        Ok(())
    }
}

impl DefaultInstance for WithdrawalServiceV2 {
    const INSTANCE_ID: u32 = 2;
    const INSTANCE_NAME: &'static str = "withdrawal";

    fn default_instance(&self) -> InstanceInitParams {
        self.artifact_id()
            .into_default_instance(Self::INSTANCE_ID, Self::INSTANCE_NAME)
            .with_constructor(INITIAL_BALANCE)
    }
}

impl MigrateData for WithdrawalServiceV2 {
    fn migration_scripts(
        &self,
        start_version: &Version,
    ) -> Result<Vec<MigrationScript>, InitMigrationError> {
        assert_eq!(start_version, &WithdrawalServiceV1.artifact_id().version);
        Ok(Vec::new())
    }
}

fn create_runtime() -> (BlockchainMut, EventsHandle, mpsc::Receiver<UpdateEndpoints>) {
    let blockchain = Blockchain::build_for_tests();
    let genesis_config = create_genesis_config_builder()
        .with_artifact(ToySupervisorService.artifact_id())
        .with_instance(ToySupervisorService.default_instance())
        .with_artifact(WithdrawalServiceV1.artifact_id())
        .with_artifact(WithdrawalServiceV2.artifact_id())
        .with_instance(WithdrawalServiceV1.default_instance())
        .build();

    let (endpoints_tx, endpoints_rx) = mpsc::channel(16);
    let inspected = Inspected::new(
        RustRuntimeBuilder::new()
            .with_factory(WithdrawalServiceV1)
            .with_migrating_factory(WithdrawalServiceV2)
            .with_factory(ToySupervisorService)
            .build(endpoints_tx),
    );
    let events_handle = inspected.events.clone();

    let blockchain = BlockchainBuilder::new(blockchain)
        .with_genesis_config(genesis_config)
        .with_runtime(inspected)
        .build();
    (blockchain, events_handle, endpoints_rx)
}

#[test]
fn endpoints_are_updated_on_service_start() {
    let (mut blockchain, _, mut endpoints_rx) = create_runtime();
    let keypair = blockchain.as_ref().service_keypair().clone();
    let endpoint_paths = get_endpoint_paths(&mut endpoints_rx);
    assert!(endpoint_paths.contains("services/supervisor"));
    assert!(endpoint_paths.contains("services/withdrawal"));

    let start_service = StartService {
        spec: InstanceSpec::from_raw_parts(
            WithdrawalServiceV1::INSTANCE_ID + 1,
            "other-withdrawal".to_owned(),
            WithdrawalServiceV1.artifact_id(),
        ),
        constructor: 1_000_u64.to_bytes(),
    };
    let start_service = keypair.start_service(ToySupervisorService::INSTANCE_ID, start_service);
    execute_transaction(&mut blockchain, start_service).unwrap();

    let endpoint_paths = get_endpoint_paths(&mut endpoints_rx);
    assert!(endpoint_paths.contains("services/supervisor"));
    assert!(endpoint_paths.contains("services/withdrawal"));
    assert!(endpoint_paths.contains("services/other-withdrawal"));
}

fn test_resume_without_migration(freeze_service: bool) {
    let (mut blockchain, events_handle, mut endpoints_rx) = create_runtime();
    let keypair = blockchain.as_ref().service_keypair().clone();
    // We are not interested in blockchain initialization events.
    drop(events_handle.take());
    // Get initial set of endpoints.
    let endpoint_paths = get_endpoint_paths(&mut endpoints_rx);
    assert!(endpoint_paths.contains("services/supervisor"));
    assert!(endpoint_paths.contains("services/withdrawal"));

    // Make withdrawal.
    let amount = 10_000;
    execute_transaction(
        &mut blockchain,
        keypair.withdraw(WithdrawalServiceV1::INSTANCE_ID, amount),
    )
    .unwrap();
    // We not interested in events in this case.
    drop(events_handle.take());

    // Stop / freeze the running service instance.
    let tx = if freeze_service {
        keypair.freeze_service(
            ToySupervisorService::INSTANCE_ID,
            WithdrawalServiceV1::INSTANCE_ID,
        )
    } else {
        keypair.stop_service(
            ToySupervisorService::INSTANCE_ID,
            WithdrawalServiceV1::INSTANCE_ID,
        )
    };
    execute_transaction(&mut blockchain, tx).unwrap();
    // We not interested in events in this case.
    drop(events_handle.take());

    if !freeze_service {
        // Check that endpoints have been updated and no longer contain the stopped service.
        let endpoint_paths = get_endpoint_paths(&mut endpoints_rx);
        assert!(endpoint_paths.contains("services/supervisor"));
        assert!(!endpoint_paths.contains("services/withdrawal"));
    } else {
        assert_no_endpoint_update(&mut endpoints_rx);
    }

    // Resume stopped service instance.
    execute_transaction(
        &mut blockchain,
        keypair.resume_service(
            ToySupervisorService::INSTANCE_ID,
            ResumeService {
                instance_id: WithdrawalServiceV1::INSTANCE_ID,
                params: vec![],
            },
        ),
    )
    .unwrap();

    let withdrawal_service = WithdrawalServiceV1.default_instance().instance_spec;
    assert_eq!(
        events_handle.take(),
        vec![
            RuntimeEvent::BeforeTransactions(Height(3), ToySupervisorService::INSTANCE_ID),
            RuntimeEvent::StartResumingService(withdrawal_service, vec![]),
            RuntimeEvent::AfterTransactions(Height(3), ToySupervisorService::INSTANCE_ID),
            RuntimeEvent::CommitService(
                Height(4),
                WithdrawalServiceV1.default_instance().instance_spec,
                InstanceStatus::Active,
            ),
            RuntimeEvent::AfterCommit(Height(4)),
        ]
    );

    if !freeze_service {
        // Check that endpoints have been updated again.
        let endpoint_paths = get_endpoint_paths(&mut endpoints_rx);
        assert!(endpoint_paths.contains("services/supervisor"));
        assert!(endpoint_paths.contains("services/withdrawal"));
    } else {
        assert_no_endpoint_update(&mut endpoints_rx);
    }

    // Make another withdrawal.
    let amount_2 = 20_000;
    execute_transaction(
        &mut blockchain,
        keypair.withdraw(WithdrawalServiceV1::INSTANCE_ID, amount_2),
    )
    .unwrap();
    drop(events_handle.take());

    // Check balance and history.
    let snapshot = blockchain.snapshot();
    let schema = Schema::new(
        snapshot
            .for_service(WithdrawalServiceV1::INSTANCE_NAME)
            .unwrap(),
    );

    assert_eq!(
        schema.balance.get(),
        Some(INITIAL_BALANCE + amount + amount_2)
    );
    assert_eq!(
        schema.withdrawals.iter().collect::<Vec<_>>(),
        vec![amount, amount_2]
    );
}

#[test]
fn resume_without_migration() {
    test_resume_without_migration(false);
}

#[test]
fn resume_without_migration_and_frozen_service() {
    test_resume_without_migration(true);
}

#[test]
fn frozen_to_stopped_transition() {
    let (mut blockchain, _, mut endpoints_rx) = create_runtime();
    let keypair = blockchain.as_ref().service_keypair().clone();

    let endpoint_paths = get_endpoint_paths(&mut endpoints_rx);
    assert!(endpoint_paths.contains("services/supervisor"));
    assert!(endpoint_paths.contains("services/withdrawal"));

    // Freeze and then stop the service.
    let tx = keypair.freeze_service(
        ToySupervisorService::INSTANCE_ID,
        WithdrawalServiceV1::INSTANCE_ID,
    );
    execute_transaction(&mut blockchain, tx).unwrap();
    let tx = keypair.stop_service(
        ToySupervisorService::INSTANCE_ID,
        WithdrawalServiceV1::INSTANCE_ID,
    );
    execute_transaction(&mut blockchain, tx).unwrap();

    let endpoint_paths = get_endpoint_paths(&mut endpoints_rx);
    assert!(endpoint_paths.contains("services/supervisor"));
    assert!(!endpoint_paths.contains("services/withdrawal"));
}

fn test_resume_with_fast_forward_migration(freeze_service: bool) {
    let (mut blockchain, events_handle, mut endpoints_rx) = create_runtime();
    let keypair = blockchain.as_ref().service_keypair().clone();
    // We are not interested in blockchain initialization events.
    drop(events_handle.take());
    // Take initial set of endpoints.
    get_endpoint_paths(&mut endpoints_rx);

    // Make withdrawal.
    let amount = 10_000;
    execute_transaction(
        &mut blockchain,
        keypair.withdraw(WithdrawalServiceV1::INSTANCE_ID, amount),
    )
    .unwrap();
    // We not interested in events in this case.
    drop(events_handle.take());

    // Stop or freeze the running service instance.
    let tx = if freeze_service {
        keypair.freeze_service(
            ToySupervisorService::INSTANCE_ID,
            WithdrawalServiceV1::INSTANCE_ID,
        )
    } else {
        keypair.stop_service(
            ToySupervisorService::INSTANCE_ID,
            WithdrawalServiceV1::INSTANCE_ID,
        )
    };
    execute_transaction(&mut blockchain, tx).unwrap();
    // We not interested in events in this case.
    drop(events_handle.take());

    if freeze_service {
        assert_no_endpoint_update(&mut endpoints_rx);
    } else {
        let paths = get_endpoint_paths(&mut endpoints_rx);
        assert!(paths.contains("services/supervisor"));
        assert!(!paths.contains("services/withdrawal"));
    }

    // Make fast-forward migration to the WithdrawalServiceV2.
    execute_transaction(
        &mut blockchain,
        keypair.migrate_service(
            ToySupervisorService::INSTANCE_ID,
            MigrateService {
                instance_name: WithdrawalServiceV1::INSTANCE_NAME.to_owned(),
                artifact: WithdrawalServiceV2.artifact_id(),
            },
        ),
    )
    .unwrap();

    let withdrawal_service = WithdrawalServiceV2.default_instance().instance_spec;
    let expected_events = vec![
        RuntimeEvent::BeforeTransactions(Height(3), ToySupervisorService::INSTANCE_ID),
        RuntimeEvent::MigrateService(
            withdrawal_service.artifact.clone(),
            WithdrawalServiceV1.artifact_id().version,
        ),
        RuntimeEvent::AfterTransactions(Height(3), ToySupervisorService::INSTANCE_ID),
        RuntimeEvent::CommitService(
            Height(4),
            withdrawal_service.clone(),
            InstanceStatus::Stopped,
        ),
        RuntimeEvent::AfterCommit(Height(4)),
    ];
    assert_eq!(events_handle.take(), expected_events);

    if freeze_service {
        let paths = get_endpoint_paths(&mut endpoints_rx);
        assert!(paths.contains("services/supervisor"));
        assert!(!paths.contains("services/withdrawal"));
    } else {
        assert_no_endpoint_update(&mut endpoints_rx);
    }

    // Resume stopped service instance.
    execute_transaction(
        &mut blockchain,
        keypair.resume_service(
            ToySupervisorService::INSTANCE_ID,
            ResumeService {
                instance_id: WithdrawalServiceV2::INSTANCE_ID,
                params: vec![],
            },
        ),
    )
    .unwrap();
    assert_eq!(
        events_handle.take(),
        vec![
            RuntimeEvent::BeforeTransactions(Height(4), ToySupervisorService::INSTANCE_ID),
            RuntimeEvent::StartResumingService(withdrawal_service.clone(), vec![]),
            RuntimeEvent::AfterTransactions(Height(4), ToySupervisorService::INSTANCE_ID),
            RuntimeEvent::CommitService(
                Height(5),
                WithdrawalServiceV2.default_instance().instance_spec,
                InstanceStatus::Active,
            ),
            RuntimeEvent::AfterCommit(Height(5)),
        ]
    );

    // Check instance state after migration and resume.
    let instance_state = blockchain
        .snapshot()
        .for_dispatcher()
        .get_instance(WithdrawalServiceV2::INSTANCE_ID)
        .unwrap();

    assert_eq!(instance_state.spec, withdrawal_service);
    assert_eq!(instance_state.status, Some(InstanceStatus::Active));
    assert_eq!(
        instance_state.data_version(),
        &withdrawal_service.artifact.version
    );

    let paths = get_endpoint_paths(&mut endpoints_rx);
    assert!(paths.contains("services/supervisor"));
    assert!(paths.contains("services/withdrawal"));

    // Make another withdrawal.
    let amount_2 = 20_000;
    execute_transaction(
        &mut blockchain,
        keypair.withdraw(WithdrawalServiceV2::INSTANCE_ID, amount_2),
    )
    .unwrap();
    assert_eq!(
        events_handle.take(),
        vec![
            RuntimeEvent::BeforeTransactions(Height(5), ToySupervisorService::INSTANCE_ID),
            RuntimeEvent::BeforeTransactions(Height(5), WithdrawalServiceV2::INSTANCE_ID),
            RuntimeEvent::AfterTransactions(Height(5), ToySupervisorService::INSTANCE_ID),
            RuntimeEvent::AfterTransactions(Height(5), WithdrawalServiceV2::INSTANCE_ID),
            RuntimeEvent::AfterCommit(Height(6)),
        ]
    );

    // Check balance and history.
    let snapshot = blockchain.snapshot();
    let schema = Schema::new(
        snapshot
            .for_service(WithdrawalServiceV2::INSTANCE_NAME)
            .unwrap(),
    );

    assert_eq!(
        schema.balance.get(),
        Some(INITIAL_BALANCE - amount - amount_2)
    );
}

#[test]
fn resume_with_fast_forward_migration() {
    test_resume_with_fast_forward_migration(false);
}

#[test]
fn resume_with_fast_forward_migration_and_freeze() {
    test_resume_with_fast_forward_migration(true);
}

#[test]
fn test_resume_service_error() {
    let (mut blockchain, ..) = create_runtime();
    let keypair = blockchain.as_ref().service_keypair().clone();

    // Make withdrawal.
    let amount = 10_000;
    execute_transaction(
        &mut blockchain,
        keypair.withdraw(WithdrawalServiceV1::INSTANCE_ID, amount),
    )
    .unwrap();

    // Stop running service instance.
    execute_transaction(
        &mut blockchain,
        keypair.stop_service(
            ToySupervisorService::INSTANCE_ID,
            WithdrawalServiceV1::INSTANCE_ID,
        ),
    )
    .unwrap();

    // Make fast-forward migration to the WithdrawalServiceV2.
    execute_transaction(
        &mut blockchain,
        keypair.migrate_service(
            ToySupervisorService::INSTANCE_ID,
            MigrateService {
                instance_name: WithdrawalServiceV1::INSTANCE_NAME.to_owned(),
                artifact: WithdrawalServiceV2.artifact_id(),
            },
        ),
    )
    .unwrap();

    // Resume stopped service instance.
    let actual_err = execute_transaction(
        &mut blockchain,
        keypair.resume_service(
            ToySupervisorService::INSTANCE_ID,
            ResumeService {
                instance_id: WithdrawalServiceV2::INSTANCE_ID,
                params: vec![1, 2, 3, 4],
            },
        ),
    )
    .unwrap_err();
    assert_eq!(
        actual_err,
        ErrorMatch::from_fail(&CommonError::MalformedArguments)
            .with_description_containing("Resuming parameters should be empty")
    );

    // Verify the service instance after migration and unsuccessful resume.
    let instance_state = blockchain
        .snapshot()
        .for_dispatcher()
        .get_instance(WithdrawalServiceV1::INSTANCE_ID)
        .unwrap();

    let mut expected_spec = WithdrawalServiceV1.default_instance().instance_spec;
    expected_spec.artifact = WithdrawalServiceV2.artifact_id();
    assert_eq!(instance_state.spec, expected_spec);
    assert_eq!(instance_state.status, Some(InstanceStatus::Stopped));
    assert_eq!(
        *instance_state.data_version(),
        WithdrawalServiceV2.artifact_id().version
    );
}

#[test]
fn resume_non_existent_service_error() {
    let (mut blockchain, ..) = create_runtime();
    let keypair = blockchain.as_ref().service_keypair().clone();

    let actual_err = execute_transaction(
        &mut blockchain,
        keypair.resume_service(
            ToySupervisorService::INSTANCE_ID,
            ResumeService {
                instance_id: WithdrawalServiceV2::INSTANCE_ID + 1,
                params: vec![],
            },
        ),
    )
    .unwrap_err();
    assert_eq!(
        actual_err,
        ErrorMatch::from_fail(&CoreError::IncorrectInstanceId)
    );
}

#[test]
fn resume_active_service_error() {
    let (mut blockchain, ..) = create_runtime();
    let keypair = blockchain.as_ref().service_keypair().clone();

    let actual_err = execute_transaction(
        &mut blockchain,
        keypair.resume_service(
            ToySupervisorService::INSTANCE_ID,
            ResumeService {
                instance_id: WithdrawalServiceV1::INSTANCE_ID,
                params: vec![],
            },
        ),
    )
    .unwrap_err();
    assert_eq!(
        actual_err,
        ErrorMatch::from_fail(&CoreError::InvalidServiceTransition)
            .with_description_containing("Cannot resume service `2:withdrawal`")
    );
}