以太坊 - 高 gas 成本

Ethereum - High gas costs

不知何故,我的汽油成本高得离谱?有人可以帮忙吗?

我在solidity中有一个函数:

function setMessage(uint8[] _fMessage) public returns (uint256){
    emit Message(_fMessage);
}

我的活动:

 event Message(uint8[] message);

当我用这个调用它时:EthProj.setMessage.sendTransaction([72, 193, 77], {from: my address})

其中 EthProj 是我的合约。这如何耗尽 25027 气体。当我 运行 eth.getTransactionReceipt 和我交易的 transactionHash 时,它说:gasUsed: 25027。根据这个:https://github.com/djrtwo/evm-opcode-gas-costs/blob/master/opcode-gas-costs_EIP-150_revision-1e18248_2017-04-12.csv,更具体地说,这部分 LOG0 375 + 8 * (number of bytes in log data) 它应该只花费大约 400 gas,因为事件中只有 3 个字节。这笔巨额费用从何而来?

每笔交易都有 21000 的即时内在 gas 成本。来自 Ethereum documentation:

21000 gas is charged for any transaction as a "base fee". This covers the cost of an elliptic curve operation to recover the sender address from the signature as well as the disk and bandwidth space of storing the transaction.

从那里开始,每个执行的操作码都会消耗气体。这包括读取状态、返回值、发出事件等。您所指的计算仅针对事件数据写入状态的部分。

要更全面地解释一笔交易会消耗多少 gas,请查看关于交易成本的opcode gas spreadsheet (this version is from the initial launch, but I haven't seen an updated one yet). You can also look at this write-up