Explore Contracts in TEN
1. Accessing Storage Values
While both Ethereum and TEN allow easy access to public variables, their handling of private variables differs significantly, highlighting Ethereum's transparency challenges and TEN's privacy solutions.
Ethereum's Transparency Challenge
In Ethereum, private variables are intended to be accessed solely through functions. However, due to Ethereum's transparent nature, a workaround exists using the getStorageAt
function. This method can bypass the designated functions, making true private data storage unattainable.
Example: Accessing a private variable in Ethereum:
uint256 value = eth.getStorageAt(contractAddress, position);
TEN's Privacy Solution
To provide privacy on Ethereum, TEN has disabled the getStorageAt
function. This ensures that private variables can only be accessed via their associated functions, providing genuine programmable privacy.
Example: Accessing a private variable in TEN:
private uint256 privateVariable;
function getPrivateVariable() public view returns (uint256) {
return privateVariable;
}
In summary, while Ethereum's transparency poses challenges for true data privacy, TEN offers a robust solution by ensuring that private data remains genuinely private.
2. Access Control for Functions
In smart contract development, it's essential to ensure that only authorized entities can access certain functions. This is achieved using access control mechanisms.
Access Control Using require
require
The require
statement in Solidity is a straightforward way to enforce access control. It checks a condition, and if the condition is not met, the function execution stops, and an optional error message is thrown.
Example:
address owner = msg.sender;
function restrictedFunction() public {
require(msg.sender == owner, "Only the owner can call this function.");
// Rest of the function logic
}
This example ensures that only the contract's owner can call the restrictedFunction
.
3. Event Visibility
TEN has specific event visibility rules:
Lifecycle events without an address parameter are public.
Events with an address parameter related to an account are private.
Example:
// Public event on TEN
event LifecycleEvent(uint256 indexed value);
// Private event on TEN
event AccountEvent(address indexed account, uint256 value);
Last updated