【发布时间】:2022-12-18 11:46:04
【问题描述】:
我希望有像Block(5).hashdata这样的东西。 Solidity中有类似的东西来获取另一个块的哈希数据吗?
【问题讨论】:
我希望有像Block(5).hashdata这样的东西。 Solidity中有类似的东西来获取另一个块的哈希数据吗?
【问题讨论】:
我不相信,但我能够使用 ChatGPT 解决这个问题!我在这里添加答案
是的,您可以使用 Solidity 中的其他块获取数据
block.number财产。此属性返回块的编号 该代码当前正在执行,因此您可以使用它来 从以前的块访问数据。这是一个如何使用
block.number属性的示例 从前一个块访问数据:previousBlockNumber = block.number - 1; // Get the data from the previous block bytes32 previousBlockData = block.blockhash(previousBlockNumber); ``` Keep in mind that the `blockhash` function is a low-level function that can be expensive to use, so you should use it with caution and only when absolutely necessary. Also, keep in mind that the `blockhash` function only works for blocks that are within the last 256 blocks, so it may not be possible to access data from blocks that are further back in time.
【讨论】: