当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 只有1个字节的文件实际占用多少磁盘空间
- Interface pressure test: installation, use and instruction of siege pressure test
- 如何对数据库账号权限进行精细化管理?
- Python saves the list data
- Behind the record breaking Q2 revenue of Alibaba cloud, the cloud opening mode is reshaping
- Python基础变量类型——List浅析
- FastThreadLocal 是什么鬼?吊打 ThreadLocal 的存在!!
- Analysis of serilog source code -- how to use it
- 文件过多时ls命令为什么会卡住?
- Five vuex plug-ins for your next vuejs project
猜你喜欢
给字节的学姐讲如何准备“系统设计面试”
What knowledge do Python automated testing learn?
Free patent download tutorial (HowNet, Espacenet)
零基础打造一款属于自己的网页搜索引擎
[Xinge education] poor learning host computer series -- building step 7 Simulation Environment
C# 调用SendMessage刷新任务栏图标(强制结束时图标未消失)
Using NLP and ml to extract and construct web data
Use modelarts quickly, zero base white can also play AI!
大道至简 html + js 实现最朴实的小游戏俄罗斯方块
How to hide part of barcode text in barcode generation software
随机推荐
Analysis of etcd core mechanism
Building and visualizing decision tree with Python
大会倒计时|2020 PostgreSQL亚洲大会-中文分论坛议程安排
新建一个空文件占用多少磁盘空间?
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
JNI-Thread中start方法的呼叫與run方法的回撥分析
Building a new generation cloud native data lake with iceberg on kubernetes
Shh! Is this really good for asynchronous events?
Python saves the list data
如何在终端启动Coda 2中隐藏的首选项?
Interface pressure test: installation, use and instruction of siege pressure test
開源一套極簡的前後端分離專案腳手架
Xmppmini project details: step by step from the principle of practical XMPP technology development 4. String decoding secrets and message package
StickEngine-架构12-通信协议
electron 實現檔案下載管理器
Behind the first lane level navigation in the industry
Try to build my mall from scratch (2): use JWT to protect our information security and perfect swagger configuration
GUI engine evaluation index
游戏开发中的新手引导与事件管理系统
这个项目可以让你在几分钟快速了解某个编程语言