前人栽树后人乘凉 - 开放 LikeCoin Chain 节点的 API
汝要是问咱什么 RPC REST 都是啥的话呢,其实咱也不特别清楚,不如汝自己去网上搜索看看。不过有那么两点咱还是能说清楚的:
- 汝在 LikeCoin Chain 上的各种活动,都要靠和这些 API 交互来完成,虽然汝自己并不一定需要直接面对它们而是交给各种软件和网页代劳。
- 如果开放 API 的节点足够多的话,既可以提高性能(不用所有的请求挤在一个节点上)也可以提升网络整体的健壮性。
所以虽然咱还有像是有没有方法让应用使用不同的 API 地址一类的考虑,还是先把 API 访问开起来了,万一以后能用的上呢。(笑)
以及基于 Cosmos SDK 的 LikeCoin Chain 其实还有 gRPC 和 Rosetta API,但是前面那个咱没找到有什么不直接暴露 API 端口的方法,后面那个咱完全不知道是啥,所以这次就先打开了比较常用的 RPC 和 REST。如果哪位找到方法的话能不能分享给咱一下?
启用 API 终结点
其实启用还是挺容易的,只要修改 ~/.liked/config/app.toml 和 ~/.liked/config/config.toml 就行。
~/.liked/config/app.toml 的话,要修改的是 [api] 这个部分。
############################################################################### ### API Configuration ### ############################################################################### [api] # 要启用 REST API 的话,肯定要把这个改成 true 啦。 # Enable defines if the API server should be enabled. enable = true # 要不要启用通过 swagger 展示所有可用的路径? # Swagger defines if swagger documentation should automatically be registered. swagger = true # API 服务器的监听地址,小于 1024 的端口通常需要 root 权限才能使用。 # 不过因为 Mozilla Infosec 教导咱们的 API 终结点要完全禁用 HTTP # 访问这回事,后面咱会用 Nginx 反向代理这个地址,所以之监听在本地也是 OK 的。 # Address defines the API server to listen on. address = "tcp://127.0.0.1:1317"
~/.liked/config/config.toml 要修改的是 [rpc] 的部分:
####################################################### ### RPC Server Configuration Options ### ####################################################### [rpc] # RPC 服务器的监听地址,写法和上面的 address 是一样的。 # TCP or UNIX socket address for the RPC server to listen on laddr = "tcp://127.0.0.1:26657"
保存然后重启 liked 服务,如果一切都 OK 的话,汝应该可以在汝的服务器上访问到 API:
# 一个 RPC 请求的例子 , RPC 服务器的根路径(/)有所有路径的列表。 $ curl http://localhost:26657/status { "jsonrpc": "2.0", "id": -1, "result": { "node_info": { "protocol_version": { "p2p": "8", "block": "11", "app": "0" }, "id": "a1b98d239ed4b0d3cfc55f28e4bbf988a8bacf35", "listen_addr": "tcp://0.0.0.0:26656", "network": "likecoin-mainnet-2", "version": "0.34.19", "channels": "40202122233038606100", "moniker": "Yoitsu", "other": { "tx_index": "on", "rpc_address": "tcp://0.0.0.0:26657" } }, "sync_info": { "latest_block_hash": "29AFD11FE27392594EF9D68BCA0D444802564E02AAEC9848D4D3FA0D53AB6560", "latest_app_hash": "D36D8F5D1E5B77AB78AE5B1D4E9434C8388FCE678B4567BD2C94C419A1E735C1", "latest_block_height": "7899104", "latest_block_time": "2023-02-20T08:11:15.359941991Z", "earliest_block_hash": "77A14BB37CCB6E426D521229A564E4AB37672915F97C47976EA56E165D35B618", "earliest_app_hash": "41D0A50BB85AAAE7789C8DFBD0C3ED4B99A84F0A8A0E6F2FAEC9F4824AA47B7C", "earliest_block_height": "4709200", "earliest_block_time": "2022-07-14T13:07:40.67338786Z", "catching_up": false }, "validator_info": { "address": "353558D7C7D69DF83A6C9D37BB8204B38561217C", "pub_key": { "type": "tendermint/PubKeyEd25519", "value": "cEwyDK/M1mJ+fJHXASeA0kaXRlVXYE1Ma4Ldne3Wh68=" }, "voting_power": "109783021" } } # 一个 REST 请求的例子。 # REST API 的响应没有格式化,如果想看的舒服一些的话,可以用 jq 一类的命令先格式化一下。 $ curl "http://localhost:1317/node_info" | jq { "node_info": { "protocol_version": { "p2p": "8", "block": "11", "app": "0" }, "id": "a1b98d239ed4b0d3cfc55f28e4bbf988a8bacf35", "listen_addr": "tcp://0.0.0.0:26656", "network": "likecoin-mainnet-2", "version": "0.34.19", "channels": "40202122233038606100", "moniker": "Yoitsu", "other": { "tx_index": "on", "rpc_address": "tcp://0.0.0.0:26657" } }, "application_version": { "name": "likecoin-chain", "server_name": "liked", "version": "v3.0.1", "commit": "36fc4c93961fb1a667a7e2a7cd206f38acbfb88f", "build_tags": "netgo,ledger", "go": "go version go1.18.3 linux/amd64", "build_deps": [ "filippo.io/[email protected]", "github.com/99designs/[email protected] => github.com/cosmos/[email protected]", "github.com/ChainSafe/[email protected]", "github.com/Workiva/[email protected]", "github.com/armon/[email protected]", "github.com/beorn7/[email protected]", "github.com/bgentry/[email protected]", "github.com/btcsuite/[email protected]", "github.com/cespare/xxhash/[email protected]", "github.com/coinbase/[email protected]", "github.com/confio/ics23/[email protected] => ./ics23/@(devel)", "github.com/cosmos/[email protected]", "github.com/cosmos/[email protected] => github.com/likecoin/[email protected]", "github.com/cosmos/[email protected]", "github.com/cosmos/[email protected]", "github.com/cosmos/ibc-go/[email protected]", "github.com/cosmos/[email protected]", "github.com/cosmos/[email protected]", "github.com/davecgh/[email protected]", "github.com/desertbit/[email protected]", "github.com/dvsekhvalnov/[email protected]", "github.com/felixge/[email protected]", "github.com/fsnotify/[email protected]", "github.com/go-kit/[email protected]", "github.com/go-kit/[email protected]", "github.com/go-logfmt/[email protected]", "github.com/godbus/[email protected]", "github.com/gogo/[email protected]", "github.com/gogo/[email protected] => github.com/regen-network/[email protected]", "github.com/golang/[email protected]", "github.com/golang/[email protected]", "github.com/google/[email protected]", "github.com/google/[email protected]", "github.com/gorilla/[email protected]", "github.com/gorilla/[email protected]", "github.com/gorilla/[email protected]", "github.com/grpc-ecosystem/[email protected]", "github.com/grpc-ecosystem/[email protected]", "github.com/gsterjov/[email protected]", "github.com/gtank/[email protected]", "github.com/gtank/[email protected]", "github.com/hashicorp/[email protected]", "github.com/hashicorp/[email protected]", "github.com/hashicorp/[email protected]", "github.com/hdevalence/[email protected]", "github.com/improbable-eng/[email protected]", "github.com/ipfs/[email protected]", "github.com/klauspost/[email protected]", "github.com/klauspost/cpuid/[email protected]", "github.com/lib/[email protected]", "github.com/libp2p/[email protected]", "github.com/magiconair/[email protected]", "github.com/mattn/[email protected]", "github.com/matttproud/[email protected]", "github.com/mimoo/[email protected]", "github.com/minio/[email protected]", "github.com/minio/[email protected]", "github.com/minio/[email protected]", "github.com/mitchellh/[email protected]", "github.com/mr-tron/[email protected]", "github.com/mtibben/[email protected]", "github.com/multiformats/[email protected]", "github.com/multiformats/[email protected]", "github.com/multiformats/[email protected]", "github.com/multiformats/[email protected]", "github.com/multiformats/[email protected]", "github.com/pelletier/[email protected]", "github.com/pkg/[email protected]", "github.com/pmezard/[email protected]", "github.com/prometheus/[email protected]", "github.com/prometheus/[email protected]", "github.com/prometheus/[email protected]", "github.com/prometheus/[email protected]", "github.com/rakyll/[email protected]", "github.com/rcrowley/[email protected]", "github.com/regen-network/[email protected]", "github.com/rs/[email protected]", "github.com/rs/[email protected]", "github.com/spf13/[email protected]", "github.com/spf13/[email protected]", "github.com/spf13/[email protected]", "github.com/spf13/[email protected]", "github.com/spf13/[email protected]", "github.com/spf13/[email protected]", "github.com/stretchr/[email protected]", "github.com/subosito/[email protected]", "github.com/syndtr/[email protected]", "github.com/tendermint/[email protected]", "github.com/tendermint/[email protected]", "github.com/tendermint/[email protected]", "github.com/tendermint/[email protected]", "github.com/tendermint/[email protected]", "github.com/zondax/[email protected]", "golang.org/x/[email protected]", "golang.org/x/[email protected]", "golang.org/x/[email protected]", "golang.org/x/[email protected]", "golang.org/x/[email protected]", "google.golang.org/[email protected]", "google.golang.org/[email protected] => google.golang.org/[email protected]", "google.golang.org/[email protected]", "gopkg.in/[email protected]", "gopkg.in/[email protected]", "gopkg.in/[email protected]", "nhooyr.io/[email protected]" ], "cosmos_sdk_version": "v0.45.6" } }
~/.liked/config/config.toml 里似乎提供了指定证书和私钥的选项,但是咱还是更习惯用 Nginx 来反向代理了。以及汝还需要一个域名来申请证书,这里咱用 certbot 来申请 Let’s Encrypt 证书,方法在这里 : https://certbot.eff.org/instructions
Nginx 的配置文件看起来像这样。(可以参考 Mozilla SSL Config Generator?)
# generated 2023-02-20, Mozilla Guideline v5.6, nginx 1.17.7, OpenSSL 1.1.1k, intermediate configuration, no HSTS # https://ssl-config.mozilla.org/#server=nginx&version=1.17.7&config=intermediate&openssl=1.1.1k&hsts=false&guideline=5.6 server { listen 443 ssl http2; listen [::]:443 ssl http2; ssl_certificate /path/to/signed_cert_plus_intermediates; ssl_certificate_key /path/to/private_key; ssl_session_timeout 1d; ssl_session_cache shared:MozSSL:10m; # about 40000 sessions ssl_session_tickets off; # curl https://ssl-config.mozilla.org/ffdhe2048.txt > /path/to/dhparam ssl_dhparam /path/to/dhparam; # intermediate configuration ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384; ssl_prefer_server_ciphers off; # OCSP stapling ssl_stapling on; ssl_stapling_verify on; # verify chain of trust of OCSP response using Root CA and Intermediate certs ssl_trusted_certificate /path/to/root_CA_cert_plus_intermediates; # replace with the IP address of your resolver resolver 127.0.0.1; location / { # 这里指向的是 REST API 的监听位置,如果汝要反向代理 RPC API 的话, # 修改这里就好啦。 proxy_pass http://localhost:1317/; proxy_redirect off; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; } }
如果 Nginx 和 DNS 的配置没问题的话,汝现在应该可以在汝设置的位置访问到汝开启的 API 了。例如咱的 https://likecoin-rest.yoitsu.xyz/ 和 https://likecoin-rpc.yoitsu.xyz/ 。
提供 State Sync 快照
就是 https://docs.like.co/validator/likecoin-chain-node/setup-a-node#state-sync 这一步用到的啦。要让自己的节点也能提供 State Sync 的快照的话,要修改的是 ~/.liked/config/app.toml 里 [state-sync] 的部分。
############################################################################### ### State Sync Configuration ### ############################################################################### # State sync snapshots allow other nodes to rapidly join the network without replaying historical # blocks, instead downloading and applying a snapshot of the application state at a given height. [state-sync] # 间隔多少区块保存快照? # 要让自己的节点提供快照的话,这个应该大于 0 而且是 pruning-keep-every 的倍数。 # 如果 pruning = "default" 的话,因为默认策略是保留每 500 个区块的状态, # 所以是 500 的倍数。以及显然的 pruning = "everything" 的话是不行的, # 记得规划一下硬盘空间。 # snapshot-interval specifies the block interval at which local state sync snapshots are # taken (0 to disable). Must be a multiple of pruning-keep-every. snapshot-interval = 1000 # 要在本地保留多少快照? # 设置成 0 的话,会保留所有生成的快照,当然占用的硬盘空间也会更多。 # snapshot-keep-recent specifies the number of recent snapshots to keep and serve (0 to keep all). snapshot-keep-recent = 2
接下来其他人要用到 State Sync 的时候,就能填写汝的 RPC 地址了。(虽然现在好像并没有新节点的样子……)
原文连结:约伊兹的萌狼乡手札
----
然后发现 WordPress 上用的 Keplr 的钱包地址注册的 ISCN ,但是那个地址不在 Writing NFT 的白名单里 😂
喜欢我的作品吗?别忘了给予支持与赞赏,让我知道在创作的路上有你陪伴,一起延续这份热忱!
- 来自作者
- 相关推荐