From 2b93dcdc1d59436ec5670200044427a95d542066 Mon Sep 17 00:00:00 2001
From: Gav Wood <i@gavwood.com>
Date: Tue, 14 Oct 2014 12:35:06 +0300
Subject: [PATCH 1/6] Move over to new GetTransactions semantics.

---
 libethereum/EthereumHost.cpp | 20 +++++++++++---------
 libethereum/EthereumHost.h   |  2 +-
 libethereum/EthereumPeer.cpp | 10 +++++-----
 libethereum/Executive.cpp    | 12 ++++++------
 libethereum/State.h          |  3 ++-
 5 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/libethereum/EthereumHost.cpp b/libethereum/EthereumHost.cpp
index ee5105cd6..5daf67fb9 100644
--- a/libethereum/EthereumHost.cpp
+++ b/libethereum/EthereumHost.cpp
@@ -148,17 +148,19 @@ void EthereumHost::doWork()
 {
 	bool netChange = ensureInitialised();
 	auto h = m_chain.currentHash();
-	maintainTransactions(h);
-	maintainBlocks(h);
+	// If we've finished our initial sync (including getting all the blocks into the chain so as to reduce invalid transactions), start trading transactions & blocks
+	if (!isSyncing() && m_chain.isKnown(m_latestBlockSent))
+	{
+		maintainTransactions();
+		maintainBlocks(h);
+	}
 //	return netChange;
 	// TODO: Figure out what to do with netChange.
 	(void)netChange;
 }
 
-void EthereumHost::maintainTransactions(h256 _currentHash)
+void EthereumHost::maintainTransactions()
 {
-	bool resendAll = (!isSyncing() && m_chain.isKnown(m_latestBlockSent) && _currentHash != m_latestBlockSent);
-
 	// Send any new transactions.
 	for (auto const& p: peers())
 		if (auto ep = p->cap<EthereumPeer>())
@@ -166,14 +168,14 @@ void EthereumHost::maintainTransactions(h256 _currentHash)
 			bytes b;
 			unsigned n = 0;
 			for (auto const& i: m_tq.transactions())
-				if ((!m_transactionsSent.count(i.first) && !ep->m_knownTransactions.count(i.first)) || ep->m_requireTransactions || resendAll)
+				if (ep->m_requireTransactions || (!m_transactionsSent.count(i.first) && !ep->m_knownTransactions.count(i.first)))
 				{
 					b += i.second;
 					++n;
 					m_transactionsSent.insert(i.first);
 				}
 			ep->clearKnownTransactions();
-			
+
 			if (n || ep->m_requireTransactions)
 			{
 				RLPStream ts;
@@ -186,8 +188,8 @@ void EthereumHost::maintainTransactions(h256 _currentHash)
 
 void EthereumHost::maintainBlocks(h256 _currentHash)
 {
-	// If we've finished our initial sync send any new blocks.
-	if (!isSyncing() && m_chain.isKnown(m_latestBlockSent) && m_chain.details(m_latestBlockSent).totalDifficulty < m_chain.details(_currentHash).totalDifficulty)
+	// Send any new blocks.
+	if (m_chain.details(m_latestBlockSent).totalDifficulty < m_chain.details(_currentHash).totalDifficulty)
 	{
 		clog(NetMessageSummary) << "Sending a new block (current is" << _currentHash << ", was" << m_latestBlockSent << ")";
 
diff --git a/libethereum/EthereumHost.h b/libethereum/EthereumHost.h
index 2add925c6..18ba765aa 100644
--- a/libethereum/EthereumHost.h
+++ b/libethereum/EthereumHost.h
@@ -84,7 +84,7 @@ private:
 	/// Sync with the BlockChain. It might contain one of our mined blocks, we might have new candidates from the network.
 	void doWork();
 
-	void maintainTransactions(h256 _currentBlock);
+	void maintainTransactions();
 	void maintainBlocks(h256 _currentBlock);
 
 	/// Get a bunch of needed blocks.
diff --git a/libethereum/EthereumPeer.cpp b/libethereum/EthereumPeer.cpp
index 6c10524ca..24b400c1d 100644
--- a/libethereum/EthereumPeer.cpp
+++ b/libethereum/EthereumPeer.cpp
@@ -82,7 +82,11 @@ void EthereumPeer::transition(Asking _a, bool _force)
 {
 	clogS(NetMessageSummary) << "Transition!" << ::toString(_a) << "from" << ::toString(m_asking) << ", " << (isSyncing() ? "syncing" : "holding") << (needsSyncing() ? "& needed" : "");
 
+	if (m_asking == Asking::State && _a != Asking::State)
+		m_requireTransactions = true;
+
 	RLPStream s;
+
 	if (_a == Asking::State)
 	{
 		if (m_asking == Asking::Nothing)
@@ -324,11 +328,7 @@ bool EthereumPeer::interpret(unsigned _id, RLP const& _r)
 		}
 		break;
 	}
-	case GetTransactionsPacket:
-	{
-		m_requireTransactions = true;
-		break;
-	}
+	case GetTransactionsPacket: break;	// DEPRECATED.
 	case TransactionsPacket:
 	{
 		clogS(NetMessageSummary) << "Transactions (" << dec << (_r.itemCount() - 1) << "entries)";
diff --git a/libethereum/Executive.cpp b/libethereum/Executive.cpp
index 93900b51e..193010cfa 100644
--- a/libethereum/Executive.cpp
+++ b/libethereum/Executive.cpp
@@ -54,14 +54,14 @@ bool Executive::setup(bytesConstRef _rlp)
 	auto nonceReq = m_s.transactionsFrom(m_sender);
 	if (m_t.nonce != nonceReq)
 	{
-		clog(StateChat) << "Invalid Nonce: Require" << nonceReq << " Got" << m_t.nonce;
+		clog(StateDetail) << "Invalid Nonce: Require" << nonceReq << " Got" << m_t.nonce;
 		BOOST_THROW_EXCEPTION(InvalidNonce(nonceReq, m_t.nonce));
 	}
 
 	// Don't like transactions whose gas price is too low. NOTE: this won't stay here forever - it's just until we get a proper gas price discovery protocol going.
 	if (m_t.gasPrice < m_s.m_currentBlock.minGasPrice)
 	{
-		clog(StateChat) << "Offered gas-price is too low: Require >" << m_s.m_currentBlock.minGasPrice << " Got" << m_t.gasPrice;
+		clog(StateDetail) << "Offered gas-price is too low: Require >" << m_s.m_currentBlock.minGasPrice << " Got" << m_t.gasPrice;
 		BOOST_THROW_EXCEPTION(GasPriceTooLow());
 	}
 
@@ -70,7 +70,7 @@ bool Executive::setup(bytesConstRef _rlp)
 
 	if (m_t.gas < gasCost)
 	{
-		clog(StateChat) << "Not enough gas to pay for the transaction: Require >" << gasCost << " Got" << m_t.gas;
+		clog(StateDetail) << "Not enough gas to pay for the transaction: Require >" << gasCost << " Got" << m_t.gas;
 		BOOST_THROW_EXCEPTION(OutOfGas());
 	}
 
@@ -79,14 +79,14 @@ bool Executive::setup(bytesConstRef _rlp)
 	// Avoid unaffordable transactions.
 	if (m_s.balance(m_sender) < cost)
 	{
-		clog(StateChat) << "Not enough cash: Require >" << cost << " Got" << m_s.balance(m_sender);
+		clog(StateDetail) << "Not enough cash: Require >" << cost << " Got" << m_s.balance(m_sender);
 		BOOST_THROW_EXCEPTION(NotEnoughCash());
 	}
 
 	u256 startGasUsed = m_s.gasUsed();
 	if (startGasUsed + m_t.gas > m_s.m_currentBlock.gasLimit)
 	{
-		clog(StateChat) << "Too much gas used in this block: Require <" << (m_s.m_currentBlock.gasLimit - startGasUsed) << " Got" << m_t.gas;
+		clog(StateDetail) << "Too much gas used in this block: Require <" << (m_s.m_currentBlock.gasLimit - startGasUsed) << " Got" << m_t.gas;
 		BOOST_THROW_EXCEPTION(BlockGasLimitReached());
 	}
 
@@ -94,7 +94,7 @@ bool Executive::setup(bytesConstRef _rlp)
 	m_s.noteSending(m_sender);
 
 	// Pay...
-//	cnote << "Paying" << formatBalance(cost) << "from sender (includes" << m_t.gas << "gas at" << formatBalance(m_t.gasPrice) << ")";
+	clog(StateDetail) << "Paying" << formatBalance(cost) << "from sender (includes" << m_t.gas << "gas at" << formatBalance(m_t.gasPrice) << ")";
 	m_s.subBalance(m_sender, cost);
 
 	if (m_ms)
diff --git a/libethereum/State.h b/libethereum/State.h
index 6914b9f5e..5552ba454 100644
--- a/libethereum/State.h
+++ b/libethereum/State.h
@@ -47,8 +47,9 @@ namespace eth
 
 class BlockChain;
 
-struct StateChat: public LogChannel { static const char* name() { return "=S="; } static const int verbosity = 4; };
+struct StateChat: public LogChannel { static const char* name() { return "-S-"; } static const int verbosity = 4; };
 struct StateTrace: public LogChannel { static const char* name() { return "=S="; } static const int verbosity = 7; };
+struct StateDetail: public LogChannel { static const char* name() { return "/S/"; } static const int verbosity = 14; };
 
 struct TransactionReceipt
 {

From a0211076939acc246ed90188a343e6cfaac6506a Mon Sep 17 00:00:00 2001
From: Gav Wood <i@gavwood.com>
Date: Tue, 14 Oct 2014 12:37:46 +0300
Subject: [PATCH 2/6] Version bump.

---
 libdevcore/Common.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libdevcore/Common.cpp b/libdevcore/Common.cpp
index a463cf4b1..f99e7da4c 100644
--- a/libdevcore/Common.cpp
+++ b/libdevcore/Common.cpp
@@ -27,7 +27,7 @@ using namespace dev;
 namespace dev
 {
 
-char const* Version = "0.7.3";
+char const* Version = "0.7.4";
 
 #if defined(__GNUC__)
 __attribute__((gnu_inline, always_inline))

From d8fc0f2a2cc4e26aa8abda53c7e085da362041c0 Mon Sep 17 00:00:00 2001
From: Gav Wood <i@gavwood.com>
Date: Tue, 14 Oct 2014 12:41:30 +0300
Subject: [PATCH 3/6] Avoid failing inline.

---
 libdevcore/Common.h | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/libdevcore/Common.h b/libdevcore/Common.h
index f0f2c192b..cda1fbd82 100644
--- a/libdevcore/Common.h
+++ b/libdevcore/Common.h
@@ -117,15 +117,9 @@ inline unsigned int toLog2(u256 _x)
 #define asserts(A) ::dev::assertAux(A, #A, __LINE__, __FILE__, ETH_FUNC)
 #define assertsEqual(A, B) ::dev::assertEqualAux(A, B, #A, #B, __LINE__, __FILE__, ETH_FUNC)
 
-#if defined(__GNUC__)
-__attribute__((gnu_inline, always_inline))
-#endif
 inline bool assertAux(bool _a, char const* _aStr, unsigned _line, char const* _file, char const* _func);
 	
 template<class A, class B>
-#if defined(__GNUC__)
-__attribute__((gnu_inline, always_inline))
-#endif
 inline bool assertEqualAux(A const& _a, B const& _b, char const* _aStr, char const* _bStr, unsigned _line, char const* _file, char const* _func)
 {
 	bool ret = _a == _b;

From 0a3478bf5f5941df9ae4a7a3e19413d847c7a6cf Mon Sep 17 00:00:00 2001
From: Gav Wood <i@gavwood.com>
Date: Tue, 14 Oct 2014 12:41:56 +0300
Subject: [PATCH 4/6] Avoid failing inline.

---
 libdevcore/Common.cpp | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/libdevcore/Common.cpp b/libdevcore/Common.cpp
index f99e7da4c..a59bc2739 100644
--- a/libdevcore/Common.cpp
+++ b/libdevcore/Common.cpp
@@ -29,9 +29,6 @@ namespace dev
 
 char const* Version = "0.7.4";
 
-#if defined(__GNUC__)
-__attribute__((gnu_inline, always_inline))
-#endif
 inline bool assertAux(bool _a, char const* _aStr, unsigned _line, char const* _file, char const* _func)
 {
 	bool ret = _a;

From 39e71f3fa1e0ee810b29516d0bf8d5386105c006 Mon Sep 17 00:00:00 2001
From: Gav Wood <i@gavwood.com>
Date: Tue, 14 Oct 2014 12:52:59 +0300
Subject: [PATCH 5/6] Actuall inline assertAux code.

---
 libdevcore/Common.h | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/libdevcore/Common.h b/libdevcore/Common.h
index cda1fbd82..87cc069b3 100644
--- a/libdevcore/Common.h
+++ b/libdevcore/Common.h
@@ -117,8 +117,19 @@ inline unsigned int toLog2(u256 _x)
 #define asserts(A) ::dev::assertAux(A, #A, __LINE__, __FILE__, ETH_FUNC)
 #define assertsEqual(A, B) ::dev::assertEqualAux(A, B, #A, #B, __LINE__, __FILE__, ETH_FUNC)
 
-inline bool assertAux(bool _a, char const* _aStr, unsigned _line, char const* _file, char const* _func);
-	
+inline bool assertAux(bool _a, char const* _aStr, unsigned _line, char const* _file, char const* _func)
+{
+	bool ret = _a;
+	if (!ret)
+	{
+		std::cerr << "Assertion failed:" << _aStr << " [func=" << _func << ", line=" << _line << ", file=" << _file << "]" << std::endl;
+#if ETH_DEBUG
+		debug_break();
+#endif
+	}
+	return !ret;
+}
+
 template<class A, class B>
 inline bool assertEqualAux(A const& _a, B const& _b, char const* _aStr, char const* _bStr, unsigned _line, char const* _file, char const* _func)
 {

From eeb0c6476e7a881dc9c03e82f06860039af49942 Mon Sep 17 00:00:00 2001
From: Gav Wood <i@gavwood.com>
Date: Tue, 14 Oct 2014 13:00:45 +0300
Subject: [PATCH 6/6] Minor fix.

---
 libdevcore/Common.cpp | 13 -------------
 1 file changed, 13 deletions(-)

diff --git a/libdevcore/Common.cpp b/libdevcore/Common.cpp
index a59bc2739..335e2b387 100644
--- a/libdevcore/Common.cpp
+++ b/libdevcore/Common.cpp
@@ -29,18 +29,5 @@ namespace dev
 
 char const* Version = "0.7.4";
 
-inline bool assertAux(bool _a, char const* _aStr, unsigned _line, char const* _file, char const* _func)
-{
-	bool ret = _a;
-	if (!ret)
-	{
-		std::cerr << "Assertion failed:" << _aStr << " [func=" << _func << ", line=" << _line << ", file=" << _file << "]" << std::endl;
-#if ETH_DEBUG
-		debug_break();
-#endif
-	}
-	return !ret;
-}
-	
 }