cosmos-sdk-tutorials namechain 体验过程解析

namechain 体验过程解析

nsd init --chain-id=namechain

初始化配置文件和创世文件
生成文件结构如下:

├── config
│   ├── app.toml
│   ├── config.toml
│   ├── genesis.json
│   ├── node_key.json
│   └── priv_validator_key.json
└── data
    └── priv_validator_state.json

app.toml

minimum-gas-prices = ""

最小交易gas费用

halt-height=0

暂停的区块高度,用于升级(升级高度)或测试

halt-time=0

暂停的区块时间,到达该时间时暂停出块,用于升级或测试

inter-block-cache = true

启用 inter-block cache

pruning = "syncable"

state 存储策略 (syncable, nothing, everything)

config.toml

advanced configuration options

proxy_app="tcp://127.0.0.1:26658"

ABCI socket 地址

moniker="namechain"

节点名称

fast_sync=true

快速同步,允许并发下载区块,同时验证commit

db_backend="goleveldb"

数据库 (goleveldb | cleveldb | boltdb | rocksdb)

db_dir="data"

数据库存放位置

log_level="main:info,state:info,*:error"

日志输出

log_format="plain"

日志格式化

genesis_file="config/genesis.json"

创世文件位置,包含 初始 validator 集合和其他元数据

priv_validator_key_file = "config/priv_validator_key.json"

validator 私钥文件

priv_validator_state_file = "data/priv_validator_state.json"

存放 validator 最后的签名状态

priv_validator_laddr = ""

Tendermint 监听来自外部 validator 连接进程的地址

node_key_file="config/node_key.json"

在 P2P 中进行节点验证的私钥

abci="socket"

abci 连接机制 (socket | grpc)

prof_laddr = "localhost:6060"

profiling server 监听地址

filter_peers = false

连接新节点时,是否查询 ABCI 应用

rpc server configuration options

laddr = "tcp://127.0.0.1:26657"

RPC server 监听地址

cors_allowed_origins = []

允许的跨域请求 origins

cors_allowed_methods = ["HEAD", "GET", "POST", ]

允许的跨域请求方法

cors_allowed_headers = ["Origin", "Accept", "Content-Type", "X-Requested-With", "X-Server-Time", ]

跨域请求中允许携带的请求头

grpc_laddr = ""

grpc 监听地址

grpc_max_open_connections = 900

grpc 最大并发连接数

unsafe=false

是否允许非安全 RPC 命令

max_open_connections = 900

最大并发连接数

genesis.json

{
  "genesis_time": "2020-03-23T10:04:56.163902Z",
  "chain_id": "namechain",
  "consensus_params": {
    "block": {
      "max_bytes": "22020096",
      "max_gas": "-1",
      "time_iota_ms": "1000"
    },
    "evidence": {
      "max_age_num_blocks": "100000",
      "max_age_duration": "172800000000000"
    },
    "validator": {
      "pub_key_types": [
        "ed25519"
      ]
    }
  },
  "app_hash": "",
  "app_state": {
    "distribution": {
      "params": {
        "community_tax": "0.020000000000000000",
        "base_proposer_reward": "0.010000000000000000",
        "bonus_proposer_reward": "0.040000000000000000",
        "withdraw_addr_enabled": true
      },
      "fee_pool": {
        "community_pool": []
      },
      "delegator_withdraw_infos": [],
      "previous_proposer": "",
      "outstanding_rewards": [],
      "validator_accumulated_commissions": [],
      "validator_historical_rewards": [],
      "validator_current_rewards": [],
      "delegator_starting_infos": [],
      "validator_slash_events": []
    },
    "genutil": {
      "gentxs": []
    },
    "auth": {
      "params": {
        "max_memo_characters": "256",
        "tx_sig_limit": "7",
        "tx_size_cost_per_byte": "10",
        "sig_verify_cost_ed25519": "590",
        "sig_verify_cost_secp256k1": "1000"
      },
      "accounts": []
    },
    "staking": {
      "params": {
        "unbonding_time": "1814400000000000",
        "max_validators": 100,
        "max_entries": 7,
        "historical_entries": 0,
        "bond_denom": "stake"
      },
      "last_total_power": "0",
      "last_validator_powers": null,
      "validators": null,
      "delegations": null,
      "unbonding_delegations": null,
      "redelegations": null,
      "exported": false
    },
    "bank": {
      "send_enabled": true
    },
    "nameservice": {
      "whois_records": []
    },
    "params": null,
    "slashing": {
      "params": {
        "signed_blocks_window": "100",
        "min_signed_per_window": "0.500000000000000000",
        "downtime_jail_duration": "600000000000",
        "slash_fraction_double_sign": "0.050000000000000000",
        "slash_fraction_downtime": "0.010000000000000000"
      },
      "signing_infos": {},
      "missed_blocks": {}
    },
    "supply": {
      "supply": []
    }
  }
}

nscli keys add jack

nscli keys add rose

创建两个账号,分别为 jack 和 rose,使用了 cosmos 内置命令 keys

调用链

  • nameservice/cmd/nscli/main.go#rootCmd.addCommand(keys.Commands())
  • cosmos-sdk/client/keys/root.go
  • cosmos-sdk/client/keys/add.go#AddKeyCommand()

nsd add-genesis-account $(nscli keys show jack -a) 100000000stake,1000000nametoken

nsd add-genesis-account $(nscli keys show rose -a) 1000000stake,10000nametoken

往 genesis.json 中添加创世账号,账号需要包括地址和初始的币

调用链

  • nameservice/cmd/nsd/main.go#rootCmd.AddCommand(AddGenesisAccountCmd(ctx, cdc, app.DefaultNodeHome, app.DefaultCLIHome))
  • nameservice/cmd/nsd/genaccounts.go#AddGenesisAccountCmd()
// 创建创世账户
genAccount = auth.NewBaseAccount(address, coins.Sort(), nil, 0, 0)

...

// 重入校验
if authGenState.Accounts.Contains(addr) {
    return fmt.Errorf("cannot add account at existing address %s", addr)
}

...

// 添加到 state
authGenState.Accounts = append(authGenState.Accounts, genAccount)
authGenState.Accounts = auth.SanitizeGenesisAccounts(authGenState.Accounts)

...

// 更新到 appState
appState[auth.ModuleName] = authGenStateBz

...

// 写入 genesis.json
genutil.ExportGenesisFile(genDoc, genFile)

genesis.json 文件中新增内容

"accounts": [
        {
          "type": "cosmos-sdk/Account",
          "value": {
            "address": "cosmos1d8grdhlg87vnlz6ld22tcl9y3s04gl8sc57q4w",
            "coins": [
              {
                "denom": "nametoken",
                "amount": "1000000"
              },
              {
                "denom": "stake",
                "amount": "100000000"
              }
            ],
            "public_key": "",
            "account_number": 0,
            "sequence": 0
          }
        },
        {
          "type": "cosmos-sdk/Account",
          "value": {
            "address": "cosmos1zcdqgl4d0jk4vglg058r76hx5hspln2hnxrclv",
            "coins": [
              {
                "denom": "nametoken",
                "amount": "10000"
              },
              {
                "denom": "stake",
                "amount": "1000000"
              }
            ],
            "public_key": "",
            "account_number": 0,
            "sequence": 0
          }
        }
]

nscli config output json

nscli config indent true

nscli config trust-node true

nscli config chain-id namechain

nscli config

nsd gentx --name jack

: cosmos-sdk/x/genutil/client/cli/gentx.go

创建一笔创世交易, 交易生成在 /config/gentx/filename.json

{
  "type": "cosmos-sdk/StdTx",
  "value": {
    "msg": [
      {
        "type": "cosmos-sdk/MsgCreateValidator",
        "value": {
          "description": {
            "moniker": "namechain",
            "identity": "",
            "website": "",
            "security_contact": "",
            "details": ""
          },
          "commission": {
            "rate": "0.100000000000000000",
            "max_rate": "0.200000000000000000",
            "max_change_rate": "0.010000000000000000"
          },
          "min_self_delegation": "1",
          "delegator_address": "cosmos1d8grdhlg87vnlz6ld22tcl9y3s04gl8sc57q4w",
          "validator_address": "cosmosvaloper1d8grdhlg87vnlz6ld22tcl9y3s04gl8saq24ea",
          "pubkey": "cosmosvalconspub1zcjduepqvrd5ec72t07ajaextgk7z3ue5t9zakeuqr5m5x9qk7q8falt2szqkgl8z4",
          "value": {
            "denom": "stake",
            "amount": "100000000"
          }
        }
      }
    ],
    "fee": {
      "amount": [
        
      ],
      "gas": "200000"
    },
    "signatures": [
      {
        "pub_key": {
          "type": "tendermint/PubKeySecp256k1",
          "value": "AqKWqOjfqWVKsPDhheTYtEg5gipkndUeYdma/5CWAtrH"
        },
        "signature": "o3+TzpaQVZObp6sHRv0L5yr9fBOAGsGxdGrx2I4FU2sdz4zaBj8Vwm8u57esCaaYiv4+0HBdr+Usms+wxcPZbQ=="
      }
    ],
    "memo": "[email protected]:26656"
  }
}

这里包含了一个 msg: MsgCreateValidator
表示创建 validator

nsd collect-gentxs

: cosmos-sdk/x/genutil/client/cli/collect.go
将上一步生成的创世交易写入 genesis.json

nsd validate-genesis

: cosmos-sdk/x/genutil/client/cli/validate_genesis.go
校验 genesis 信息

nsd start

启动 namachain

nscli tx nameservice buy-name namechain.com 20nametoken --from jack

jack 花费 20 nametoken 购买 namechain.com

Action: tx
Route: nameservice
Msg: buy-name

{
  "chain_id": "namechain",
  "account_number": "3",
  "sequence": "2",
  "fee": {
    "amount": [],
    "gas": "200000"
  },
  "msgs": [
    {
      "type": "nameservice/BuyName",
      "value": {
        "name": "namechain.com",
        "bid": [
          {
            "denom": "nametoken",
            "amount": "20"
          }
        ],
        "buyer": "cosmos1d8grdhlg87vnlz6ld22tcl9y3s04gl8sc57q4w"
      }
    }
  ],
  "memo": ""
}

confirm transaction before signing and broadcasting [y/N]: y

{
  "height": "0",
  "txhash": "4F5B53E7B69F5D53398341E336709B4C4B92752ABA970AF6743977A1BEA7ABE7",
  "raw_log": "[]"
}

  1. /x/nameservice/client/cli/tx.go 中 GetCmdBuyName 会对原始 cli 消息进行处理,包装成对应的 Msg 广播出去,这里是 MsgBuyName
  2. /x/nameservice/handler.go 对接受到的 Msg 进行处理和执行,这里借助 keeper 来完成具体的操作
  3. /x/nameservice/internal/keeper/keeper.go 中实现了业务的具体操作并供外部调用,这里需要使用到 GetPrice,HasOwner,SetOwner,SetPrice,数据存储在 KVStore 中

nscli tx nameservice set-name namechain.com 8.8.8.8 --from jack

jack 发起一笔交易,给 namechain 设置 DNS 解析为 8.8.8.8

{
  "chain_id": "namechain",
  "account_number": "3",
  "sequence": "3",
  "fee": {
    "amount": [],
    "gas": "200000"
  },
  "msgs": [
    {
      "type": "nameservice/SetName",
      "value": {
        "name": "namechain.com",
        "value": "8.8.8.8",
        "owner": "cosmos1d8grdhlg87vnlz6ld22tcl9y3s04gl8sc57q4w"
      }
    }
  ],
  "memo": ""
}
confirm transaction before signing and broadcasting [y/N]: y
{
  "height": "0",
  "txhash": "C3389B9D37D2018E026424783A8B1C4C973A6CC52AA9B226BEE4BFD4F2366BB5",
  "raw_log": "[]"
}
  1. /x/nameservice/client/cli/tx.go 中 GetCmdSetName 会对原始 cli 消息进行处理,包装成对应的 Msg 广播出去,这里是 MsgSetName
  2. /x/nameservice/handler.go 对接受到的 Msg 进行处理和执行
  3. /x/nameservice/internal/keeper/keeper.go handler 借助 keeper 提供的具体业务实现来完成对 Msg 的处理,结果存储在 KVStore 中

nscli query nameservice resolve namechain.com

查询 namechain.com 的信息

{
  "value": "8.8.8.8"
}
  1. /x/nameservice/client/cli/query.go 中 GetCmdResolveName 会对原始 cli 消息进行处理,向 tendermint node 查询数据,
cliCtx.QueryWithData(fmt.Sprintf("custom/%s/resolve/%s", queryRoute, name), nil)

相关文件

/x/nameservice/module.go

module 文件对 nameservice 模块向 cosmos 进行了顶层描述,

AppModuleBasic

AppModuleBasic 中 GetQueryCmd 和 GetTxCmd 传入了 nameservice 模块对交易和查询指令的具体实现;RegisterCodec 配置了所使用的 codec;DefaultGenesis 设置了该模块在链初始状态下的相关配置信息;RegisterRESTRoutes 设定 rest 服务路由。

AppModule

应用模块的最顶层包装,指定了 AppModuleBasic,注册相关 keeper,实现了 AppModule 的基本方法

type AppModule interface {
    AppModuleGenesis

    // registers
    RegisterInvariants(sdk.InvariantRegistry)

    // routes
    Route() string
    NewHandler() sdk.Handler
    QuerierRoute() string
    NewQuerierHandler() sdk.Querier

    // ABCI
    BeginBlock(sdk.Context, abci.RequestBeginBlock)
    EndBlock(sdk.Context, abci.RequestEndBlock) []abci.ValidatorUpdate
}

你可能感兴趣的:(cosmos-sdk-tutorials namechain 体验过程解析)