ETH源码阅读(GasLimit和GasPrice对交易打包的影响)

提交一笔交易后,先会被放入txPool,然后被打包。那么gasLimit和gasPrice的大小决定了是否能被放入txPool。

1.gasLimit

如果tx携带的data太大,会因为gasLimit太小而无法放入txPool
go-ethereum/core/tx_pool.go
validateTx:计算因为tx.Data()所需要的gasLimit,如果tx提供的gasLimit小于需要的gasLimit,会返回ErrIntrinsicGas这个错误。

//Intrinsic: 固有的
//8.data的大小会影响gasLimit的值
intrGas, err := IntrinsicGas(tx.Data(), tx.To() == nil, pool.homestead)
if err != nil {
    return err
}
if tx.Gas() < intrGas {//gasLimit太小(所以这里gasPrice不影响)
    return ErrIntrinsicGas
}

2. gasPrice

在txPool的交易数目达到了上限时,会将gasPrice低的交易从txPool移除。
go-ethereum/core/tx_pool.go

//GlobalSlots:4096
//GlobalQueue:1024
// If the transaction pool is full, discard underpriced transactions
if uint64(pool.all.Count()) >= pool.config.GlobalSlots+pool.config.GlobalQueue { //pool的交易数达到或者超过上限
        //gasPrice决定交易是否能被放入pool
    // If the new transaction is underpriced, don't accept it
    if !local && pool.priced.Underpriced(tx, pool.locals) { //判断tx的gasPrice是否比pool里的最低gasPrice还低
      log.Trace("Discarding underpriced transaction", "hash", hash, "price", tx.GasPrice())
            underpricedTxCounter.Inc(1)
            return false, ErrUnderpriced //是,就不能被添加到pool
    }

    //如果tx能被放入pool,那么需要把其他的交易给移除
    //1.处理pool.priced
    // New transaction is better than our worse ones, make room for it
    //为啥不是pool.config.GlobalSlots+pool.config.GlobalQueue+2
    drop := pool.priced.Discard(pool.all.Count()-int(pool.config.GlobalSlots+pool.config.GlobalQueue-1), pool.locals)

    //2.pool其他的数据结构也需要同步更新
    for _, tx := range drop {
            log.Trace("Discarding freshly underpriced transaction", "hash", tx.Hash(), "price", tx.GasPrice())
        underpricedTxCounter.Inc(1)
        pool.removeTx(tx.Hash(), false)
    }
}

3.总结

gasLimit:可以根据tx的data计算出需要的gasLimit
gasPrice:跟其他tx竞争,如果设定太小会导致竞争失败,如果太大,又很浪费。

你可能感兴趣的:(ETH源码阅读(GasLimit和GasPrice对交易打包的影响))