Skip to content

Commit

Permalink
Reduce amount selected
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed Dec 13, 2020
1 parent 3751c17 commit b0a01dc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/omnicore/encoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* https://github.com/mastercoin-MSC/spec#class-b-transactions-also-known-as-the-multisig-method
*/
bool OmniCore_Encode_ClassB(const std::string& senderAddress, const CPubKey& redeemingPubKey,
const std::vector<unsigned char>& vchPayload, std::vector<std::pair<CScript, int64_t> >& vecOutputs)
const std::vector<unsigned char>& vchPayload, std::vector<std::pair<CScript, int64_t> >& vecOutputs, CAmount *outputAmount)
{
unsigned int nRemainingBytes = vchPayload.size();
unsigned int nNextByte = 0;
Expand Down Expand Up @@ -65,11 +65,15 @@ bool OmniCore_Encode_ClassB(const std::string& senderAddress, const CPubKey& red

// Push back a bare multisig output with obfuscated data
CScript scriptMultisigOut = GetScriptForMultisig(1, vKeys);
if (outputAmount)
*outputAmount += OmniGetDustThreshold(scriptMultisigOut);
vecOutputs.push_back(std::make_pair(scriptMultisigOut, OmniGetDustThreshold(scriptMultisigOut)));
}

// Add the Exodus marker output
CScript scriptExodusOutput = GetScriptForDestination(ExodusAddress());
if (outputAmount)
*outputAmount += OmniGetDustThreshold(scriptExodusOutput);
vecOutputs.push_back(std::make_pair(scriptExodusOutput, OmniGetDustThreshold(scriptExodusOutput)));
return true;
}
Expand Down
4 changes: 3 additions & 1 deletion src/omnicore/encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class CPubKey;
class CTxOut;

#include <amount.h>
#include <script/script.h>

#include <stdint.h>
Expand All @@ -12,7 +13,8 @@ class CTxOut;
#include <vector>

/** Embeds a payload in obfuscated multisig outputs, and adds an Exodus marker output. */
bool OmniCore_Encode_ClassB(const std::string& senderAddress, const CPubKey& redeemingPubKey, const std::vector<unsigned char>& vchPayload, std::vector<std::pair<CScript, int64_t> >& vecOutputs);
bool OmniCore_Encode_ClassB(const std::string& senderAddress, const CPubKey& redeemingPubKey, const std::vector<unsigned char>& vchPayload,
std::vector<std::pair<CScript, int64_t> >& vecOutputs, CAmount* outputAmount = nullptr);

/** Embeds a payload in an OP_RETURN output, prefixed with a transaction marker. */
bool OmniCore_Encode_ClassC(const std::vector<unsigned char>& vecPayload, std::vector<std::pair<CScript, int64_t> >& vecOutputs);
Expand Down
10 changes: 7 additions & 3 deletions src/omnicore/wallettxbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ int WalletTxBuilder(
// Next, we set the change address to the sender
coinControl.destChange = DecodeDestination(senderAddress);

// Select the inputs
if (0 > mastercore::SelectCoins(*iWallet, senderAddress, coinControl, referenceAmount)) { return MP_INPUTS_INVALID; }
// Amount required for outputs
CAmount outputAmount{0};

// Encode the data outputs
switch(omniTxClass) {
Expand All @@ -77,7 +77,7 @@ int WalletTxBuilder(
if (!AddressToPubKey(iWallet, sAddress, redeemingPubKey)) {
return MP_REDEMP_BAD_VALIDATION;
}
if (!OmniCore_Encode_ClassB(senderAddress,redeemingPubKey,payload,vecSend)) { return MP_ENCODING_ERROR; }
if (!OmniCore_Encode_ClassB(senderAddress, redeemingPubKey, payload, vecSend, &outputAmount)) { return MP_ENCODING_ERROR; }
break; }
case OMNI_CLASS_C:
if(!OmniCore_Encode_ClassC(payload,vecSend)) { return MP_ENCODING_ERROR; }
Expand All @@ -87,9 +87,13 @@ int WalletTxBuilder(
// Then add a paytopubkeyhash output for the recipient (if needed) - note we do this last as we want this to be the highest vout
if (!receiverAddress.empty()) {
CScript scriptPubKey = GetScriptForDestination(DecodeDestination(receiverAddress));
outputAmount += 0 < referenceAmount ? referenceAmount : OmniGetDustThreshold(scriptPubKey);
vecSend.push_back(std::make_pair(scriptPubKey, 0 < referenceAmount ? referenceAmount : OmniGetDustThreshold(scriptPubKey)));
}

// Select the inputs
if (0 > mastercore::SelectCoins(*iWallet, senderAddress, coinControl, outputAmount)) { return MP_INPUTS_INVALID; }

// Now we have what we need to pass to the wallet to create the transaction, perform some checks first

if (!coinControl.HasSelected()) return MP_ERR_INPUTSELECT_FAIL;
Expand Down
4 changes: 2 additions & 2 deletions src/omnicore/walletutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ int64_t SelectCoins(interfaces::Wallet& iWallet, const std::string& fromAddress,
int64_t nTotal = 0;
int nHeight = ::ChainActive().Height();

// select coins to cover up to 20 kB max. transaction size
CAmount nMax = 20 * GetEstimatedFeePerKb(iWallet);
// select coins to cover up to 3 kB max. transaction size
CAmount nMax = 3 * GetEstimatedFeePerKb(iWallet);

// if referenceamount is set it is needed to be accounted for here too
if (0 < additional) nMax += additional;
Expand Down

0 comments on commit b0a01dc

Please sign in to comment.