当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 【自学unity2d传奇游戏开发】地图编辑器
- 零基础打造一款属于自己的网页搜索引擎
- Lane change detection
- Analysis of ThreadLocal principle
- hdu3974 Assign the task線段樹 dfs序
- 前端未來趨勢之原生API:Web Components
- MongoDB与SQL常用语法对应表
- Introduction to quantitative investment and Trading (Python introduction to financial analysis)
- The dynamic thread pool in Kitty supports Nacos and Apollo multi configuration centers
- Construction of encoder decoder model with keras LSTM
猜你喜欢
C#和C/C++混合编程系列5-内存管理之GC协同
【字节跳动 秋招岗位开放啦】Ohayoo!放学别走,我想约你做游戏!!!
Unity性能优化整理
It's time for your financial report to change to a more advanced style -- financial analysis cockpit
百万年薪,国内工作6年的前辈想和你分享这四点
FastThreadLocal 是什么鬼?吊打 ThreadLocal 的存在!!
It's easy to operate. ThreadLocal can also be used as a cache
【自学unity2d传奇游戏开发】地图编辑器
If PPT is drawn like this, can the defense of work report be passed?
零基础打造一款属于自己的网页搜索引擎
随机推荐
How to get started with new HTML5 (2)
Details of dapr implementing distributed stateful service
Staying up late summarizes the key points of report automation, data visualization and mining, which is different from what you think
How to demote domain controllers and later in Windows Server 2012
Pattern matching: The gestalt approach一种序列的文本相似度方法
The AI method put forward by China has more and more influence. Tianda et al. Mined the development law of AI from a large number of literatures
Introduction to quantitative investment and Trading (Python introduction to financial analysis)
It is really necessary to build a distributed ID generation service
Analysis of partial source codes of qthread
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
Five vuex plug-ins for your next vuejs project
Behind the record breaking Q2 revenue of Alibaba cloud, the cloud opening mode is reshaping
Discussion on the technical scheme of text de duplication (1)
Python基础变量类型——List浅析
C#和C/C++混合编程系列5-内存管理之GC协同
一篇文章教会你使用Python网络爬虫下载酷狗音乐
Basic usage of GDB debugging
nacos、ribbon和feign的簡明教程
Shh! Is this really good for asynchronous events?
Vite + TS quickly build vue3 project and introduce related features