本文初步分析了一个交易在以太坊内部的处理流程,涉及到交易的接收,检查,执行,同步,区块的构建以及挖矿,结合前面一篇基于黄皮书的理解总结,对以太坊有了更多的认识。因为主要的工作在c++层面,所以这里以c++版本的以太坊源码作为学习资源。
如有理解错误的地方,希望看到的人不吝赐教,共同学习,谢谢。
1. 发送交易 eth_sendTransaction
// 一个交易框架的构成,实际就是一个交易构成元素
struct TransactionSkeleton
{
bool creation = false;
Address from;
Address to;
u256 value;
bytes data;
u256 nonce = Invalid256;
u256 gas = Invalid256;
u256 gasPrice = Invalid256;
std::string userReadable(bool _toProxy, std::function(TransactionSkeleton const&)> const& _getNatSpec, std::function const& _formatAddress) const;
};
string Eth::eth_sendTransaction(Json::Value const& _json)
{
try
{
// 从Json结构中构建交易骨架
TransactionSkeleton t = toTransactionSkeleton(_json);
setTransactionDefaults(t);
......
// 向客户端提交这个交易
h256 txHash = client()->submitTransaction(t, ar.second);
// 返回交易hash
return toJS(txHash);
......
}
....
}
学习中省略了很多代码,有些是辅助性的,有些可能是功能性,这里的目的是梳理一个流程,建立一个框架印象,省略的代码可以后续继续学习
这里很简单,就是从json中构建了一个交易框架,然后传给了客户端
2. 接收交易 importTransaction
h256 Client::submitTransaction(TransactionSkeleton const& _t, Secret const& _secret)
{
// 对交易框架 缺省字段进行补充,最后行程一个交易
TransactionSkeleton ts = populateTransactionWithDefaults(_t);
ts.from = toAddress(_secret);
Transaction t(ts, _secret);
// 把交易添加到交易队列中
return importTransaction(t);
}
// 添加交易到交易队列中
h256 Client::importTransaction(Transaction const& _t)
{
// Client继承于Worker,准备线程处理交易列表
prepareForTransaction();
// Use the Executive to perform basic validation of the transaction
// (e.g. transaction signature, account balance) using the state of
// the latest block in the client's blockchain. This can throw but
// we'll catch the exception at the RPC level.
// 这里借用了Executive的initialize来做基本的交易合法性验证
// 实际上Executive就是一个交易真正执行的主体,Evm computation,后面还会看到
Block currentBlock = block(bc().currentHash());
Executive e(currentBlock, bc());
e.initialize(_t);
// 将交易加入到交易队列中
ImportResult res = m_tq.import(_t.rlp());
......
// 最后返回交易的hash值
return _t.sha3();
}
客户端收到交易后,借用 Executive 初步检验了交易构成的合法性,然后加入到自己的交易池(m_tq)中,后面就继续跟踪m_tq是被如何处理的呢?
3. 处理交易
前面说 Client是Worker的子类,启动了处理交易的线程
Client::prepareForTransaction --> void startWorking() { Worker::startWorking(); }; --> Worker::workLoop()
void Worker::workLoop()
{
while (m_state == WorkerState::Started)
{
if (m_idleWaitMs)
this_thread::sleep_for(chrono::milliseconds(m_idleWaitMs));
// 等待一段定时时间后 缺省30ms,执行工作
doWork();
}
}
void Client::doWork(bool _doWait)
{
......
// 同步交易队列,实际内部处理内容比较多
syncTransactionQueue();
tick();
// 开始挖矿 POW
rejigSealing();
.....
}
交易处理到区块处理分了两大步,一个就是同步交易,实际也执行了交易,然后是进行挖矿,计算POW,下面分开来看
3.1 交易同步及执行
void Client::syncTransactionQueue()
{
......
// 这里构建了区块 m_working, 在Block.sync中会执行交易,执行完后,这个m_working就是我们后面要验证的区块了
// 这里就涉及上上面提到过的 Executive 对象了,它是交易的真正执行者,内容也比较复杂,后面看合适位置再详细记录下
// 这里的主要过程罗列一下:
// Block::sync --> ExecutionResult Block::execute(...) --> m_state.execute --> State::executeTransaction ---> _e.initialize _e.execute _e.finalize
// --> m_transactions.push_back(_t); // 交易执行完 加入到交易列表中
// --> m_receipts.push_back(resultReceipt.second);
tie(newPendingReceipts, m_syncTransactionQueue) = m_working.sync(bc(), m_tq, *m_gp);
}
......
m_postSeal = m_working;
DEV_READ_GUARDED(x_postSeal)
// 更新 bloomer filter
for (size_t i = 0; i < newPendingReceipts.size(); i++)
appendFromNewPending(newPendingReceipts[i], changeds, m_postSeal.pending()[i].sha3());
// Tell farm about new transaction (i.e. restart mining).
onPostStateChanged();
// Tell watches about the new transactions.
noteChanged(changeds);
// Tell network about the new transactions.
// 在网络上同步交易,这里跟下去,就会到 P2P网络部分 session host 这些都可以看到了
// 这里置位host的标记位 m_newTransactions
// 到了host内部会有如下流程:
// Host::startedWorking --> h.second->onStarting()(EthereumCapability::onStarting()) --> EthereumCapability::doBackgroundWork() 这里就看到了客户端下面这个函数职位的标记位
// 然后 EthereumCapability::maintainTransactions() --> m_host->sealAndSend --> Session::sealAndSend --> Session::send --> Session::write()
if (auto h = m_host.lock())
h->noteNewTransactions();
......
}
上面再代码中注释了很多,每一步可以顺着看到很多内容。
前面总结了一个交易在以太坊中的整个流程,中间一笔带过了交易的执行主体 Executive ,今天补上这一块
交易执行时交给 State来完成的,State is Model of an Ethereum state, essentially a facade for the trie,即以太坊状态模型,本质上就是 state trie的外在表现,允许查询账户状态,以及创建账户和修改账户
仔细看下State.h 会发现这就是以太坊世界状态的操作接口,连接了DB,State,Cache,Accounts,Balance, Code,里面注释很清楚,很容易理解函数的作用,剩下的就看怎么调用起来的。
// 交易在以太坊内部执行的入口,主要是通过Executive来完成
std::pair State::execute(EnvInfo const& _envInfo, SealEngineFace const& _sealEngine, Transaction const& _t, Permanence _p, OnOpFunc const& _onOp)
{
// Create and initialize the executive. This will throw fairly cheaply and quickly if the
// transaction is bad in any way.
//创建一个 executive实例,
Executive e(*this, _envInfo, _sealEngine);
// 日志相关
ExecutionResult res;
e.setResultRecipient(res);
.....
// 区块目前已用的gas数量
u256 const startGasUsed = _envInfo.gasUsed();
// 执行交易,后续接着看这个
bool const statusCode = executeTransaction(e, _t, onOp);
// 交易执行完,清理工作,revert或者清理掉空账户
bool removeEmptyAccounts = false;
switch (_p)
{
case Permanence::Reverted:
m_cache.clear();
break;
case Permanence::Committed:
removeEmptyAccounts = _envInfo.number() >= _sealEngine.chainParams().EIP158ForkBlock;
commit(removeEmptyAccounts ? State::CommitBehaviour::RemoveEmptyAccounts : State::CommitBehaviour::KeepEmptyAccounts);
break;
case Permanence::Uncommitted:
break;
}
// 更新日志
TransactionReceipt const receipt = _envInfo.number() >= _sealEngine.chainParams().byzantiumForkBlock ?
TransactionReceipt(statusCode, startGasUsed + e.gasUsed(), e.logs()) :
TransactionReceipt(rootHash(), startGasUsed + e.gasUsed(), e.logs());
return make_pair(res, receipt);
}
流程比较清楚,1.创建交易执行体 executive,2. 执行交易, 3. 收尾工作 4. 日志更新
执行交易
/// @returns true when normally halted; false when exceptionally halted; throws when internal VM exception occurred.
bool State::executeTransaction(Executive& _e, Transaction const& _t, OnOpFunc const& _onOp)
{
// save point, 记录一下当前changelog的位置,为了以后出现异常后的回滚
// changelog 是一个vector,记录所有账户的状态改变,他的size位置即记录点
// changelog 可以通过 savepoint() rollback() commit() 来操作
size_t const savept = savepoint();
try
{
// 初始化交易 主要是校验一个交易的合法性
_e.initialize(_t);
// 执行交易, 返回true,执行finalize,返回false则执行go,如果执行有异常,执行evm的rollback
if (!_e.execute())
_e.go(_onOp);
return _e.finalize();
}
catch (Exception const&)
{
rollback(savept);
throw;
}
}
这里的流程也比较清楚,记录回滚标记点,校验交易,执行交易,检查交易结果:成功 失败 异常,依次看下执行过程
校验交易
void Executive::initialize(Transaction const& _transaction)
{
// 记录交易
m_t = _transaction;
// 根据区块号,选择使用哪个evm调度策略 EVMSchedule 设定好了调度相关的数据,例如某个步骤的gas是多少都是在这里面定义好的,根据不同的区块号阶段,可能有些不同,这里就是取到正确的EVM配置
m_baseGasRequired = m_t.baseGasRequired(m_sealEngine.evmSchedule(m_envInfo.number()));
try
{
// 校验一部分交易参数的合法性
m_sealEngine.verifyTransaction(ImportRequirements::Everything, m_t, m_envInfo.header(), m_envInfo.gasUsed());
}
catch (Exception const& ex)
{
m_excepted = toTransactionException(ex);
throw;
}
if (!m_t.hasZeroSignature())
{
// Avoid invalid transactions.
u256 nonceReq;
try
{
nonceReq = m_s.getNonce(m_t.sender());
}
catch (InvalidSignature const&)
{
m_excepted = TransactionException::InvalidSignature;
throw;
}
// 交易的nonce是否跟发起交易的账户的nonce相同
if (m_t.nonce() != nonceReq)
{
m_excepted = TransactionException::InvalidNonce;
BOOST_THROW_EXCEPTION(InvalidNonce() << RequirementError((bigint)nonceReq, (bigint)m_t.nonce()));
}
// 交易需要的gas是否超过了账户本身的balance
// Avoid unaffordable transactions.
bigint gasCost = (bigint)m_t.gas() * m_t.gasPrice();
bigint totalCost = m_t.value() + gasCost;
if (m_s.balance(m_t.sender()) < totalCost)
{
m_excepted = TransactionException::NotEnoughCash;
m_excepted = TransactionException::NotEnoughCash;
BOOST_THROW_EXCEPTION(NotEnoughCash() << RequirementError(totalCost, (bigint)m_s.balance(m_t.sender())) << errinfo_comment(m_t.sender().hex()));
}
m_gasCost = (u256)gasCost; // Convert back to 256-bit, safe now.
}
}
这里首先选择了EVM配置,然后校验交易参数,入参是ImportRequirements::Everything
//
void SealEngineFace::verifyTransaction(ImportRequirements::value _ir, TransactionBase const& _t,
BlockHeader const& _header, u256 const& _gasUsed) const
{
if ((_ir & ImportRequirements::TransactionSignatures) && _header.number() < chainParams().EIP158ForkBlock && _t.isReplayProtected())
BOOST_THROW_EXCEPTION(InvalidSignature());
if ((_ir & ImportRequirements::TransactionSignatures) &&
_header.number() < chainParams().experimentalForkBlock && _t.hasZeroSignature())
BOOST_THROW_EXCEPTION(InvalidSignature());
if ((_ir & ImportRequirements::TransactionBasic) &&
_header.number() >= chainParams().experimentalForkBlock && _t.hasZeroSignature() &&
(_t.value() != 0 || _t.gasPrice() != 0 || _t.nonce() != 0))
BOOST_THROW_EXCEPTION(InvalidZeroSignatureTransaction() << errinfo_got((bigint)_t.gasPrice()) << errinfo_got((bigint)_t.value()) << errinfo_got((bigint)_t.nonce()));
if (_header.number() >= chainParams().homesteadForkBlock && (_ir & ImportRequirements::TransactionSignatures) && _t.hasSignature())
_t.checkLowS();
eth::EVMSchedule const& schedule = evmSchedule(_header.number());
// Pre calculate the gas needed for execution
if ((_ir & ImportRequirements::TransactionBasic) && _t.baseGasRequired(schedule) > _t.gas())
BOOST_THROW_EXCEPTION(OutOfGasIntrinsic() << RequirementError(
(bigint)(_t.baseGasRequired(schedule)), (bigint)_t.gas()));
// Avoid transactions that would take us beyond the block gas limit.
if (_gasUsed + (bigint)_t.gas() > _header.gasLimit())
BOOST_THROW_EXCEPTION(BlockGasLimitReached() << RequirementErrorComment(
(bigint)(_header.gasLimit() - _gasUsed), (bigint)_t.gas(),
string("_gasUsed + (bigint)_t.gas() > _header.gasLimit()")));
}
执行交易
bool Executive::execute()
{
// Entry point for a user-executed transaction.
// 根据注释可以明白 这个就是用户触发的交易的真正入口了
// Pay...
// 这里就是两种交易类型了
// 扣掉交易消耗
m_s.subBalance(m_t.sender(), m_gasCost);
assert(m_t.gas() >= (u256)m_baseGasRequired);
if (m_t.isCreation())
// 1. 合约创建
return create(m_t.sender(), m_t.value(), m_t.gasPrice(), m_t.gas() - (u256)m_baseGasRequired, &m_t.data(), m_t.sender());
else
// 2. 消息调用
return call(m_t.receiveAddress(), m_t.sender(), m_t.value(), m_t.gasPrice(), bytesConstRef(&m_t.data()), m_t.gas() - (u256)m_baseGasRequired);
}
这里分了两步,也就是ethereum的两种消息类型:合约创建和消息调用,这里的判断条件isCreate() 是在上面文章提到的jsonrpc最初入口的时候就确定了的。
TransactionSkeleton toTransactionSkeleton(Json::Value const& _json)
{
......
// to 为空即为合约创建,否则就是消息调用
if (!_json["to"].empty() && _json["to"].asString() != "0x" && !_json["to"].asString().empty())
ret.to = jsToAddress(_json["to"].asString());
else
ret.creation = true;
......
}
h256 Client::submitTransaction(TransactionSkeleton const& _t, Secret const& _secret)
{
......
// 构造交易
Transaction t(ts, _secret);
......
}
Transaction(TransactionSkeleton const& _ts, Secret const& _s = Secret()): TransactionBase(_ts, _s) {}
TransactionBase::TransactionBase(TransactionSkeleton const& _ts, Secret const& _s):
// 这里记录到交易参数中,是否为合约创建
m_type(_ts.creation ? ContractCreation : MessageCall),
m_nonce(_ts.nonce),
m_value(_ts.value),
m_receiveAddress(_ts.to),
m_gasPrice(_ts.gasPrice),
m_gas(_ts.gas),
m_data(_ts.data),
m_sender(_ts.from)
{
if (_s)
sign(_s);
}
合约创建
bool Executive::create(Address const& _txSender, u256 const& _endowment, u256 const& _gasPrice, u256 const& _gas, bytesConstRef _init, Address const& _origin)
{
// 如果消息调用执行中也有创建合约,会执行CREATE操作符,也是这个之行流程
// Contract creation by an external account is the same as CREATE opcode
return createOpcode(_txSender, _endowment, _gasPrice, _gas, _init, _origin);
}
bool Executive::createOpcode(Address const& _sender, u256 const& _endowment, u256 const& _gasPrice, u256 const& _gas, bytesConstRef _init, Address const& _origin)
{
// 新合约账户的地址的生成
u256 nonce = m_s.getNonce(_sender);
m_newAddress = right160(sha3(rlpList(_sender, nonce)));
return executeCreate(_sender, _endowment, _gasPrice, _gas, _init, _origin);
}
bool Executive::executeCreate(Address const& _sender, u256 const& _endowment, u256 const& _gasPrice, u256 const& _gas, bytesConstRef _init, Address const& _origin)
{
if (_sender != MaxAddress ||
m_envInfo.number() < m_sealEngine.chainParams().experimentalForkBlock) // EIP86
m_s.incNonce(_sender);
// 重新记录回滚点,可以得出nonce的增加是不可改变的
m_savepoint = m_s.savepoint();
m_isCreation = true;
// We can allow for the reverted state (i.e. that with which m_ext is constructed) to contain the m_orig.address, since
// we delete it explicitly if we decide we need to revert.
m_gas = _gas;
bool accountAlreadyExist = (m_s.addressHasCode(m_newAddress) || m_s.getNonce(m_newAddress) > 0);
if (accountAlreadyExist)
{
LOG(m_detailsLogger) << "Address already used: " << m_newAddress;
m_gas = 0;
m_excepted = TransactionException::AddressAlreadyUsed;
revert();
m_ext = {}; // cancel the _init execution if there are any scheduled.
return !m_ext;
}
// Transfer ether before deploying the code. This will also create new
// account if it does not exist yet.
// 给新账户转账,这个内部执行中,判断是不存在的地址,会创建账户,再最后state commit的时候,写入到DB中
m_s.transferBalance(_sender, m_newAddress, _endowment);
u256 newNonce = m_s.requireAccountStartNonce();
if (m_envInfo.number() >= m_sealEngine.chainParams().EIP158ForkBlock)
newNonce += 1;
// 增加nonce
m_s.setNonce(m_newAddress, newNonce);
// 清理存储
m_s.clearStorage(m_newAddress);
// Schedule _init execution if not empty.
// tx.data不为空,即有代码,这里初始化EVM实例
if (!_init.empty())
m_ext = make_shared(m_s, m_envInfo, m_sealEngine, m_newAddress, _sender, _origin,
_endowment, _gasPrice, bytesConstRef(), _init, sha3(_init), m_depth, true, false);
return !m_ext;
}
这里返回false,表示后面要执行go(),
bool Executive::go(OnOpFunc const& _onOp)
{
if (m_ext)
{
try
{
// 创建VM的实例,VM类型有三种,缺省VMKind::DLL 还有可配置的VMKind::Interpreter VMKind::Legacy
// Create VM instance. Force Interpreter if tracing requested.
auto vm = VMFactory::create();
if (m_isCreation)
{
进入vm,执行合约创建
auto out = vm->exec(m_gas, *m_ext, _onOp);
if (m_res)
{
m_res->gasForDeposit = m_gas;
m_res->depositSize = out.size();
}
if (m_res)
m_res->output = out.toVector(); // copy output to execution result
m_s.setCode(m_ext->myAddress, out.toVector());
}
else
// 非合约创建,即消息调用,执行
m_output = vm->exec(m_gas, *m_ext, _onOp);
}
}
return true;
}
// 最终进入这里
/// Handy wrapper for evmc_execute().
EVM::Result EVM::execute(ExtVMFace& _ext, int64_t gas)
{
auto mode = toRevision(_ext.evmSchedule());
evmc_call_kind kind = _ext.isCreate ? EVMC_CREATE : EVMC_CALL;
uint32_t flags = _ext.staticCall ? EVMC_STATIC : 0;
assert(flags != EVMC_STATIC || kind == EVMC_CALL); // STATIC implies a CALL.
evmc_message msg = {kind, flags, static_cast(_ext.depth), gas, toEvmC(_ext.myAddress),
toEvmC(_ext.caller), _ext.data.data(), _ext.data.size(), toEvmC(_ext.value),
toEvmC(0x0_cppui256)};
return EVM::Result{
evmc_execute(m_instance, &_ext, mode, &msg, _ext.code.data(), _ext.code.size())};
}
从上面的流程可以验证前面的一遍关于以太坊黄皮书理解的总结的,最红的交易会转成message(evmc_message)然后进入EVM computation,开始处理了。
消息调用
bool Executive::call(Address const& _receiveAddress, Address const& _senderAddress, u256 const& _value, u256 const& _gasPrice, bytesConstRef _data, u256 const& _gas)
{
// 准备下参数,receiveAddress也是codeAddress,后面会根据这个地址取得code
CallParameters params{_senderAddress, _receiveAddress, _receiveAddress, _value, _value, _gas, _data, {}};
return call(params, _gasPrice, _senderAddress);
}
bool Executive::call(CallParameters const& _p, u256 const& _gasPrice, Address const& _origin)
{
......
m_gas = _p.gas;
if (m_s.addressHasCode(_p.codeAddress))
{
//取得code
bytes const& c = m_s.code(_p.codeAddress);
//取得codehash
h256 codeHash = m_s.codeHash(_p.codeAddress);
m_ext = make_shared(m_s, m_envInfo, m_sealEngine, _p.receiveAddress,
_p.senderAddress, _origin, _p.apparentValue, _gasPrice, _p.data, &c, codeHash,
m_depth, false, _p.staticCall);
}
......
// Transfer ether.
m_s.transferBalance(_p.senderAddress, _p.receiveAddress, _p.valueTransfer);
return !m_ext;
}
代码也很好理解,后面就回到了上面的go()函数里面的else,还是转成message,进入EVM computation开始计算
小结
以太坊两种消息类型,分别进行处理,就像前面对黄皮书的总结,根据所处的环境上下文(区块好,区块gaslimit等等),原始的交易(from to code data等)构建message,然后由EVM进行执行
3.2 区块POW
这里就开始涉及到ethereum的POW过程了,再次之前要增加些其他的流程代码
3.2.1
POW的参与者分为 Farm 和 Miner,即农场和矿工,对应不同的接口
// A miner - a member and adoptee of the Farm.
template class GenericMiner
// Class for hosting one or more Miners.
template class GenericFarmFace
//只有一个工作,就是提交工作量证明
// _p 即找到的解决方案
struct Solution
{
Nonce nonce;
h256 mixHash;
};
// _finder即方案的发现者
virtual bool submitProof(Solution const& _p, Miner* _finder) = 0;
// A collective of Miners. Miners ask for work, then submit proofs
template class GenericFarm: public GenericFarmFace
3.2.2
POW的执行算法 Ethash,在 aleth\main.cpp中对Ethash进行了初始化
int main(int argc, char** argv)
{
......
Ethash::init();
......
}
void Ethash::init()
{
ETH_REGISTER_SEAL_ENGINE(Ethash);
}
#define ETH_REGISTER_SEAL_ENGINE(Name) static SealEngineFactory __eth_registerSealEngineFactory ## Name = SealEngineRegistrar::registerSealEngine(#Name)
template static SealEngineFactory registerSealEngine(std::string const& _name) { return (get()->m_sealEngines[_name] = [](){return new SealEngine;}); }
private:
//单例模式
static SealEngineRegistrar* get() { if (!s_this) s_this = new SealEngineRegistrar; return s_this; }
最终是new了 Ethash 对象,即一个 SealEngine,最终放入到 SealEngineRegistrar 的 std::unordered_map
讲过上面步骤的一步一步执行,最终Ethash实例保存到了 SealEngineRegistrar的m_sealEngines中,名字就叫 Ethash
这里也要看下Ethash的构造过程
Ethash::Ethash()
{
// 这里创建一个叫做cpu的矿工,又叫做sealer
map::SealerDescriptor> sealers;
sealers["cpu"] = GenericFarm::SealerDescriptor{&EthashCPUMiner::instances, [](GenericMiner::ConstructionInfo ci){ return new EthashCPUMiner(ci); }};
// 把矿工加入到农场中
m_farm.setSealers(sealers);
// 为农场设置onSolutionFound 方法,传入Solution,进行检验
m_farm.onSolutionFound([=](EthashProofOfWork::Solution const& sol)
{
std::unique_lock l(m_submitLock);
// cdebug << m_farm.work().seedHash << m_farm.work().headerHash << sol.nonce << EthashAux::eval(m_farm.work().seedHash, m_farm.work().headerHash, sol.nonce).value;
setMixHash(m_sealing, sol.mixHash);
setNonce(m_sealing, sol.nonce);
// 对POW的检验
if (!quickVerifySeal(m_sealing))
return false;
if (m_onSealGenerated)
{
RLPStream ret;
m_sealing.streamRLP(ret);
l.unlock();
m_onSealGenerated(ret.out());
}
return true;
});
}
综上,Ethash里面有农场(GenericFarm
怎么取用Ethash实例呢,通过名字
static SealEngineFace* create(std::string const& _name) {
if (!get()->m_sealEngines.count(_name))
return nullptr;
return get()->m_sealEngines[_name]();
}
BlockChain又是怎么来取用的呢?
1. 在struct ChainOperationParams 有三类,默认是 NoProof
/// The chain sealer name: e.g. Ethash, NoProof, BasicAuthority (POA)
std::string sealEngineName = "NoProof";
2. 在加载配置时,
ChainParams ChainParams::loadConfig 会确定用哪一种,例如名称为 Ethash (pow)
3. 区块链初始化时,会初始化自己的 m_sealEngine
void BlockChain::init(ChainParams const& _p)
m_sealEngine.reset(m_params.createSealEngine());
SealEngineFace* ChainParams::createSealEngine()
{
SealEngineFace* ret = SealEngineRegistrar::create(sealEngineName);
....
}
4. 至此,BlockChain 有了自己的检验引擎,即 m_sealEngine,又即 Ethash 实例
客户端又怎么获取到BlockChain的Ethash?
SealEngineFace* sealEngine() const override { return bc().sealEngine(); } 这样就获得了上面的检验引擎
3.2.3
上面分析了POW检验者的建立过程,再接上最上面提到的最后一个步骤,开始挖矿
void Client::rejigSealing()
{
if ((wouldSeal() || remoteActive()) && !isMajorSyncing())
{
// 这里实际就获得了Ethash的实例
if (sealEngine()->shouldSeal(this))
{
......
//这里的m_working就是上面执行完交易后的区块,下面的函数是封装区块内容
// 由注释就可以知道,最后区块剩下没有完成的部分就是计算nonce mixhash了
//Sealing
/// Prepares the current state for mining.
/// Commits all transactions into the trie, compiles uncles and transactions list, applies all
/// rewards and populates the current block header with the appropriate hashes.
/// The only thing left to do after this is to actually mine().
///
m_working.commitToSeal(bc(), m_extraData);
......
if (wouldSeal())
{
sealEngine()->onSealGenerated([=](bytes const& _header) {
if (this->submitSealed(_header))
m_onBlockSealed(_header);
else
LOG(m_logger) << "Submitting block failed...";
});
// 开始挖矿
sealEngine()->generateSeal(m_sealingInfo);
}
}
......
}
......
}
void Ethash::generateSeal(BlockHeader const& _bi)
{
Guard l(m_submitLock);
m_sealing = _bi;
m_farm.setWork(m_sealing);
m_farm.start(m_sealer);
m_farm.setWork(m_sealing);
}
上面的流程注释了一些,构建了区块,剩下nonce mixhash,再最后调用Ethash开始挖矿计算,最终到了
void EthashCPUMiner::kickOff()
void EthashCPUMiner::startWorking()
{
if (!m_thread)
{
m_shouldStop = false;
m_thread.reset(new thread(&EthashCPUMiner::minerBody, this));
}
}
void EthashCPUMiner::minerBody()
{
setThreadName("miner" + toString(index()));
auto tid = std::this_thread::get_id();
static std::mt19937_64 s_eng((utcTime() + std::hash()(tid)));
// 先计算一个nonce的随机起始值
uint64_t tryNonce = s_eng();
// FIXME: Use epoch number, not seed hash in the work package.
WorkPackage w = work();
// 根据seedHash找到现在的纪元 DAG
int epoch = ethash::find_epoch_number(toEthash(w.seedHash));
auto& ethashContext = ethash::get_global_epoch_context_full(epoch);
// 获取现在的难度值
h256 boundary = w.boundary;
// 开始穷举nonce,再DAG范围内寻找答案了,即POW的计算过程
for (unsigned hashCount = 1; !m_shouldStop; tryNonce++, hashCount++)
{
auto result = ethash::hash(ethashContext, toEthash(w.headerHash()), tryNonce);
h256 value = h256(result.final_hash.bytes, h256::ConstructFromPointer);
if (value <= boundary && submitProof(EthashProofOfWork::Solution{(h64)(u64)tryNonce,
h256(result.mix_hash.bytes, h256::ConstructFromPointer)}))
break;
if (!(hashCount % 100))
accumulateHashes(100);
}
}
这里到了ethereum的核心 精华部分,POW的计算过程,根据协议的实现会有个难度值的计算,我在前面总结过了,这里就是穷举nonce,找到value使得它小于boundary,找到后,就提交工作量证明 submitProof
onSolutionFound ---> Ethash::quickVerifySeal
暂时代码梳理到这里,基本上算是一个交易的流程,后面深入学习下 Executive 以及 信息的同步过程等
————————————————
原文链接:https://blog.csdn.net/laorenmen/article/details/85478282