diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index 33a3158d7..24e0f4034 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -142,13 +142,12 @@ void Main::refresh() ui->accounts->addItem(QString("%1 @ %2").arg(formatBalance(i.second).c_str()).arg(asHex(i.first.asArray()).c_str())); ui->transactionQueue->clear(); - for (pair const& i: m_client->transactionQueue().transactions()) + for (pair const& i: m_client->pending()) { - Transaction t(i.second); ui->transactionQueue->addItem(QString("%1 @ %2 <- %3") - .arg(formatBalance(t.value).c_str()) - .arg(asHex(t.receiveAddress.asArray()).c_str()) - .arg(asHex(t.sender().asArray()).c_str()) ); + .arg(formatBalance(i.second.value).c_str()) + .arg(asHex(i.second.receiveAddress.asArray()).c_str()) + .arg(asHex(i.second.sender().asArray()).c_str()) ); } ui->transactions->clear(); diff --git a/eth/main.cpp b/eth/main.cpp index d2399aeb8..9d0e546ac 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -352,12 +352,12 @@ int main(int argc, char** argv) cout << "Address: " << endl << asHex(us.address().asArray()) << endl; c.startNetwork(listenPort, remoteHost, remotePort, mode, peers, publicIP, upnp); eth::uint n = c.blockChain().details().number; + if (mining) + c.startMining(); while (true) { - if (c.blockChain().details().number - n >= mining) + if (c.blockChain().details().number - n == mining) c.stopMining(); - else - c.startMining(); this_thread::sleep_for(chrono::milliseconds(100)); } } diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index 4132b7478..ac0fd0bc5 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -42,6 +42,7 @@ Client::Client(std::string const& _clientVersion, Address _us, std::string const // TODO: currently it contains keys for *all* blocks. Make it remove old ones. m_s.sync(m_bc); m_s.sync(m_tq); + m_mined = m_s; m_changed = true; static const char* c_threadName = "eth"; @@ -68,7 +69,7 @@ void Client::startNetwork(short _listenPort, std::string const& _seedHost, short m_net = new PeerServer(m_clientVersion, m_bc, 0, _listenPort, _mode, _publicIP, _upnp); m_net->setIdealPeerCount(_peers); if (_seedHost.size()) - m_net->connect(_seedHost, _port); + connect(_seedHost, _port); } void Client::connect(std::string const& _seedHost, short _port) @@ -139,6 +140,7 @@ void Client::work() changed = true; m_mined = m_s; } + m_mined.sync(m_tq); } if (m_doMine) @@ -146,8 +148,8 @@ void Client::work() if (m_miningStarted) { lock_guard l(m_lock); - m_mined = m_s; - m_mined.sync(m_tq); +// m_mined = m_s; +// m_mined.sync(m_tq); m_mined.commitToMine(m_bc); } diff --git a/libethereum/Client.h b/libethereum/Client.h index f117d6537..637ada9c7 100644 --- a/libethereum/Client.h +++ b/libethereum/Client.h @@ -93,8 +93,8 @@ public: State const& state() const { return m_s; } /// Get the object representing the current canonical blockchain. BlockChain const& blockChain() const { return m_bc; } - /// Get the object representing the transaction queue. - TransactionQueue const& transactionQueue() const { return m_tq; } + /// Get a map containing each of the pending transactions. + std::map const& pending() const { return m_mined.pending(); } void setClientVersion(std::string const& _name) { m_clientVersion = _name; } diff --git a/libethereum/PeerNetwork.cpp b/libethereum/PeerNetwork.cpp index f4160fb60..ae78afeaf 100644 --- a/libethereum/PeerNetwork.cpp +++ b/libethereum/PeerNetwork.cpp @@ -787,6 +787,19 @@ void PeerServer::ensureAccepting() } } +void PeerServer::connect(std::string const& _addr, uint _port) noexcept +{ + try + { + connect(bi::tcp::endpoint(bi::address::from_string(_addr), _port)); + } + catch (exception const& e) + { + // Couldn't connect + clog(NetNote) << "Bad host " << _addr << " (" << e.what() << ")"; + } +} + void PeerServer::connect(bi::tcp::endpoint const& _ep) { clog(NetNote) << "Attempting connection to " << _ep; diff --git a/libethereum/PeerNetwork.h b/libethereum/PeerNetwork.h index e50d238a4..47856f99f 100644 --- a/libethereum/PeerNetwork.h +++ b/libethereum/PeerNetwork.h @@ -158,7 +158,7 @@ public: ~PeerServer(); /// Connect to a peer explicitly. - void connect(std::string const& _addr, uint _port = 30303) { connect(bi::tcp::endpoint(bi::address::from_string(_addr), _port)); } + void connect(std::string const& _addr, uint _port = 30303) noexcept; void connect(bi::tcp::endpoint const& _ep); /// Sync with the BlockChain. It might contain one of our mined blocks, we might have new candidates from the network. diff --git a/libethereum/State.cpp b/libethereum/State.cpp index 44b51ed15..4c01b3a57 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -274,6 +274,29 @@ void State::resetCurrent() m_state.setRoot(m_currentBlock.stateRoot); } +bool State::cull(TransactionQueue& _tq) const +{ + bool ret = false; + auto ts = _tq.transactions(); + for (auto const& i: ts) + if (!m_transactions.count(i.first)) + try + { + Transaction t(i.second); + if (t.nonce <= transactionsFrom(t.sender())) + { + _tq.drop(i.first); + ret = true; + } + } + catch (...) + { + _tq.drop(i.first); + ret = true; + } + return ret; +} + bool State::sync(TransactionQueue& _tq) { // TRANSACTIONS diff --git a/libethereum/State.h b/libethereum/State.h index 1338e3adc..180ab0ddf 100644 --- a/libethereum/State.h +++ b/libethereum/State.h @@ -119,6 +119,8 @@ public: /// Sync our transactions, killing those from the queue that we have and assimilating those that we don't. bool sync(TransactionQueue& _tq); + /// Like sync but only operate on _tq, killing the invalid/old ones. + bool cull(TransactionQueue& _tq) const; /// Execute a given transaction. void execute(bytes const& _rlp) { return execute(&_rlp); } @@ -158,6 +160,9 @@ public: /// The hash of the root of our state tree. h256 rootHash() const { return m_state.root(); } + /// Get the list of pending transactions. + std::map const& pending() const { return m_transactions; } + /// Finalise the block, applying the earned rewards. void applyRewards(Addresses const& _uncleAddresses);