When writing canisters (services, smart contracts) on the Internet Computer, and these services are meant to call other canisters, or if you create external services or agents that interact with the Internet Computer, it is probably important to test the behaviour of your code when some of these calls are still pending, or if they come back in particular order.
The Internet Computer of course does not give you control over its scheduling. But there is an interesting aspect of the IC’s behavior that allows us to send an inter-canister or ingress call, and externally control when it returns (without resorting to even more horrible hacks like looping self-calls): If a canister tries to stop itself, then the call to stop_canister
will prevent this stopping from happening, and the canister will be stuck in state stopping
. This situation can then be externally resolved using canister_start
.
I wrote code for two canisters that can be used to demonstrate this, and can be used in tests:
The “barrier” canister provides this interface:
service : {
enter: () -> ();
lock: () -> ();
release: () -> ();
transaction_notification: () -> ();
wallet_receive: () -> ();
}
First you invoke lock
to close the barrier. Then you can call enter
as often as you want, all these calls with not respond. Once you call release
, all of these calls will return.
The barrier canister is installed at cuptx-eaaaa-aaaai-aa67q-cai, you can play around with it there, or you can get the source code and install it yourself.
The endpoints transaction_notification
and wallet_receive
will behave like enter
and can be used to test the behaviour of the ICP-ledger (or compatible ledger) respectively the cycle wallet.
CAUTION: Most canisters with outstanding calls cannot be upgraded safely. This means this canister could be used to stage attacks against any service that you can trick to call you (e.g. by asking someone to transfer some cycles to your canister). But of course we woudn’t do that. Still, if this worries you, read
how to protect your canisters from such attacks.