什么时候 eth 合约函数需要 return 值存储?为什么不只是内存类型?
When eth contract function need return value with storage? and why not just with memory type?
我正在学习 gnosis/safe-contracts 的源代码。对于这个示例文件,我有一个问题,为什么这个函数需要 return 具有 storage 的数据,我知道 storage 之间的区别] 和 内存.
/*
*https://github.com/gnosis/safe-contracts/blob/2620a21c0844f23df39ea98438b82e378bb334f0/contracts/examples/guards/ReentrancyTransactionGuard.sol#L21
*/
function getGuard() internal pure returns (GuardValue storage guard) {
bytes32 slot = GUARD_STORAGE_SLOT;
// solhint-disable-next-line no-inline-assembly
assembly {
guard.slot := slot
}
}
一个更普遍的问题可能是关于当我们需要或必须使用 存储 时的 return 值?我可以参考任何文档吗?真的谢谢你!
此函数从 checkTransaction()
调用,它设置 guard
属性 active
.
GuardValue storage guard = getGuard();
require(!guard.active, "Reentrancy detected");
guard.active = true;
如果 getGuard()
返回一个内存变量(而不是存储变量),guard
变量也需要是一个内存变量 - 否则你会在尝试分配时出现不匹配将内存变量转换为存储变量,无法编译。
如果 guard
变量有一个内存位置,它只会在内存副本中更改 active
值,而不是在实际存储中。
TLDR:存储指针允许更改持久存储中的值。内存变量“只是”存储值的 non-persistent 副本。
我正在学习 gnosis/safe-contracts 的源代码。对于这个示例文件,我有一个问题,为什么这个函数需要 return 具有 storage 的数据,我知道 storage 之间的区别] 和 内存.
/*
*https://github.com/gnosis/safe-contracts/blob/2620a21c0844f23df39ea98438b82e378bb334f0/contracts/examples/guards/ReentrancyTransactionGuard.sol#L21
*/
function getGuard() internal pure returns (GuardValue storage guard) {
bytes32 slot = GUARD_STORAGE_SLOT;
// solhint-disable-next-line no-inline-assembly
assembly {
guard.slot := slot
}
}
一个更普遍的问题可能是关于当我们需要或必须使用 存储 时的 return 值?我可以参考任何文档吗?真的谢谢你!
此函数从 checkTransaction()
调用,它设置 guard
属性 active
.
GuardValue storage guard = getGuard();
require(!guard.active, "Reentrancy detected");
guard.active = true;
如果 getGuard()
返回一个内存变量(而不是存储变量),guard
变量也需要是一个内存变量 - 否则你会在尝试分配时出现不匹配将内存变量转换为存储变量,无法编译。
如果 guard
变量有一个内存位置,它只会在内存副本中更改 active
值,而不是在实际存储中。
TLDR:存储指针允许更改持久存储中的值。内存变量“只是”存储值的 non-persistent 副本。