Systems
One of the design principles of MUD is to separate the state of the World
from the business logic.
The business logic is implemented in stateless System
contracts.
System
s are called through the World
, and call back to the World
to read and write state from tables.
Detailed illustration
-
An account calls a function called
namespace__function
via theWorld
. This function was registered by the owner of thenamespace
namespace and points to thefunction
function in one of theSystem
s in thenamespace
namespace. -
The
World
verifies that access is permitted (for example, becausenamespace:system
is publicly accessible) and if so callsfunction
on thenamespace:system
contract with the provided parameters. -
At some point in its execution
function
decides to update the data in the tablenamespace:table
. As with all other tables, this table is stored in theWorld
's storage. To modify it,function
calls a function on theWorld
contract. -
The
World
verifies that access is permitted (by default it would be, becausenamespace:system
has access to thenamespace
namespace). If so, it modifies the data in thenamespace:table
table.
The World
serves as a central entry point and forwards calls to systems, which allows it to provide access control.
Calling systems
To call a System
, you call the World
in one of these ways:
- If a function selector for the
System
is registered in theWorld
, you can call it viaworld.<namespace>__<function>(<arguments>)
. - You can use
call
(opens in a new tab). - If you have the proper delegation you can use
callFrom
(opens in a new tab).
Writing systems
A System
should not have any internal state, but store all of it in tables in the World
.
There are several reasons for this:
- It allows a
World
to enforce access controls. - It allows the same
System
to be used by multipleWorld
contracts. - Upgrades are a lot simpler when all the state is centralized outside of the
System
contract.
Because calls to systems are proxied through the World
, some message fields don't reflect the original call.
Use these substitutes:
Vanilla Solidity | System replacement |
---|---|
msg.sender | _msgSender() |
msg.value | _msgValue() |
When calling other contracts from a System
, be aware that if you use delegatecall
the called contract inherits the System
's permissions and can modify data in the World
on behalf of the System
.
Calling one System
from another
There are two ways to call one System
from another one.
Call type | call to the World | delegatecall directly to the System |
---|---|---|
Permissions | those of the called System | those of the calling System |
_msgSender() | calling System (unless you can use callFrom , which is only available when the user delegates to your System ) | can use WorldContextProvider (opens in a new tab) to transfer the correct information |
_msgValue() | zero | can use WorldContextProvider (opens in a new tab) to transfer the correct information |
Can be used by systems in the root namespace | No (it's a security measure) | Yes |
Calling from a root System
If you need to call a System
from a System
in the root namespace you can use SystemSwitch
(opens in a new tab).
-
Import
SystemSwitch
.import { SystemSwitch } from "@latticexyz/world-modules/src/utils/SystemSwitch.sol";
-
Import the interface for the system you wish to call.
import { IIncrementSystem } from "../codegen/world/IIncrementSystem.sol";
-
Call the function using
SystemSwitch.call
. For example, here is how you can callIncrementSystem.increment()
.uint32 returnValue = abi.decode( SystemSwitch.call( abi.encodeCall(IIncrementSystem.increment, ()) ), (uint32) );
Explanation
abi.encodeCall(IIncrementSystem.increment, ())
Use
abi.encodeCall
(opens in a new tab) to create the calldata. The first parameter is a pointer to the function. The second parameter is a tuple (opens in a new tab) with the function parameters. In this case, there aren't any.The advantage of
abi.encodeCall
is that it checks the types of the function parameters are correct.SystemSwitch.call( abi.encodeCall(...) )
Using
SystemSwitch.call
with the calldata created byabi.encodeCall
.SystemSwitch.call
takes care of figuring out details, such as what type of call to use.uint32 retval = abi.decode( SystemSwitch.call(...), (uint32) );
Use
abi.decode
(opens in a new tab) to decode the call's return value. The second parameter is the data type (or types if there are multiple return values).
Registering systems
For a System
to be callable from a World
it has to be registered (opens in a new tab).
Only the namespace owner can register a System
in a namespace.
System
s can be registered once per World
, but the same system can be registered in multiple World
s.
If you need multiple instances of a System
in the same world, you can deploy the System
multiple times and register the individual deployments individually.
Upgrading systems
The namespace owner can upgrade a System
.
This is a two-step process: deploy the contract for the new System
and then call registerSystem
with the same ResourceId
as the old one and the new contract address.
This upgrade process removes the old System
contract's access to the namespace, and gives access to the new contract.
Any access granted manually to the old System
is not revoked, nor granted to the upgraded System
.
Note: You should make sure to remove any such manually granted access.
System
access is based on the contract address, so somebody else could register a namespace they'd own, register the old System
contract as a system in their namespace, and then abuse those permissions (if the System
has code that can be used for that, of course).
Access control
When you register a System
, you can specify whether it is going to be private or public.
-
A public
System
has no access control checks, it can be called by anybody. This is the main mechanism for user interaction with a MUD application. -
A private
System
can only be called by accounts that have access. This access can be the result of:- Access permission to the namespace in which the
System
is registered. - Access permission specifically to the
System
.
- Access permission to the namespace in which the
Note that System
s have access to their own namespace by default, so public System
s can call private System
s in their namespace.
Root systems
The World
uses call
for systems in other namespaces, but delegatecall
for those in the root namespace (bytes14(0)
).
As a result, root systems have access to the World
contract's storage.
Because of this access, root systems use the internal StoreCore
methods (opens in a new tab), which are slightly cheaper than calling the external IStore
methods (opens in a new tab) used by other systems.
Note that the table libraries abstract this difference, so normally there is no reason to be concerned about it.
Another effect of having access to the storage of the World
is that root systems could, in theory, overwrite any information in any table regardless of access control.
Only the owner of the root namespace can register root systems.
We recommend to only use the root namespace when strictly necessary.