Consider a strategy for reducing contract size
Closed this issue · 6 comments
yu23ki14 commented
改善詳細 / Details of Improvement
- コントラクトサイズが大きくなってきており、ほしい機能が積みにくい状態になっているのでコントラクトサイズの削減方法を考える。
- hardhat-contract-sizerとconfig optimizerをつかってUpgradeableコントラクトのUpgradeタイミングでも削減できるのか確認。おそらくできると思うがどうだろうか...
動作環境・ブラウザ / Environment
- EventManager.solとMintNFT.sol
tayutaedomo commented
hardhat-contract-sizer と config optimizer でどうなるか試してみます!
tayutaedomo commented
途中報告
結果
optimizer (runs:200)
を試した所、アップグレードはエラーになりませんでした。
調査ログ
調査の流れ
- ローカル環境にて最新の staging ブランチコードを hardhat network にデプロイ
- 最適化の設定を追加
- v1.2.0 のアップグレードスクリプトでアップグレードを実行
hardhat.config.ts の変更
- 最適化を有効にするために hardhat.config.ts は以下のように変更
const config: HardhatUserConfig = {
- solidity: "0.8.9",
+ solidity: {
+ version: "0.8.9",
+ settings: {
+ optimizer: {
+ enabled: true,
+ runs: 200,
+ },
+ },
+ },
変更前後のサイズ
変更前サイズ
>> npx hardhat size-contracts
Nothing to compile
No need to generate any newer typings.
·-------------------------------|--------------------------------|--------------------------------·
| Solc version: 0.8.9 · Optimizer enabled: false · Runs: 200 │
································|································|·································
| Contract Name · Deployed size (KiB) (change) · Initcode size (KiB) (change) │
································|································|·································
| console · 0.084 (0.000) · 0.162 (0.000) │
································|································|·································
| Hashing · 0.084 (0.000) · 0.162 (0.000) │
································|································|·································
| AddressUpgradeable · 0.084 (0.000) · 0.162 (0.000) │
································|································|·································
| StringsUpgradeable · 0.084 (0.000) · 0.162 (0.000) │
································|································|·································
| Counters · 0.084 (0.000) · 0.162 (0.000) │
································|································|·································
| Strings · 0.084 (0.000) · 0.162 (0.000) │
································|································|·································
| ECDSAUpgradeable · 0.084 (0.000) · 0.162 (0.000) │
································|································|·································
| ECDSA · 0.084 (0.000) · 0.162 (0.000) │
································|································|·································
| MinimalForwarderUpgradeable · 4.743 (0.000) · 4.774 (0.000) │
································|································|·································
| MintRallyForwarder · 4.934 (0.000) · 5.585 (0.000) │
································|································|·································
| SecretPhraseVerifier · 7.692 (0.000) · 7.724 (0.000) │
································|································|·································
| ERC721Upgradeable · 8.396 (0.000) · 8.428 (0.000) │
································|································|·································
| EventManager · 13.388 (0.000) · 13.419 (0.000) │
································|································|·································
| MintNFT · 22.907 (0.000) · 22.939 (0.000) │
·-------------------------------|--------------------------------|--------------------------------·
最適化後のサイズ
>> npx hardhat size-contracts
Generating typings for: 32 artifacts in dir: typechain for target: ethers-v5
Successfully generated 47 typings!
Compiled 32 Solidity files successfully
·-------------------------------|--------------------------------|--------------------------------·
| Solc version: 0.8.9 · Optimizer enabled: true · Runs: 200 │
································|································|·································
| Contract Name · Deployed size (KiB) (change) · Initcode size (KiB) (change) │
································|································|·································
| console · 0.084 (0.000) · 0.138 (-0.024) │
································|································|·································
| Hashing · 0.084 (0.000) · 0.138 (-0.024) │
································|································|·································
| AddressUpgradeable · 0.084 (0.000) · 0.138 (-0.024) │
································|································|·································
| StringsUpgradeable · 0.084 (0.000) · 0.138 (-0.024) │
································|································|·································
| Strings · 0.084 (0.000) · 0.138 (-0.024) │
································|································|·································
| Counters · 0.084 (0.000) · 0.138 (-0.024) │
································|································|·································
| ECDSAUpgradeable · 0.084 (0.000) · 0.138 (-0.024) │
································|································|·································
| ECDSA · 0.084 (0.000) · 0.138 (-0.024) │
································|································|·································
| MinimalForwarderUpgradeable · 2.560 (-2.184) · 2.591 (-2.184) │
································|································|·································
| MintRallyForwarder · 2.721 (-2.213) · 3.065 (-2.520) │
································|································|·································
| ERC721Upgradeable · 4.317 (-4.079) · 4.349 (-4.079) │
································|································|·································
| SecretPhraseVerifier · 5.625 (-2.067) · 5.656 (-2.067) │
································|································|·································
| EventManager · 8.505 (-4.883) · 8.536 (-4.883) │
································|································|·································
| MintNFT · 12.896 (-10.011) · 12.928 (-10.012) │
·-------------------------------|--------------------------------|--------------------------------·
アップグレードの実行
- アップグレード実行のためにはいくつか修正が必要であったため修正を実施
- v1.2.0 アップグレードスクリプトの修正
- MintNFT の initializer の第三引数に誤りがあるため削除
- EventManager のアドレスをローカルのものに合わせて変更
- EventManager がアップグレードされるようにコメントアウトを解除
- MintNFT の修正
- reinitializer のバージョン番号を +1
- EventManager の修正
reinitializer(2)
に変更
コマンド実行
>> npx hardhat run scripts/upgrades/v1.2.0/upgrade_local.ts --network local
Generating typings for: 2 artifacts in dir: typechain for target: ethers-v5
Successfully generated 7 typings!
Compiled 2 Solidity files successfully
mintNFT address: 0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9
eventManager address: 0x0165878A594ca255338adfa4d48449f69242Eb8F
この後の調査
- hardhat-contract-sizer のオプションを試す
- 特に only オプションでコントラクトを指定する部分は優先して調査する予定
tayutaedomo commented
以下、口頭で頂いた話のノートです。
追加調査:デグレ確認
- ローカルにデプロイ後、イベント作成/mint を実行してその後にアップグレードした場合を検証する。
- アップグレード後にイベントが取得できるか、mint できるか、イベント作成できるか、など確認する。
tayutaedomo commented
contractSizer の調査報告
結果
- contractSizer は出力の制御だけでコンパイル時の最適化に影響があるわけではない模様。
- そのため最適化という文脈では contractSizer の設定は特に考えないこととする。
- 出力を制御したい場合に設定する。
調査ログ
- 初期のサイズを確認
実行ログ
>> npx hardhat size-contracts
Nothing to compile
No need to generate any newer typings.
·-------------------------------|--------------------------------|--------------------------------·
| Solc version: 0.8.9 · Optimizer enabled: false · Runs: 200 │
································|································|·································
| Contract Name · Deployed size (KiB) (change) · Initcode size (KiB) (change) │
································|································|·································
| console · 0.084 (0.000) · 0.162 (0.000) │
································|································|·································
| Hashing · 0.084 (0.000) · 0.162 (0.000) │
································|································|·································
| AddressUpgradeable · 0.084 (0.000) · 0.162 (0.000) │
································|································|·································
| StringsUpgradeable · 0.084 (0.000) · 0.162 (0.000) │
································|································|·································
| Counters · 0.084 (0.000) · 0.162 (0.000) │
································|································|·································
| Strings · 0.084 (0.000) · 0.162 (0.000) │
································|································|·································
| ECDSAUpgradeable · 0.084 (0.000) · 0.162 (0.000) │
································|································|·································
| ECDSA · 0.084 (0.000) · 0.162 (0.000) │
································|································|·································
| MinimalForwarderUpgradeable · 4.743 (0.000) · 4.774 (0.000) │
································|································|·································
| MintRallyForwarder · 4.934 (0.000) · 5.585 (0.000) │
································|································|·································
| SecretPhraseVerifier · 7.692 (0.000) · 7.724 (0.000) │
································|································|·································
| ERC721Upgradeable · 8.396 (0.000) · 8.428 (0.000) │
································|································|·································
| EventManager · 13.388 (0.000) · 13.419 (0.000) │
································|································|·································
| MintNFT · 22.907 (0.000) · 22.939 (0.000) │
·-------------------------------|--------------------------------|--------------------------------·
optimizer (runs:200)
を追加- 以下オプションを追加
contractSizer: {
only: ["MintNFT", "EventManager"],
},
>> npx hardhat size-contracts
Generating typings for: 32 artifacts in dir: typechain for target: ethers-v5
Successfully generated 47 typings!
Compiled 32 Solidity files successfully
·-----------------------|--------------------------------|--------------------------------·
| Solc version: 0.8.9 · Optimizer enabled: true · Runs: 200 │
························|································|·································
| Contract Name · Deployed size (KiB) (change) · Initcode size (KiB) (change) │
························|································|·································
| EventManager · 8.505 (-4.883) · 8.536 (-4.883) │
························|································|·································
| MintNFT · 12.896 (-10.011) · 12.928 (-10.012) │
·-----------------------|--------------------------------|--------------------------------·
- 他のコントラクトがどうなっているのかを確認するために再度サイズを確認する。
- only オプションはコメントアウト
- コマンドはコンパイル無しで実行
実行ログ
>> npx hardhat size-contracts --no-compile
·-------------------------------|--------------------------------|--------------------------------·
| Solc version: 0.8.9 · Optimizer enabled: true · Runs: 200 │
································|································|·································
| Contract Name · Deployed size (KiB) (change) · Initcode size (KiB) (change) │
································|································|·································
| console · 0.084 () · 0.138 () │
································|································|·································
| Hashing · 0.084 () · 0.138 () │
································|································|·································
| AddressUpgradeable · 0.084 () · 0.138 () │
································|································|·································
| StringsUpgradeable · 0.084 () · 0.138 () │
································|································|·································
| Counters · 0.084 () · 0.138 () │
································|································|·································
| Strings · 0.084 () · 0.138 () │
································|································|·································
| ECDSAUpgradeable · 0.084 () · 0.138 () │
································|································|·································
| ECDSA · 0.084 () · 0.138 () │
································|································|·································
| MinimalForwarderUpgradeable · 2.560 () · 2.591 () │
································|································|·································
| MintRallyForwarder · 2.721 () · 3.065 () │
································|································|·································
| ERC721Upgradeable · 4.317 () · 4.349 () │
································|································|·································
| SecretPhraseVerifier · 5.625 () · 5.656 () │
································|································|·································
| EventManager · 8.505 (0.000) · 8.536 (0.000) │
································|································|·································
| MintNFT · 12.896 (0.000) · 12.928 (0.000) │
·-------------------------------|--------------------------------|--------------------------------·
- 他のコントラクトのサイズが減っていることを確認
- そのため only オプションで最適化の対象を絞ることができるわけではないと判断
tayutaedomo commented
[DONE] デグレ有無の調査
- #408 (comment)
- MintRally を操作してデグレ発生の有無を検証します。
結果
- MintRally を確認しましたが、特に壊れている部分などは見つかりませんでした。
検証方法
- ローカルで MintRally を起動
- staging ブランチの最新コミットをデプロイする
- アップグレード前の MintRally での操作
- イベントを作成する
- mint する
- アップグレードを実施
- 最適化を有効にする
- reintialize を +1 してアップグレードを実行する
- アップグレード後の MintRally での操作・確認
- 作成済みイベントの内容を確認する
- 作成済みイベントの mint 済み情報を確認する
- 作成済みイベントで mint できることを確認する
- 新規イベントを作成する
- 新規イベントで mint できることを確認する
- 作成済みイベントの内容を再度確認する
- 作成済みイベントの mint 済み情報を再度確認する
実行ログ
ローカルにデプロイ
>> yarn deploy:local
yarn run v1.22.17
$ npx hardhat run scripts/deploy_local.ts --network local
No need to generate any newer typings.
forwarder address: 0x5FbDB2315678afecb367f032d93F642f64180aa3
secretPhraseVerifier address: 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
mintNFT address: 0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9
eventManager address: 0x0165878A594ca255338adfa4d48449f69242Eb8F
最適化してサイズを確認
>> npx hardhat size-contracts
Generating typings for: 32 artifacts in dir: typechain for target: ethers-v5
Successfully generated 47 typings!
Compiled 32 Solidity files successfully
·-------------------------------|--------------------------------|--------------------------------·
| Solc version: 0.8.9 · Optimizer enabled: true · Runs: 200 │
································|································|·································
| Contract Name · Deployed size (KiB) (change) · Initcode size (KiB) (change) │
································|································|·································
| console · 0.084 (0.000) · 0.138 (-0.024) │
································|································|·································
| Hashing · 0.084 (0.000) · 0.138 (-0.024) │
································|································|·································
| AddressUpgradeable · 0.084 (0.000) · 0.138 (-0.024) │
································|································|·································
| StringsUpgradeable · 0.084 (0.000) · 0.138 (-0.024) │
································|································|·································
| Counters · 0.084 (0.000) · 0.138 (-0.024) │
································|································|·································
| Strings · 0.084 (0.000) · 0.138 (-0.024) │
································|································|·································
| ECDSAUpgradeable · 0.084 (0.000) · 0.138 (-0.024) │
································|································|·································
| ECDSA · 0.084 (0.000) · 0.138 (-0.024) │
································|································|·································
| MinimalForwarderUpgradeable · 2.560 (-2.184) · 2.591 (-2.184) │
································|································|·································
| MintRallyForwarder · 2.721 (-2.213) · 3.065 (-2.520) │
································|································|·································
| ERC721Upgradeable · 4.317 (-4.079) · 4.349 (-4.079) │
································|································|·································
| SecretPhraseVerifier · 5.625 (-2.067) · 5.656 (-2.067) │
································|································|·································
| EventManager · 8.505 (-4.883) · 8.536 (-4.883) │
································|································|·································
| MintNFT · 12.896 (-10.011) · 12.928 (-10.012) │
·-------------------------------|--------------------------------|--------------------------------·
アップグレードを実行
>> npx hardhat run scripts/upgrades/v1.2.0/upgrade_local.ts --network local
Generating typings for: 2 artifacts in dir: typechain for target: ethers-v5
Successfully generated 7 typings!
Compiled 2 Solidity files successfully
mintNFT address: 0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9
eventManager address: 0x0165878A594ca255338adfa4d48449f69242Eb8F
tayutaedomo commented
@yu23ki14
PR 作成しておきました!問題があればご連絡くださいmm