为什么我们不能在智能合约上从 sendTransaction() 运行 获得返回值?
Why can't we get a returned value from sendTransaction() run on a smart contract?
关于此的所有讨论都提到不可能从合同函数的 sendTransaction() 运行 获得返回值,合同状态正在更改。我不明白为什么返回值不能记录在区块链的交易日志中,类似于事件,然后可以在交易确认时检索:
web3.eth.sendTransaction(...)
.on('confirmation', function(1, receipt){ ... // retrieving value returned by smart contract function here })
创建日志是为了描述从合约发出的事件——这是从交易中获取数据的当前解决方案——所以 return 数据不能进入那里。
不过,在收据中包含一个 return_data 已经被讨论过,但显然被遗忘了。 EIP758, has the following sollution:
EIP 658 originally proposed adding return data to transaction receipts. However, return data is not charged for (as it is not stored on the blockchain), so adding it to transaction receipts could result in DoS and spam opportunities. Instead, a simple Boolean status
field was added to transaction receipts. This modified version of EIP 658 was included in the Byzantium hard fork. While the status
field is useful, applications often need the return data as well.
The primary advantage of using the strategy outlined here is efficiency: no extra data needs to be stored on the blockchain, and minimal extra computational load is imposed on nodes. Since light clients have the current state, they can compute and send return data notifications without contacting a server. Although after-the-fact lookups of the return value would not be supported, this is consistent with the conventional use of return data, which are only accessible to the caller when the function returns, and are not stored for later use.
还有这个 go client pull request,它没有通过,因为最好的解决方案是以太坊硬分叉——尽管从那时起我们有一些,但它没有发生。
关于此的所有讨论都提到不可能从合同函数的 sendTransaction() 运行 获得返回值,合同状态正在更改。我不明白为什么返回值不能记录在区块链的交易日志中,类似于事件,然后可以在交易确认时检索:
web3.eth.sendTransaction(...)
.on('confirmation', function(1, receipt){ ... // retrieving value returned by smart contract function here })
创建日志是为了描述从合约发出的事件——这是从交易中获取数据的当前解决方案——所以 return 数据不能进入那里。
不过,在收据中包含一个 return_data 已经被讨论过,但显然被遗忘了。 EIP758, has the following sollution:
EIP 658 originally proposed adding return data to transaction receipts. However, return data is not charged for (as it is not stored on the blockchain), so adding it to transaction receipts could result in DoS and spam opportunities. Instead, a simple Boolean
status
field was added to transaction receipts. This modified version of EIP 658 was included in the Byzantium hard fork. While thestatus
field is useful, applications often need the return data as well.
The primary advantage of using the strategy outlined here is efficiency: no extra data needs to be stored on the blockchain, and minimal extra computational load is imposed on nodes. Since light clients have the current state, they can compute and send return data notifications without contacting a server. Although after-the-fact lookups of the return value would not be supported, this is consistent with the conventional use of return data, which are only accessible to the caller when the function returns, and are not stored for later use.
还有这个 go client pull request,它没有通过,因为最好的解决方案是以太坊硬分叉——尽管从那时起我们有一些,但它没有发生。