All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.citahub.cita.protocol.system.CITASystemContract Maven / Gradle / Ivy

The newest version!
package com.citahub.cita.protocol.system;

import com.citahub.cita.abi.FunctionEncoder;
import com.citahub.cita.abi.FunctionReturnDecoder;
import com.citahub.cita.abi.TypeReference;
import com.citahub.cita.abi.datatypes.Function;
import com.citahub.cita.abi.datatypes.Type;
import com.citahub.cita.protocol.CITAj;
import com.citahub.cita.protocol.core.DefaultBlockParameter;
import com.citahub.cita.protocol.core.DefaultBlockParameterName;
import com.citahub.cita.protocol.core.methods.request.Call;
import com.citahub.cita.protocol.core.methods.request.Transaction;
import com.citahub.cita.protocol.core.methods.response.AppCall;
import com.citahub.cita.protocol.core.methods.response.AppSendTransaction;
import com.citahub.cita.protocol.core.methods.response.Log;
import com.citahub.cita.protocol.core.methods.response.TransactionReceipt;

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Please check system smart contracts at
 * https://github.com/citahub/cita/tree/develop/scripts/contracts/src
 */

public interface CITASystemContract {
    DefaultBlockParameter DEFAULT_BLOCK_PARAMETER
            = DefaultBlockParameterName.PENDING;

    Long DEFAULT_QUOTA = 10000000L;

    //Syc manager
    String SYC_MANAGER_ADDR = "0xffffffffffffffffffffffffffffffffff020000";
    String SYC_MANAGER_SET_BLOCK_INTERVAL = "setBlockInterval";

    //Allowance
    String ALLOWANCE_ADDR = "0xffffffffffffffffffffffffffffffffff020041";
    String ALLOWANCE_IN_ALLOWANCE = "inAllowance";
    String ALLOWANCE_QUERY_INFO = "queryInfo";
    String ALLOWANCE_QUERY_NAME = "queryName";
    String ALLOWANCE_QUERY_DOORNUM = "queryDoornum";

    //Allowance manager
    String ALLOWANCE_MANAGER_ADDR = "0xffffffffffffffffffffffffffffffffff020040";
    String ALLOWANCE_MANAGER_ADD_DOORNUMS = "addDoornums";
    String ALLOWANCE_MANAGER_UPDATE_DOORNUM = "updateDoornum";
    String ALLOWANCE_MANAGER_DELETE_DOORNUMS = "deleteDoornums";
    String ALLOWANCE_MANAGER_UPDATE_ALLOWANCE_NAME = "updateAllowanceName";

    //Cert manager
    String CER_MANAGER_ADDR = "0xffffffffffffffffffffffffffffffffff020030";
    String CER_MANAGER_REVOKE = "revoke";
    String CER_MANAGER_GETCRL = "getCrl";

    //Node manager
    String NODE_MANAGER_ADDR = "0xffffffffffffffffffffffffffffffffff020001";
    String NODE_MANAGER_LIST_NODE = "listNode";
    String NODE_MANAGER_GET_STATUS = "getStatus";
    String NODE_MANAGER_LIST_STAKE = "listStake";
    String NODE_MANAGER_STAKE_PERMILLAGE = "stakePermillage";
    String NODE_MANAGER_APPROVE_NODE = "approveNode";
    String NODE_MANAGER_DELETE_NODE = "deleteNode";
    String NODE_MANAGER_SET_STAKE = "setStake";

    //Quota manager
    String QUOTA_MANAGER_ADDR = "0xffffffffffffffffffffffffffffffffff020003";
    String QUOTA_MANAGER_SET_BQL = "setBQL";
    String QUOTA_MANAGER_SET_DEFAULT_AQL = "setDefaultAQL";
    String QUOTA_MANAGER_SET_AQL = "setAQL";
    String QUOTA_MANAGER_GET_BQL = "getBQL";
    String QUOTA_MANAGER_GET_DEFAULT_AQL = "getDefaultAQL";
    String QUOTA_MANAGER_GET_AQL = "getAQL";
    String QUOTA_MANAGER_GET_ACCOUNTS = "getAccounts";
    String QUOTA_MANAGER_GET_QUOTAS = "getQuotas";

    //Price Manager
    String PRICE_MANAGER_ADDR = "0xffffffffffffffffffffffffffffffffff020010";
    String PRICE_MANAGER_GET_QUOTA_PRICE = "getQuotaPrice";

    //Permission manager
    String PERMISSION_MANAGER_ADDR = "0xffffffffffffffffffffffffffffffffff020004";
    //Permission manager manipulation
    String PERMISSION_MANAGER_NEW_PERMISSION = "newPermission";
    String PERMISSION_MANAGER_DELETE_PERMISSION = "deletePermission";
    String PERMISSION_MANAGER_UPDATE_PERMISSION_NAME = "updatePermissionName";
    String PERMISSION_MANAGER_ADD_RESOURCES = "addResources";
    String PERMISSION_MANAGER_DELETE_RESOURCES = "deleteResources";
    String PERMISSION_MANAGER_SET_AUTHORIZATION = "setAuthorization";
    String PERMISSION_MANAGER_SET_AUTHORIZATIONS = "setAuthorizations";
    String PERMISSION_MANAGER_CANCEL_AUTHORIZATION = "cancelAuthorization";
    String PERMISSION_MANAGER_CANCEL_AUTHORIZATIONS = "cancelAuthorizations";
    String PERMISSION_MANAGER_CLEAR_AUTHORIZATION = "clearAuthorization";

    //Permission manager query
    String PERMISSION_MANAGER_IN_PERMISSION = "inPermission";
    String PERMISSION_MANAGER_QUERY_INFO = "queryInfo";
    String PERMISSION_MANAGER_QUERY_NAME = "queryName";
    String PERMISSION_MANAGER_QUERY_RESOURCE = "queryResource";

    //Auth manager query
    String Authorization_MANAGER_ADDRESS = "0xffffffffffffffffffffffffffffffffff020006";
    String Authorization_MANAGER_QUERY_ALL_ACCOUNTS = "queryAllAccounts";
    String Authorization_MANAGER_QUERY_PERMISSION = "queryPermissions";
    String Authorization_MANAGER_QUERY_ACCOUNTS = "queryAccounts";
    String Authorization_MANAGER_CHECK_PERMISSON = "checkPermission";
    String Authorization_MANAGER_CHECK_RESOURCE = "checkResource";

    //User manager
    String INTRA_GROUP_USER_MANAGEMENT_ADDR = "0xfFFfFFFFFffFFfffFFFFfffffFffffFFfF020009";
    String USER_MANAGER_ADDR = "0xffffffffffffffffffffffffffffffffff02000a";
    //User manager manipulation
    String USER_MANAGER_NEW_GROUP = "newGroup";
    String USER_MANAGER_DELETE_GROUP = "deleteGroup";
    String USER_MANAGER_UPDATE_GROUP_NAME = "updateGroupName";
    String USER_MANAGER_ADD_ACCOUNTS = "addAccounts";
    String USER_MANAGER_DELETE_ACCOUNTS = "deleteAccounts";
    //User manager query (within group)
    String USER_MANAGER_QUERY_INFO = "queryInfo";
    String USER_MANAGER_QUERY_NAME = "queryName";
    String USER_MANAGER_QUERY_ACCOUNTS = "queryAccounts";
    String USER_MANAGER_QUERY_CHILD = "queryChild";
    String USER_MANAGER_QUERY_CHILD_LENGHT = "queryChildLength";
    String USER_MANAGER_QUERY_PARENT = "queryParent";
    String USER_MANAGER_IN_GROUP = "inGroup";
    //User manager query (query group)
    String USER_MANAGER_CHECK_SCOPE = "checkScope";
    String USER_MANAGER_QUERY_GROUPS = "queryGroups";

    @SuppressWarnings("unchecked")
    static List> convert(List> input) {
        List> result = new ArrayList<>(input.size());
        for (TypeReference typeReference : input) {
            result.add((TypeReference) typeReference);
        }
        return result;
    }

    static String encodeCall(String methodName) {
        Function callFunc = new Function(
                methodName,
                Collections.emptyList(),
                Collections.emptyList());
        return FunctionEncoder.encode(callFunc);
    }

    static String encodeFunction(String methodName, List inputParameters) {
        Function func = new Function(
                methodName,
                inputParameters,
                Collections.emptyList()
        );
        return FunctionEncoder.encode(func);
    }

    static String sendTxAndGetHash(
            String contractAddr,
            CITAj service,
            String adminKey,
            String funcData,
            int version,
            BigInteger chainId)
            throws IOException {

        Transaction tx = new Transaction(
                contractAddr,
                Util.getNonce(),
                10000000,
                Util.getValidUtilBlock(service).longValue(),
                version,
                chainId,
                "0",
                funcData);

        AppSendTransaction appSendTransaction =
                service.appSendRawTransaction(tx.sign(adminKey)).send();

        if (appSendTransaction.getError() != null) {
            String message = appSendTransaction.getError().getMessage();
            System.out.println("Failed to get hash. Error message: " + message);
            return "";
        }
        return appSendTransaction.getSendTransactionResult().getHash();
    }

    static boolean checkReceipt(CITAj service, String hash)
            throws IOException, InterruptedException {
        int count = 0;
        while (true) {
            TransactionReceipt receipt
                    = service.appGetTransactionReceipt(hash).send().getTransactionReceipt();

            if (receipt != null) {
                return receipt.getErrorMessage() == null;
            } else {
                if (count > 5) {
                    break;
                }
                count++;
                TimeUnit.SECONDS.sleep(5);
            }
        }
        return false;
    }

    static Log getReceiptLog(CITAj service, String hash, int logIndex)
            throws IOException, InterruptedException {
        int count = 0;
        while (true) {
            TransactionReceipt receipt
                    = service.appGetTransactionReceipt(hash).send().getTransactionReceipt();

            if (receipt != null) {
                if (receipt.getErrorMessage() != null) {
                    return null;
                } else {
                    List logs = receipt.getLogs();
                    return logs.get(logIndex);
                }
            } else {
                if (count > 5) {
                    break;
                }
                count++;
                TimeUnit.SECONDS.sleep(5);
            }
        }
        return null;
    }

    static List decodeCallResult(AppCall appCall, List> outputParameters) {
        return FunctionReturnDecoder.decode(
                appCall.getValue(),
                CITASystemContract.convert(outputParameters)
        );
    }


    static AppCall sendCall(String from, String addr,
                            String callData, CITAj service)
            throws IOException {
        Call call = new Call(from, addr, callData);
        return service.appCall(call, DEFAULT_BLOCK_PARAMETER).send();
    }

    long getQuotaPrice(String from) throws IOException;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy