Skip to content

Latest commit

 

History

History
109 lines (90 loc) · 4.4 KB

bitcoinrpc.h.adoc

File metadata and controls

109 lines (90 loc) · 4.4 KB

比特币源码分析之通用RPC接口的实现

1. 对山寨开发的重要性:三星

二次开发的客户通常会问到rpc接口,自定义接口也需要用到

2. HTTPStatusCode—​HTTP状态码

状态码 说明

HTTP_OK = 200

…​

HTTP_BAD_REQUEST = 400

…​

HTTP_UNAUTHORIZED = 401

…​

HTTP_FORBIDDEN = 403

…​

HTTP_NOT_FOUND = 404

…​

HTTP_INTERNAL_SERVER_ERROR = 500

…​

3. RPCErrorCode—​比特币RPC错误码

3.1. Standard JSON-RPC 2.0 errors—​标准RPC错误

状态码 说明

RPC_INVALID_REQUEST = -32600

…​

RPC_METHOD_NOT_FOUND = -32601

…​

RPC_INVALID_PARAMS = -32602

…​

RPC_INTERNAL_ERROR = -32603

…​

RPC_PARSE_ERROR = -32700

…​

3.2. General application defined errors---一般程序定义错误

状态码 说明

RPC_MISC_ERROR = -1

std::exception thrown in command handling

RPC_FORBIDDEN_BY_SAFE_MODE = -2

Server is in safe mode, and command is not allowed in safe mode

RPC_TYPE_ERROR = -3

Unexpected type was passed as parameter

RPC_INVALID_ADDRESS_OR_KEY = -5

Invalid address or key

RPC_OUT_OF_MEMORY = -7

Ran out of memory during operation

RPC_INVALID_PARAMETER = -8

Invalid, missing or duplicate parameter

RPC_DATABASE_ERROR = -20

Database error

RPC_DESERIALIZATION_ERROR = -22

Error parsing or validating structure in raw format

3.3. P2P client errors—​P2P客户端错误

状态码 说明

RPC_CLIENT_NOT_CONNECTED = -9

Bitcoin is not connected

RPC_CLIENT_IN_INITIAL_DOWNLOAD = -10

Still downloading initial blocks

3.4. Wallet errors—​钱包错误

状态码 说明

RPC_WALLET_ERROR = -4

Unspecified problem with wallet (key not found etc.)

RPC_WALLET_INSUFFICIENT_FUNDS = -6

Not enough funds in wallet or account

RPC_WALLET_INVALID_ACCOUNT_NAME = -11

Invalid account name

RPC_WALLET_KEYPOOL_RAN_OUT = -12

Keypool ran out, call keypoolrefill first

RPC_WALLET_UNLOCK_NEEDED = -13

Enter the wallet passphrase with walletpassphrase first

RPC_WALLET_PASSPHRASE_INCORRECT = -14

The wallet passphrase entered was incorrect

RPC_WALLET_WRONG_ENC_STATE = -15

Command given in wrong wallet encryption state (encrypting an encrypted wallet etc.)

RPC_WALLET_ENCRYPTION_FAILED = -16

Failed to encrypt the wallet

RPC_WALLET_ALREADY_UNLOCKED = -17

Wallet is already unlocked

4. 重要函数

函数名 说明

void StartRPCThreads();

void StopRPCThreads();

int CommandLineRPC(int argc, char *argv[]);

json_spirit::Array RPCConvertValues(const std::string &strMethod, const std::vector<std::string> &strParams);

Convert parameter values for RPC call from strings to command-specific JSON objects

void RPCTypeCheck(const json_spirit::Array& params, const std::list<json_spirit::Value_type>& typesExpected, bool fAllowNull=false);

Type-check arguments; throws JSONRPCError if wrong type given. Does not check that the right number of arguments are passed, just that any passed are the correct type. Use like: RPCTypeCheck(params, boost::assign::list_of(str_type)(int_type)(obj_type));

void RPCTypeCheck(const json_spirit::Object& o, const std::map<std::string, json_spirit::Value_type>& typesExpected, bool fAllowNull=false);

Check for expected keys/value types in an Object. Use like: RPCTypeCheck(object, boost::assign::map_list_of("name", str_type)("value", int_type));

5. CRPCCommand—​RPC命令

RPC命令

5.1. 成员变量

  • std::string name;

  • rpcfn_type actor;

  • bool okSafeMode;

  • bool threadSafe;