当前位置: 首页 > 工具软件 > Substrate > 使用案例 >

substrate chain spec

翟沈义
2023-12-01

简介

链规范是一组配置信息,这些信息指示区块链节点将连接到哪个网络、它最初将与哪些节点进行通信,以及它在创世时必须具有什么样的共识关键状态。

结构

ChainSpec主要分两部分client spec和genesis state

client spec

大部分用于与网络中的其它节点进行通信,例如一组引导节点、遥测端点、可读名称连接。其中许多配置项可以被命令行标志覆盖,并且可以在区块链启动后更改这些值。

struct ClientSpec<E> {
	name: String,
	id: String,
	#[serde(default)]
	chain_type: ChainType,
	boot_nodes: Vec<MultiaddrWithPeerId>,
	telemetry_endpoints: Option<TelemetryEndpoints>,
	protocol_id: Option<String>,
	properties: Option<Properties>,
	#[serde(flatten)]
	extensions: E,
	// Never used, left only for backward compatibility.
	consensus_engine: (),
	#[serde(skip_serializing)]
	genesis: serde::de::IgnoredAny,
	/// Mapping from `block_hash` to `wasm_code`.
	///
	/// The given `wasm_code` will be used to substitute the on-chain wasm code from the given
	/// block hash onwards.
	#[serde(default)]
	code_substitutes: HashMap<String, Bytes>,
}

genesis state

链规范的第二部分是共识关键的创世配置。网络中的所有节点必须就这个初始状态达成一致,然后才能就任何后续区块达成一致。因此,这些信息必须在链开始时建立,此后如果不启动全新的区块链,则无法更改。没有命令行标志可以覆盖链规范的创世部分中的值。
链规范的创世部分包括初始代币余额、最初属于治理委员会的帐户或 sudo 密钥的持有者。Substrate 节点还将编译后的 Wasm 运行时逻辑放在链上,因此初始运行时也必须在链规范中提供。

链规范配置信息

  • Rust struct 。Substrate 节点通常包括至少一个,通常是许多,硬编码到客户端的链规范。在具有“主网”概念的协议中,此规范通常在客户端中进行硬编码。
  • JSON 格式。 链规范结构具有将其数据序列化为 JSON 的方法以及将 JSON 数据反序列化为链规范实例的函数。在启动测试网或私有链时,通常会随节点二进制文件一起分发 JSON 编码的链规范。

如何使用chain spec

启动链

启动一个测试网或私有网络,其行为类似于现有协议,但未连接到主网。为了实现这一点,可以通过使用命令行标志来选择替代的硬编码链规范,例如–chain local指示节点使用与字符串“local”相关联的规范。第三个选项是将链规范作为带有命令行标志的 JSON 文件提供,例如 --chain=someCustomSpec.json,在这种情况下,节点将尝试反序列化所提供的 JSON 链规范,然后使用它。

开发runtime

每个 Substrate 运行时都会有需要在创世时配置的存储项。使用FRAME开发时,任何使用该config() 选项声明的存储项都需要在创世时进行配置。配置这样的存储值是链规范的工作,特别是创世部分

自定义链规格

在为开发、测试或演示目的创建一次性网络时,可能需要真正定制的链规范。节点运营商可以将协议的默认链规范导出为 JSON 格式,然后进行编辑。substrate的子命令build-spec 可以用来导出json文件。

substrate build-spec > myCustomSpec.json

导出链规范后,操作员可以自由修改其任何字段。修改网络的名称和引导节点以及运营商希望的任何创世配置(例如代币余额)是很常见的。编辑完成后,运营商可以通过提供定制的 JSON 来启动他们的定制链。

substrate --chain=myCustomSpec.json

原始链规范(Raw chain spec)

Substrate 节点支持运行时升级,这意味着区块链的runtime可能与链开始时不同。
从 Substrate 节点的链规范.json文件中摘录:

"sudo": {
  "key": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"
}

在此规范可用于初始化节点的创世存储之前,人类可读的密钥必须转换为存储 trie (类似以太坊mpt)的实际存储密钥。

如果升级runtime的节点尝试从创世同步链,它将无法理解人类可读的链规范信息。出于这个原因,链规范有第二种编码方式,称为“原始”链规范。

当以 JSON 格式分发链规范时,它们应该以这种原始格式分发,以确保所有节点即使在runtime升级后也可以同步链。基于substrate的节点支持–raw标记以生成raw链规范。

substrate build-spec --chain=myCustomSpec.json --raw > customSpecRaw.json

转换后 ,上面的配置变成了这样

"0x50a63a871aced22e88ee6466fe5aa5d9": "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d",

build-spec

默认可配置的flag比较少,具体实现在chain_spec.rs

  • –chain 可以设置dev, local ,dev通常是一个本地节点,local通常是多个本地节点(账户也多些)
  • –chain dev 和–dev等价
  • –disable-default-bootnode 不生产bootnode配置,默认值/ip4/127.0.0.1/tcp/30333/p2p/NODE_PEER_ID
  • –raw 将人类可读的链规范转换成原始(只有机器可读)链规范
  • –node-key-type libp2p 节点id ,默认值Ed25519,目前只支持Ed25519,也可以使用其它两个flag:–node-key-file --node-key
FLAGS:
        --dev
            Specify the development chain

        --disable-default-bootnode
            Disable adding the default bootnode to the specification.

            By default the `/ip4/127.0.0.1/tcp/30333/p2p/NODE_PEER_ID` bootnode is added to the specification when no
            bootnode exists.
        --disable-log-color
            Disable log color output

        --disable-log-reloading
            Disable feature to dynamically update and reload the log filter.

            By default this feature is enabled, however it leads to a small performance decrease. The
            `system_addLogFilter` and `system_resetLogFilter` RPCs will have no effect with this option set.
    -h, --help
            Prints help information

        --raw
            Force raw genesis storage output

    -V, --version
            Prints version information


OPTIONS:
    -d, --base-path <PATH>
            Specify custom base path

        --chain <CHAIN_SPEC>
            Specify the chain specification.

            It can be one of the predefined ones (dev, local, or staging) or it can be a path to a file with the
            chainspec (such as one exported by the `build-spec` subcommand).
    -l, --log <LOG_PATTERN>...
            Sets a custom logging filter. Syntax is <target>=<level>, e.g. -lsync=debug.

            Log levels (least to most verbose) are error, warn, info, debug, and trace. By default, all targets log
            `info`. The global log level can be set with -l<level>.
        --node-key <KEY>
            The secret key to use for libp2p networking.

            The value is a string that is parsed according to the choice of `--node-key-type` as follows:

            `ed25519`: The value is parsed as a hex-encoded Ed25519 32 byte secret key, i.e. 64 hex characters.

            The value of this option takes precedence over `--node-key-file`.

            WARNING: Secrets provided as command-line arguments are easily exposed. Use of this option should be limited
            to development and testing. To use an externally managed secret key, use `--node-key-file` instead.
        --node-key-file <FILE>
            The file from which to read the node's secret key to use for libp2p networking.

            The contents of the file are parsed according to the choice of `--node-key-type` as follows:

            `ed25519`: The file must contain an unencoded 32 byte or hex encoded Ed25519 secret key.

            If the file does not exist, it is created with a newly generated secret key of the chosen type.
        --node-key-type <TYPE>
            The type of secret key to use for libp2p networking.

            The secret key of the node is obtained as follows:

            * If the `--node-key` option is given, the value is parsed as a secret key according to the type. See the
            documentation for `--node-key`.

            * If the `--node-key-file` option is given, the secret key is read from the specified file. See the
            documentation for `--node-key-file`.

            * Otherwise, the secret key is read from a file with a predetermined, type-specific name from the chain-
            specific network config directory inside the base directory specified by `--base-dir`. If this file
            does not exist, it is created with a newly generated secret key of the chosen type.

            The node's secret key determines the corresponding public key and hence the node's peer ID in the context of
            libp2p. [default: Ed25519]  [possible values: Ed25519]
        --tracing-receiver <RECEIVER>
            Receiver to process tracing messages [default: Log]  [possible values: Log]

        --tracing-targets <TARGETS>
            Sets a custom profiling filter. Syntax is the same as for logging: <target>=<level>

生成一个看看

./target/release/parachain-collator build-spec --chain local
2021-10-31 21:11:12 Building chain spec
{
  "name": "Local Testnet",
  "id": "local_testnet",
  "chainType": "Local",
  "bootNodes": [   "/ip4/127.0.0.1/tcp/30333/p2p/12D3KooWNirG2PZNHZh8YEdwVAurZPR42vfPS8ZAjnjPhPrWbFQv"
  ],
  "telemetryEndpoints": null,
  "protocolId": null,
  "properties": null,
  "relay_chain": "rococo-local",
  "para_id": 2000,
  "consensusEngine": null,
  "codeSubstitutes": {},
  "genesis": {
    "runtime": {
      "system": {
        "changesTrieConfig": null,
        "code": " <<Runtime wasm Code>>"
      },
      "sudo": {
        "key": "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"
      },
      "parachainSystem": null,
      "parachainInfo": {
        "parachainId": 2000
      },
      "balances": {
        "balances": [
          [
            "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
            1152921504606846976
          ],
          [
            "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty",
            1152921504606846976
          ],
          [
            "5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y",
            1152921504606846976
          ],
          [
            "5DAAnrj7VHTznn2AWBemMuyBwZWs6FNFjdyVXUeYum3PTXFy",
            1152921504606846976
          ],
          [
            "5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw",
            1152921504606846976
          ],
          [
            "5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL",
            1152921504606846976
          ],
          [
            "5GNJqTPyNqANBkUVMN1LPPrxXnFouWXoe2wNSmmEoLctxiZY",
            1152921504606846976
          ],
          [
            "5HpG9w8EBLe5XCrbczpwq5TSXvedjrBGCwqxK1iQ7qUsSWFc",
            1152921504606846976
          ],
          [
            "5Ck5SLSHYac6WFt5UZRSsdJjwmpSZq85fd5TRNAdZQVzEAPT",
            1152921504606846976
          ],
          [
            "5HKPmK9GYtE1PSLsS1qiYU9xQ9Si1NcEhdeCq9sw5bqu4ns8",
            1152921504606846976
          ],
          [
            "5FCfAonRZgTFrTd9HREEyeJjDpT397KMzizE6T3DvebLFE7n",
            1152921504606846976
          ],
          [
            "5CRmqmsiNFExV6VbdmPJViVxrWmkaXXvBrSX8oqBT8R9vmWk",
            1152921504606846976
          ]
        ]
      },
      "aura": {
        "authorities": [
          "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
          "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"
        ]
      },
      "auraExt": null
    }
  }
}
 类似资料:

相关阅读

相关文章

相关问答