Skip to content

Commit

Permalink
Merge pull request #158 from DeFiCh/getcustomtx-updates
Browse files Browse the repository at this point in the history
Reduce getcustomtx length, rename JSON height and time keys
  • Loading branch information
monstrobishi authored Jan 13, 2021
2 parents 729e94c + eebac3b commit 3639d01
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 75 deletions.
126 changes: 68 additions & 58 deletions src/masternodes/mn_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3984,6 +3984,69 @@ UniValue sendtokenstoaddress(const JSONRPCRequest& request) {

}

static bool GetCustomTXInfo(const int nHeight, const CTransactionRef tx, CustomTxType& guess, Res& res, UniValue& txResults)
{
std::vector<unsigned char> metadata;
guess = GuessCustomTxType(*tx, metadata);
CCustomCSView mnview_dummy(*pcustomcsview);

switch (guess)
{
case CustomTxType::CreateMasternode:
res = ApplyCreateMasternodeTx(mnview_dummy, *tx, nHeight, metadata, &txResults);
break;
case CustomTxType::ResignMasternode:
res = ApplyResignMasternodeTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, true, &txResults);
break;
case CustomTxType::CreateToken:
res = ApplyCreateTokenTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::UpdateToken:
res = ApplyUpdateTokenTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::UpdateTokenAny:
res = ApplyUpdateTokenAnyTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::MintToken:
res = ApplyMintTokenTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::CreatePoolPair:
res = ApplyCreatePoolPairTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::UpdatePoolPair:
res = ApplyUpdatePoolPairTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::PoolSwap:
res = ApplyPoolSwapTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::AddPoolLiquidity:
res = ApplyAddPoolLiquidityTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::RemovePoolLiquidity:
res = ApplyRemovePoolLiquidityTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::UtxosToAccount:
res = ApplyUtxosToAccountTx(mnview_dummy, *tx, nHeight, metadata, Params().GetConsensus(), &txResults);
break;
case CustomTxType::AccountToUtxos:
res = ApplyAccountToUtxosTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::AccountToAccount:
res = ApplyAccountToAccountTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::SetGovVariable:
res = ApplySetGovernanceTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::AnyAccountsToAccounts:
res = ApplyAnyAccountsToAccountsTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
default:
return false;
}

return true;
}

static UniValue getcustomtx(const JSONRPCRequest& request)
{
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
Expand Down Expand Up @@ -4096,63 +4159,10 @@ static UniValue getcustomtx(const JSONRPCRequest& request)
return "Coinbase transaction. Not a custom transaction.";
}

std::vector<unsigned char> metadata;
guess = GuessCustomTxType(*tx, metadata);
CCustomCSView mnview_dummy(*pcustomcsview);

switch (guess)
{
case CustomTxType::CreateMasternode:
res = ApplyCreateMasternodeTx(mnview_dummy, *tx, nHeight, metadata, &txResults);
break;
case CustomTxType::ResignMasternode:
res = ApplyResignMasternodeTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, true, &txResults);
break;
case CustomTxType::CreateToken:
res = ApplyCreateTokenTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::UpdateToken:
res = ApplyUpdateTokenTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::UpdateTokenAny:
res = ApplyUpdateTokenAnyTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::MintToken:
res = ApplyMintTokenTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::CreatePoolPair:
res = ApplyCreatePoolPairTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::UpdatePoolPair:
res = ApplyUpdatePoolPairTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::PoolSwap:
res = ApplyPoolSwapTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::AddPoolLiquidity:
res = ApplyAddPoolLiquidityTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::RemovePoolLiquidity:
res = ApplyRemovePoolLiquidityTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::UtxosToAccount:
res = ApplyUtxosToAccountTx(mnview_dummy, *tx, nHeight, metadata, Params().GetConsensus(), &txResults);
break;
case CustomTxType::AccountToUtxos:
res = ApplyAccountToUtxosTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::AccountToAccount:
res = ApplyAccountToAccountTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::SetGovVariable:
res = ApplySetGovernanceTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
case CustomTxType::AnyAccountsToAccounts:
res = ApplyAnyAccountsToAccountsTx(mnview_dummy, ::ChainstateActive().CoinsTip(), *tx, nHeight, metadata, Params().GetConsensus(), true, &txResults);
break;
default:
return "Not a custom transaction";
if (!GetCustomTXInfo(nHeight, tx, guess, res, txResults)) {
return "Not a custom transaction";
}

} else {
// Should not really get here without prior failure.
return "Could not find matching transaction.";
Expand All @@ -4179,8 +4189,8 @@ static UniValue getcustomtx(const JSONRPCRequest& request)

result.pushKV("blockhash", hashBlock.GetHex());
if (blockindex) {
result.pushKV("block height", blockindex->nHeight);
result.pushKV("blocktime", blockindex->GetBlockTime());
result.pushKV("blockHeight", blockindex->nHeight);
result.pushKV("blockTime", blockindex->GetBlockTime());
result.pushKV("confirmations", 1 + ::ChainActive().Height() - blockindex->nHeight);
} else {
result.pushKV("confirmations", 0);
Expand Down
34 changes: 17 additions & 17 deletions test/functional/rpc_getcustomtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def run_test(self):
assert_equal(result['results']['mintable'], True)
assert_equal(result['results']['tradeable'], True)
assert_equal(result['results']['finalized'], False)
assert_equal(result['block height'], blockheight)
assert_equal(result['blockHeight'], blockheight)
assert_equal(result['blockhash'], blockhash)
assert_equal(result['confirmations'], 1)

Expand Down Expand Up @@ -82,7 +82,7 @@ def run_test(self):
assert_equal(result['type'], "MintToken")
assert_equal(result['valid'], True)
assert_equal(result['results'][str(token_a)], Decimal("300.00000000"))
assert_equal(result['block height'], blockheight)
assert_equal(result['blockHeight'], blockheight)
assert_equal(result['blockhash'], blockhash)
assert_equal(result['confirmations'], 1)

Expand All @@ -91,7 +91,7 @@ def run_test(self):
assert_equal(result['type'], "MintToken")
assert_equal(result['valid'], True)
assert_equal(result['results'][str(token_a)], Decimal("300.00000000"))
assert_equal(result['block height'], blockheight)
assert_equal(result['blockHeight'], blockheight)
assert_equal(result['blockhash'], blockhash)
assert_equal(result['confirmations'], 1)

Expand All @@ -108,7 +108,7 @@ def run_test(self):
assert_equal(result['type'], "MintToken")
assert_equal(result['valid'], True)
assert_equal(result['results'][str(token_a)], Decimal("300.00000000"))
assert_equal(result['block height'], blockheight)
assert_equal(result['blockHeight'], blockheight)
assert_equal(result['blockhash'], blockhash)
assert_equal(result['confirmations'], 1)

Expand All @@ -135,7 +135,7 @@ def run_test(self):
assert_equal(result['results']['name'], "silver")
assert_equal(result['results']['mintable'], False)
assert_equal(result['results']['tradeable'], False)
assert_equal(result['block height'], blockheight)
assert_equal(result['blockHeight'], blockheight)
assert_equal(result['blockhash'], blockhash)
assert_equal(result['confirmations'], 1)

Expand All @@ -157,7 +157,7 @@ def run_test(self):
assert_equal(result['type'], "CreateMasternode")
assert_equal(result['valid'], True)
assert_equal(result['results']['masternodeoperator'], collateral)
assert_equal(result['block height'], blockheight)
assert_equal(result['blockHeight'], blockheight)
assert_equal(result['blockhash'], blockhash)
assert_equal(result['confirmations'], 1)

Expand All @@ -177,7 +177,7 @@ def run_test(self):
assert_equal(result['type'], "ResignMasternode")
assert_equal(result['valid'], True)
assert_equal(result['results']['id'], mn_txid)
assert_equal(result['block height'], blockheight)
assert_equal(result['blockHeight'], blockheight)
assert_equal(result['blockhash'], blockhash)
assert_equal(result['confirmations'], 1)

Expand Down Expand Up @@ -241,7 +241,7 @@ def run_test(self):
assert_equal(result['results']['mineable'], False)
assert_equal(result['results']['tradeable'], True)
assert_equal(result['results']['finalized'], True)
assert_equal(result['block height'], blockheight)
assert_equal(result['blockHeight'], blockheight)
assert_equal(result['blockhash'], blockhash)
assert_equal(result['confirmations'], 1)

Expand All @@ -262,7 +262,7 @@ def run_test(self):
assert_equal(result['results']['from'], address_b_scriptpubkey)
assert_equal([*result['results']['to']][0], address_a_scriptpubkey)
assert_equal(list(result['results']['to'].values())[0], "100.00000000@" + token_b)
assert_equal(result['block height'], blockheight)
assert_equal(result['blockHeight'], blockheight)
assert_equal(result['blockhash'], blockhash)
assert_equal(result['confirmations'], 1)

Expand All @@ -285,7 +285,7 @@ def run_test(self):
assert_equal(result['results'][token_a], Decimal("100.00000000"))
assert_equal(result['results'][token_b], Decimal("100.00000000"))
assert_equal(result['results']['shareaddress'], pool_share_scriptpubkey)
assert_equal(result['block height'], blockheight)
assert_equal(result['blockHeight'], blockheight)
assert_equal(result['blockhash'], blockhash)
assert_equal(result['confirmations'], 1)

Expand Down Expand Up @@ -314,7 +314,7 @@ def run_test(self):
assert_equal(result['results']['toAddress'], address_b_scriptpubkey)
assert_equal(result['results']['toToken'], token_b)
assert_equal(result['results']['maxPrice'], Decimal("2.00000000"))
assert_equal(result['block height'], blockheight)
assert_equal(result['blockHeight'], blockheight)
assert_equal(result['blockhash'], blockhash)
assert_equal(result['confirmations'], 1)

Expand All @@ -332,7 +332,7 @@ def run_test(self):
assert_equal(result['valid'], True)
assert_equal(result['results']['from'], pool_share_scriptpubkey)
assert_equal(result['results']['amount'], "25.00000000@1")
assert_equal(result['block height'], blockheight)
assert_equal(result['blockHeight'], blockheight)
assert_equal(result['blockhash'], blockhash)
assert_equal(result['confirmations'], 1)

Expand All @@ -356,7 +356,7 @@ def run_test(self):
assert_equal(result['results']['commission'], Decimal("0.10000000"))
assert_equal(result['results']['status'], False)
assert_equal(result['results']['ownerAddress'], owner_scriptpubkey)
assert_equal(result['block height'], blockheight)
assert_equal(result['blockHeight'], blockheight)
assert_equal(result['blockhash'], blockhash)
assert_equal(result['confirmations'], 1)

Expand All @@ -373,7 +373,7 @@ def run_test(self):
assert_equal(result['type'], "UtxosToAccount")
assert_equal(result['valid'], True)
assert_equal(result['results'][address_a_scriptpubkey], "1.00000000@0")
assert_equal(result['block height'], blockheight)
assert_equal(result['blockHeight'], blockheight)
assert_equal(result['blockhash'], blockhash)
assert_equal(result['confirmations'], 1)

Expand All @@ -392,7 +392,7 @@ def run_test(self):
assert_equal(result['results']['from'], address_a_scriptpubkey)
assert_equal(list(result['results']['to'].keys())[0], address_b_scriptpubkey)
assert_equal(list(result['results']['to'].values())[0], "1.00000000@0")
assert_equal(result['block height'], blockheight)
assert_equal(result['blockHeight'], blockheight)
assert_equal(result['blockhash'], blockhash)
assert_equal(result['confirmations'], 1)

Expand All @@ -415,7 +415,7 @@ def run_test(self):
assert_equal(list(result['results']['from'].values())[0], "1.00000000@0")
assert_equal(list(result['results']['to'].keys())[0], address_b_scriptpubkey)
assert_equal(list(result['results']['to'].values())[0], "1.00000000@0")
assert_equal(result['block height'], blockheight)
assert_equal(result['blockHeight'], blockheight)
assert_equal(result['blockhash'], blockhash)
assert_equal(result['confirmations'], 1)

Expand All @@ -433,7 +433,7 @@ def run_test(self):
assert_equal(result['valid'], True)
assert_equal(list(result['results'].keys())[0], "LP_DAILY_DFI_REWARD")
assert_equal(list(result['results'].values())[0], Decimal("35.00000000"))
assert_equal(result['block height'], blockheight)
assert_equal(result['blockHeight'], blockheight)
assert_equal(result['blockhash'], blockhash)
assert_equal(result['confirmations'], 1)

Expand Down

0 comments on commit 3639d01

Please sign in to comment.