Skip to content

Commit

Permalink
Port rpc raw transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Mar 11, 2024
1 parent f04a42c commit e23c4d5
Show file tree
Hide file tree
Showing 13 changed files with 587 additions and 51 deletions.
4 changes: 4 additions & 0 deletions src/addresstype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,7 @@ PKHash ExtractPublicKeyHash(const CScript& scriptPubKey, bool* OK)
{
return {};
}

bool IsValidContractSenderAddress(const CTxDestination& dest) {
return {};
}
13 changes: 13 additions & 0 deletions src/addresstype.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
*/
CScript GetScriptForDestination(const CTxDestination& dest);

enum addresstype
{
PUBKEYHASH = 1,
SCRIPTHASH = 2,
WITNESSSCRIPTHASH = 3,
WITNESSPUBKEYHASH = 4,
WITNESSTAPROOT = 5,
NONSTANDARD = 6
};

/** Check whether a CTxDestination can be used as contract sender address. */
bool IsValidContractSenderAddress(const CTxDestination& dest);

PKHash ExtractPublicKeyHash(const CScript& scriptPubKey, bool* OK = nullptr);

#endif // BITCOIN_ADDRESSTYPE_H
5 changes: 0 additions & 5 deletions src/consensus/consensus.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,12 @@

/** The maximum allowed size for a serialized block, in bytes (only for buffer size limits) */
extern unsigned int dgpMaxBlockSerSize;
static const unsigned int MAX_BLOCK_SERIALIZED_SIZE = 4000000;
/** The maximum allowed weight for a block, see BIP 141 (network rule) */
extern unsigned int dgpMaxBlockWeight;

extern unsigned int dgpMaxBlockSize; // qtum

static const unsigned int MAX_BLOCK_WEIGHT = 4000000;
/** The maximum allowed number of signature check operations in a block (network rule) */
static const int64_t MAX_BLOCK_SIGOPS_COST = 80000;
/** Coinbase transaction outputs can only be spent after this number of new blocks (network rule) */
static const int COINBASE_MATURITY = 100;
extern int64_t dgpMaxBlockSigOps;

extern unsigned int dgpMaxProtoMsgLength;
Expand Down
6 changes: 3 additions & 3 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ struct Params {
int64_t nPowTargetTimespan;
int64_t nPowTargetTimespanV2;
int64_t nRBTPowTargetTimespan;
std::chrono::seconds PowTargetSpacing() const
std::chrono::seconds TargetSpacingChrono(int height) const
{
return std::chrono::seconds{nPowTargetSpacing};
return std::chrono::seconds{TargetSpacing(height)};
}
int64_t DifficultyAdjustmentInterval(int height=0) const
int64_t DifficultyAdjustmentInterval(int height) const
{
int64_t targetTimespan = TargetTimespan(height);
int64_t targetSpacing = TargetSpacing(height);
Expand Down
2 changes: 1 addition & 1 deletion src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ bool PeerManagerImpl::TipMayBeStale()

bool PeerManagerImpl::CanDirectFetch()
{
return m_chainman.ActiveChain().Tip()->Time() > GetAdjustedTime() - m_chainparams.GetConsensus().PowTargetSpacing() * 20;
return m_chainman.ActiveChain().Tip()->Time() > GetAdjustedTime() - m_chainparams.GetConsensus().TargetSpacingChrono(m_chainman.ActiveChain().Height()) * 20;
}

static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Expand Down
2 changes: 1 addition & 1 deletion src/node/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ bool BlockAssembler::TestPackage(uint64_t packageSize, int64_t packageSigOpsCost
if (nBlockWeight + WITNESS_SCALE_FACTOR * packageSize >= m_options.nBlockMaxWeight) {
return false;
}
if (nBlockSigOpsCost + packageSigOpsCost >= MAX_BLOCK_SIGOPS_COST) {
if (nBlockSigOpsCost + packageSigOpsCost >= (uint64_t)dgpMaxBlockSigOps) {
return false;
}
return true;
Expand Down
3 changes: 2 additions & 1 deletion src/qt/transactiondesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <policy/policy.h>
#include <validation.h>
#include <wallet/types.h>
#include <consensus/params.h>

#include <stdint.h>
#include <string>
Expand Down Expand Up @@ -333,7 +334,7 @@ QString TransactionDesc::toHTML(interfaces::Node& node, interfaces::Wallet& wall

if (wtx.is_coinbase)
{
quint32 numBlocksToMaturity = COINBASE_MATURITY + 1;
quint32 numBlocksToMaturity = Params().GetConsensus().CoinbaseMaturity(numBlocks) + 1;
strHTML += "<br>" + tr("Generated coins must mature %1 blocks before they can be spent. When you generated this block, it was broadcast to the network to be added to the block chain. If it fails to get into the chain, its state will change to \"not accepted\" and it won't be spendable. This may occasionally happen if another node generates a block within a few seconds of yours.").arg(QString::number(numBlocksToMaturity)) + "<br>";
}

Expand Down
4 changes: 2 additions & 2 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1927,7 +1927,7 @@ static RPCHelpMan getblockstats()
CAmount totalfee = 0;
int64_t inputs = 0;
int64_t maxtxsize = 0;
int64_t mintxsize = MAX_BLOCK_SERIALIZED_SIZE;
int64_t mintxsize = dgpMaxBlockSerSize;
int64_t outputs = 0;
int64_t swtotal_size = 0;
int64_t swtotal_weight = 0;
Expand Down Expand Up @@ -2050,7 +2050,7 @@ static RPCHelpMan getblockstats()
ret_all.pushKV("mediantxsize", CalculateTruncatedMedian(txsize_array));
ret_all.pushKV("minfee", (minfee == MAX_MONEY) ? 0 : minfee);
ret_all.pushKV("minfeerate", (minfeerate == MAX_MONEY) ? 0 : minfeerate);
ret_all.pushKV("mintxsize", mintxsize == MAX_BLOCK_SERIALIZED_SIZE ? 0 : mintxsize);
ret_all.pushKV("mintxsize", mintxsize == dgpMaxBlockSerSize ? 0 : mintxsize);
ret_all.pushKV("outs", outputs);
ret_all.pushKV("subsidy", GetBlockSubsidy(pindex.nHeight, chainman.GetParams().GetConsensus()));
ret_all.pushKV("swtotal_size", swtotal_size);
Expand Down
66 changes: 62 additions & 4 deletions src/rpc/mempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,26 @@ static RPCHelpMan sendrawtransaction()
"Reject transactions with provably unspendable outputs (e.g. 'datacarrier' outputs that use the OP_RETURN opcode) greater than the specified value, expressed in " + CURRENCY_UNIT + ".\n"
"If burning funds through unspendable outputs is desired, increase this value.\n"
"This check is based on heuristics and does not guarantee spendability of outputs.\n"},
{"showcontractdata", RPCArg::Type::BOOL, RPCArg::Default{false}, "Show created contract data, ignored when no contracts created"},
},
RPCResult{
RPCResult::Type::STR_HEX, "", "The transaction hash in hex"
{
RPCResult{
RPCResult::Type::STR_HEX, "", "The transaction hash in hex"
},
RPCResult{"for create contract with showcontractdata = true",
RPCResult::Type::OBJ, "", "",
{
{RPCResult::Type::STR_HEX, "txid", "The transaction hash in hex"},
{RPCResult::Type::ARR, "contracts", "",
{
{RPCResult::Type::OBJ, "", "",
{
{RPCResult::Type::STR_HEX, "address", "The expected contract address"},
{RPCResult::Type::NUM, "index", "The index of the output"},
}}
}},
}
},
},
RPCExamples{
"\nCreate a transaction\n"
Expand All @@ -73,7 +90,8 @@ static RPCHelpMan sendrawtransaction()
}

for (const auto& out : mtx.vout) {
if((out.scriptPubKey.IsUnspendable() || !out.scriptPubKey.HasValidOps()) && out.nValue > max_burn_amount) {
bool isContract = out.scriptPubKey.HasOpCreate() || out.scriptPubKey.HasOpCall();
if(!isContract && (out.scriptPubKey.IsUnspendable() || !out.scriptPubKey.HasValidOps()) && out.nValue > max_burn_amount) {
throw JSONRPCTransactionError(TransactionError::MAX_BURN_EXCEEDED);
}
}
Expand All @@ -95,7 +113,47 @@ static RPCHelpMan sendrawtransaction()
throw JSONRPCTransactionError(err, err_string);
}

return tx->GetHash().GetHex();
std::string txid = tx->GetHash().GetHex();

bool showcontractdata = false;
if (!request.params[3].isNull()) showcontractdata = request.params[3].get_bool();

if(showcontractdata && tx->HasOpCreate()){
uint32_t voutNumber=0;
UniValue result(UniValue::VOBJ);
result.pushKV("txid", txid);

UniValue contracts(UniValue::VARR);
for (const CTxOut& txout : tx->vout) {
if(txout.scriptPubKey.HasOpCreate()){
std::vector<unsigned char> SHA256TxVout(32);
std::vector<unsigned char> contractAddress(20);
std::vector<unsigned char> txIdAndVout(tx->GetHash().begin(), tx->GetHash().end());
std::vector<unsigned char> voutNumberChrs;

if (voutNumberChrs.size() < sizeof(voutNumber))voutNumberChrs.resize(sizeof(voutNumber));
std::memcpy(voutNumberChrs.data(), &voutNumber, sizeof(voutNumber));
txIdAndVout.insert(txIdAndVout.end(),voutNumberChrs.begin(),voutNumberChrs.end());
CSHA256().Write(txIdAndVout.data(), txIdAndVout.size()).Finalize(SHA256TxVout.data());
CRIPEMD160().Write(SHA256TxVout.data(), SHA256TxVout.size()).Finalize(contractAddress.data());

UniValue contract(UniValue::VOBJ);
contract.pushKV("address", HexStr(contractAddress));
contract.pushKV("index", (int64_t)voutNumber);
contracts.push_back(contract);

SHA256TxVout.clear();
contractAddress.clear();
txIdAndVout.clear();
}
voutNumber++;
}
result.pushKV("contracts", contracts);

return result;
}

return txid;
},
};
}
Expand Down
8 changes: 4 additions & 4 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static UniValue GetNetworkHashPS(int lookup, int height, const CChain& active_ch

// If lookup is -1, then use blocks since last difficulty change.
if (lookup <= 0)
lookup = pb->nHeight % Params().GetConsensus().DifficultyAdjustmentInterval() + 1;
lookup = pb->nHeight % Params().GetConsensus().DifficultyAdjustmentInterval(pb->nHeight) + 1;

// If lookup is larger than chain, then set it to chain length.
if (lookup > pb->nHeight)
Expand Down Expand Up @@ -923,8 +923,8 @@ static RPCHelpMan getblocktemplate()
result.pushKV("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1);
result.pushKV("mutable", aMutable);
result.pushKV("noncerange", "00000000ffffffff");
int64_t nSigOpLimit = MAX_BLOCK_SIGOPS_COST;
int64_t nSizeLimit = MAX_BLOCK_SERIALIZED_SIZE;
int64_t nSigOpLimit = dgpMaxBlockSigOps;
int64_t nSizeLimit = dgpMaxBlockSerSize;
if (fPreSegWit) {
CHECK_NONFATAL(nSigOpLimit % WITNESS_SCALE_FACTOR == 0);
nSigOpLimit /= WITNESS_SCALE_FACTOR;
Expand All @@ -934,7 +934,7 @@ static RPCHelpMan getblocktemplate()
result.pushKV("sigoplimit", nSigOpLimit);
result.pushKV("sizelimit", nSizeLimit);
if (!fPreSegWit) {
result.pushKV("weightlimit", (int64_t)MAX_BLOCK_WEIGHT);
result.pushKV("weightlimit", (int64_t)dgpMaxBlockWeight);
}
result.pushKV("curtime", pblock->GetBlockTime());
result.pushKV("bits", strprintf("%08x", pblock->nBits));
Expand Down
Loading

0 comments on commit e23c4d5

Please sign in to comment.