Skip to content

Commit 6b5ea99

Browse files
authored
Merge pull request #16 from jialinpeng/patch-2
当前 go-ethereum master 版本有部分修改
2 parents 2aa8fa2 + 4021359 commit 6b5ea99

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

miner挖矿部分源码分析CPU挖矿.md

+18-3
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,11 @@ worker
444444

445445
coinbase common.Address // 挖矿者的地址
446446
extra []byte //
447-
447+
448+
snapshotMu sync.RWMutex // 快照 RWMutex(快照读写锁)
449+
snapshotBlock *types.Block // 快照 Block
450+
snapshotState *state.StateDB // 快照 StateDB
451+
448452
currentMu sync.Mutex
449453
current *Work
450454

@@ -708,12 +712,23 @@ makeCurrent,未当前的周期创建一个新的环境。
708712
commitTransactions
709713

710714
func (env *Work) commitTransactions(mux *event.TypeMux, txs *types.TransactionsByPriceAndNonce, bc *core.BlockChain, coinbase common.Address) {
711-
gp := new(core.GasPool).AddGas(env.header.GasLimit)
715+
// 由于是打包新的区块中交易,所以将总 gasPool 初始化为 env.header.GasLimit
716+
if env.gasPool == nil {
717+
env.gasPool = new(core.GasPool).AddGas(env.header.GasLimit)
718+
}
712719

713720
var coalescedLogs []*types.Log
714721

715722
for {
723+
// If we don't have enough gas for any further transactions then we're done
724+
// 如果当前区块中所有 Gas 消耗已经使用完,则退出打包交易
725+
if env.gasPool.Gas() < params.TxGas {
726+
log.Trace("Not enough gas for further transactions", "have", env.gasPool, "want", params.TxGas)
727+
break
728+
}
729+
716730
// Retrieve the next transaction and abort if all done
731+
// 检索下一笔交易,如果交易集合为空则退出 commit
717732
tx := txs.Peek()
718733
if tx == nil {
719734
break
@@ -966,4 +981,4 @@ Start
966981
log.Info("Starting mining operation")
967982
self.worker.start() // 启动worker 开始挖矿
968983
self.worker.commitNewWork() //提交新的挖矿任务。
969-
}
984+
}

0 commit comments

Comments
 (0)