Mina 文档 - 高级篇 之 归档节点

  • MinaFans
  • 更新于 2021-12-11 21:36
  • 阅读 1965

Mina 开发者文档

4.1 归档节点

默认情况下,Mina节点是轻量级,这意味着它们不需要维护关于网络、区块或交易的历史信息。在某些情况下,维护历史数据是很有用的,可以通过运行归档节点来实现这个操作。

归档节点只是一个和现行的归档进程相连接的常规mina守护进程。守护进程将定期向归档进程发送区块链数据,归档进程将把数据存储在Postgres数据库中。

因此,运行归档节点需要一些管理Postgres数据库实例的知识。在本节中,我们将建立一个数据库,运行归档节点,将它连接到守护进程,并尝试对数据进行一些查询。现在让我们开始安装我们需要的东西。

安装

  1. 安装最新版本的mina。如果您还没有升级到守护进程的最新版本,请返回文档以获取最新版本。您可以运行mina -help来检查安装是否成功。
  2. 安装Postgres
  3. 安装归档节点包。

● Ubuntu / Debian:

sudo apt-get install mina-archive=1.1.5-a42bdee

● Docker:

minaprotocol/mina-archive:1.1.5-a42bdee

设置

下面是一些关于如何设置使归档节点在本地运行所需的基本说明。如果您连接到postgres的云实例,如果您使用docker进行部署,或者您想在不同的机器上运行这些进程,那么这些进程将略有不同。

请注意

注意:其中一些说明可能取决于您的操作系统如何安装postgres(并假设它已经安装在第一个地方)。

  1. 启动本地postgres服务器。该服务器将在前台运行并进行测试,您可能希望在后台运行它或使用操作系统的服务管理器(如systemd)为您运行它。或者,您可以使用一个由云提供商托管的postgres服务。
postgres -p 5432 -D /usr/local/var/postgres

对于macOS系统,运行brew services start postgres来启动本地postgres服务器。

  1. 在服务器上创建数据库(这里称为archive)并将模式加载到其中。这只需要在第一次设置存档节点时执行。
createdb -h localhost -p 5432 -e archive

psql -h localhost -p 5432 -d archive -f <(curl -Ls
https://raw.githubusercontent.com/MinaProtocol/mina/master/src/app/archive/create_schema.sql)
  1. 在端口3086上启动归档进程,并连接步骤1中在端口5432上启动的postgres数据库。
coda-archive run \
 -postgres-uri postgres://localhost:5432/archive \
 -server-port 3086
  1. 启动守护进程,将其连接到我们刚刚在端口3086上启动的归档进程。如果您要连接到另一台机器上的存档进程,您还可以指定一个主机名,比如localhost:3086
mina daemon \
  .....
 -archive-address 3086\

使用归档节点

现在我们已经运行了归档节点,让我们看一下数据库中的表。

要列出数据库中的表,您可以在psql中运行\dt命令。在这里查看表的完整模式。

下面是每个表中一些值得注意的字段。

表1:user_commands

该表跟踪网络上的交易。

...

 user_command_type      Type of transaction being made

              Possible values: `'payment', 'delegation'

              To see a specific type of transaction, i.e. payments or creating a token, specify this field in your queries.

 source_id          public key of the sender

 receiver_id         public key of the receiver

 amount           amount being sent from the sender to the receiver

 token            ID of a token If you are querying for different type of token transactions, specify this field.

表2:internal_commands

这个表记录了从snark工作或区块生产中获得的奖励。

...

 internal_command_type    represents whether the command is a `fee_transfer` from snark work or a `coinbase` reward from block producing.

                  Use this field for information about block rewards and snark rewards (there is also an extra fee_transfer added to support sending all the transaction fees summed together to the block_creator)

 receiver_id         public key ID of the receiver

 fee             amount being sent from the protocol to the receiver

 token            ID of a token  If you are querying for different type of token transactions, specify this field.

表3:blocks

...
 id
 parent_id          ID of the previous block in the blockchain
                Use this field for information about block rewards and snark rewards (there is also an extra fee_transfer added to support sending all the transaction fees summed together to the block_creator)
 creator_id         public key of the block creator

连接表

归档数据库中有两个连接表,它们将区块和交易相连接。通过链接区块表和命令表,这些表使得您可以识别区块中的特定交易。

JOIN TABLE 1: BLOCKS_USER_COMMANDS

...
block_id           ID of the block containing the user command
user_command_id        ID of the user command
sequence_no          0-based order of the user command among the block transactions

JOIN TABLE 2: BLOCKS_INTERNAL_COMMANDS

...
block_id           ID of the block containing the internal command
internal_command_id      ID of the internal command
sequence_no          0-based order of the internal command among the block transactions
secondary_sequence_no     0-based order of a fee transfer within a coinbase internal command

尝试查询

现在我们已经了解了数据的结构,让我们尝试做一个查询。

例1:查找公钥创建的所有区块

SELECT *
FROM blocks AS b
INNER JOIN public_keys AS pk1 ON b.creator_id = pk1.id
WHERE value = 'MY_PK'

例2:查找您通过公钥收到的所有付款

SELECT *
FROM user_commands AS uc
JOIN blocks_user_commands AS buc ON uc.id = buc.user_command_id
JOIN public_keys AS pk ON uc.receiver_id = pk.id
WHERE value = 'MY_PK'
AND type = 'payment'

例3:查找合法链上高度为12的区块

WITH RECURSIVE chain AS (
 (SELECT ... FROM blocks b WHERE height = (select MAX(height) from blocks)
 ORDER BY timestamp ASC
 LIMIT 1)

 UNION ALL

 SELECT ... FROM blocks b
 INNER JOIN chain
 ON b.id = chain.parent_id AND chain.id <> chain.parent_id
) SELECT ..., pk.value as creator FROM chain c
 INNER JOIN public_keys pk
 ON pk.id = c.creator_id
 WHERE c.height = 12

例4:列出每个公钥创建的区块的计数,并按降序排序

SELECT p.value, COUNT(*) FROM blocks
INNER JOIN public_keys AS p ON creator_id = ip.id
GROUP BY p.value
ORDER BY count DESC;

例5:列出每个公钥创建的应用支付计数,并按降序排序

SELECT p.value, COUNT(*) FROM user_commands
INNER JOIN public_keys AS p ON source_id = p.id
WHERE status = 'applied'
AND type = 'payment'
GROUP BY p.value ORDER BY count DESC;
点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。
该文章收录于 Mina 开发者文档
44 订阅 26 篇文章

0 条评论

请先 登录 后评论
MinaFans
MinaFans
minafans.tech