配置介绍

标签:c-sdk config


bcos-c-sdk支持配置对象和配置文件两种方式的初始化, 接口分别为:

  • void* bcos_sdk_create(struct bcos_sdk_c_config* config)

  • void* bcos_sdk_create_by_config_file(const char* config_file)

本节内容介绍struct bcos_sdk_c_config对象以及config_file配置文件的格式

配置对象

源码定义位置: github链接gitee链接

struct bcos_sdk_c_config
{
    // common config
    int thread_pool_size;
    int message_timeout_ms;
    int reconnect_period_ms;
    int heartbeat_period_ms;

    // connected peers
    struct bcos_sdk_c_endpoint* peers;
    size_t peers_count;

    // the switch for disable ssl connection
    int disable_ssl;

    // ssl or sm_ssl
    char* ssl_type;
    // cert config items is the content of the cert or the path of the cert file
    int is_cert_path;
    // ssl connection cert, effective with ssl_type is 'ssl'
    struct bcos_sdk_c_cert_config* cert_config;
    // sm ssl connection cert, effective with ssl_type is 'sm_ssl'
    struct bcos_sdk_c_sm_cert_config* sm_cert_config;
};

struct bcos_sdk_c_endpoint
{
    char* host;  // c-style string,end with '\0', ipv4、ipv6 support
    uint16_t port;
};

struct bcos_sdk_c_cert_config
{
    char* ca_cert;  // cert and key should be in pem format
    char* node_key;
    char* node_cert;
};

struct bcos_sdk_c_sm_cert_config
{
    char* ca_cert;  // cert and key should be in pem format
    char* node_key;
    char* node_cert;
    char* en_node_key;
    char* en_node_cert;
};

字段含义:

bcos_sdk_c_endpoint

  • 功能

    • 连接ip:port

  • 字段

    • host: 节点rpc连接,支持ipv4ipv6格式 注意: 使用strdup或者malloc初始化,确保可以使用free释放

    • port: 节点rpc端口

bcos_sdk_c_cert_config

  • 功能:

    • ssl连接证书配置, ssl_typessl时有效

  • 字段:

    • ca_cert: 根证书配置, 支持文件路径和文件内容两种方式, 参考is_cert_path字段, 注意: 使用strdup或者malloc初始化,确保可以使用free释放

    • node_cert: sdk证书, 支持文件路径和文件内容两种方式,参考is_cert_path字段, 注意: 使用strdup或者malloc初始化,确保可以使用free释放

    • node_key: sdk私钥, 支持文件路径和文件内容两种方式,参考is_cert_path字段, 注意: 使用strdup或者malloc初始化,确保可以使用free释放

bcos_sdk_c_sm_cert_config

  • 功能:

    • 国密ssl连接证书配置, ssl_typesm_ssl时有效

  • 字段:

    • ca_cert: 国密根证, 支持文件路径和文件内容两种方式 注意: 使用strdup或者malloc初始化,确保可以使用free释放

    • node_cert: sdk国密签名证书, 支持文件路径和文件内容两种方式,参考is_cert_path字段 注意: 使用strdup或者malloc初始化,确保可以使用free释放

    • node_key: sdk国密签名私钥, 支持文件路径和文件内容两种方式,参考is_cert_path字段 注意: 使用strdup或者malloc初始化,确保可以使用free释放

    • en_node_key: sdk国密加密证书, 支持文件路径和文件内容两种方式,参考is_cert_path字段 注意: 使用strdup或者malloc初始化,确保可以使用free释放

    • en_node_crt: sdk国密加密私钥, 支持文件路径和文件内容两种方式,参考is_cert_path字段 注意: 使用strdup或者malloc初始化,确保可以使用free释放

bcos_sdk_c_config

  • 字段:

    • thread_pool_size: 线程池大小,该线程池用于处理网络消息

    • message_timeout_ms: 消息超时时间

    • peers: 连接列表, 注意: 使用malloc初始化,确保可以使用free释放

    • peers_count: 连接列表大小

    • disable_ssl: 是否屏蔽ssl, 0: 否, 1: 是

    • ssl_type: ssl类型, 支持sslsm_ssl两种 注意: 使用strdup或者malloc初始化,确保可以使用free释放

    • is_cert_path: ssl连接证书的配置项为文件路径或者文件内容, 0:内容, 1:路径

    • bcos_sdk_c_cert_config: ssl连接的证书配置, ssl_typessl时有效 注意: 使用malloc初始化,确保可以使用free释放

    • bcos_sdk_c_sm_cert_config: 国密ssl连接的证书配置, ssl_typesm_ssl时有效 注意: 使用malloc初始化,确保可以使用free释放

配置文件

配置文件中的字段与配置对象中的字段对应

示例配置文件

  • ssl连接配置文件

[common]
    ; if ssl connection is disabled, default: false
    ; disable_ssl = true
    ; thread pool size for network message sending receiving handing
    thread_pool_size = 8
    ; send message timeout(ms)
    message_timeout_ms = 10000

; ssl cert config items,  
[cert]
    ; ssl_type: ssl or sm_ssl, default: ssl
    ssl_type = ssl
    ; directory the certificates located in, default: ./conf
    ca_path=./conf
    ; the ca certificate file
    ca_cert=ca.crt
    ; the node private key file
    sdk_key=sdk.key
    ; the node certificate file
    sdk_cert=sdk.crt

[peers]
# supported ipv4 and ipv6 
    node.0=127.0.0.1:20200
    node.1=127.0.0.1:20201
  • 国密ssl连接配置文件

[common]
    ; if ssl connection is disabled, default: false
    ; disable_ssl = true
    ; thread pool size for network message sending receiving handing
    thread_pool_size = 8
    ; send message timeout(ms)
    message_timeout_ms = 10000

[cert]
    ; ssl_type: ssl or sm_ssl, default: ssl
    ssl_type = sm_ssl
    ; directory the certificates located in, default: ./conf
    ca_path=./conf
    ; the ca certificate file
    sm_ca_cert=sm_ca.crt
    ; the node private key file
    sm_sdk_key=sm_sdk.key
    ; the node certificate file
    sm_sdk_cert=sm_sdk.crt
    ; the node private key file
    sm_ensdk_key=sm_ensdk.key
    ; the node certificate file
    sm_ensdk_cert=sm_ensdk.crt

[peers]
# supported ipv4 and ipv6 
    node.0=127.0.0.1:20200
    node.1=127.0.0.1:20201

使用示例

bcos-c-sdk/sample目录提供了sdk使用的一些示例:

bcos-c-sdk/sample/
├── CMakeLists.txt
├── amop        # amop示例
├── eventsub    # 合约事件订阅示例
└── rpc         # rpc示例
├── config      # 配置文件示例

bcos-c-sdk/sample/rpc/rpc.c 通过配置对象方式初始化sdk. github链接gitee链接

bcos-c-sdk/sample/eventsub/eventsub.c 通过配置文件初始化sdk. github链接gitee链接