Skip to content

Commit

Permalink
Unittests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
levonpetrosyan93 committed Dec 18, 2023
1 parent 1f63745 commit d8289be
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/libspark/mint_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ MintTransaction::MintTransaction(
value_statement.emplace_back(this->coins[j].C + this->params->get_G().inverse()*Scalar(this->coins[j].v));
value_witness.emplace_back(SparkUtils::hash_val(k));
} else {
Coin coin;
Coin coin(params);
coin.type = 0;
coin.r_.ciphertext.resize(82); // max possible size
coin.r_.key_commitment.resize(64);
coin.r_.key_commitment.resize(32);
coin.r_.tag.resize(16);
coin.v = 0;
this->coins.emplace_back(coin);
Expand Down
34 changes: 32 additions & 2 deletions src/test/spark_state_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os, const p

} // namespace std

// Generate a random char vector from a random scalar
static std::vector<unsigned char> random_char_vector() {
Scalar temp;
temp.randomize();
std::vector<unsigned char> result;
result.resize(spark::SCALAR_ENCODING);
temp.serialize(result.data());

return result;
}

class SparkStateTests : public SparkTestingSetup
{
Expand Down Expand Up @@ -173,8 +183,28 @@ BOOST_AUTO_TEST_CASE(mempool)
// - can not add on-chain coin
BOOST_CHECK(!sparkState->CanAddMintToMempool(pwalletMain->sparkWallet->getCoinFromMeta(mint)));

// - can not add duplicated coin
spark::Coin randMint;
// Generate keys
const spark::Params* params = spark::Params::get_default();
spark::SpendKey spend_key(params);
spark::FullViewKey full_view_key(spend_key);
spark::IncomingViewKey incoming_view_key(full_view_key);

// Generate address
spark::Address address(incoming_view_key, 1);

// Generate coin
Scalar k;
k.randomize();
spark::Coin randMint = spark::Coin(
params,
spark::COIN_TYPE_MINT,
k,
address,
100,
"memo",
random_char_vector()
);

BOOST_CHECK(sparkState->CanAddMintToMempool(randMint));
sparkState->AddMintsToMempool({randMint});
BOOST_CHECK(!sparkState->CanAddMintToMempool(randMint));
Expand Down
4 changes: 3 additions & 1 deletion src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,9 @@ void CTxMemPool::removeUnchecked(txiter it, MemPoolRemovalReason reason)
{
if (txout.scriptPubKey.IsSparkMint() || txout.scriptPubKey.IsSparkSMint()) {
try {
spark::Coin txCoin;
const spark::Params* params = spark::Params::get_default();

spark::Coin txCoin(params);
spark::ParseSparkMintCoin(txout.scriptPubKey, txCoin);
sparkState.RemoveMintFromMempool(txCoin);
}
Expand Down
4 changes: 3 additions & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3360,7 +3360,9 @@ void static RemoveConflictingPrivacyTransactionsFromMempool(const CBlock &block)

if (txout.scriptPubKey.IsSparkMint() || txout.scriptPubKey.IsSparkSMint()) {
try {
spark::Coin txCoin;
const spark::Params* params = spark::Params::get_default();

spark::Coin txCoin(params);
spark::ParseSparkMintCoin(txout.scriptPubKey, txCoin);
sparkState->RemoveMintFromMempool(txCoin);
} catch (std::invalid_argument&) {
Expand Down

0 comments on commit d8289be

Please sign in to comment.