Skip to content

配置

# 配置

以下頁面將指導您完成節點和客戶端的配置。 該節點用於運行區塊鍊網絡、生產區塊和驗證交易。 客戶端用作網關,通過發送交易和查詢狀態與區塊鍊網絡進行交互。 此外,我們還介紹了 運行 JSON-RPC 服務器

這些配置會影響節點的性能、安全性和功能。 因此,了解並正確配置您的節點和客戶端至關重要。

配置和數據目錄

默認情況下,您的配置和數據存儲在位於 ~/.stcd 目錄的文件夾中。 您可以使用 --home 標誌輕鬆更改默認目錄。 請務必注意,您可以有多個主目錄,每個主目錄代表不同的區塊鏈。

.                                   # ~/.stcd
  ├── data/                           # Contains the databases used by the node.
  └── config/
      ├── app.toml                   # Application-related configuration file.
      ├── config.toml                # Tendermint-related configuration file.
      ├── genesis.json               # The genesis file.
      ├── node_key.json              # Private key to use for node authentication in the p2p protocol.
      └── priv_validator_key.json    # Private key to use as a validator in the consensus protocol.

指定stcd配置和數據存儲目錄;您可以使用全局標誌 --home <directory> 更新它。

節點配置

Cosmos SDK 在 ~/.stcd/config 中自動生成兩個配置文件:

  • config.toml:用於配置 Tendermint,在 Tendermint 的文檔 中了解更多信息,- app.toml :由 Cosmos SDK 生成,用於配置您的應用程序,例如狀態修剪策略、遙測、gRPC 和 REST 服務器配置、狀態同步、JSON-RPC 等。

這兩個文件都有大量評論,請直接參考它們以調整您的節點。

一個要調整的示例配置是 app.toml 中的 minimum-gas-prices 字段,它定義了驗證節點願意接受的用於處理交易的最小數量。這是一種反垃圾郵件機制,它將拒絕低於最低 gas 價格的傳入交易。

如果它是空的,請確保使用一些值編輯該字段,例如0dst,否則節點將在啟動時停止。

 # The minimum gas prices a validator is willing to accept for processing a
 # transaction. A transaction's fees must meet the minimum of any denomination
 # specified in this config (e.g. 0.25token1;0.0001token2).
 minimum-gas-prices = "0dst"

狀態修剪

修剪狀態有四種策略。這些策略只適用於狀態,不適用於塊存儲。 要設置修剪,請調整 ~/.stcd/config/app.toml 文件中的 pruning 參數。 以下修剪狀態設置可用:

  • everything:修剪當前狀態以外的所有保存狀態。
  • nothing:保存所有狀態,不刪除任何內容。
  • default:保存最後 100 個狀態和每 10,000 個塊的狀態。
  • custom:使用 pruning-keep-recentpruning-keep-everypruning-interval 參數指定修剪設置。

默認情況下,每個節點都處於default 模式,這是大多數環境的推薦設置。 如果你想改變你的節點修剪策略,那麼你必須在節點初始化時這樣做。在啟動 daodst 時傳遞一個標誌將始終覆蓋 app.toml 文件中的設置,如果您想將節點更改為 everything 模式,那麼您可以在調用時傳遞 --pruning everything 標誌stcd stc-start

🚨 危險: 當你修剪狀態時,你將無法查詢已被修剪掉的高度。

客戶端配置

我們可以使用 stcd config 命令查看默認的客戶端配置設置:

stcd config
{
 "chain-id": "",
 "keyring-backend": "os",
 "output": "text",
 "node": "tcp://localhost:26657",
 "broadcast-mode": "sync"
}

我們可以根據自己的選擇對默認設置進行更改,因此它允許用戶一次性預先設置所有配置,這樣之後就可以使用相同的配置了。

例如,可以使用以下命令將鏈標識符從空白名稱更改為 daodst_7000-1

stcd config "chain-id" daodst_7000-1
stcd config
{
 "chain-id": "daodst_7000-1",
 "keyring-backend": "os",
 "output": "text",
 "node": "tcp://localhost:26657",
 "broadcast-mode": "sync"
}

可以用同樣的方式更改其他值。

或者,我們可以直接在 client.toml 的一個地方更改配置值。

在我們安裝daodst的文件夾中的.stcd/config/client.toml路徑下:

############################################################################
### Client Configuration ###

############################################################################

# The network chain ID

chain-id = "daodst_7000-1"

# The keyring's backend, where the keys are stored (os|file|kwallet|pass|test|memory)

keyring-backend = "os"

# CLI output format (text|json)

output = "number"

# <host>:<port> to Tendermint RPC interface for this chain

node = "tcp://localhost:26657"

# Transaction broadcasting mode (sync|async|block)

broadcast-mode = "sync"

client.toml 中進行必要的更改後,然後保存。比如我們直接把chain-id從daodst_7000-1改成daodst_7000-2,輸出成number,會瞬間變如下圖。

stcd config
{
 "chain-id": "daodst_7000-2",
 "keyring-backend": "os",
 "output": "number",
 "node": "tcp://localhost:26657",
 "broadcast-mode": "sync"
}

運行 JSON-RPC 服務器

本節介紹啟用 JSON-RPC 服務器的步驟。 JSON-RPC 在多種傳輸上提供。 Daodst 支持基於 HTTP 和 WebSocket 的 JSON-RPC。 就要求而言,我們建議使用至少 8 核 CPU 和 64GB RAM 的服務器。 您必須在防火牆上打開端口 8545 和 8546。

📣 提示:除非您的節點在本地存儲區塊鏈的整個副本,否則您不能使用所有 JSON RPC 方法。您需要我們網絡的檔案/快照嗎?轉到本節

啟用服務器

要啟用 RPC 服務器,請使用以下標誌(默認設置為 true)。

stcd start --json-rpc.enable

定義命名空間

EthNetWeb3 命名空間默認啟用,但對於 JSON-RPC,您需要添加更多命名空間。 為了啟用其他命名空間,請編輯 app.toml 文件。

# API defines a list of JSON-RPC namespaces that should be enabled
# Example: "eth,txpool,personal,net,debug,web3"
api = "eth,net,web3,txpool,debug,personal"

設置一個 Gas 量

eth_calleth_estimateGas 為 DoS 保護定義了一個全局 gas cap over rpc。您可以通過在 app.toml 中傳遞自定義值來覆蓋默認的 gas cap 值 25,000,000:

# GasCap sets a cap on gas that can be used in eth_call/estimateGas (0=infinite). Default: 25,000,000.
gas-cap = 25000000

CORS

如果從瀏覽器訪問 RPC,則需要使用適當的域集啟用 CORS。否則,JavaScript 調用受同源策略限制,請求將失敗。

可以從 app.toml 更新 CORS 設置

###############################################################################
###                           API Configuration                             ###
###############################################################################

[api]

# ...

# EnableUnsafeCORS defines if CORS should be enabled (unsafe - use it at your own risk).
enabled-unsafe-cors = true # default false

修剪

為了使所有方法正常工作,您的節點必須是存檔的(在本地存儲區塊鏈的整個副本)。必須禁用修剪。 修剪設置可以從 app.toml 更新

###############################################################################
###                           Base Configuration                            ###
###############################################################################

# The minimum gas prices a validator is willing to accept for processing a
# transaction. A transaction's fees must meet the minimum of any denomination
# specified in this config (e.g. 0.25token1;0.0001token2).

# ...

# default: the last 100 states are kept in addition to every 500th state; pruning at 10 block intervals
# nothing: all historic states will be saved, nothing will be deleted (i.e. archiving node)
# everything: all saved states will be deleted, storing only the current state; pruning at 10 block intervals
# custom: allow pruning options to be manually specified through 'pruning-keep-recent', 'pruning-keep-every', >
pruning = "nothing"
pruning-keep-recent = "0"
pruning-keep-every = "0"
pruning-interval = "0"

WebSocket 服務器

Websocket 是一種雙向傳輸協議。 Websocket 連接由客戶端和服務器維護,直到它被一個顯式終止。大多數現代瀏覽器都支持 Websocket,這意味著它有很好的工具。

因為 Websocket 是雙向的,服務器可以將事件推送給客戶端。這使得 Websocket 成為涉及事件訂閱的用例的不錯選擇。 Websocket 的另一個好處是在握手過程之後,單個消息的開銷很低,非常適合發送大量請求。 可以從 app.toml 啟用 WebSocket 服務器

# Address defines the EVM WebSocket server address to bind to.
ws-address = "0.0.0.0:8546"