void WhisperPeer::sendMessages() { RLPStream amalg; unsigned n = 0; Guard l(x_unseen); while (m_unseen.size()) { auto p = *m_unseen.begin(); m_unseen.erase(m_unseen.begin()); host()->streamMessage(p.second, amalg); n++; } if (n) { RLPStream s; prep(s); s.appendList(n + 1) << MessagesPacket; s.appendRaw(amalg.out(), n); sealAndSend(s); } else // just pause if no messages to send this_thread::sleep_for(chrono::milliseconds(100)); }
MineInfo State::mine(uint _msTimeout) { // Update difficulty according to timestamp. m_currentBlock.difficulty = m_currentBlock.calculateDifficulty(m_previousBlock); // TODO: Miner class that keeps dagger between mine calls (or just non-polling mining). MineInfo ret = m_dagger.mine(/*out*/m_currentBlock.nonce, m_currentBlock.headerHashWithoutNonce(), m_currentBlock.difficulty, _msTimeout); if (ret.completed) { // Got it! // Commit to disk. m_db.commit(); // Compile block: RLPStream ret; ret.appendList(3); m_currentBlock.fillStream(ret, true); ret.appendRaw(m_currentTxs); ret.appendRaw(m_currentUncles); ret.swapOut(m_currentBytes); m_currentBlock.hash = sha3(m_currentBytes); cnote << "Mined " << m_currentBlock.hash << "(parent: " << m_currentBlock.parentHash << ")"; } else m_currentBytes.clear(); return ret; }
void TransactionReceipt::streamRLP(RLPStream& _s) const { _s.appendList(4) << m_stateRoot << m_gasUsed << m_bloom; _s.appendList(m_log.size()); for (LogEntry const& l: m_log) l.streamRLP(_s); }
void Manifest::streamOut(RLPStream& _s) const { _s.appendList(7) << from << to << value << altered << input << output; _s.appendList(internal.size()); for (auto const& i: internal) i.streamOut(_s); }
h256 RLPXDatagramFace::sign(Secret const& _k) { assert(packetType()); RLPStream rlpxstream; // rlpxstream.appendRaw(toPublic(_k).asBytes()); // for mdc-based signature rlpxstream.appendRaw(bytes(1, packetType())); // prefix by 1 byte for type streamRLP(rlpxstream); bytes rlpxBytes(rlpxstream.out()); bytesConstRef rlpx(&rlpxBytes); h256 sighash(dev::sha3(rlpx)); // H(type||data) Signature sig = dev::sign(_k, sighash); // S(H(type||data)) data.resize(h256::size + Signature::size + rlpx.size()); bytesRef rlpxHash(&data[0], h256::size); bytesRef rlpxSig(&data[h256::size], Signature::size); bytesRef rlpxPayload(&data[h256::size + Signature::size], rlpx.size()); sig.ref().copyTo(rlpxSig); rlpx.copyTo(rlpxPayload); bytesConstRef signedRLPx(&data[h256::size], data.size() - h256::size); h256 hash(dev::sha3(signedRLPx)); hash.ref().copyTo(rlpxHash); return hash; }
void generateSeal(BlockInfo const& _bi) { BasicAuthority::BlockHeader h(_bi); h.m_sig = sign(m_secret, _bi.hashWithout()); RLPStream ret; h.streamRLP(ret); m_onSealGenerated(ret.out()); }
void MemTrieNode::putRLP(RLPStream& _parentStream) const { RLPStream s; makeRLP(s); if (s.out().size() < 32) _parentStream.APPEND_CHILD(s.out()); else _parentStream << dev::sha3(s.out()); }
void NoProof::generateSeal(BlockHeader const& _bi) { BlockHeader header(_bi); header.setSeal(NonceField, h64{0}); header.setSeal(MixHashField, h256{0}); RLPStream ret; header.streamRLP(ret); if (m_onSealGenerated) m_onSealGenerated(ret.out()); }
// @returns the block that represents the difference between m_previousBlock and m_currentBlock. // (i.e. all the transactions we executed). void State::commitToMine(BlockChain const& _bc) { if (m_currentBlock.sha3Transactions != h256() || m_currentBlock.sha3Uncles != h256()) { Addresses uncleAddresses; for (auto i: RLP(m_currentUncles)) uncleAddresses.push_back(i[2].toHash<Address>()); unapplyRewards(uncleAddresses); } cnote << "Commiting to mine on" << m_previousBlock.hash; RLPStream uncles; Addresses uncleAddresses; if (m_previousBlock != BlockInfo::genesis()) { // Find uncles if we're not a direct child of the genesis. // cout << "Checking " << m_previousBlock.hash << ", parent=" << m_previousBlock.parentHash << endl; auto us = _bc.details(m_previousBlock.parentHash).children; assert(us.size() >= 1); // must be at least 1 child of our grandparent - it's our own parent! uncles.appendList(us.size() - 1); // one fewer - uncles precludes our parent from the list of grandparent's children. for (auto const& u: us) if (u != m_previousBlock.hash) // ignore our own parent - it's not an uncle. { BlockInfo ubi(_bc.block(u)); ubi.fillStream(uncles, true); uncleAddresses.push_back(ubi.coinbaseAddress); } } else uncles.appendList(0); applyRewards(uncleAddresses); RLPStream txs(m_transactions.size()); for (auto const& i: m_transactions) i.fillStream(txs); txs.swapOut(m_currentTxs); uncles.swapOut(m_currentUncles); m_currentBlock.sha3Transactions = sha3(m_currentTxs); m_currentBlock.sha3Uncles = sha3(m_currentUncles); // Commit any and all changes to the trie that are in the cache, then update the state root accordingly. commit(); cnote << "stateRoot:" << m_state.root(); // cnote << m_state; // cnote << *this; m_currentBlock.stateRoot = m_state.root(); m_currentBlock.parentHash = m_previousBlock.hash; }
void BlockHeader::streamRLP(RLPStream &_s, IncludeSeal _i) const { if (_i != OnlySeal) { _s.appendList(BlockHeader::BasicFields + (_i == WithoutSeal ? 0 : m_seal.size())); BlockHeader::streamRLPFields(_s); } if (_i != WithoutSeal) for (unsigned i = 0; i < m_seal.size(); ++i) _s.appendRaw(m_seal[i]); }
h256 BlockHeader::hash(IncludeSeal _i) const { h256 dummy; h256 &memo = _i == WithSeal ? m_hash : _i == WithoutSeal ? m_hashWithout : dummy; if (!memo) { RLPStream s; streamRLP(s, _i); memo = sha3(s.out()); } return memo; }
h256 hash256(StringMap const& _s) { // build patricia tree. if (_s.empty()) return h256(); HexMap hexMap; for (auto i = _s.rbegin(); i != _s.rend(); ++i) hexMap[toHex(i->first)] = i->second; RLPStream s; hash256rlp(hexMap, hexMap.cbegin(), hexMap.cend(), 0, s); return sha3(s.out()); }
bytes rlp256(StringMap const& _s) { // build patricia tree. if (_s.empty()) return bytes(); HexMap hexMap; for (auto i = _s.rbegin(); i != _s.rend(); ++i) hexMap[asNibbles(i->first)] = i->second; RLPStream s; hash256aux(hexMap, hexMap.cbegin(), hexMap.cend(), 0, s); return s.out(); }
h256 hash256(u256Map const& _s) { // build patricia tree. if (_s.empty()) return h256(); HexMap hexMap; for (auto i = _s.rbegin(); i != _s.rend(); ++i) hexMap[asNibbles(toBigEndianString(i->first))] = asString(rlp(i->second)); RLPStream s; hash256rlp(hexMap, hexMap.cbegin(), hexMap.cend(), 0, s); return sha3(s.out()); }
h256 TransactionBase::sha3(IncludeSignature _sig) const { if (_sig == WithSignature && m_hashWith) return m_hashWith; RLPStream s; streamRLP(s, _sig, m_chainId > 0 && _sig == WithoutSignature); auto ret = dev::sha3(s.out()); if (_sig == WithSignature) m_hashWith = ret; return ret; }
void MixClient::mine() { WriteGuard l(x_state); m_postMine.commitToSeal(bc()); NoProof::BlockHeader h(m_postMine.info()); RLPStream header; h.streamRLP(header); m_postMine.sealBlock(header.out()); bc().import(m_postMine.blockData(), m_stateDB, ImportRequirements::Everything & ~ImportRequirements::ValidSeal); m_postMine.sync(bc()); m_preMine = m_postMine; }
bytes PeerServer::savePeers() const { RLPStream ret; int n = 0; for (auto& i: m_peers) if (auto p = i.second.lock()) if (p->m_socket.is_open() && p->endpoint().port()) { ret.appendList(3) << p->endpoint().address().to_v4().to_bytes() << p->endpoint().port() << p->m_id; n++; } return RLPStream(n).appendRaw(ret.out(), n).out(); }
bytes Raft::authBytes() { bytes ret; RLPStream authListStream; authListStream.appendList(m_idVoted.size()*2); for(auto it : m_idVoted){ authListStream << it.first; authListStream << it.second; } authListStream.swapOut(ret); return ret;
void EthashSealEngine::onSealGenerated(std::function<void(bytes const&)> const& _f) { m_farm.onSolutionFound([=](EthashProofOfWork::Solution const& sol) { // cdebug << m_farm.work().seedHash << m_farm.work().headerHash << sol.nonce << EthashAux::eval(m_farm.work().seedHash, m_farm.work().headerHash, sol.nonce).value; m_sealing.m_mixHash = sol.mixHash; m_sealing.m_nonce = sol.nonce; if (!m_sealing.preVerify()) return false; RLPStream ret; m_sealing.streamRLP(ret); _f(ret.out()); return true; }); }
void TransactionBase::streamRLP(RLPStream& _s, IncludeSignature _sig, bool _forEip155hash) const { if (m_type == NullTransaction) return; _s.appendList((_sig || _forEip155hash ? 3 : 0) + 6); _s << m_nonce << m_gasPrice << m_gas; if (m_type == MessageCall) _s << m_receiveAddress; else _s << ""; _s << m_value << m_data; if (_sig) { if (!m_vrs) BOOST_THROW_EXCEPTION(TransactionIsUnsigned()); if (hasZeroSignature()) _s << m_chainId; else { int const vOffset = m_chainId * 2 + 35; _s << (m_vrs->v + vOffset); } _s << (u256)m_vrs->r << (u256)m_vrs->s; } else if (_forEip155hash) _s << m_chainId << 0 << 0; }
void Transaction::fillStream(RLPStream& _s, bool _sig) const { _s.appendList(_sig ? 7 : 4); _s << nonce << receiveAddress << value << data; if (_sig) _s << vrs.v << vrs.r << vrs.s; }
void TrieInfixNode::makeRLP(RLPStream& _intoStream) const { assert(m_next); _intoStream.appendList(2); _intoStream << hexPrefixEncode(m_ext, false); m_next->putRLP(_intoStream); }
void PingNode::streamRLP(RLPStream& _s) const { _s.appendList(4); _s << dev::p2p::c_protocolVersion; source.streamRLP(_s); destination.streamRLP(_s); _s << ts; }
Json::Value Eth::eth_signTransaction(Json::Value const& _json) { try { TransactionSkeleton ts = toTransactionSkeleton(_json); setTransactionDefaults(ts); ts = client()->populateTransactionWithDefaults(ts); pair<bool, Secret> ar = m_ethAccounts.authenticate(ts); Transaction t(ts, ar.second); RLPStream s; t.streamRLP(s); return toJson(t, s.out()); } catch (Exception const&) { throw JsonRpcException(exceptionToErrorMessage()); } }
void TrieBranchNode::makeRLP(RLPStream& _intoStream) const { _intoStream.appendList(17); for (auto i: m_nodes) if (i) i->putRLP(_intoStream); else _intoStream << ""; _intoStream << m_value; }
void NodeIPEndpoint::streamRLP(RLPStream& _s, RLPAppend _append) const { if (_append == StreamList) _s.appendList(3); if (address.is_v4()) _s << bytesConstRef(&address.to_v4().to_bytes()[0], 4); else if (address.is_v6()) _s << bytesConstRef(&address.to_v6().to_bytes()[0], 16); else _s << bytes(); _s << udpPort << tcpPort; }
void BlockInfo::verifyInternals(bytesConstRef _block) const { RLP root(_block); auto txList = root[1]; auto expectedRoot = trieRootOver(txList.itemCount(), [&](unsigned i){ return rlp(i); }, [&](unsigned i){ return txList[i].data().toBytes(); }); clog(BlockInfoDiagnosticsChannel) << "Expected trie root:" << toString(expectedRoot); if (m_transactionsRoot != expectedRoot) { MemoryDB tm; GenericTrieDB<MemoryDB> transactionsTrie(&tm); transactionsTrie.init(); vector<bytesConstRef> txs; for (unsigned i = 0; i < txList.itemCount(); ++i) { RLPStream k; k << i; transactionsTrie.insert(&k.out(), txList[i].data()); txs.push_back(txList[i].data()); cdebug << toHex(k.out()) << toHex(txList[i].data()); } cdebug << "trieRootOver" << expectedRoot; cdebug << "orderedTrieRoot" << orderedTrieRoot(txs); cdebug << "TrieDB" << transactionsTrie.root(); cdebug << "Contents:"; for (auto const& t: txs) cdebug << toHex(t); BOOST_THROW_EXCEPTION(InvalidTransactionsRoot() << Hash256RequirementError(expectedRoot, m_transactionsRoot)); } clog(BlockInfoDiagnosticsChannel) << "Expected uncle hash:" << toString(sha3(root[2].data())); if (m_sha3Uncles != sha3(root[2].data())) BOOST_THROW_EXCEPTION(InvalidUnclesHash() << Hash256RequirementError(sha3(root[2].data()), m_sha3Uncles)); }
void State::completeMine() { cdebug << "Completing mine!"; // Got it! // Compile block: RLPStream ret; ret.appendList(3); m_currentBlock.streamRLP(ret, WithNonce); ret.appendRaw(m_currentTxs); ret.appendRaw(m_currentUncles); ret.swapOut(m_currentBytes); m_currentBlock.hash = sha3(RLP(m_currentBytes)[0].data()); cnote << "Mined " << m_currentBlock.hash.abridged() << "(parent: " << m_currentBlock.parentHash.abridged() << ")"; // Quickly reset the transactions. // TODO: Leave this in a better state than this limbo, or at least record that it's in limbo. m_transactions.clear(); m_receipts.clear(); m_transactionSet.clear(); m_lastTx = m_db; }
int operator()() { js::mValue v; string s = asString(contents("../../tests/rlptest.json")); js::read_string(s, v); bool passed = true; for (auto& i: v.get_obj()) { js::mObject& o = i.second.get_obj(); cnote << i.first; RLPStream s; buildRLP(o["in"], s); if (!o["out"].is_null() && o["out"].get_str() != toHex(s.out())) { cwarn << "Test failed."; cwarn << "Test says:" << o["out"].get_str(); cwarn << "Impl says:" << toHex(s.out()); passed = false; } } return passed ? 0 : 1; }
void Transaction::streamRLP(RLPStream& _s, IncludeSignature _sig) const { if (m_type == NullTransaction) return; _s.appendList((_sig ? 3 : 0) + 6); _s << m_nonce << m_gasPrice << m_gas; if (m_type == MessageCall) _s << m_receiveAddress; else _s << ""; _s << m_value << m_data; if (_sig) _s << (m_vrs.v + 27) << (u256)m_vrs.r << (u256)m_vrs.s; }