1. 程式人生 > >EOS智慧合約開發(十八)從EOS資料分析history_plugin外掛和MongoDB外掛區別

EOS智慧合約開發(十八)從EOS資料分析history_plugin外掛和MongoDB外掛區別

EOS資料
從EOS上線一段時間來看,他產生的Block已經遠遠超過了以太坊。以太坊在交易完成後,很容易過濾出交易資訊(如何獲取在我之前以太坊文章中有詳細描述)。EOS機制與以太坊有卻別,EOS如此大的資料量,我們就會問,EOS資料時如何獲取,查詢。今天我們就這個問題,給大家分析一下。
Data的價值分析
隨著區塊量各種應用的爆棚,EOS每天增加的使用者量上萬。我們從https://www.dappradar.com/ 上分析當前DApp排行榜,就可以清檢視到目前的熱點和應用方向。
在這裡插入圖片描述
如此多的應用,大家非常關心就是,交易資料如何獲取。
通過history_plugin外掛獲取交易資料
在EOSIO的1.3.0版本之前,官方提供文件通過history_plugin外掛獲取交易歷史資料,通過一下配置,可以獲取交易歷史資料。
如果你想蒐集所有的交易資訊,包括收集所有traces資訊,
我們可以檢視V1.1.0版本的官方文件描述
Options

  -f [ --filter-on ] arg                Track actions which match
                                        receiver:action:actor. Actor may be
                                        blank to include all. Receiver and
                                        Action may not be blank.

Config Options for eosio::http_client_plugin:
  --https-client-root-cert arg          PEM encoded trusted root certificate
                                        (or path to file containing one) used
                                        to validate any TLS connections made.
                                        (may specify multiple times)

  --https-client-validate-peers arg (=1)
                                        true: validate that the peer
                                        certificates are valid and trusted,
                                        false: ignore cert errors

可以通過config.ini 檔案配置。
需要配置如下外掛,配置方式

# config.ini
plugin = eosio::history_plugin

通過啟動node節點,通過引數配置如下:

# nodeos startup params
--plugin eosio::history_plugin

如果我們需要收集所有traces資訊,那麼歡需要在config.ini檔案中開啟下面配置

# Track actions which match receiver:action:actor. Actor may be blank to include all. Action and Actor both blank allows all from Recieiver. Receiver may not be blank. (eosio::history_plugin)
# filter-on =   //預設
filter-on =*   #開啟配置

# Do not track actions which match receiver:action:actor. Action and Actor both blank excludes all from Reciever. Actor blank excludes all from reciever:action. Receiver may not be blank. (eosio::history_plugin)
# filter-out =

# PEM encoded trusted root certificate (or path to file containing one) used to validate any TLS connections made.  (may specify multiple times)
#  (eosio::http_client_plugin)
# https-client-root-cert =

那麼我們如何獲取資料,典型的做法是,我們同步一個node節點,加入EOS主網,同步最新的鏈上資料,同時就能很方便的執行轉賬,使用命令列玩各種DApps。當然如果你懶得配置nodeos,也可以使用各個超級BP提供的RPC API。
比如從鏈上取到的一個Transaction如下:

account1111-> account2222
{
  "timestamp": "2018-08-16T12:45:57.000",
  "producer": "eosbixinboot",
  "confirmed": 0,
  "previous": "00ae4ccb182c2906e443ab99e2fa85cf8744f4ee5951a40a526c54e2f629202e",
  "transaction_mroot": "47a441fd376307c48fe4a081ad90b3ed731c2e659dd97950510f2938f7585b11",
  "action_mroot": "9462c900fa1b4db6aead6f38cfde7ae5a5020c283d703e8f31bad3719e66afcb",
  "schedule_version": 291,
  "new_producers": null,
  "header_extensions": [],
  "producer_signature": "SIG_K1_JuPWPNAFyjPNqZSLx27NiNVBY2eU6fGcJK91hxuXwHMQxXLFAxVzUuPeTS1RoPg9r53SvCrpRwpLBetff7Hu7QxiqsjCWz",
  "transactions": [
    {
      "status": "executed",
      "cpu_usage_us": 1313,
      "net_usage_words": 16,
      "trx": {
        "id": "678c8b715d84bdf9d99627c5dc54c62a3ae0a0f06b7a99663aa769b252ea3af6",
        "signatures": [
          "SIG_K1_KWMARsmdRjwBKkdsz2uxM2KtbkLVxQRnNEj4yrjPvoXLUNo3ougmkgeLnvfcJPPpyHcrtiAJN3cPgowwJRQjJP7PX9HmKz"
        ],
        "compression": "none",
        "packed_context_free_data": "",
        "context_free_data": [],
        "packed_trx": "2979755b754be0ef6f12000000000100a6823403ea3055000000572d3ccdcd0170b4a29d6aefa6de00000000a8ed32322170b4a29d6aefa6de30c61843a169a23958474c000000000004454f53000000000000",
        "transaction": {
          "expiration": "2018-08-16T13:16:25",
          "ref_block_num": 19317,
          "ref_block_prefix": 309325792,
          "max_net_usage_words": 0,
          "max_cpu_usage_ms": 0,
          "delay_sec": 0,
          "context_free_actions": [],
          "actions": [
            {
              "account": "eosio.token",
              "name": "transfer",
              "authorization": [
                {
                  "actor": "account1111",
                  "permission": "active"
                }
              ],
              "data": {
                "from": "account1111",
                "to": "account2222",
                "quantity": "499.9000 EOS",
                "memo": ""
              },
              "hex_data": "70b4a29d6aefa6de30c61843a169a23958474c000000000004454f530000000000"
            }
          ],
          "transaction_extensions": []
        }
      }
    }
  ],
  "block_extensions": [],
  "id": "00ae4ccc6879ed8936a6b90e133c6f8a37a3c2dbc6e1486a4df677ce148f0840",
  "block_num": 11422924,
  "ref_block_prefix": 247047734
}

只有一部分Transaction資訊,完整的可點選連結檢視。

首先獲取到的塊是一個json串,在塊資訊中,包含了塊生成的時間,BP名稱等基本資訊,不細說,看名稱就能識別。其中的Transaction資訊,看上圖,能看到執行的合約是eosio.token,執行的是轉賬(Transfer)操作,從data中能看到,傳送者是account1111,傳送給account2222,數量是499.9000 EOS,很簡單,很直觀。一般從區塊瀏覽器中看到這些就夠了,轉賬的確認再加上確認數就可以了。

如何配置history_plugin外掛
在history_plugin外掛的配置下,可以查詢一個賬號的Transaction和Action都依賴於此外掛,因為這種配置也有非常大的問題,但是在EOSIO的1.3.0版本,已經移除了此外掛。那麼還有其他地方記錄這些狀態資訊麼?

通過MongoDB外掛獲取資料
EOS上的資料對外輸出都直接是json格式的,而MongoDB非常適合儲存這樣的資料。在EOSIO官方文件中是mongo_db_plugin,檢視此文件能幫助你如何使用,更多的資訊還得看下它的歷史。在EOSIO1.0版本中,其實你是看不到此外掛的,而在1.1的版本中才恢復,而且在當時的版本,問題不少,功能也有欠缺;在1.2版本之後才越來越穩定好用。
看看使用該外掛都儲存了哪些資訊:
官方文件中,描述的非常清晰,我們就簡單挑選出主要部分
https://developers.eos.io/eosio-nodeos/v1.3.0/docs/mongo_db_plugin

accounts  - created on applied transaction
action_traces - created on applied transaction
block_states - created on accepted block
 - block_num
 - block_id
 - block_header_state -
 - see eosio::chain::block_header_state
 - validated
 - in_current_chain
blocks - created on accepted block
 - block_num
 - block_id
 - block - signed block - see eosio::chain::signed_block
 - validated - added on irreversible block
 - in_current_chain - added on irreversible block
 - irreversible=true - added on irreversible block
transaction_traces - created on applied transaction
 - see chain::eosio::transaction_trace
transactions - created on accepted transaction - does not include inline actions

see eosio::chain::signed_transaction. In addition to signed_transaction data the following are also stored.
 - trx_id - transaction id
 - irreversible=true - added on irreversible block
 - block_id - added on irreversble block
 - block_num - added on irreversible block
 - signing_keys
 - accepted
 - implicit
 - scheduled
account_controls - created on applied transaction. Always updated even if mongodb-store-action-traces=false.
 - controlled_account
 - controlling_permission
 - controlling_account
可以通過一下 地址查詢 /v1/history/get_controlled_acounts
db.account_controls.find({"controlling_account":"hellozhangyi"}).pretty()
pub_keys - created on applied transaction. Always updated even if mongodb-store-action-traces=false.
 - account
 - permission
 - public_key

我們可以參考github上大家對MongoDB的討論,https://github.com/EOSIO/eos/pull/5066 分析這個問題。
在沒有action_traces之前,前面說的traces資訊主要看transaction_traces,裡面包含了所有合約呼叫互動的細節,比如買賣RAM的所有轉賬資訊。
那如何使用呢,上面的官方文件裡面有配置說明,通常在配置檔案config.ini中增加如下配置即可,當然還得啟動mongodb(下面的參考連結裡有)

    abi-serializer-max-time-ms = 5000
    plugin = eosio::mongo_db_plugin
    mongodb-uri = mongodb://localhost:27017/eos
    # 曾經刷測TPS的賬號,目前已經停止
    mongodb-filter-out = blocktwitter:tweet:
    # 可以控制儲存哪些資訊
    #mongodb-store-blocks = false
    # 可以選擇從哪個塊開始儲存
    #mongodb-block-start = 19455000

在新的官方文件中,提供了其他一些外掛連線,後續我們再繼續分析。
我們對一些問題的思考的探索
EOSIO的程式碼更新很快,有時候覺得後進入整理資料的人有時候是幸運的,nodeos穩定許多,mongodb_db_plugin外掛還增加了更多的功能。不過EOS的鏈上資料增長也很快,儲存blocks的資料夾都已經有61G,mongodb光儲存transactions_traces資訊都有幾百G了。

  • state資料庫
    早期想儲存所有traces資訊的人,如果使用filter-on=*配置,簡直就不可能。因為traces資訊很快將state資料庫填滿,使你在config.ini中配置的chain-state-db-size-mb不夠用,而這也將使nodeos的同步停止,IO飆升,只能升機器的記憶體,當時用過128G記憶體的機器,很快就不夠用了。這個也和EOSIO使用的狀態資料庫chainbase有關,有興趣的可以查查,這也是,我們鏈執行幾周後,無法同步資料,讓我們聯合分析了很久才查到的原因,也正是我也整理這篇文章的主要原因。
  • 垃圾資料
    在主網正式執行後不久,就一直能看到blocktwitter賬號執行tweet,來壓測整個網路的TPS,導致了大量無用的資訊,這也是前面配置過濾該賬號的原因。而最近又有部分非常規資料出現,比如賬號cryptohongbo,在10月11日左右的資料,Actions雖然不明顯,但是在traces中能發現大量資料,還不知道是用來做什麼的。
  • 新增賬號
    前面提到塊中資訊的不完整,導致的一個問題就是新增賬號數統計,有可能不準確。比如直接呼叫系統合約建立賬號,那麼在塊資訊中會有體現。但是很多合約如signupeoseos提供了通過轉賬,自動幫你建立賬號的功能。而這些資訊是在塊中看不到的,只有traces新中才有。

2018年11月9日寫於深圳