当前位置:网站首页>Diamond standard

Diamond standard

2020-11-06 20:21:55 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

Here's the author of the diamond standard Nick Mudge Several standards are provided to implement
- [ diamond-1 ] ( https://github.com/mudgen/diamond-1 )
- [ diamond-2 ] ( https://github.com/mudgen/diamond-2 )
- [ diamond-3 ] ( https://github.com/mudgen/diamond-3 )

 

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

  1. https://medium.com/1milliondevs/new-storage-layout-for-proxy-contracts-and-diamonds-98d01d0eadb
  2. https://dev.to/mudgen/understanding-diamonds-on-ethereum-1fb
  3. https://learnblockchain.cn/article/1398
  4. 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
  5. https://medium.com/1milliondevs/solidity-storage-layout-for-proxy-contracts-and-diamonds-c4f009b6903
  6. https://hiddentao.com/archives/2020/05/28/upgradeable-smart-contracts-using-diamond-standard
  7. https://hiddentao.com/archives/2019/10/03/upgradeable-smart-contracts-with-eternal-storage
  8. https://hiddentao.com/archives/2020/03/19/nested-delegate-call-in-solidity
  9. https://hiddentao.com/archives/2020/05/28/upgradeable-smart-contracts-using-diamond-standard

 

版权声明
本文为[How did her eyes turn when she was about to leave]所创,转载请带上原文链接,感谢