Skip to content

Commit 8b09188

Browse files
committed
refactor: store tx_hash instead of calling GetHash() repeatedly
1 parent 5d80378 commit 8b09188

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

src/txmempool.cpp

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -685,45 +685,46 @@ void CTxMemPool::addUncheckedProTx(indexed_transaction_set::iterator& newit, con
685685
{
686686
assert(m_dmnman);
687687

688+
const uint256 tx_hash{tx.GetHash()};
688689
if (tx.nType == TRANSACTION_PROVIDER_REGISTER) {
689690
auto proTx = *Assert(GetTxPayload<CProRegTx>(tx));
690691
if (!proTx.collateralOutpoint.hash.IsNull()) {
691-
mapProTxRefs.emplace(tx.GetHash(), proTx.collateralOutpoint.hash);
692+
mapProTxRefs.emplace(tx_hash, proTx.collateralOutpoint.hash);
692693
}
693-
mapProTxAddresses.emplace(proTx.addr, tx.GetHash());
694-
mapProTxPubKeyIDs.emplace(proTx.keyIDOwner, tx.GetHash());
695-
mapProTxBlsPubKeyHashes.emplace(proTx.pubKeyOperator.GetHash(), tx.GetHash());
694+
mapProTxAddresses.emplace(proTx.addr, tx_hash);
695+
mapProTxPubKeyIDs.emplace(proTx.keyIDOwner, tx_hash);
696+
mapProTxBlsPubKeyHashes.emplace(proTx.pubKeyOperator.GetHash(), tx_hash);
696697
if (!proTx.collateralOutpoint.hash.IsNull()) {
697-
mapProTxCollaterals.emplace(proTx.collateralOutpoint, tx.GetHash());
698+
mapProTxCollaterals.emplace(proTx.collateralOutpoint, tx_hash);
698699
} else {
699-
mapProTxCollaterals.emplace(COutPoint(tx.GetHash(), proTx.collateralOutpoint.n), tx.GetHash());
700+
mapProTxCollaterals.emplace(COutPoint(tx_hash, proTx.collateralOutpoint.n), tx_hash);
700701
}
701702
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_SERVICE) {
702703
auto proTx = *Assert(GetTxPayload<CProUpServTx>(tx));
703-
mapProTxRefs.emplace(proTx.proTxHash, tx.GetHash());
704-
mapProTxAddresses.emplace(proTx.addr, tx.GetHash());
704+
mapProTxRefs.emplace(proTx.proTxHash, tx_hash);
705+
mapProTxAddresses.emplace(proTx.addr, tx_hash);
705706
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REGISTRAR) {
706707
auto proTx = *Assert(GetTxPayload<CProUpRegTx>(tx));
707-
mapProTxRefs.emplace(proTx.proTxHash, tx.GetHash());
708-
mapProTxBlsPubKeyHashes.emplace(proTx.pubKeyOperator.GetHash(), tx.GetHash());
708+
mapProTxRefs.emplace(proTx.proTxHash, tx_hash);
709+
mapProTxBlsPubKeyHashes.emplace(proTx.pubKeyOperator.GetHash(), tx_hash);
709710
auto dmn = Assert(m_dmnman->GetListAtChainTip().GetMN(proTx.proTxHash));
710711
newit->validForProTxKey = ::SerializeHash(dmn->pdmnState->pubKeyOperator);
711712
if (dmn->pdmnState->pubKeyOperator != proTx.pubKeyOperator) {
712713
newit->isKeyChangeProTx = true;
713714
}
714715
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REVOKE) {
715716
auto proTx = *Assert(GetTxPayload<CProUpRevTx>(tx));
716-
mapProTxRefs.emplace(proTx.proTxHash, tx.GetHash());
717+
mapProTxRefs.emplace(proTx.proTxHash, tx_hash);
717718
auto dmn = Assert(m_dmnman->GetListAtChainTip().GetMN(proTx.proTxHash));
718719
newit->validForProTxKey = ::SerializeHash(dmn->pdmnState->pubKeyOperator);
719720
if (dmn->pdmnState->pubKeyOperator.Get() != CBLSPublicKey()) {
720721
newit->isKeyChangeProTx = true;
721722
}
722723
} else if (tx.nType == TRANSACTION_ASSET_UNLOCK) {
723724
auto assetUnlockTx = *Assert(GetTxPayload<CAssetUnlockPayload>(tx));
724-
mapAssetUnlockExpiry.insert({tx.GetHash(), assetUnlockTx.getHeightToExpiry()});
725+
mapAssetUnlockExpiry.insert({tx_hash, assetUnlockTx.getHeightToExpiry()});
725726
} else if (tx.nType == TRANSACTION_MNHF_SIGNAL) {
726-
PrioritiseTransaction(tx.GetHash(), 0.1 * COIN);
727+
PrioritiseTransaction(tx_hash, 0.1 * COIN);
727728
}
728729
}
729730

@@ -784,29 +785,30 @@ void CTxMemPool::removeUncheckedProTx(const CTransaction& tx)
784785
}
785786
};
786787

788+
const uint256 tx_hash{tx.GetHash()};
787789
if (tx.nType == TRANSACTION_PROVIDER_REGISTER) {
788790
auto proTx = *Assert(GetTxPayload<CProRegTx>(tx));
789791
if (!proTx.collateralOutpoint.IsNull()) {
790-
eraseProTxRef(tx.GetHash(), proTx.collateralOutpoint.hash);
792+
eraseProTxRef(tx_hash, proTx.collateralOutpoint.hash);
791793
}
792794
mapProTxAddresses.erase(proTx.addr);
793795
mapProTxPubKeyIDs.erase(proTx.keyIDOwner);
794796
mapProTxBlsPubKeyHashes.erase(proTx.pubKeyOperator.GetHash());
795797
mapProTxCollaterals.erase(proTx.collateralOutpoint);
796-
mapProTxCollaterals.erase(COutPoint(tx.GetHash(), proTx.collateralOutpoint.n));
798+
mapProTxCollaterals.erase(COutPoint(tx_hash, proTx.collateralOutpoint.n));
797799
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_SERVICE) {
798800
auto proTx = *Assert(GetTxPayload<CProUpServTx>(tx));
799-
eraseProTxRef(proTx.proTxHash, tx.GetHash());
801+
eraseProTxRef(proTx.proTxHash, tx_hash);
800802
mapProTxAddresses.erase(proTx.addr);
801803
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REGISTRAR) {
802804
auto proTx = *Assert(GetTxPayload<CProUpRegTx>(tx));
803-
eraseProTxRef(proTx.proTxHash, tx.GetHash());
805+
eraseProTxRef(proTx.proTxHash, tx_hash);
804806
mapProTxBlsPubKeyHashes.erase(proTx.pubKeyOperator.GetHash());
805807
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REVOKE) {
806808
auto proTx = *Assert(GetTxPayload<CProUpRevTx>(tx));
807-
eraseProTxRef(proTx.proTxHash, tx.GetHash());
809+
eraseProTxRef(proTx.proTxHash, tx_hash);
808810
} else if (tx.nType == TRANSACTION_ASSET_UNLOCK) {
809-
mapAssetUnlockExpiry.erase(tx.GetHash());
811+
mapAssetUnlockExpiry.erase(tx_hash);
810812
}
811813
}
812814

@@ -1015,17 +1017,18 @@ void CTxMemPool::removeProTxConflicts(const CTransaction &tx)
10151017
{
10161018
removeProTxSpentCollateralConflicts(tx);
10171019

1020+
const uint256 tx_hash{tx.GetHash()};
10181021
if (tx.nType == TRANSACTION_PROVIDER_REGISTER) {
10191022
const auto opt_proTx = GetTxPayload<CProRegTx>(tx);
10201023
if (!opt_proTx) {
1021-
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx.GetHash().ToString());
1024+
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString());
10221025
return;
10231026
}
10241027
auto& proTx = *opt_proTx;
10251028

10261029
if (mapProTxAddresses.count(proTx.addr)) {
10271030
uint256 conflictHash = mapProTxAddresses[proTx.addr];
1028-
if (conflictHash != tx.GetHash() && mapTx.count(conflictHash)) {
1031+
if (conflictHash != tx_hash && mapTx.count(conflictHash)) {
10291032
removeRecursive(mapTx.find(conflictHash)->GetTx(), MemPoolRemovalReason::CONFLICT);
10301033
}
10311034
}
@@ -1034,25 +1037,25 @@ void CTxMemPool::removeProTxConflicts(const CTransaction &tx)
10341037
if (!proTx.collateralOutpoint.hash.IsNull()) {
10351038
removeProTxCollateralConflicts(tx, proTx.collateralOutpoint);
10361039
} else {
1037-
removeProTxCollateralConflicts(tx, COutPoint(tx.GetHash(), proTx.collateralOutpoint.n));
1040+
removeProTxCollateralConflicts(tx, COutPoint(tx_hash, proTx.collateralOutpoint.n));
10381041
}
10391042
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_SERVICE) {
10401043
const auto opt_proTx = GetTxPayload<CProUpServTx>(tx);
10411044
if (!opt_proTx) {
1042-
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx.GetHash().ToString());
1045+
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString());
10431046
return;
10441047
}
10451048

10461049
if (mapProTxAddresses.count(opt_proTx->addr)) {
10471050
uint256 conflictHash = mapProTxAddresses[opt_proTx->addr];
1048-
if (conflictHash != tx.GetHash() && mapTx.count(conflictHash)) {
1051+
if (conflictHash != tx_hash && mapTx.count(conflictHash)) {
10491052
removeRecursive(mapTx.find(conflictHash)->GetTx(), MemPoolRemovalReason::CONFLICT);
10501053
}
10511054
}
10521055
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REGISTRAR) {
10531056
const auto opt_proTx = GetTxPayload<CProUpRegTx>(tx);
10541057
if (!opt_proTx) {
1055-
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx.GetHash().ToString());
1058+
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString());
10561059
return;
10571060
}
10581061

@@ -1061,7 +1064,7 @@ void CTxMemPool::removeProTxConflicts(const CTransaction &tx)
10611064
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REVOKE) {
10621065
const auto opt_proTx = GetTxPayload<CProUpRevTx>(tx);
10631066
if (!opt_proTx) {
1064-
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx.GetHash().ToString());
1067+
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString());
10651068
return;
10661069
}
10671070

@@ -1371,10 +1374,11 @@ bool CTxMemPool::existsProviderTxConflict(const CTransaction &tx) const {
13711374
return false;
13721375
};
13731376

1377+
const uint256 tx_hash{tx.GetHash()};
13741378
if (tx.nType == TRANSACTION_PROVIDER_REGISTER) {
13751379
const auto opt_proTx = GetTxPayload<CProRegTx>(tx);
13761380
if (!opt_proTx) {
1377-
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx.GetHash().ToString());
1381+
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString());
13781382
return true; // i.e. can't decode payload == conflict
13791383
}
13801384
auto& proTx = *opt_proTx;
@@ -1394,15 +1398,15 @@ bool CTxMemPool::existsProviderTxConflict(const CTransaction &tx) const {
13941398
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_SERVICE) {
13951399
const auto opt_proTx = GetTxPayload<CProUpServTx>(tx);
13961400
if (!opt_proTx) {
1397-
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx.GetHash().ToString());
1401+
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString());
13981402
return true; // i.e. can't decode payload == conflict
13991403
}
14001404
auto it = mapProTxAddresses.find(opt_proTx->addr);
14011405
return it != mapProTxAddresses.end() && it->second != opt_proTx->proTxHash;
14021406
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REGISTRAR) {
14031407
const auto opt_proTx = GetTxPayload<CProUpRegTx>(tx);
14041408
if (!opt_proTx) {
1405-
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx.GetHash().ToString());
1409+
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString());
14061410
return true; // i.e. can't decode payload == conflict
14071411
}
14081412
auto& proTx = *opt_proTx;
@@ -1425,7 +1429,7 @@ bool CTxMemPool::existsProviderTxConflict(const CTransaction &tx) const {
14251429
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REVOKE) {
14261430
const auto opt_proTx = GetTxPayload<CProUpRevTx>(tx);
14271431
if (!opt_proTx) {
1428-
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx.GetHash().ToString());
1432+
LogPrint(BCLog::MEMPOOL, "%s: ERROR: Invalid transaction payload, tx: %s\n", __func__, tx_hash.ToString());
14291433
return true; // i.e. can't decode payload == conflict
14301434
}
14311435
auto& proTx = *opt_proTx;

0 commit comments

Comments
 (0)