Skip to content

Commit

Permalink
Manually set TransactionBuilder transaction height and minor code cle…
Browse files Browse the repository at this point in the history
…anup of z_createdbuildinstuctions and z_buildrawtransaction
  • Loading branch information
CryptoForge committed Jun 4, 2022
1 parent 01deb10 commit 0c1fa13
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
16 changes: 9 additions & 7 deletions src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,8 @@ UniValue z_buildrawtransaction(const UniValue& params, bool fHelp, const CPubKey
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
}

tb.SetHeight(Params().GetConsensus(), chainActive.Tip()->GetHeight());

libzcash::SaplingExtendedSpendingKey primaryKey;
for (int i = 0; i < tb.rawSpends.size(); i++) {
SaplingOutPoint op = tb.rawSpends[i].op;
Expand Down Expand Up @@ -1598,7 +1600,7 @@ UniValue z_createbuildinstructions(const UniValue& params, bool fHelp, const CPu
" {\n"
" \"address\":address (string, required) Pirate zaddr\n"
" \"amount\":amount (numeric, required) The numeric amount in ARRR\n"
" \"memo\": \"string\" (string, optional) String memo in UTF8 ro Hexidecimal format\n"
" \"memo\": \"string\" (string, optional) String memo in UTF8 or Hexidecimal format\n"
" ,...\n"
" }\n"
" ]\n"
Expand All @@ -1608,7 +1610,7 @@ UniValue z_createbuildinstructions(const UniValue& params, bool fHelp, const CPu
"\"transaction\" (string) hex string of the transaction\n"


+ HelpExampleCli("z_createbuildinstructions", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"index\\\":0\\\"type\\\":\\\"sapling\\\"},...]\"")
+ HelpExampleCli("z_createbuildinstructions", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"index\\\":0},...]\" \"[{\\\"address\\\":\\\"sendtoaddress\\\",\\\"amount\\\":1.0000,\\\"memo\\\":\\\"memostring\\\"},...]\" 0.0001 200")
);

LOCK(cs_main);
Expand All @@ -1620,8 +1622,8 @@ UniValue z_createbuildinstructions(const UniValue& params, bool fHelp, const CPu
UniValue outputs = params[1].get_array();

CAmount total = 0;
TransactionBuilder tx = TransactionBuilder(Params().GetConsensus(), chainActive.Tip()->GetHeight() + 1, pwalletMain);

int nHeight = chainActive.Tip()->GetHeight();
TransactionBuilder tx = TransactionBuilder(Params().GetConsensus(), nHeight + 1, pwalletMain);

CAmount nFee = 10000;
if (!params[2].isNull()) {
Expand All @@ -1634,11 +1636,11 @@ UniValue z_createbuildinstructions(const UniValue& params, bool fHelp, const CPu
total -= nFee;

if (!params[3].isNull()) {
int nHeight = params[3].get_int();
if (nHeight < 0)
int expHeight = params[3].get_int();
if (expHeight < 0)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expiry height must be positive");

tx.SetExpiryHeight(nHeight);
tx.SetExpiryHeight(nHeight + expHeight);
}

for (size_t idx = 0; idx < inputs.size(); idx++) {
Expand Down
12 changes: 9 additions & 3 deletions src/transaction_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ bool TransactionBuilder::AddSaplingSpend(
memcpy (&sWitness.cArray[0], reinterpret_cast<unsigned char*>(local_witness.data()), sizeof(sWitness.cArray) );
asWitness.emplace_back(sWitness);

alWitnessPosition.emplace_back( witness.position() );
alWitnessPosition.emplace_back(witness.position());

mtx.valueBalance += note.value();

Expand Down Expand Up @@ -401,9 +401,15 @@ void TransactionBuilder::SetMinConfirmations(int iMinConf)
this->iMinConf=iMinConf;
}

void TransactionBuilder::SetExpiryHeight(int nHeight)
void TransactionBuilder::SetHeight(const Consensus::Params& consensusParams, int nHeight)
{
this->mtx.nExpiryHeight = nHeight;
this->nHeight = nHeight;
consensusBranchId = CurrentEpochBranchId(nHeight, consensusParams);
}

void TransactionBuilder::SetExpiryHeight(int expHeight)
{
this->mtx.nExpiryHeight = expHeight;
}

void TransactionBuilder::SendChangeTo(libzcash::SaplingPaymentAddress changeAddr, uint256 ovk)
Expand Down
3 changes: 2 additions & 1 deletion src/transaction_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ class TransactionBuilder
void SetFee(CAmount fee);
void SetMinConfirmations(int iMinConf);

void SetExpiryHeight(int nHeight);
void SetHeight(const Consensus::Params& consensusParams, int nHeight);
void SetExpiryHeight(int expHeight);

CTransaction getTransaction() {return mtx;}

Expand Down

0 comments on commit 0c1fa13

Please sign in to comment.