Skip to main content

Week11-9.13

内置合约

在conflux的v2版本的分叉后,增加了ConfluxContext, PoSRegister, CrossSpaceCall, ParamsControl四个内置合约,那么这四个内置合约实现了什么功能呢?可以参见本周的devtips

Day1 ConfluxContext

该内置合约提供了三个方法去查询conflux相关的信息,包括当前的epochNumber, posHeight和最终确认的pivot块。 该合约的地址为0x0888000000000000000000000000000000000004.

该合约的abi为:

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.15;

interface ConfluxContext {
    /*** Query Functions ***/
    /**
     * @dev get the current epoch number
     * @return the current epoch number
     */
    function epochNumber() external view returns (uint256);
    /**
     * @dev get the height of the referred PoS block in the last epoch
`    * @return the current PoS block height
     */
    function posHeight() external view returns (uint256);
    /**
     * @dev get the epoch number of the finalized pivot block.
     * @return the finalized epoch number
     */
    function finalizedEpochNumber() external view returns (uint256);
}

那么如何去调用该合约呢?以java sdk为例:

        
    public static void test(String contractAddr, String caller) throws Exception {
        Cfx cfx = Cfx.create("https://test.confluxrpc.com");
        Account acc = Account.create(cfx, caller);
        Address address = new Address(contractAddr);
        String hash = acc.call(address, "epochNumber");
        cfx.waitForReceipt(hash);
        Optional<Receipt> receipt = cfx.getTransactionReceipt(hash).sendAndGet();
        if (receipt.isPresent()) {
				.......
        } else {
           .......
        }

    }

Day2 PoSRegister

PoSRegister合约提供了一系列方法去和PoS链进行交互

该合约的方法介绍如下:

  • register - 将本地节点注册为pos链上的一个pos节点
  • increaseStake - 提高pos权重
  • retire - 降低pos权重
  • getVotes - 查询一个账户的vote信息
  • identifierToAddress - 查询pos账户的pow地址
  • addressToIdentifier - 查询一个pow账户的pos地址

该合约的合约地址为0x0888000000000000000000000000000000000005

该合约的abi如下:

// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0;

interface PoSRegister {
    /**
     * @dev Register PoS account
     * @param indentifier - PoS account address to register
     * @param votePower - votes count
     * @param blsPubKey - BLS public key
     * @param vrfPubKey - VRF public key
     * @param blsPubKeyProof - BLS public key's proof of legality, used to against some attack, generated by conflux-rust fullnode
     */
    function register(
        bytes32 indentifier,
        uint64 votePower,
        bytes calldata blsPubKey,
        bytes calldata vrfPubKey,
        bytes[2] calldata blsPubKeyProof
    ) external;

    /**
     * @dev Increase specified number votes for msg.sender
     * @param votePower - count of votes to increase
     */
    function increaseStake(uint64 votePower) external;

    /**
     * @dev Retire specified number votes for msg.sender
     * @param votePower - count of votes to retire
     */
    function retire(uint64 votePower) external;

    /**
     * @dev Query PoS account's lock info. Include "totalStakedVotes" and "totalUnlockedVotes"
     * @param identifier - PoS address
     */
    function getVotes(bytes32 identifier) external view returns (uint256, uint256);

    /**
     * @dev Query the PoW address binding with specified PoS address
     * @param identifier - PoS address
     */
    function identifierToAddress(bytes32 identifier) external view returns (address);

    /**
     * @dev Query the PoS address binding with specified PoW address
     * @param addr - PoW address
     */
    function addressToIdentifier(address addr) external view returns (bytes32);

    /**
     * @dev Emitted when register method executed successfully
     */
    event Register(bytes32 indexed identifier, bytes blsPubKey, bytes vrfPubKey);

    /**
     * @dev Emitted when increaseStake method executed successfully
     */
    event IncreaseStake(bytes32 indexed identifier, uint64 votePower);

    /**
     * @dev Emitted when retire method executed successfully
     */
    event Retire(bytes32 indexed identifier, uint64 votePower);
}

具体的介绍可见posRegister

那么如何去调用该合约呢?可以参考java-examples

Day3 CrossSpaceCall

Conflux通过分片,存在core space与esapce。core space与espace的账户间存在一一对应关系。CrossSpaceCall为用户提供了一系列方法实现两个space的交互。

该合约的方法如下:

  • createEVM - 在espace上部署一个合约
  • transferEVM - 向espace的映射账户转账
  • callEVM - 调用espace上的合约
  • staticCallEVM - 静态调用espace上的合约
  • withdrawFromMapped - 从espace上的账户中收回一部分的token
  • mappedBalance - 查询映射账户的余额
  • mappedNonce - 查询映射账户的nonce

该合约的地址为0x0888000000000000000000000000000000000006

该合约的abi如下:

// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0;

interface CrossSpaceCall {

    event Call(bytes20 indexed sender, bytes20 indexed receiver, uint256 value, uint256 nonce, bytes data);

    event Create(bytes20 indexed sender, bytes20 indexed contract_address, uint256 value, uint256 nonce, bytes init);

    event Withdraw(bytes20 indexed sender, address indexed receiver, uint256 value, uint256 nonce);

    event Outcome(bool success);

    function createEVM(bytes calldata init) external payable returns (bytes20);
    
    function transferEVM(bytes20 to) external payable returns (bytes memory output);

    function callEVM(bytes20 to, bytes calldata data) external payable returns (bytes memory output);

    function staticCallEVM(bytes20 to, bytes calldata data) external view returns (bytes memory output);

    function withdrawFromMapped(uint256 value) external;

    function mappedBalance(address addr) external view returns (uint256);

    function mappedNonce(address addr) external view returns (uint256);
}

具体的介绍可见CrossSpaceCall

那么如何去调用该合约呢?可以参考java-examples

那么如何将core space的地址转为espace的地址呢?java-sdk提供了方法

    public String getMappedEVMSpaceAddress() {
        String hexAddr =  this.getHexAddress();
        hexAddr = hexAddr.substring(2, hexAddr.length());
        byte[] t = Hash.sha3(Numeric.hexStringToByteArray(hexAddr));

        byte[] mappedBuf = new byte[20];
		   System.arraycopy(t, t.length - 20, mappedBuf, 0, 20);

        return Keys.toChecksumAddress("0x" + BaseEncoding.base16().encode(mappedBuf));
    }

Day4 ParamsControl

ParamsControl合约为在不用硬分叉的情况下去修改区块奖励等全局参数成为可能。该合约由CIP-94引入。

该合约的方法如下:

  • readVote - 查询指定账户的投票数据
  • castVote - 为链参数进行投票
  • readVote - 从espace上的账户中收回一部分的token
  • currentRound - 查询目前的投票轮数
  • totalVotes - 查询在指定的投票轮中的投票数量

该合约的地址为0x0888000000000000000000000000000000000006

该合约的abi如下:

// SPDX-License-Identifier: MIT

pragma solidity >=0.8.0;

interface ParamsControl {
    struct Vote {
        uint16 topic_index;
        uint256[3] votes;
    }

    /*** Query Functions ***/
    /**
     * @dev cast vote for parameters
     * @param vote_round The round to vote for
     * @param vote_data The list of votes to cast
     */
    function castVote(uint64 vote_round, Vote[] calldata vote_data) external;

    /**
     * @dev read the vote data of an account
     * @param addr The address of the account to read
     */
    function readVote(address addr) external view returns (Vote[] memory);

    /**
     * @dev Current vote round
     */
    function currentRound() external view returns (uint64);

    /**
     * @dev read the total votes of given round
     * @param vote_round The vote number
     */
    function totalVotes(uint64 vote_round) external view returns (Vote[] memory);

    event CastVote(uint64 indexed vote_round, address indexed addr, uint16 indexed topic_index, uint256[3] votes);
    event RevokeVote(uint64 indexed vote_round, address indexed addr, uint16 indexed topic_index, uint256[3] votes);
}

调用该合约的方法与更加详细的讲解可见JS交互方法

v2分叉后的内置合约讲完啦~其余的V1版本的合约介绍可见内置合约