Introduction
Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.
I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.
I also build and publish a variety of tools, libraries, and open-source projects.
This article examines ERC7509, a proposal addressing security and data consistency when adopting the Entity Component System (ECS) design pattern in smart contract development.
The following material is translated, summarized, and supplemented with explanations.
I have also covered various other EIPs.
Overview
This proposal defines a minimal Entity Component System (ECS). In ECS, unique identifiers called entities are associated with multiple components (data), which are then processed by systems (logic). The proposal standardizes interfaces for using ECS in smart contracts and provides basic functionality for composing and managing multiple contract applications.
ECS is a design pattern commonly used in game development. This proposal applies it to smart contracts. An entity is simply an identifier with no functionality of its own. A component is a collection of data that gives an entity particular attributes or state. A system uses this data to perform operations.
Consider a game running on a blockchain. Players, items, and enemies are entities. Components such as position, health, and attack power can be assigned to these entities. The game logic (systems) reads these components and processes actions such as player movement and combat.
The proposal defines a standard way to use ECS in smart contracts. This is expected to make complex applications easier to build.
Motivation
Entity Component System (ECS) is a design pattern that improves code reuse by separating data from behavior. Although primarily used in game development, the proposed system implements ECS with smart contracts, eliminates unnecessary complexity, and introduces features suited to contract interactions. Components and systems can be composed freely and easily. The following are the benefits of adopting ECS for smart contract developers.
Benefits of a Simple Design
By separating data (components) from behavior (systems), ECS adopts a simple design based on decoupling, encapsulation, and modularity. This simplifies game and application architecture.
Decoupling is a design technique that minimizes dependencies between the parts of a system or program. Each part can function independently, reducing the impact of changes or updates on other parts. Decoupling improves code reuse and simplifies maintenance and extension. It also simplifies testing and debugging, improving overall development efficiency.
Flexible Composition
Each entity can combine different components, and different systems can be defined to manipulate the data of new entities. This makes applications easier to extend and customize.
Extensibility
ECS is well suited to extensions: defining new components and systems can allow two games or applications to interact.
Easier Feature Additions and Upgrades
Because data and behavior are separate, features and upgrades can be introduced without affecting existing data.
Easier Management
When an application comprises multiple contracts, ECS helps manage the state of each contract effectively.
Component Reuse
Components are reusable, and sharing them with the community can improve the development efficiency of other developers.
The proposal is expected to make smart contract application development more flexible, reusable, and manageable. ECS enables more efficient and extensible applications and games.
Specification
The general workflow for building an ECS-based program defines the application structure using three main elements: entities, components, and systems. A world contract acts as a container for these elements and manages them. The following steps illustrate how to build an ECS-based program.
-
Create a world contract
- Create a world contract implementing the
IWorldinterface. - This contract establishes and manages the relationships between entities, component contracts, and system contracts.
- Create a world contract implementing the
-
Create an entity
- Call the world contract's
createEntity()function to create an entity. - An entity is a unique identifier that can have multiple components.
- Call the world contract's
-
Create a component contract
- Create a component contract implementing the
IComponentinterface. - Components are data containers attached to entities.
- Create a component contract implementing the
-
Register a component
- Call
registerComponent()on the world contract to register the component contract.
- Call
-
Attach the component to an entity
- Call
addComponent()on the world contract to attach a component to an entity.
- Call
-
Create a system contract
- Create a system contract without interface restrictions.
- System contracts can define arbitrary functions to modify entity data (components) dynamically.
-
Register a system
- Call
registerSystem()on the world contract to register the system contract.
- Call
-
Execute the system
- Finally, execute the registered system to change the entity's state.
These steps allow developers to build flexible, extensible applications combining entities (objects), components (data), and systems (logic). The world contract integrates these elements and manages application state effectively.
IWorld.sol
// SPDX-License-Identifier: CC0-1.0
pragma solidity >=0.8.0;
interface IWorld {
/**
* Create a new entity.
* @dev The entity MUST be assigned a unique Id.
* If the state of the entity is true, it means it is available, and if it is false, it means it is not available.
* When the state of the entity is false, you cannot add or remove components for the entity.
* @return New entity id.
*/
function createEntity() external returns (uint256);
/**
* Does the entity exist in the world.
* @param _entityId is the Id of the entity.
* @return true exists, false does not exist.
*/
function entityExists(uint256 _entityId) external view returns (bool);
/**
* Get the total number of entities in the world.
* @return The total number of entities.
*/
function getEntityCount() external view returns (uint256);
/**
* Set the state of an entity.
* @dev Entity MUST exist.
* @param _entityId is the Id of the entity.
* @param _entityState is the state of the entity, true means available, false means unavailable.
*/
function setEntityState(uint256 _entityId, bool _entityState) external;
/**
* Get the state of an entity.
* @param _entityId Id of the entity.
* @return The current state of the entity.
*/
function getEntityState(uint256 _entityId) external view returns (bool);
/**
* Register a component to the world.
* @dev A component MUST be registered with the world before it can be attached to an entity.
* MUST NOT register the same component to the world repeatedly.
* It SHOULD be checked that the contract address returned by world() of the component contract is the same as the current world contract.
* The state of the component is true means it is available, and false means it is not available. When the component state is set to false, it cannot be attached to the entity.
* @param _componentAddress is the contract address of the component.
*/
function registerComponent(address _componentAddress) external;
/**
* Does the component exist in the world.
* @param _componentAddress is the contract address of the component.
* @return true exists, false does not exist.
*/
function componentExists(address _componentAddress)
external
view
returns (bool);
/**
* Get the contract addresses of all components registered in the world.
* @return Array of contract addresses.
*/
function getComponents() external view returns (address[] memory);
/**
* Set component state.
* @dev Component MUST exist.
* @param _componentAddress is the contract address of the component.
* @param _componentState is the state of the component, true means available, false means unavailable.
*/
function setComponentState(address _componentAddress, bool _componentState)
external;
/**
* Get the state of a component.
* @param _componentAddress is the contract address of the component.
* @return true means available, false means unavailable.
*/
function getComponentState(address _componentAddress)
external
view
returns (bool);
/**
* Attach a component to the entity.
* @dev Entity MUST be available.Component MUST be available.A component MUST NOT be added to an entity repeatedly.
* @param _entityId is the Id of the entity.
* @param _componentAddress is the address of the component to be attached.
*/
function addComponent(uint256 _entityId, address _componentAddress)
external;
/**
* Whether the entity has a component attached,
* @dev Entity MUST exist.Component MUST be registered.
* @param _entityId is the Id of the entity.
* @param _componentAddress is the component address.
* @return true is attached, false is not attached
*/
function hasComponent(uint256 _entityId, address _componentAddress)
external
view
returns (bool);
/**
* Remove a component from the entity.
* @dev Entity MUST be available.The component MUST have been added to the entity before.
* @param _entityId is the Id of the entity.
* @param _componentAddress is the address of the component to be removed.
*/
function removeComponent(uint256 _entityId, address _componentAddress)
external;
/**
* Get the contract addresses of all components attached to the entity.
* @dev Entity MUST exist.
* @param _entityId is the Id of the entity.
* @return An array of contract addresses of the components owned by this entity.
*/
function getEntityComponents(uint256 _entityId)
external
view
returns (address[] memory);
/**
* Register a system to the world.
* @dev MUST NOT register the same system to the world repeatedly.The system state is true means available, false means unavailable.
* @param _systemAddress is the contract address of the system.
*/
function registerSystem(address _systemAddress) external;
/**
* Does the system exist in the world.
* @param _systemAddress is the contract address of the system.
* @return true exists, false does not exist.
*/
function systemExists(address _systemAddress) external view returns (bool);
/**
* Get the contract addresses of all systems registered in the world.
* @return Array of contract addresses.
*/
function getSystems() external view returns (address[] memory);
/**
* Set the system State.
* @dev System MUST exist.
* @param _systemAddress is the contract address of the system.
* @param _systemState is the state of the system.
*/
function setSystemState(address _systemAddress, bool _systemState) external;
/**
* Get the state of a system.
* @param _systemAddress is the contract address of the system.
* @return The state of the system.
*/
function getSystemState(address _systemAddress)
external
view
returns (bool);
}createEntity
function createEntity() external returns (uint256);Overview
Creates a new entity.
Details
This function creates a new entity with a unique ID.
The entity is enabled when its state is true and disabled when it is false.
When an entity's state is false, components cannot be added to or removed from it.
Return Value
uint256- The ID of the new entity.
entityExists
function entityExists(uint256 _entityId) external view returns (bool);Overview
Checks whether an entity exists in the world.
Details
This function checks whether the entity with the specified ID exists in the world.
It returns true if the entity exists and false otherwise.
Arguments
_entityId- The ID of the entity to check.
Return Value
bool- Whether the entity exists.
getEntityCount
function getEntityCount() external view returns (uint256);Overview
Retrieves the total number of entities in the world.
Details
This function returns the total number of entities registered in the world.
Return Value
uint256- The total number of entities.
setEntityState
function setEntityState(uint256 _entityId, bool _entityState) external;Overview
Sets the state of an entity.
Details
This function sets the state of the entity with the specified ID.
The entity must already exist.
The entity is enabled when _entityState is true and disabled when it is false.
Arguments
_entityId- The ID of the entity whose state is being set.
_entityState- The new state of the entity.
getEntityState
function getEntityState(uint256 _entityId) external view returns (bool);Overview
Retrieves the state of an entity.
Details
This function retrieves the current state of the entity with the specified ID.
An enabled state is represented by true and a disabled state by false.
Arguments
_entityId- The ID of the entity whose state is being checked.
Return Value
bool- The current state of the entity.
registerComponent
function registerComponent(address _componentAddress) external;Overview
Registers a component in the world.
Details
A component must be registered in the world before it can be attached to an entity.
The same component cannot be registered repeatedly.
The address returned by the component contract's world() function should match the address of the current world contract.
A component is enabled when its state is true and disabled when it is false.
A component whose state is false cannot be attached to an entity.
Arguments
_componentAddress- The contract address of the component to register.
componentExists
function componentExists(address _componentAddress) external view returns (bool);Overview
Checks whether a component exists in the world.
Details
This function checks whether the specified component address exists in the world.
It returns true if the component exists and false otherwise.
Arguments
_componentAddress- The contract address of the component to check.
Return Value
bool- Whether the component exists.
getComponents
function getComponents() external view returns (address[] memory);Overview
Retrieves the contract addresses of all components registered in the world.
Details
This function returns an array of the contract addresses of all components registered in the world.
Return Value
address[] memory- An array of the contract addresses of all registered components.
setComponentState
function setComponentState(address _componentAddress, bool _componentState) external;Overview
Sets the state of a component.
Details
This function sets the state of the component at the specified component address.
The component must already exist.
The component is enabled when _componentState is true and disabled when it is false.
Arguments
_componentAddress- The contract address of the component whose state is being set.
_componentState- The new state of the component.
getComponentState
function getComponentState(address _componentAddress) external view returns (bool);Overview
Retrieves the state of a component.
Details
This function retrieves the current state of the component at the specified component address.
An enabled state is represented by true and a disabled state by false.
Arguments
_componentAddress- The contract address of the component whose state is being checked.
Return Value
bool- The current state of the component.
addComponent
function addComponent(uint256 _entityId, address _componentAddress) external;Overview
Attaches a component to an entity.
Details
This function attaches the component at the specified address to the entity with the specified ID. Both the entity and the component must be enabled. The same component cannot be added to an entity repeatedly.
Arguments
_entityId- The ID of the entity to which the component will be attached.
_componentAddress- The address of the component to attach.
hasComponent
function hasComponent(uint256 _entityId, address _componentAddress) external view returns (bool);Overview
Checks whether a particular component is attached to an entity.
Details
This function checks whether the component at the specified address is attached to the entity with the specified ID. The entity must exist and the component must be registered.
Arguments
_entityId- The ID of the entity whose component is being checked.
_componentAddress- The address of the component to check.
Return Value
booltrueif the component is attached, andfalseotherwise.
removeComponent
function removeComponent(uint256 _entityId, address _componentAddress) external;Overview
Removes a component from an entity.
Details
This function removes the component at the specified address from the entity with the specified ID. The entity must be enabled, and the component must have previously been added to it.
Arguments
_entityId- The ID of the entity from which the component will be removed.
_componentAddress- The address of the component to remove.
getEntityComponents
function getEntityComponents(uint256 _entityId) external view returns (address[] memory);Overview
Retrieves the addresses of all components attached to an entity.
Details
This function returns an array of the addresses of all components attached to the entity with the specified ID. The entity must exist.
Arguments
_entityId- The ID of the entity whose component addresses are being retrieved.
Return Value
address[] memory- An array of the addresses of all components attached to the entity.
registerSystem
function registerSystem(address _systemAddress) external;Overview
Registers a system in the world.
Details
This function registers the system at the specified address in the world.
The same system cannot be registered in the world repeatedly.
The system is enabled when its state is true and disabled when it is false.
Arguments
_systemAddress- The contract address of the system to register.
systemExists
function systemExists(address _systemAddress) external view returns (bool);Overview
Checks whether a system exists in the world.
Details
This function checks whether the system at the specified address exists in the world.
Arguments
_systemAddress- The contract address of the system to check.
Return Value
booltrueif the system exists, andfalseotherwise.
getSystems
function getSystems() external view returns (address[] memory);Overview
Retrieves the contract addresses of all systems registered in the world.
Details
This function returns an array of the contract addresses of all systems registered in the world.
Return Value
address[] memory- An array of the contract addresses of all registered systems.
setSystemState
function setSystemState(address _systemAddress, bool _systemState) external;Overview
Sets the state of a system.
Details
This function sets the state of the system at the specified address.
The system must already exist.
The system is enabled when _systemState is true and disabled when it is false.
Arguments
_systemAddress- The contract address of the system whose state is being set.
_systemState- The new state of the system.
getSystemState
function getSystemState(address _systemAddress) external view returns (bool);Overview
Retrieves the state of a system.
Details
This function retrieves the current state of the system at the specified address.
An enabled state is represented by true and a disabled state by false.
Arguments
_systemAddress- The contract address of the system whose state is being checked.
Return Value
bool- The current state of the system.
IComponent.sol
// SPDX-License-Identifier: CC0-1.0
pragma solidity >=0.8.0;
import "./Types.sol";
interface IComponent {
/**
* The world contract address registered by the component.
* @return world contract address.
*/
function world() external view returns (address);
/**
*Get the data type and get() parameter type of the component
* @dev SHOULD Import Types Library, which is an enumeration Library containing all data types.
* Entity data can be stored according to the data type.
* The get() parameter data type can be used to get entity data.
* @return the data type array of the entity
* @return get parameter data type array
*/
function types()
external
view
returns (Types.Type[] memory, Types.Type[] memory);
/**
*Store entity data.
* @dev entity MUST be available. The system that operates on it MUST be available.
* The entity has the component attached.
* @param _entityId is the Id of the entity.
* @param _data is the data to be stored.
*/
function set(uint256 _entityId, bytes memory _data) external;
/**
*Get the data of the entity according to the entity Id.
* @param _entityId is the Id of the entity.
* @return Entity data.
*/
function get(uint256 _entityId) external view returns (bytes memory);
/** Get the data of the entity according to the entity Id and parameters.
* @param _entityId is the Id of the entity.
* @param _params is an extra parameter, it SHOULD depend on whether you need it.
* @return Entity data.
*/
function get(uint256 _entityId, bytes memory _params)
external
view
returns (bytes memory);
}world
function world() external view returns (address);Overview
Retrieves the address of the world contract with which the component is registered.
Details
This function returns the address of the world contract in which the component was registered. It identifies the world contract to which the component belongs.
Return Value
address- The address of the world contract.
types
function types() external view returns (Types.Type[] memory, Types.Type[] memory);Overview
Retrieves the entity data types and the parameter types of the get() function.
Details
This function assumes that the Types library has been imported.
Types is a library containing an enumeration of all data types.
Entity data is stored according to its data types.
The parameter data types of get() are used when retrieving entity data.
Return Value
Types.Type[] memory- An array of entity data types.
Types.Type[] memory- An array of data types for get parameters.
set
function set(uint256 _entityId, bytes memory _data) external;Overview
Stores entity data.
Details
This function stores data for an enabled entity. The system performing the operation must also be enabled. The entity must have the specified component attached.
Arguments
_entityId- The ID of the entity whose data is being stored.
_data- The data to store.
get
function get(uint256 _entityId) external view returns (bytes memory);Overview
Retrieves entity data by entity ID.
Details
This function retrieves the data of the entity with the specified ID.
Arguments
_entityId- The ID of the entity whose data is being retrieved.
Return Value
bytes memory- Entity data.
get
function get(uint256 _entityId, bytes memory _params) external view returns (bytes memory);Overview
Retrieves entity data using an entity ID and additional parameters.
Details
This function retrieves entity data using the specified entity ID and additional parameters. Additional parameters are used as needed.
Arguments
_entityId- The ID of the entity whose data is being retrieved.
_params- Additional parameters.
Return Value
bytes memory- Entity data.
Library
The Types.sol library contains an enumeration of the Solidity types used in the interfaces above.
Additional Details
Why Include Type Information Instead of a Simple Byte Array?
The primary reason for including type information is to ensure type correctness when using components and prevent potential errors or inconsistencies. External developers can set and retrieve data explicitly according to its type. This reduces bugs caused by unexpected data handling.
Why Distinguish Nonexistent Entities From Entities Whose State Is false?
An entity's state alone does not establish whether the entity actually exists.
If an external contributor creates a component based on an entity that does not exist, the component may serve no purpose.
Component creators should first check whether an entity exists. If it does, creating a component remains meaningful even if the entity's state is false.
They can wait until the entity's state becomes true before attaching the component.
Why Does getEntityComponents Return Component Addresses Instead of IDs?
getEntityComponents is designed to return the addresses of all components attached to an entity directly.
This avoids requiring users to request component addresses repeatedly when an entity has many component IDs.
Can registerComponent and registerSystem Be Made Accessible to External Contributors?
This depends on how open the application or game is intended to be.
When encouraging developer participation, components and systems they register should have an initial state of false, and their submitted code should be checked for malicious behavior before use.
They can then be enabled through setComponentState and setSystemState.
When Should the Component's get Function With Additional Parameters Be Used?
A component has two get functions.
One requires only an entity ID, while the other accepts _params as additional parameters for retrieving data.
For example, a component could store HP corresponding to an entity's level. To retrieve HP for a particular level, call the get function with the entity's level as _params.
Implementation
The implementation is available below.
Security
The following describes security and consistency considerations when implementing Entity Component System (ECS).
Importance of Type Information
- Purpose
- Ensure type correctness when using components and prevent potential errors or inconsistencies.
- Approach
- Include type information instead of a simple byte array so external developers can set and retrieve data according to its type.
Distinguishing Nonexistent Entities From Entities Whose State Is false
- Rationale
- An entity's state alone cannot establish whether it actually exists.
- Approach
- Check whether the entity exists before creating a component.
- If it exists, creating a component for attachment to the entity remains meaningful even if its state is false.
Design of the getEntityComponents Function
- Design choice
- Return the addresses of all components attached to an entity.
- Rationale
- An entity may have many component IDs, requiring users to request component addresses repeatedly otherwise.
External Access to registerComponent and registerSystem
- Policy
- This depends on the openness of the application or game.
- When encouraging developer participation, registered components and systems should initially have a state of
false, and their code should be checked for malicious behavior before use.
Using the set Function in Component Contracts
- Guideline
- Do not provide other functions for modifying entities. Within
set, check that both the entity and the system performing the operation are enabled.
- Do not provide other functions for modifying entities. Within
System Registration and Security Checks
- Process
- Once a system is registered in the world, it can manipulate component data for all entities in that world.
- The security of every system contract's code must therefore be checked and audited before registration.
Deprecation in New Versions
- Management
- Entities, component contracts, and system contracts deprecated in new versions must be disabled promptly using
setEntityState(),setComponentState(), andsetSystemState().
- Entities, component contracts, and system contracts deprecated in new versions must be disabled promptly using
Citation
Rickey (@HelloRickey), "ERC-7509: Entity Component System [DRAFT]," Ethereum Improvement Proposals, no. 7509, September 2023. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-7509.
Conclusion
This article covered ERC7509, a proposal addressing security and data consistency when adopting the Entity Component System (ECS) design pattern in smart contract development. I hope you found it useful.
If you have questions, feel free to contact me by direct message on Twitter.
I also share information on other platforms, so feel free to explore them.



