以太坊的交易

交易数据结构

//go-ethereum/core/types/transaction.go
type Transaction struct {
    data txdata
    // caches
    hash atomic.Value
    size atomic.Value
    from atomic.Value
}

type txdata struct {
    AccountNonce uint64          `json:"nonce"    gencodec:"required"`
    Price        *big.Int        `json:"gasPrice" gencodec:"required"`
    GasLimit     uint64          `json:"gas"      gencodec:"required"`
    Recipient    *common.Address `json:"to"       rlp:"nil"` // nil means contract creation
    Amount       *big.Int        `json:"value"    gencodec:"required"`
    Payload      []byte          `json:"input"    gencodec:"required"`

    // Signature values
    V *big.Int `json:"v" gencodec:"required"`
    R *big.Int `json:"r" gencodec:"required"`
    S *big.Int `json:"s" gencodec:"required"`

    // This is only used when marshaling to JSON.
    Hash *common.Hash `json:"hash" rlp:"-"`
}

交易的执行

Block 类型的基本目的之一,就是为了执行交易。狭义的交易可能仅仅是一笔转帐,而广义的交易同时还会支持许多其他的意图。Ethereum 中采用的是广义交易概念。按照其架构设计,交易的执行可大致分为内外两层结构:

第一层是虚拟机外

包括执行前将Transaction类型转化成Message,创建虚拟机(EVM)对象,计算一些Gas消耗,以及执行交易完毕后创建收据(Receipt)对象并返回等;

go-ethereum/core/state_processor.go

func (p *StateProcessor) Process(block *Block, statedb *StateDB, cfg vm.Config) (types.Receipts, []*types.Log, *big.Int, error) {  
    var {  
        receipts     types.Receipts  
        totalUsedGas = big.NewInt(0)  
        header       = block.Header()  
        allLogs      []*types.Log  
        gp           = new(GasPool).AddGas(block.GasLimit())  
    }  
    ...  
    for i, tx := range block.Transactions() {  
        statedb.Prepare(tx.Hash(), block.Hash(), i)  
        receipt, _, err := ApplyTransaction(p.config, p.bc, author:nil, gp, statedb, header, tx, totalUsedGas, cfg)  
        if err != nil { return nil, nil, nil, err}  
        receipts = append(receipts, receipt)  
        allLogs = append(allLogs, receipt.Logs...)  
    }  
    p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles(), receipts)  
    return receipts, allLogs, totalUsedGas, nil  
}

第二层是虚拟机内

包括执行转帐,和创建合约并执行合约的指令数组。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 简介 不管你们知不知道以太坊(Ethereum blockchain)是什么,但是你们大概都听说过以太坊。最近在新...
    Lilymoana阅读 9,331评论 1 22
  • 1区块 所有的交易都被分组为“区块”。区块链包含一系列链接在一 起的这样的块。 在以太坊,一个区块包括: 区块头 ...
    布尼区块链阅读 7,030评论 0 2
  • 以太坊(Ethereum ):下一代智能合约和去中心化应用平台 翻译:巨蟹 、少平 译者注:中文读者可以到以太坊爱...
    车圣阅读 9,200评论 1 7
  • 安徽有个20岁的年轻人,到处找不到工作,就去东莞打工、创业,去国企上班,给人当电工,工作换了一份又一份,开摩的都被...
    零越飞阅读 8,669评论 0 0
  • 我不在飞机场等一艘船 我在这,等你 拒绝所有暧昧 只为一份真诚 你愿,我敢 此生不换
    北方先臣阅读 1,911评论 0 0