当前位置:网站首页>Diamond standard
Diamond standard
2020-11-06 20:21:00 【How did her eyes turn when she was about to leave】
The diamond standard is mainly to match the smart contract size on Ethereum 24K Byte limit . At the same time, it can also be applied to deal with the problem of seamless upgrade of smart contracts . The diamond contract is such a contract : It calls the function call proxy (delegatecall) To contracts that have been deployed externally . Such externally deployed contracts are called diamond faces (facets)
standard
The diamond standard is defined in EIP2535 in . The standard text is in here .https://github.com/ethereum/EIPs/issues/2535
Example implementation
overview
diamondCut Function is used to contract upgrade function , Can increase , Replace and delete any function in the diamond contract . It receives a bytes[] Parameter input of type , Indicates the method needed to modify the internal mapping table - Diamonds face . such as , call diamondCut Function can be added to a transaction at one time 2 A new function , Replace 3 Function and delete 4 A function . meanwhile diamondCut Functions can trigger events , Record all additions , Replace and delete .
Magnifier (The Loupe) It is used to check the internal status of diamond contracts . The diamond contract provides 4 Function to provide the function and diamond face currently stored in the diamond contract . These functions are collectively referred to as magnifiers . All diamond contracts must implement these functions
Example explanation
Now let's diamond-1 For example
data structure
stay IDiamondCut.sol, There are the following data structures :
struct FacetCut {
address facetAddress; // The current diamond face (Facet) The address of
FacetCutAction action; // At present DiamondCut The operation of , Additions and deletions
bytes4[] functionSelectors; // The diamond face (Facet) The set of supported function selectors
}
stay IFacet.sol, There are the following data structures :
struct Facet {
address facetAddress; // This diamond face (Facet) Address
bytes4[] functionSelectors; // This diamond face (Facet) The set of supported function selectors
}
IDiamondLoupe.sol Defines what needs to be implemented in 4 A function :
/// @notice Gets all facet addresses and their four byte function selectors.
/// @return facets_ Facet
function facets() external view returns (Facet[] memory facets_);
/// @notice Gets all the function selectors supported by a specific facet.
/// @param _facet The facet address.
/// @return facetFunctionSelectors_
function facetFunctionSelectors(address _facet) external view returns (bytes4[] memory facetFunctionSelectors_);
/// @notice Get all the facet addresses used by a diamond.
/// @return facetAddresses_
function facetAddresses() external view returns (address[] memory facetAddresses_);
/// @notice Gets the facet that supports the given selector.
/// @dev If facet is not found return address(0).
/// @param _functionSelector The function selector.
/// @return facetAddress_ The facet address.
function facetAddress(bytes4 _functionSelector) external view returns (address facetAddress_);
stay LibDiamond.sol in , Define the following data structure
struct FacetAddressAndSelectorPosition {
address facetAddress;
uint16 selectorPosition;
}
struct DiamondStorage {
// function selector => facet address and selector position in selectors array
mapping(bytes4 => FacetAddressAndSelectorPosition) facetAddressAndSelectorPosition;
bytes4[] selectors;
mapping(bytes4 => bool) supportedInterfaces;
// owner of the contract
address contractOwner;
}
Packaging contract
Dianmond Contracts are the standard entry point .Diamond In addition to some initialization work in the contract , Most of all, the following Proxy function .
The following procedure points :
- ds.slot It's an inline assembly , It means getting state variables ds Of Slot( Storage tank ) Location . see https://solidity.readthedocs.io/en/v0.7.4/assembly.html
- Get through the function selector passed by the call facet The address of
- adopt Delegatecall Assemble instructions to call the corresponding diamond face (facet) The function in
fallback() external payable {
LibDiamond.DiamondStorage storage ds;
bytes32 position = LibDiamond.DIAMOND_STORAGE_POSITION;
assembly {
ds.slot := position
}
address facet = address(bytes20(ds.facetAddressAndSelectorPosition[msg.sig].facetAddress));
require(facet != address(0), "Diamond: Function does not exist");
assembly {
calldatacopy(0, 0, calldatasize())
let result := delegatecall(gas(), facet, 0, calldatasize(), 0, 0)
returndatacopy(0, 0, returndatasize())
switch result
case 0 {
revert(0, returndatasize())
}
default {
return(0, returndatasize())
}
}
}
reference
- https://medium.com/1milliondevs/new-storage-layout-for-proxy-contracts-and-diamonds-98d01d0eadb
- https://dev.to/mudgen/understanding-diamonds-on-ethereum-1fb
- https://learnblockchain.cn/article/1398
- https://naturaldao.io/collaboration/blog-cn/113-eip-2535%E5%8D%B3%E9%92%BB%E7%9F%B3%E6%A0%87%E5%87%86%E4%BB%8B%E7%BB%8D.html
- https://medium.com/1milliondevs/solidity-storage-layout-for-proxy-contracts-and-diamonds-c4f009b6903
- https://hiddentao.com/archives/2020/05/28/upgradeable-smart-contracts-using-diamond-standard
- https://hiddentao.com/archives/2019/10/03/upgradeable-smart-contracts-with-eternal-storage
- https://hiddentao.com/archives/2020/03/19/nested-delegate-call-in-solidity
- https://hiddentao.com/archives/2020/05/28/upgradeable-smart-contracts-using-diamond-standard
版权声明
本文为[How did her eyes turn when she was about to leave]所创,转载请带上原文链接,感谢
边栏推荐
- A course on word embedding
- C#和C/C++混合编程系列5-内存管理之GC协同
- 大道至简 html + js 实现最朴实的小游戏俄罗斯方块
- What knowledge do Python automated testing learn?
- [C] (original) step by step teach you to customize the control element - 04, ProgressBar (progress bar)
- With the advent of tensorflow 2.0, can pytoch still shake the status of big brother?
- JNI-Thread中start方法的呼叫與run方法的回撥分析
- html+vue.js 實現分頁可相容IE
- Outsourcing is really difficult. As an outsourcer, I can't help sighing.
- It's time for your financial report to change to a more advanced style -- financial analysis cockpit
猜你喜欢
What are the criteria for selecting a cluster server?
消息队列(MessageQueue)-分析
Unity性能优化整理
What knowledge do Python automated testing learn?
JVM内存分配 -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m
Music generation through deep neural network
Staying up late summarizes the key points of report automation, data visualization and mining, which is different from what you think
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
What are the common problems of DTU connection
StickEngine-架构11-消息队列(MessageQueue)
随机推荐
StickEngine-架构11-消息队列(MessageQueue)
Interface pressure test: installation, use and instruction of siege pressure test
Analysis of etcd core mechanism
Unity性能优化整理
Construction of encoder decoder model with keras LSTM
JVM内存分配 -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m
常用SQL语句总结
Python基础变量类型——List浅析
Named entity recognition in natural language processing: tanford core LP ner (1)
Discussion on the development practice of aspnetcore, a cross platform framework
MeterSphere开发者手册
視覺滾動[反差美]
Elasticsearch Part 6: aggregate statistical query
零基础打造一款属于自己的网页搜索引擎
The dynamic thread pool in Kitty supports Nacos and Apollo multi configuration centers
Solve the problem of database insert data garbled in PL / SQL developer
How to demote domain controllers and later in Windows Server 2012
消息队列(MessageQueue)-分析
TensorFlow中的Tensor是什么?
【:: 是什么语法?】