Browse Source

Added timestamp to keva zmq results.

idb-fix
Just Wonder 4 years ago
parent
commit
1d7ddc7a23
  1. 21
      src/keva/main.cpp
  2. 13
      src/keva/main.h
  3. 11
      src/test/keva_tests.cpp
  4. 2
      src/validation.cpp
  5. 18
      src/validationinterface.cpp
  6. 12
      src/validationinterface.h
  7. 2
      src/zmq/zmqabstractnotifier.cpp
  8. 2
      src/zmq/zmqabstractnotifier.h
  9. 16
      src/zmq/zmqnotificationinterface.cpp
  10. 6
      src/zmq/zmqnotificationinterface.h
  11. 17
      src/zmq/zmqpublishnotifier.cpp
  12. 6
      src/zmq/zmqpublishnotifier.h

21
src/keva/main.cpp

@ -234,24 +234,24 @@ CKevaNotifier::CKevaNotifier(CMainSignals* signals) { @@ -234,24 +234,24 @@ CKevaNotifier::CKevaNotifier(CMainSignals* signals) {
this->signals = signals;
}
void CKevaNotifier::KevaNamespaceCreated(const CTransaction& tx, unsigned nHeight, const std::string& nameSpace) {
void CKevaNotifier::KevaNamespaceCreated(const CTransaction& tx, const CBlockIndex &pindex, const std::string& nameSpace) {
if (signals) {
CTransactionRef ptx = MakeTransactionRef(tx);
signals->KevaNamespaceCreated(ptx, nHeight, nameSpace);
signals->KevaNamespaceCreated(ptx, pindex, nameSpace);
}
}
void CKevaNotifier::KevaUpdated(const CTransaction& tx, unsigned nHeight, const std::string& nameSpace, const std::string& key, const std::string& value) {
void CKevaNotifier::KevaUpdated(const CTransaction& tx, const CBlockIndex &pindex, const std::string& nameSpace, const std::string& key, const std::string& value) {
if (signals) {
CTransactionRef ptx = MakeTransactionRef(tx);
signals->KevaUpdated(ptx, nHeight, nameSpace, key, value);
signals->KevaUpdated(ptx, pindex, nameSpace, key, value);
}
}
void CKevaNotifier::KevaDeleted(const CTransaction& tx, unsigned nHeight, const std::string& nameSpace, const std::string& key) {
void CKevaNotifier::KevaDeleted(const CTransaction& tx, const CBlockIndex &pindex, const std::string& nameSpace, const std::string& key) {
if (signals) {
CTransactionRef ptx = MakeTransactionRef(tx);
signals->KevaDeleted(ptx, nHeight, nameSpace, key);
signals->KevaDeleted(ptx, pindex, nameSpace, key);
}
}
@ -373,9 +373,10 @@ CheckKevaTransaction (const CTransaction& tx, unsigned nHeight, @@ -373,9 +373,10 @@ CheckKevaTransaction (const CTransaction& tx, unsigned nHeight,
return true;
}
void ApplyKevaTransaction(const CTransaction& tx, unsigned nHeight,
void ApplyKevaTransaction(const CTransaction& tx, const CBlockIndex& pindex,
CCoinsViewCache& view, CBlockUndo& undo, CKevaNotifier& notifier)
{
unsigned int nHeight = pindex.nHeight;
assert (nHeight != MEMPOOL_HEIGHT);
if (!tx.IsKevacoin())
return;
@ -403,7 +404,7 @@ void ApplyKevaTransaction(const CTransaction& tx, unsigned nHeight, @@ -403,7 +404,7 @@ void ApplyKevaTransaction(const CTransaction& tx, unsigned nHeight,
CKevaData data;
data.fromScript(nHeight, COutPoint(tx.GetHash(), i), op);
view.SetName(nameSpace, key, data, false);
notifier.KevaNamespaceCreated(tx, nHeight, EncodeBase58Check(nameSpace));
notifier.KevaNamespaceCreated(tx, pindex, EncodeBase58Check(nameSpace));
} else if (op.isAnyUpdate()) {
const valtype& nameSpace = op.getOpNamespace();
const valtype& key = op.getOpKey();
@ -419,12 +420,12 @@ void ApplyKevaTransaction(const CTransaction& tx, unsigned nHeight, @@ -419,12 +420,12 @@ void ApplyKevaTransaction(const CTransaction& tx, unsigned nHeight,
CKevaData oldData;
if (view.GetName(nameSpace, key, oldData)) {
view.DeleteName(nameSpace, key);
notifier.KevaDeleted(tx, nHeight, EncodeBase58Check(nameSpace), ValtypeToString(key));
notifier.KevaDeleted(tx, pindex, EncodeBase58Check(nameSpace), ValtypeToString(key));
}
} else {
data.fromScript(nHeight, COutPoint(tx.GetHash(), i), op);
view.SetName(nameSpace, key, data, false);
notifier.KevaUpdated(tx, nHeight, EncodeBase58Check(nameSpace), ValtypeToString(key), ValtypeToString(data.getValue()));
notifier.KevaUpdated(tx, pindex, EncodeBase58Check(nameSpace), ValtypeToString(key), ValtypeToString(data.getValue()));
}
}
}

13
src/keva/main.h

@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
#define H_BITCOIN_KEVA_MAIN
#include <amount.h>
#include <chain.h>
#include <script/script.h>
#include <keva/common.h>
#include <primitives/transaction.h>
@ -229,9 +230,9 @@ private: @@ -229,9 +230,9 @@ private:
public:
CKevaNotifier(CMainSignals*);
void KevaNamespaceCreated(const CTransaction& tx, unsigned nHeight, const std::string& nameSpace);
void KevaUpdated(const CTransaction& tx, unsigned nHeight, const std::string& nameSpace, const std::string& key, const std::string& value);
void KevaDeleted(const CTransaction& tx, unsigned nHeight, const std::string& nameSpace, const std::string& key);
void KevaNamespaceCreated(const CTransaction& tx, const CBlockIndex &pindex, const std::string& nameSpace);
void KevaUpdated(const CTransaction& tx, const CBlockIndex &pindex, const std::string& nameSpace, const std::string& key, const std::string& value);
void KevaDeleted(const CTransaction& tx, const CBlockIndex &pindex, const std::string& nameSpace, const std::string& key);
};
/* ************************************************************************** */
@ -252,13 +253,13 @@ bool CheckKevaTransaction (const CTransaction& tx, unsigned nHeight, @@ -252,13 +253,13 @@ bool CheckKevaTransaction (const CTransaction& tx, unsigned nHeight,
CValidationState& state, unsigned flags);
/**
* Apply the changes of a name transaction to the name database.
* Apply the changes of a keva transaction to the database.
* @param tx The transaction to apply.
* @param nHeight Height at which the tx is. Used for CNameData.
* @param pindex Pointer to the block.
* @param view The chain state to update.
* @param undo Record undo information here.
*/
void ApplyKevaTransaction (const CTransaction& tx, unsigned nHeight,
void ApplyKevaTransaction (const CTransaction& tx, const CBlockIndex& pindex,
CCoinsViewCache& view, CBlockUndo& undo, CKevaNotifier& notifier);
/**

11
src/test/keva_tests.cpp

@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
#include <base58.h>
#include <coins.h>
#include <chain.h>
#include <consensus/validation.h>
#include <keva/main.h>
#include <policy/policy.h>
@ -728,17 +729,20 @@ BOOST_AUTO_TEST_CASE(keva_updates_undo) @@ -728,17 +729,20 @@ BOOST_AUTO_TEST_CASE(keva_updates_undo)
/* The constructed tx needs not be valid. We only test
ApplyKevaTransaction and not validation. */
CBlockIndex pindex = CBlockIndex();
CMutableTransaction mtx;
mtx.SetKevacoin();
mtx.vout.push_back(CTxOut(COIN, scrNew));
ApplyKevaTransaction(mtx, 100, view, undo, kevaNotifier);
pindex.nHeight = 100;
ApplyKevaTransaction(mtx, pindex, view, undo, kevaNotifier);
BOOST_CHECK(!view.GetName(nameSpace, key1, data));
BOOST_CHECK(view.GetNamespace(nameSpace, data));
BOOST_CHECK(undo.vkevaundo.size() == 1);
mtx.vout.clear();
mtx.vout.push_back(CTxOut(COIN, scr1_1));
ApplyKevaTransaction(mtx, 200, view, undo, kevaNotifier);
pindex.nHeight = 200;
ApplyKevaTransaction(mtx, pindex, view, undo, kevaNotifier);
BOOST_CHECK(view.GetName(nameSpace, key1, data));
BOOST_CHECK(data.getHeight() == 200);
BOOST_CHECK(data.getValue() == value1_old);
@ -748,7 +752,8 @@ BOOST_AUTO_TEST_CASE(keva_updates_undo) @@ -748,7 +752,8 @@ BOOST_AUTO_TEST_CASE(keva_updates_undo)
mtx.vout.clear();
mtx.vout.push_back(CTxOut(COIN, scr1_2));
ApplyKevaTransaction(mtx, 300, view, undo, kevaNotifier);
pindex.nHeight = 300;
ApplyKevaTransaction(mtx, pindex, view, undo, kevaNotifier);
BOOST_CHECK(view.GetName(nameSpace, key1, data));
BOOST_CHECK(data.getHeight() == 300);
BOOST_CHECK(data.getValue() == value1_new);

2
src/validation.cpp

@ -2023,7 +2023,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl @@ -2023,7 +2023,7 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
}
UpdateCoins(tx, view, i == 0 ? undoDummy : blockundo.vtxundo.back(), pindex->nHeight);
CKevaNotifier kevaNotifier(&GetMainSignals());
ApplyKevaTransaction(tx, pindex->nHeight, view, blockundo, kevaNotifier);
ApplyKevaTransaction(tx, *pindex, view, blockundo, kevaNotifier);
}
int64_t nTime3 = GetTimeMicros(); nTimeConnect += nTime3 - nTime2;
LogPrint(BCLog::BENCH, " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs (%.2fms/blk)]\n", (unsigned)block.vtx.size(), MILLI * (nTime3 - nTime2), MILLI * (nTime3 - nTime2) / block.vtx.size(), nInputs <= 1 ? 0 : MILLI * (nTime3 - nTime2) / (nInputs-1), nTimeConnect * MICRO, nTimeConnect * MILLI / nBlocksTotal);

18
src/validationinterface.cpp

@ -31,9 +31,9 @@ struct MainSignalsInstance { @@ -31,9 +31,9 @@ struct MainSignalsInstance {
boost::signals2::signal<void (const CBlockIndex *, const std::shared_ptr<const CBlock>&)> NewPoWValidBlock;
/** Keva related */
boost::signals2::signal<void (const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace)> KevaNamespaceCreated;
boost::signals2::signal<void (const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace, const std::string& key, const std::string& value)> KevaUpdated;
boost::signals2::signal<void (const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace, const std::string& key)> KevaDeleted;
boost::signals2::signal<void (const CTransactionRef &ptx, const CBlockIndex &pindex, const std::string& nameSpace)> KevaNamespaceCreated;
boost::signals2::signal<void (const CTransactionRef &ptx, const CBlockIndex &pindex, const std::string& nameSpace, const std::string& key, const std::string& value)> KevaUpdated;
boost::signals2::signal<void (const CTransactionRef &ptx, const CBlockIndex &pindex, const std::string& nameSpace, const std::string& key)> KevaDeleted;
// We are not allowed to assume the scheduler only runs in one thread,
// but must ensure all callbacks happen in-order, so we end up creating
@ -200,14 +200,14 @@ void CMainSignals::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared @@ -200,14 +200,14 @@ void CMainSignals::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared
m_internals->NewPoWValidBlock(pindex, block);
}
void CMainSignals::KevaNamespaceCreated(const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace) {
m_internals->KevaNamespaceCreated(ptx, height, nameSpace);
void CMainSignals::KevaNamespaceCreated(const CTransactionRef &ptx, const CBlockIndex &pindex, const std::string& nameSpace) {
m_internals->KevaNamespaceCreated(ptx, pindex, nameSpace);
}
void CMainSignals::KevaUpdated(const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace, const std::string& key, const std::string& value) {
m_internals->KevaUpdated(ptx, height, nameSpace, key, value);
void CMainSignals::KevaUpdated(const CTransactionRef &ptx, const CBlockIndex &pindex, const std::string& nameSpace, const std::string& key, const std::string& value) {
m_internals->KevaUpdated(ptx, pindex, nameSpace, key, value);
}
void CMainSignals::KevaDeleted(const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace, const std::string& key) {
m_internals->KevaDeleted(ptx, height, nameSpace, key);
void CMainSignals::KevaDeleted(const CTransactionRef &ptx, const CBlockIndex &pindex, const std::string& nameSpace, const std::string& key) {
m_internals->KevaDeleted(ptx, pindex, nameSpace, key);
}

12
src/validationinterface.h

@ -115,19 +115,19 @@ protected: @@ -115,19 +115,19 @@ protected:
* Keva related interface.
* Notifies listeners of a new namespace.
*/
virtual void KevaNamespaceCreated(const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace) {}
virtual void KevaNamespaceCreated(const CTransactionRef &ptx, const CBlockIndex &pindex, const std::string& nameSpace) {}
/**
* Keva related interface.
* Notifies listeners of a key creation or update.
*/
virtual void KevaUpdated(const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace, const std::string& key, const std::string& value) {}
virtual void KevaUpdated(const CTransactionRef &ptx, const CBlockIndex &pindex, const std::string& nameSpace, const std::string& key, const std::string& value) {}
/**
* Keva related interface.
* Notifies listeners of a key creation or update.
*/
virtual void KevaDeleted(const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace, const std::string& key) {}
virtual void KevaDeleted(const CTransactionRef &ptx, const CBlockIndex &pindex, const std::string& nameSpace, const std::string& key) {}
/**
* Notifies listeners that a block which builds directly on our current tip
@ -175,9 +175,9 @@ public: @@ -175,9 +175,9 @@ public:
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr<const CBlock>&);
/** Keva related */
void KevaNamespaceCreated(const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace);
void KevaUpdated(const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace, const std::string& key, const std::string& value);
void KevaDeleted(const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace, const std::string& key);
void KevaNamespaceCreated(const CTransactionRef &ptx, const CBlockIndex& pindex, const std::string& nameSpace);
void KevaUpdated(const CTransactionRef &ptx, const CBlockIndex& pindex, const std::string& nameSpace, const std::string& key, const std::string& value);
void KevaDeleted(const CTransactionRef &ptx, const CBlockIndex& pindex, const std::string& nameSpace, const std::string& key);
};
CMainSignals& GetMainSignals();

2
src/zmq/zmqabstractnotifier.cpp

@ -21,7 +21,7 @@ bool CZMQAbstractNotifier::NotifyTransaction(const CTransaction &/*transaction*/ @@ -21,7 +21,7 @@ bool CZMQAbstractNotifier::NotifyTransaction(const CTransaction &/*transaction*/
return true;
}
bool CZMQAbstractNotifier::NotifyKeva(const CTransactionRef &ptx, unsigned int height, unsigned int type, const std::string& nameSpace, const std::string& key, const std::string& value)
bool CZMQAbstractNotifier::NotifyKeva(const CTransactionRef &ptx, const CBlockIndex &pindex, unsigned int type, const std::string& nameSpace, const std::string& key, const std::string& value)
{
return true;
}

2
src/zmq/zmqabstractnotifier.h

@ -35,7 +35,7 @@ public: @@ -35,7 +35,7 @@ public:
virtual bool NotifyBlock(const CBlockIndex *pindex);
virtual bool NotifyTransaction(const CTransaction &transaction);
virtual bool NotifyKeva(const CTransactionRef &ptx, unsigned int height, unsigned int type,
virtual bool NotifyKeva(const CTransactionRef &ptx, const CBlockIndex &pindex, unsigned int type,
const std::string& nameSpace,
const std::string& key = std::string(),
const std::string& value = std::string());

16
src/zmq/zmqnotificationinterface.cpp

@ -10,10 +10,6 @@ @@ -10,10 +10,6 @@
#include <streams.h>
#include <util.h>
const static unsigned int KEVA_TYPE_CREATE_NAMESPACE = 0;
const static unsigned int KEVA_TYPE_UPDATE_KEY = 1;
const static unsigned int KEVA_TYPE_DELETE_KEY = 2;
void zmqError(const char *str)
{
LogPrint(BCLog::ZMQ, "zmq: Error: %s, errno=%s\n", str, zmq_strerror(errno));
@ -195,12 +191,12 @@ void CZMQNotificationInterface::BlockDisconnected(const std::shared_ptr<const CB @@ -195,12 +191,12 @@ void CZMQNotificationInterface::BlockDisconnected(const std::shared_ptr<const CB
}
}
void CZMQNotificationInterface::KevaNamespaceCreated(const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace)
void CZMQNotificationInterface::KevaNamespaceCreated(const CTransactionRef &ptx, const CBlockIndex &pindex, const std::string& nameSpace)
{
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )
{
CZMQAbstractNotifier *notifier = *i;
if (notifier->NotifyKeva(ptx, height, KEVA_TYPE_CREATE_NAMESPACE, nameSpace)) {
if (notifier->NotifyKeva(ptx, pindex, KEVA_TYPE_CREATE_NAMESPACE, nameSpace)) {
i++;
} else {
notifier->Shutdown();
@ -209,12 +205,12 @@ void CZMQNotificationInterface::KevaNamespaceCreated(const CTransactionRef &ptx, @@ -209,12 +205,12 @@ void CZMQNotificationInterface::KevaNamespaceCreated(const CTransactionRef &ptx,
}
}
void CZMQNotificationInterface::KevaUpdated(const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace, const std::string& key, const std::string& value)
void CZMQNotificationInterface::KevaUpdated(const CTransactionRef &ptx, const CBlockIndex &pindex, const std::string& nameSpace, const std::string& key, const std::string& value)
{
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )
{
CZMQAbstractNotifier *notifier = *i;
if (notifier->NotifyKeva(ptx, height, KEVA_TYPE_UPDATE_KEY, nameSpace, key, value)) {
if (notifier->NotifyKeva(ptx, pindex, KEVA_TYPE_UPDATE_KEY, nameSpace, key, value)) {
i++;
} else {
notifier->Shutdown();
@ -223,12 +219,12 @@ void CZMQNotificationInterface::KevaUpdated(const CTransactionRef &ptx, unsigned @@ -223,12 +219,12 @@ void CZMQNotificationInterface::KevaUpdated(const CTransactionRef &ptx, unsigned
}
}
void CZMQNotificationInterface::KevaDeleted(const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace, const std::string& key)
void CZMQNotificationInterface::KevaDeleted(const CTransactionRef &ptx, const CBlockIndex &pindex, const std::string& nameSpace, const std::string& key)
{
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )
{
CZMQAbstractNotifier *notifier = *i;
if (notifier->NotifyKeva(ptx, height, KEVA_TYPE_DELETE_KEY, nameSpace, key)) {
if (notifier->NotifyKeva(ptx, pindex, KEVA_TYPE_DELETE_KEY, nameSpace, key)) {
i++;
} else {
notifier->Shutdown();

6
src/zmq/zmqnotificationinterface.h

@ -35,9 +35,9 @@ protected: @@ -35,9 +35,9 @@ protected:
/**
* Keva related interface.
*/
void KevaNamespaceCreated(const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace) override;
void KevaUpdated(const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace, const std::string& key, const std::string& value) override;
void KevaDeleted(const CTransactionRef &ptx, unsigned int height, const std::string& nameSpace, const std::string& key) override;
void KevaNamespaceCreated(const CTransactionRef &ptx, const CBlockIndex &pindex, const std::string& nameSpace) override;
void KevaUpdated(const CTransactionRef &ptx, const CBlockIndex &pindex, const std::string& nameSpace, const std::string& key, const std::string& value) override;
void KevaDeleted(const CTransactionRef &ptx, const CBlockIndex &pindex, const std::string& nameSpace, const std::string& key) override;
private:
CZMQNotificationInterface();

17
src/zmq/zmqpublishnotifier.cpp

@ -198,15 +198,28 @@ bool CZMQPublishRawTransactionNotifier::NotifyTransaction(const CTransaction &tr @@ -198,15 +198,28 @@ bool CZMQPublishRawTransactionNotifier::NotifyTransaction(const CTransaction &tr
return SendMessage(MSG_RAWTX, &(*ss.begin()), ss.size());
}
bool CZMQPublishKevaNotifier::NotifyKeva(const CTransactionRef &ptx, unsigned int height, unsigned int type, const std::string& nameSpace, const std::string& key, const std::string& value)
bool CZMQPublishKevaNotifier::NotifyKeva(const CTransactionRef &ptx, const CBlockIndex &pindex, unsigned int type, const std::string& nameSpace, const std::string& key, const std::string& value)
{
uint256 hash = ptx->GetHash();
int height = pindex.nHeight;
int64_t timestamp = pindex.GetBlockTime();
LogPrint(BCLog::ZMQ, "zmq: Publish keva height: %d, tx: %s\n", height, ptx->GetHash().ToString().c_str());
UniValue entry(UniValue::VOBJ);
entry.pushKV("tx", hash.ToString());
entry.pushKV("height", (int)height);
entry.pushKV("type", (int)type);
entry.pushKV("timestamp", timestamp);
if (type == KEVA_TYPE_CREATE_NAMESPACE) {
entry.pushKV("type", "keva_namespace");
} else if (type == KEVA_TYPE_UPDATE_KEY) {
entry.pushKV("type", "keva_update");
} else if (type == KEVA_TYPE_DELETE_KEY) {
entry.pushKV("type", "keva_delete");
} else {
entry.pushKV("type", "unknown");
}
entry.pushKV("namespace", nameSpace);
if (key.size() > 0) {

6
src/zmq/zmqpublishnotifier.h

@ -7,6 +7,10 @@ @@ -7,6 +7,10 @@
#include <zmq/zmqabstractnotifier.h>
const static unsigned int KEVA_TYPE_CREATE_NAMESPACE = 0;
const static unsigned int KEVA_TYPE_UPDATE_KEY = 1;
const static unsigned int KEVA_TYPE_DELETE_KEY = 2;
class CBlockIndex;
class CZMQAbstractPublishNotifier : public CZMQAbstractNotifier
@ -55,7 +59,7 @@ public: @@ -55,7 +59,7 @@ public:
class CZMQPublishKevaNotifier : public CZMQAbstractPublishNotifier
{
public:
bool NotifyKeva(const CTransactionRef &ptx, unsigned int height, unsigned int type,
bool NotifyKeva(const CTransactionRef &ptx, const CBlockIndex &pindex, unsigned int type,
const std::string& nameSpace,
const std::string& key = std::string(),
const std::string& value = std::string()) override;

Loading…
Cancel
Save