当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Using NLP and ml to extract and construct web data
- Pattern matching: The gestalt approach一种序列的文本相似度方法
- C語言I部落格作業03
- Get twice the result with half the effort: automation without cabinet
- What are Devops
- 前端未來趨勢之原生API:Web Components
- 2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
- (1) ASP.NET Introduction to core3.1 Ocelot
- Try to build my mall from scratch (2): use JWT to protect our information security and perfect swagger configuration
- 给字节的学姐讲如何准备“系统设计面试”
猜你喜欢
Wow, elasticsearch multi field weight sorting can play like this
How to turn data into assets? Attracting data scientists
如何对数据库账号权限进行精细化管理?
美团内部讲座|周烜:华东师范大学的数据库系统研究
Digital city responds to relevant national policies and vigorously develops the construction of digital twin platform
If PPT is drawn like this, can the defense of work report be passed?
一篇文章教会你使用Python网络爬虫下载酷狗音乐
一部完整的游戏,需要制作哪些音乐?
Lane change detection
Free patent download tutorial (HowNet, Espacenet)
随机推荐
[actual combat of flutter] pubspec.yaml Configuration file details
Analysis of query intention recognition
Solve the problem of database insert data garbled in PL / SQL developer
Flink的DataSource三部曲之一:直接API
用一个例子理解JS函数的底层处理机制
Lane change detection
If PPT is drawn like this, can the defense of work report be passed?
What knowledge do Python automated testing learn?
TensorFlow中的Tensor是什么?
Discussion on the technical scheme of text de duplication (1)
What are the criteria for selecting a cluster server?
仅用六种字符来完成Hello World,你能做到吗?
MeterSphere开发者手册
The dynamic thread pool in Kitty supports Nacos and Apollo multi configuration centers
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王涛
In depth to uncover the bottom layer of garbage collection, this time let you understand her thoroughly
The difference between gbdt and XGB, and the mathematical derivation of gradient descent method and Newton method
Python基础数据类型——tuple浅析
DRF JWT authentication module and self customization
Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?