Skip to content

Commit

Permalink
Review comments applied, and more
Browse files Browse the repository at this point in the history
  • Loading branch information
levonpetrosyan93 committed Nov 25, 2024
1 parent 484e80b commit 2738e20
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 28 deletions.
24 changes: 18 additions & 6 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,24 @@ class CDiskBlockIndex : public CBlockIndex
if (nHeight >=params.nSparkCoinbase) {
READWRITE(sparkMintedCoins);
} else {
std::map<int, std::vector<spark::Coin>> sparkCoins;
READWRITE(sparkCoins);
for (auto& itr : sparkCoins) {
sparkMintedCoins[itr.first].reserve(itr.second.size());
for (auto& mint : itr.second)
sparkMintedCoins[itr.first].emplace_back(std::make_pair(mint, false));

if (ser_action.ForRead())
{
std::map<int, std::vector<spark::Coin>> sparkCoins;
READWRITE(sparkCoins);
for (auto& itr : sparkCoins) {
sparkMintedCoins[itr.first].reserve(itr.second.size());
for (auto& mint : itr.second)
sparkMintedCoins[itr.first].emplace_back(std::make_pair(mint, false));
}
} else {
std::map<int, std::vector<spark::Coin>> sparkCoins;
for (auto& itr : sparkMintedCoins) {
sparkCoins[itr.first].reserve(itr.second.size());
for (auto& mint : itr.second)
sparkCoins[itr.first].emplace_back(mint.first);
}
READWRITE(sparkCoins);
}
}
READWRITE(sparkSetHash);
Expand Down
4 changes: 2 additions & 2 deletions src/evo/deterministicmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ std::string CDeterministicMNState::ToString() const
} else {
std::string strScriptPayout = spark::ToStringSparkAddress(scriptPayout);
if (!strScriptPayout.empty())
payoutAddress = strScriptPayout;
payoutAddress = std::move(strScriptPayout);
}

if (ExtractDestination(scriptOperatorPayout, dest)) {
operatorPayoutAddress = CBitcoinAddress(dest).ToString();
} else {
std::string strScriptPayout = spark::ToStringSparkAddress(scriptOperatorPayout);
if (!strScriptPayout.empty())
operatorPayoutAddress = strScriptPayout;
operatorPayoutAddress = std::move(strScriptPayout);
}

return strprintf("CDeterministicMNState(nRegisteredHeight=%d, nLastPaidHeight=%d, nPoSePenalty=%d, nPoSeRevivedHeight=%d, nPoSeBanHeight=%d, nRevocationReason=%d, "
Expand Down
4 changes: 4 additions & 0 deletions src/libspark/coin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ void Coin::setParams(const Params* params) {
}

bool Coin::isValidMNPayment(const spark::Address& addr, const std::vector<unsigned char>& serialContext) const {
if (this->type != COIN_TYPE_COINBASE) {
return false;
}

Coin c(this->params, COIN_TYPE_COINBASE, k, addr, v, "BlockReward", serial_context);
return this->getHash() == c.getHash();
}
Expand Down
2 changes: 1 addition & 1 deletion src/libspark/keys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ unsigned char Address::decode(const std::string& str) {
return network;
}

std::vector<unsigned char> Address::toByteVector(const unsigned char network) const {
std::vector<unsigned char> Address::toByteVector(unsigned char network) const {
std::string strAddr = encode(network);
return std::vector<unsigned char>(strAddr.begin(), strAddr.end());
}
Expand Down
2 changes: 1 addition & 1 deletion src/libspark/keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Address {
std::string encode(const unsigned char network) const;
unsigned char decode(const std::string& str);

std::vector<unsigned char> toByteVector(const unsigned char network) const;
std::vector<unsigned char> toByteVector(unsigned char network) const;
unsigned char fromByteVector(const std::vector<unsigned char>& vch);

private:
Expand Down
1 change: 1 addition & 0 deletions src/libspark/test/mint_transaction_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ BOOST_AUTO_TEST_CASE(generate_verify)
output.address = Address(incoming_view_key, 12345 + j);
output.v = 678 + j;
output.memo = "Spam and eggs";
output.type = 0;

outputs.emplace_back(output);
}
Expand Down
3 changes: 2 additions & 1 deletion src/masternode-payments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ std::string GetRequiredPaymentsString(int nBlockHeight, const CDeterministicMNCP
std::string strScriptPayout = spark::ToStringSparkAddress(payee->pdmnState->scriptPayout);
if (!strScriptPayout.empty())
strPayee = strScriptPayout;
assert(false);
else
assert(false);
} else {
strPayee = CBitcoinAddress(dest).ToString();
}
Expand Down
2 changes: 1 addition & 1 deletion src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
try {
mintedCoinData.address.fromByteVector(vch);
} catch (const std::exception &) {
throw std::runtime_error(strprintf("Invalid Spark address"));
throw std::runtime_error("Invalid Spark address");
}
mintedCoinData.memo = "BlockReward";
spark_outputs.push_back(mintedCoinData);
Expand Down
10 changes: 7 additions & 3 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -843,9 +843,13 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
if (spark::IsPayToSparkAddress(txout.scriptPubKey, sparkAddr)) {
strAddr = sparkAddr.encode(network);
} else {
ExtractDestination(txout.scriptPubKey, address1);
CBitcoinAddress address2(address1);
strAddr = address2.ToString();
if (ExtractDestination(txout.scriptPubKey, address1)) {
CBitcoinAddress address2(address1);
strAddr = address2.ToString();
} else {
// Handle the error case appropriately
strAddr = "Unknown";
}
}

UniValue obj(UniValue::VOBJ);
Expand Down
18 changes: 11 additions & 7 deletions src/rpc/rpcevo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ spark::Address parseSparkAddress(const std::string strAddr) {
LOCK(cs_main);
nTxHeight = chainActive.Height();
}
if (nTxHeight > ::Params().GetConsensus().nSparkCoinbase)
if (nTxHeight < ::Params().GetConsensus().nSparkCoinbase)
throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Spark Coinbase is not active"));

return sPayoutAddress;
Expand Down Expand Up @@ -495,7 +495,7 @@ UniValue protx_register(const JSONRPCRequest& request)
parseSparkAddress(request.params[paramIdx + 5].get_str());
isSparkAddress = true;
} catch (const std::invalid_argument &) {
//contiune
//continue
}
CBitcoinAddress payoutAddress(request.params[paramIdx + 5].get_str());
if (!isSparkAddress && !payoutAddress.IsValid()) {
Expand Down Expand Up @@ -673,14 +673,13 @@ UniValue protx_update_service(const JSONRPCRequest& request)
if (request.params[4].get_str().empty()) {
ptx.scriptOperatorPayout = dmn->pdmnState->scriptOperatorPayout;
} else {
bool isSparkAddress = false;
const spark::Params* params = spark::Params::get_default();
spark::Address sPayoutAddress(params);
try {
parseSparkAddress(request.params[4].get_str());
isSparkAddress = true;
} catch (const std::invalid_argument &) {
//contiune
//continue
}
CBitcoinAddress payoutAddress(request.params[4].get_str());
if (!isSparkAddress && !payoutAddress.IsValid()) {
Expand Down Expand Up @@ -784,7 +783,7 @@ UniValue protx_update_registrar(const JSONRPCRequest& request)
parseSparkAddress(request.params[4].get_str());
isSparkAddress = true;
} catch (const std::invalid_argument &) {
//contiune
//continue
}

CBitcoinAddress payoutAddress(request.params[4].get_str());
Expand Down Expand Up @@ -886,7 +885,7 @@ UniValue protx_revoke(const JSONRPCRequest& request)
tx.nType = TRANSACTION_PROVIDER_UPDATE_REVOKE;

if (request.params.size() <= 4 && !spark::IsPayToSparkAddress(dmn->pdmnState->scriptOperatorPayout) && !spark::IsPayToSparkAddress(dmn->pdmnState->scriptPayout)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("You need to provide fee source address, payout and operator payout addresses are spark."));
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("You need to provide fee source address,in case payout and operator payout addresses are spark."));
}

if (request.params.size() > 4) {
Expand Down Expand Up @@ -963,7 +962,12 @@ static bool CheckWalletOwnsScript(CWallet* pwallet, const CScript& script) {
if (!pwallet->sparkWallet) {
return false;
}
if (pwallet->sparkWallet->isAddressMine(std::string(script.begin() + 2, script.end() - 1)))
const spark::Params* params = spark::Params::get_default();
spark::Address addr(params);
if (!spark::IsPayToSparkAddress(script, addr))
return false;

if (pwallet->sparkWallet->isAddressMine(addr))
return true;
return false;
#endif
Expand Down
8 changes: 7 additions & 1 deletion src/spark/sparkwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

const uint32_t DEFAULT_SPARK_NCOUNT = 1;

CSparkWallet::CSparkWallet(const std::string& strWalletFile, const uint32_t& height) {
CSparkWallet::CSparkWallet(const std::string& strWalletFile, uint32_t height) {

CWalletDB walletdb(strWalletFile);
this->strWalletFile = strWalletFile;
Expand Down Expand Up @@ -278,6 +278,12 @@ bool CSparkWallet::isAddressMine(const std::string& encodedAddr) {
return false;
}



return isAddressMine(address);
}

bool CSparkWallet::isAddressMine(const spark::Address& address) {
for (const auto& itr : addresses) {
if (itr.second.get_Q1() == address.get_Q1() && itr.second.get_Q2() == address.get_Q2())
return true;
Expand Down
3 changes: 2 additions & 1 deletion src/spark/sparkwallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const uint32_t SPARK_CHANGE_D = 0x270F;

class CSparkWallet {
public:
CSparkWallet(const std::string& strWalletFile, const uint32_t& height);
CSparkWallet(const std::string& strWalletFile, uint32_t height);
~CSparkWallet();
// increment diversifier and generate address for that
spark::Address generateNextAddress();
Expand All @@ -44,6 +44,7 @@ class CSparkWallet {
// get address for a diversifier
spark::Address getAddress(const int32_t& i);
bool isAddressMine(const std::string& encodedAddr);
bool isAddressMine(const spark::Address& address);
bool isChangeAddress(const uint64_t& i) const;

// list spark mint, mint metadata in memory and in db should be the same at this moment, so get from memory
Expand Down
8 changes: 5 additions & 3 deletions src/spark/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ bool IsPayToSparkAddress(const CScript& script)
}

bool IsPayToSparkAddress(const CScript& script, spark::Address& addr)
{ if (script.empty() || script[script.size()-1] != OP_SPARKMINT)
{ if (script.empty() || script.back() != OP_SPARKMINT || script.size() < 3)
return false;
unsigned char network = spark::GetNetworkType();
unsigned char coinNetwork;
Expand All @@ -102,7 +102,7 @@ bool IsPayToSparkAddress(const CScript& script, spark::Address& addr)
return network == coinNetwork;
}

std::string ToStringSparkAddress(const CScript script) {
std::string ToStringSparkAddress(const CScript& script) {
if (script.empty())
return "";

Expand Down Expand Up @@ -921,8 +921,10 @@ std::vector<unsigned char> getSerialContext(const CTransaction &tx) {
return std::vector<unsigned char>();

int height = sparkState.GetMintedCoinHeightAndId(coins[0]).first;
if (height <= 0)
return std::vector<unsigned char>();
// get the previous block
CBlockIndex *mintBlock = chainActive[height - 1];
const CBlockIndex *mintBlock = chainActive[height - 1];
serialContextStream << *mintBlock->phashBlock;
} else {
for (auto input: tx.vin) {
Expand Down
2 changes: 1 addition & 1 deletion src/spark/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ bool IsSparkAllowed(int height);
unsigned char GetNetworkType();
bool IsPayToSparkAddress(const CScript& script);
bool IsPayToSparkAddress(const CScript& script, spark::Address& addr);
std::string ToStringSparkAddress(const CScript script);
std::string ToStringSparkAddress(const CScript& script);


// Pass Scripts form mint transaction and get spark MintTransaction object
Expand Down
1 change: 1 addition & 0 deletions src/test/spark_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ BOOST_AUTO_TEST_CASE(parse_spark_mintscript)
mintedCoin.address = address;
mintedCoin.v = v;
mintedCoin.memo = memo;
mintedCoin.type = 0;

std::vector<MintedCoinData> outputs;
outputs.push_back(mintedCoin);
Expand Down
1 change: 1 addition & 0 deletions src/wallet/test/spark_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ BOOST_AUTO_TEST_CASE(create_mint_recipient)
data.address = sparkAddress;
data.v = v;
data.memo = "Test memo";
data.type = 0;

std::vector<spark::MintedCoinData> mintedCoins;
mintedCoins.push_back(data);
Expand Down

0 comments on commit 2738e20

Please sign in to comment.