Skip to content

Commit

Permalink
Spark IS fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
levonpetrosyan93 committed Aug 20, 2023
1 parent b0bea3b commit 39510e3
Showing 1 changed file with 58 additions and 11 deletions.
69 changes: 58 additions & 11 deletions src/llmq/quorums_instantsend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ uint256 CInstantSendLock::GetRequestId() const
namespace isutils
{
static int16_t const INSTANTSEND_ADAPTED_TX = std::numeric_limits<int16_t>::min();
CTransaction AdaptJsplitTx(CTransaction const & tx);
CTransaction AdaptPrivateTx(CTransaction const & tx);
}


Expand Down Expand Up @@ -491,12 +491,22 @@ bool CInstantSendManager::CheckCanLock(const CTransaction& tx, bool printDebug,
}

if (tx.nType == isutils::INSTANTSEND_ADAPTED_TX ) {
for (CTxIn const & in : tx.vin) {
Scalar serial;
serial.deserialize(&in.scriptSig.front());
LOCK(cs_main);
if (lelantus::CLelantusState::GetState()->IsUsedCoinSerial(serial))
return false;
if (tx.IsLelantusJoinSplit()) {
for (CTxIn const & in : tx.vin) {
Scalar serial;
serial.deserialize(&in.scriptSig.front());
LOCK(cs_main);
if (lelantus::CLelantusState::GetState()->IsUsedCoinSerial(serial))
return false;
}
} else if (tx.IsSparkSpend()) {
for (CTxIn const & in : tx.vin) {
GroupElement lTag;
lTag.deserialize(&in.scriptSig.front());
LOCK(cs_main);
if (spark::CSparkState::GetState()->IsUsedLTag(lTag))
return false;
}
}
return true;
}
Expand Down Expand Up @@ -607,8 +617,8 @@ void CInstantSendManager::HandleNewInputLockRecoveredSig(const CRecoveredSig& re
return;
}

if(tx && tx->IsLelantusJoinSplit()) {
tx = MakeTransactionRef(isutils::AdaptJsplitTx(*tx));
if (tx && (tx->IsLelantusJoinSplit() || tx->IsSparkSpend())) {
tx = MakeTransactionRef(isutils::AdaptPrivateTx(*tx));
}

if (LogAcceptCategory("instantsend")) {
Expand Down Expand Up @@ -1013,7 +1023,7 @@ void CInstantSendManager::SyncTransaction(const CTransaction& tx_, const CBlockI
return;
}

CTransaction const & tx{tx_.IsLelantusJoinSplit() ? isutils::AdaptJsplitTx(tx_) : tx_};
CTransaction const & tx{(tx_.IsLelantusJoinSplit() || tx_.IsSparkSpend()) ? isutils::AdaptPrivateTx(tx_) : tx_};

bool inMempool = mempool.get(tx.GetHash()) != nullptr;
bool isDisconnect = pindex && posInBlock == CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK;
Expand Down Expand Up @@ -1497,7 +1507,7 @@ CInstantSendLockPtr CInstantSendManager::GetConflictingLock(const CTransaction&
return nullptr;
}

CTransaction const & tx{tx_.IsLelantusJoinSplit() ? isutils::AdaptJsplitTx(tx_) : tx_};
CTransaction const & tx{(tx_.IsLelantusJoinSplit() || tx_.IsSparkSpend()) ? isutils::AdaptPrivateTx(tx_) : tx_};

LOCK(cs);
for (const auto& in : tx.vin) {
Expand Down Expand Up @@ -1567,6 +1577,43 @@ CTransaction AdaptJsplitTx(CTransaction const & tx)
assert(result.GetHash() == tx.GetHash());
return result;
}

CTransaction AdaptSparkTx(CTransaction const & tx)
{
static size_t const lTagSerialSize = 34;

CTransaction result{tx};
std::unique_ptr<spark::SpendTransaction> spend;
try {
spend = std::make_unique<spark::SpendTransaction>(spark::ParseSparkSpend(tx));
}
catch (...) {
return result;
}

const_cast<std::vector<CTxIn>*>(&result.vin)->clear(); //This const_cast was done intentionally as the current design allows for this way only
for (GroupElement const & lTag : spend->getUsedLTags()) {
CTxIn newin;
newin.scriptSig.resize(lTagSerialSize);
lTag.serialize(&newin.scriptSig.front());
newin.prevout.hash = primitives::GetLTagHash(lTag);
newin.prevout.n = 0;
const_cast<std::vector<CTxIn>*>(&result.vin)->push_back(newin);
}
*const_cast<int16_t*>(&result.nType) = INSTANTSEND_ADAPTED_TX;
assert(result.GetHash() == tx.GetHash());
return result;
}

CTransaction AdaptPrivateTx(CTransaction const & tx)
{
if (tx.IsLelantusJoinSplit()) {
return AdaptJsplitTx(tx);
} else if (tx.IsSparkSpend()) {
return AdaptSparkTx(tx);
}
return tx;
}
}

}
Expand Down

0 comments on commit 39510e3

Please sign in to comment.