Geth supports additional management apis in addition to the official DApp API development interface. Similar to the DApp API, these management apis are provided over the JSON-PRC protocol and follow the same specifications. Geth’s built-in console supports all of these additional administration apis. Geth management API official document Chinese version translated by Huizhi.com, access address: Geth management API documentation
If you want to learn ethereum application development quickly and efficiently, I recommend the Introduction to Ethereum Smart Contract and DApp development
Enabling the management API
To provide these management apis through Geth’s RPC access end nodes, you need to start Geth with the –${interface} API option, where ${interface} can be RPC, representing end nodes on HTTP, or WS, representing end nodes on WebSocket, or IPC, Represents an end node on a Unix socket or Windows named pipe.
For example, the following command enables access endpoints on Unix sockets and HTTP:
~$ geth --ipcapi admin,eth,miner --rpcapi eth,web3 --rpc
Copy the code
- End nodes on Unix sockets provide the following apis: admin, Miner, and the official DApp API known as ETH.
- The HTTP end node provides the following apis: Web3 and eth
The RPC interface over HTTP must also be explicitly opened with the — RPC flag.
Note that providing the API over HTTP (RPC) or WebSocket (WS) will allow everyone to access the API, so carefully review which API services you are offering. By default, Geth provides all apis on the IPC endpoint and only db, ETH, NET, and Web3 apis on HTTP and WebSocket interfaces.
To see which apis are provided, use the Modules call. For example, on a Unix system through the IPC interface:
~ $echo '{" jsonrpc ":" 2.0 ", "method" : "rpc_modules", "params" : [], "id" : 1}' | nc -U $datadir/geth.ipc
Copy the code
The response will list all enabled API modules and their version numbers:
{
"id": 1,"jsonrpc":"2.0"."result": {"admin":"1.0"."db":"1.0"."debug":"1.0"."eth":"1.0"."miner":"1.0"."net":"1.0"."personal":"1.0"."shh":"1.0"."txpool":"1.0"."web3":"1.0"}}Copy the code
Using the Management API
The management API provided by Geth follows the same specification as the official DApp API, so Web3 can be extended to support these additional apis.
Different functions are grouped into logical groups. Examples from the JavaScript console are given below, but they can easily be converted into RPC requests.
For example, start mining at the Geth terminal:
> miner.start()
Copy the code
Start mining through IPC end node:
~ $echo '{" jsonrpc ":" 2.0 ", "method" : "miner_start", "params" : [], "id" : 1}' | nc -U $datadir/geth.ipc
Copy the code
Start mining via HTTP end node:
~$ curl -X POST --data '{" jsonrpc ":" 2.0 ", "method" : "miner_start", "params" : [], "id" : 74}' localhost:8545
Copy the code
We can start mining by passing a parameter to specify the number of mining threads:
Geth terminal starts 4 threads to mine:
> miner.start(4)
Copy the code
Start 4 threads to mine through IPC end node:
~ $echo '{" jsonrpc ":" 2.0 ", "method" : "miner_start", "params" : [4], "id" : 1}' | nc -U $datadir/geth.ipc
Copy the code
Start 4 threads to mine via HTTP endpoints:
~$ curl -X POST --data '{" jsonrpc ":" 2.0 ", "method" : "miner_start", "params" : [4], "id" : 74}' localhost:8545
Copy the code
Geth management API list
In addition to the official DApp API command Spaces (eth, SHH, web3), Geth provides the following additional management API namespaces:
- Admin: Manages the Geth node
- Debug: Debugging the Geth node
- Miner: Mining and directed Acyclic Graph (DAG) management
- Personal: account management
- Txpool: Transaction pool review
admin
The Admin family of apis provides a set of non-standard RPC methods for tuning the operation of Geth instances, including, but not limited to, the management of network nodes and RPC end nodes.
The admin namespace contains the following RPC calls:
- AddPeer: adds a remote node
- Datadir: Gets the directory of linked library data
- NodeInfo: Obtains node information
- Peers: obtains information about connected remote nodes
- SetSolc: Set the solidity compiler path
- StartRPC: starts the RPC service over HTTP
- StartWS: starts the RPC service on the WebSocket
- StopRPC: Stops THE RPC service over HTTP
- StopWS: Stops RPC services on websockets
debug
The Debug family of apis provides a set of non-standard RPC methods that can be used to monitor and debug Geth running, as well as to set specific debug flags at run time.
- BacktraceAt: Sets the tracing position
- BlockProfile: enables time-limited block performance detection
- CpuProfile: enables timed CPU performance detection
- DumpBlock: Exports block data
- GcStats: Gets garbage collection statistics
- GetBlockRlp: Obtains rPL-encoded block data
- GoTrace: Enables time-limited GO runtime tracing
- MemStats: Gets memory statistics
- SeedHash: gets the block seedHash
- SetBlockProfileRate: Sets the block performance detection rate
- SetHead: Sets the local header block
- Stacks: Gets the call stack
- StartCPUProfile: enables CPU performance check
- StartGoTrace: Enables go tracing
- TraceBlock: Gets the block opcode call stack
- TraceBlockByNumber: Gets the call stack of the specified block opcode
- TraceBlockByHash: Gets the specified hash block opcode call stack
- TraceBlockFromFile: Get the block file opcode call stack
- TraceTransaction: Traces transactions
- Verbosity: Sets the log display level
- Vmodule: Sets logging visual mode
- WriteBlockProfile: Enables block performance detection
- WriteMemProfile: enables memory performance detection
miner
The Miner series API allows you to remotely control a node’s mining operations or set various mining related parameters.
- SetExtra: Set write mining to get extra data for the block
- SetGasPrice: Sets the gas price floor for acceptable transactions
- Start: Starts mining
- Stop: Stop mining
- GetHashRate: obtains the hash generation rate
- SetEtherbase: Sets the mining revenue account
personal
The Personal family of apis manages the private keys in the keystore.
- ImportRawKey: Imports the private key
- ListAccounts: Lists all accounts
- LockAccount: Locks an account
- NewAccount: creates a newAccount
- UnlockAccount: Unlocks an account
- SendTransaction: Sends a transaction
- Signature sign:
- EcRecover: extracts the address of the initiator from the signature
txpool
The TXPool family of apis provides a set of non-standard RPC methods that can be used to view the contents of a trade pool containing all pending and queued transactions.
- Content: Gets the transaction details in the pool
- Inspect: Get overview of transactions in the pool
- Status: Obtains the status of the trade pool