ブロックチェーン・仮想通貨・暗号資産の開発ブログ

バッチリクエストを利用してTruffle migrateを高速化する方法

TruffleはEthereumスマートコントラクトの開発フレームワークとして最も有名ですが、GitHubなどに上がってるコードを参考に見てみると、雰囲気で使ってるんじゃないかと思うことがあります。

中には効率が悪いものもあって、Javascriptの並行処理を使ったり、トランザクションをバッチ処理することで高速化できるようなコードを見かけることもあります。

本記事では、Truffleを使ってコントラクトをデプロイする際に、バッチリクエストをすることでデプロイを高速化するための方法について紹介します。

基本のTruffle migrate

まず次のような2つのコントラクトが存在する状況を考えます。

ContractTwoはContractOneに依存しており、したがってContractOneのデプロイが完了してからでないとContractTwoをデプロイできません。

これら2つのコントラクトをデプロイするようなマイグレーションスクリプトは以下のように書けます。

複雑なマイグレーション 

では、次のようにたくさんのコントラクトがある状況を考えます。

急に複雑になった感じがしますが、簡単にいうと

  • ContractDはContractA, ContractB, ContractCに依存している
  • ContractDがデプロイできたらContractDのアドレスをContractA, ContractB, ContractCのsetAcceptableを使ってsenderの登録をしたい

という感じです。こうなるようにデプロイするにはどうしたらいいでしょうか。単純に書くと次のようになります。

これで問題なくデプロイできるのですが、7回ものトランザクション承認が必要となり、デプロイにすごく時間がかかってしまいます。

プライベートネットでは気にならないかもしれませんが、テストネットでデプロイしようとすると急激に遅くなってしまいます。

効率化されたTruffle migrate

そこで次のようにdeployer.deploy([])構文で書き直すと、一度のトランザクションに複数のデプロイメントをまとめることができ、デプロイのスピードが速くなります。

またsetAcceptableの方はPromise.all構文を利用し並行処理することによって高速化できます。awaitの回数は当初7回に対して、改善したデプロイスクリプトでは3回と半減しています。

いかがでしょうか。ぜひ快適なデプロイ生活を!

Shun Takagiwa / 高際 隼

Blockchain Engineer at LayerX inc.
コメント機能やTwitterなどでお気軽にコメントいただけると嬉しいです。

4 件のコメント :

Reader's Comments

  1. Best bets for soccer today - Sports Toto
    Today, we're going to tell you ventureberg.com/ a หาเงินออนไลน์ few key to 토토 사이트 홍보 checking febcasino.com into soccer 바카라 사이트 betting apps. of the most popular soccer betting options and which ones will

    返信削除
  2. The King Casino Resort - Hertzaman
    Find the perfect herzamanindir.com/ place to stay, https://tricktactoe.com/ play, and septcasino unwind at Harrah's Resort Southern communitykhabar California. Get your points now! febcasino.com

    返信削除
  3. Then step inside our in depth sport library search out|to search out} something that piques your curiosity. We’re fiercely proud of the velocity at which our website operates. But we’re even prouder of our 카지노 사이트 distinctive rewards program that dynamically hands out tailored rewards in real-time.

    返信削除
  4. Whether you’re looking for basic slots or video slots, they are all free to play. There isn't any sure means of 우리카지노 telling when a slot machine is about to hit. Slot machines are ruled by Random Number Generators, which ensure a very unpredictable end result every time you spin the reels. Most slot bonuses let you play free of charge solely a specific selection of slot machines. All data about what games are a part of} every provide are included within the terms and conditions.

    返信削除