Etherum虚拟机中的Make Log指令功能如何工作 [英] How does makeLog instruction function works in Ethereum virtual machine
本文介绍了Etherum虚拟机中的Make Log指令功能如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码片段是geth中instructions.go文件的组成部分。
// make log instruction function
func makeLog(size int) executionFunc {
return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
if interpreter.readOnly {
return nil, ErrWriteProtection
}
topics := make([]common.Hash, size)
stack := scope.Stack
mStart, mSize := stack.pop(), stack.pop()
for i := 0; i < size; i++ {
addr := stack.pop()
topics[i] = addr.Bytes32()
}
d := scope.Memory.GetCopy(int64(mStart.Uint64()), int64(mSize.Uint64()))
interpreter.evm.StateDB.AddLog(&types.Log{
Address: scope.Contract.Address(),
Topics: topics,
Data: d,
// This is a non-consensus field, but assigned here because
// core/state doesn't know the current block number.
BlockNumber: interpreter.evm.Context.BlockNumber.Uint64(),
})
return nil, nil
}
}
问题是,log0、log1、log2等操作码是如何工作的,它们在以太虚拟机中有什么用途?
推荐答案
LOG<n>
操作码用于发出事件日志。
<n>
值取决于事件的索引主题和非索引主题的数量。由于<n>
值是有限的(当前为4),因此每个事件定义的最大索引主题数也有限制(当前为3,因此也可以处理同一事件的未索引主题)。
坚实度示例:
event MyEmptyEvent();
event MyEvent(bool indexed, bool indexed, bool, bool);
function foo() external {
// Produces the `LOG0` opcode as there are no topics
emit MyEmptyEvent();
// Produces the `LOG3` opcode
// as the 2 indexed topics are stored separately
// but the unindexed topics are stored as 1 topic with concatenated value
emit MyEvent(true, true, true, true);
}
在挖掘块中包含事务后,生成的事件日志与其他状态更改(例如存储值和地址余额)一起广播。
有很好的article更深入地描述细节。
这篇关于Etherum虚拟机中的Make Log指令功能如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文