Eth的区块是如何打包的?

原创
2020/12/04 16:12
阅读数 908
  1. 一个区块最多打包多少笔交易是由块的gas limit决定的,注意,这里的gas limit是块的gas limit,不是交易的gas limit。可以近似理解为,块的gas limit是这个块的所有交易的实际使用的gas 之和。
  2. 一个区块中,交易的打包顺序是按照gas price的顺序打包。当然,旷工自己的交易通常都是排在最前面。
  3. 可能是旷工的挖矿软件的原因,存在很多空块。
  4. 每个交易的gas limit与该交易实际使用的gas 可能存在很大不同。

问题:

  1. 当多笔交易的gas price相同时,在区块中的顺序是怎样决定的?

参考:

https://ethgasstation.info/blog/ethereum-block-size/

import {JsonRpcProvider} from "@ethersproject/providers";
import { ethers } from "ethers"

// 获取交易区块信息及该块的交易列表
// 交易列表中的交易没有gasUsed字段, 如果需要获取gasUsed, 需要调用getTransactionReceipt接口来查询
async function getBlockTxs(provider: JsonRpcProvider, block: number | string) {
    return provider.getBlockWithTransactions(block)
}

;(async() => {
    let localProvider = new ethers.providers.JsonRpcProvider({url: ''}, 'mainnet')
    let block = 11372138

    for (let i = 0; i < 50; i ++) {
        let txs = await getBlockTxs(localProvider, block+i)

        let gasLimit = txs.gasLimit.toNumber()
        // console.log('block gas limit:', gasLimit)
        let transactions = txs.transactions
            , totalGas = 0
            , totalUsed = 0
        // console.log(transactions[0])
        for (let i = 0; i < transactions.length; i++) {
            let tx = transactions[i]
            totalGas += tx.gasLimit.toNumber()
            let rTx = await localProvider.getTransactionReceipt(tx.hash)

            totalUsed += rTx.gasUsed.toNumber()
            // console.info('block=%d index=%d gasPrice=%d gasLimit=%d usedRatio=%d totalGas=%d totalUsed=%d', block,
            //     i, tx.gasPrice.div(GWei).toNumber(), tx.gasLimit.toNumber(), rTx.gasUsed.mul(100).div(tx.gasLimit).toNumber(), totalGas, totalUsed)
        }
        console.info('block=%d blockGasLimit=%d totalGas=%d totalUsed=%d', block+i, gasLimit, totalGas, totalUsed)
    }
})()


block=11372138 blockGasLimit=12481619 totalGas=25997923 totalUsed=12473572
block=11372139 blockGasLimit=12493807 totalGas=42940607 totalUsed=12476950
block=11372140 blockGasLimit=12481608 totalGas=25078349 totalUsed=12043096
block=11372141 blockGasLimit=12493796 totalGas=27022444 totalUsed=12487147
block=11372142 blockGasLimit=12500000 totalGas=22645996 totalUsed=12493723
block=11372143 blockGasLimit=12506095 totalGas=22893218 totalUsed=12496704
block=11372144 blockGasLimit=12500000 totalGas=31048160 totalUsed=12480116
block=11372145 blockGasLimit=12487794 totalGas=22559614 totalUsed=12309872
block=11372146 blockGasLimit=12499988 totalGas=24119898 totalUsed=12492163
block=11372147 blockGasLimit=12506081 totalGas=21924202 totalUsed=12496671
block=11372148 blockGasLimit=12493870 totalGas=24220866 totalUsed=12485457
block=11372149 blockGasLimit=12481670 totalGas=30033765 totalUsed=12427012
block=11372150 blockGasLimit=12493858 totalGas=28686643 totalUsed=12483406
block=11372151 blockGasLimit=12500000 totalGas=31961412 totalUsed=12497551
block=11372152 blockGasLimit=12487794 totalGas=33613986 totalUsed=12458057
block=11372153 blockGasLimit=12499988 totalGas=24284337 totalUsed=12491038
block=11372154 blockGasLimit=12506079 totalGas=26059761 totalUsed=12506039
block=11372155 blockGasLimit=12493868 totalGas=32729299 totalUsed=12492179
block=11372156 blockGasLimit=12499967 totalGas=23658650 totalUsed=12486884
block=11372157 blockGasLimit=12487762 totalGas=21961423 totalUsed=12474256
block=11372158 blockGasLimit=12499956 totalGas=33268172 totalUsed=12483959
block=11372159 blockGasLimit=12506038 totalGas=0 totalUsed=0
block=11372160 blockGasLimit=12493827 totalGas=39459443 totalUsed=12492541
block=11372161 blockGasLimit=12500000 totalGas=31209609 totalUsed=12498393
block=11372162 blockGasLimit=12506102 totalGas=27257576 totalUsed=12503912
block=11372163 blockGasLimit=12518284 totalGas=50218838 totalUsed=12503856
block=11372164 blockGasLimit=12506061 totalGas=27976755 totalUsed=12505035
block=11372165 blockGasLimit=12493850 totalGas=54951578 totalUsed=12477118
block=11372166 blockGasLimit=12500000 totalGas=30783166 totalUsed=12484261
block=11372167 blockGasLimit=12506081 totalGas=27590667 totalUsed=12500771
block=11372168 blockGasLimit=12493870 totalGas=32379572 totalUsed=12473748
block=11372169 blockGasLimit=12481670 totalGas=27596013 totalUsed=12472659
block=11372170 blockGasLimit=12469482 totalGas=25644061 totalUsed=12449695
block=11372171 blockGasLimit=12481658 totalGas=27973875 totalUsed=12473614
block=11372172 blockGasLimit=12493846 totalGas=28867697 totalUsed=12335617
block=11372173 blockGasLimit=12500000 totalGas=24431156 totalUsed=12489721
block=11372174 blockGasLimit=12512206 totalGas=27373387 totalUsed=12492328
block=11372175 blockGasLimit=12518288 totalGas=21555614 totalUsed=12501095
block=11372176 blockGasLimit=12506065 totalGas=56709220 totalUsed=12497070
block=11372177 blockGasLimit=12512160 totalGas=24697457 totalUsed=12505347
block=11372178 blockGasLimit=12499943 totalGas=19120028 totalUsed=12479333
block=11372179 blockGasLimit=12506018 totalGas=25932080 totalUsed=12496731
block=11372180 blockGasLimit=12493807 totalGas=41795302 totalUsed=12481165
block=11372181 blockGasLimit=12481608 totalGas=25959547 totalUsed=12469426
block=11372182 blockGasLimit=12469420 totalGas=52977503 totalUsed=12464632
block=11372183 blockGasLimit=12481596 totalGas=25338396 totalUsed=12472763
block=11372184 blockGasLimit=12493784 totalGas=35134413 totalUsed=12487281
block=11372185 blockGasLimit=12500000 totalGas=36800994 totalUsed=12498440
block=11372186 blockGasLimit=12487794 totalGas=20094351 totalUsed=12475535
block=11372187 blockGasLimit=12499988 totalGas=20561891 totalUsed=12487291
展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部