如何解决此 Hardhat CompilerError? (编译内联汇编时堆栈太深)

How do I resolve this Hardhat CompilerError? (Stack too deep when compiling inline assembly)

我正在分析 Chain运行ners 智能合约,所以我继续使用 Etherscan 并复制了 verified contract source code

当我尝试在没有 solidity 优化器的情况下编译时,我收到了这个警告:

thatguyintech@albert chainrunners % npx hardhat compile
Compiling 5 files with 0.8.4
Warning: Unused local variable.
   --> contracts/ChainRunnersBaseRenderer.sol:232:124:
    |
232 |  ... kenPalettes, uint8 numTokenLayers, string[NUM_LAYERS] memory traitTypes) = getTokenData(_dna);
    |                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


Warning: Contract code size exceeds 24576 bytes (a limit introduced in Spurious Dragon). This contract may not be deployable on mainnet. Consider enabling the optimizer (with a low "runs" value!), turning off revert strings, or using libraries.
  --> contracts/ChainRunnersBaseRenderer.sol:48:1:
   |
48 | contract ChainRunnersBaseRenderer is Ownable, ReentrancyGuard {
   | ^ (Relevant source part starts here and spans across multiple lines).

所以我尝试根据Hardhat官方文档开启优化器:https://hardhat.org/config/

这是我的安全帽配置 hardhat.config.js 的样子:

/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {
  solidity: {
    version:  "0.8.4",
    settings: {
      optimizer: {
        enabled: true,
        runs: 2000,
      }
    }
  }
};

所以现在当我尝试 运行 npx hardhat compile:

时,我得到了这顶安全帽 CompilerError
thatguyintech@albert chainrunners % npx hardhat compile
Compiling 5 files with 0.8.4
CompilerError: Stack too deep when compiling inline assembly: Variable value0 is 3 slot(s) too deep inside the stack.

有人知道我该如何解决这个问题吗?从对安全帽相关线程的几次 google 搜索中,似乎打开优化器 应该 是解决这个问题的方法,所以我很困惑。

这是我在 OpenZeppelin 论坛上找到的一个不适合我的例子:https://forum.openzeppelin.com/t/stack-to-deep-when-compiling-inline-assembly/11391/11

啊,原来在 Etherscan 页面中有一个部分显示了确切的 solidity 优化器集。 (h/t @alcuadadro)

看起来像这样:

所以我将其复制到我的 hardhat.config.js:

/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {
  solidity: {
    version:  "0.8.4",
    settings: {
      optimizer: {
        enabled: true,
        runs: 2000,
        details: {
          yul: true,
          yulDetails: {
            stackAllocation: true,
            optimizerSteps: "dhfoDgvulfnTUtnIf"
          }
        }
      }
    },
  },
};

成功了!

虽然不知道 yul 的内容是什么