Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "TX version and expiration" #1390

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions src/consensus/tx_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <consensus/tx_check.h>

#include <clientversion.h>
#include <primitives/transaction.h>
#include <consensus/validation.h>

Expand Down Expand Up @@ -69,8 +68,7 @@ bool CheckTransaction(const CTransaction& tx, CValidationState &state, bool fChe
bool ParseScriptByMarker(CScript const & script,
const std::vector<unsigned char> & marker,
std::vector<unsigned char> & metadata,
uint8_t& hasAdditionalOpcodes,
CExpirationAndVersion* customTxParams)
bool& hasAdditionalOpcodes)
{
opcodetype opcode;
auto pc = script.begin();
Expand All @@ -85,20 +83,8 @@ bool ParseScriptByMarker(CScript const & script,
}

// Check that no more opcodes are found in the script
std::vector<unsigned char> expirationAndVersion;
if (script.GetOp(pc, opcode, expirationAndVersion)) {
hasAdditionalOpcodes |= HasForks::FortCanning;
if (expirationAndVersion.size() == sizeof(uint32_t) + sizeof(uint8_t)) {
if (customTxParams) {
VectorReader stream(SER_DISK, CLIENT_VERSION, expirationAndVersion, 0);
stream >> *customTxParams;
}
} else {
hasAdditionalOpcodes |= HasForks::GreatWorld;
}
if (pc != script.end()) {
hasAdditionalOpcodes |= HasForks::GreatWorld;
}
if (script.GetOp(pc, opcode)) {
hasAdditionalOpcodes = true;
}

metadata.erase(metadata.begin(), metadata.begin() + marker.size());
Expand All @@ -110,23 +96,22 @@ bool IsAnchorRewardTx(CTransaction const & tx, std::vector<unsigned char> & meta
if (!tx.IsCoinBase() || tx.vout.size() != 2 || tx.vout[0].nValue != 0) {
return false;
}
uint8_t hasAdditionalOpcodes{HasForks::None};
bool hasAdditionalOpcodes{false};
const auto result = ParseScriptByMarker(tx.vout[0].scriptPubKey, DfAnchorFinalizeTxMarker, metadata, hasAdditionalOpcodes);
if (fortCanning && hasAdditionalOpcodes) {
return false;
}
return result;
}

bool IsAnchorRewardTxPlus(CTransaction const & tx, std::vector<unsigned char> & metadata, uint8_t hasForks)
bool IsAnchorRewardTxPlus(CTransaction const & tx, std::vector<unsigned char> & metadata, bool fortCanning)
{
if (!tx.IsCoinBase() || tx.vout.size() != 2 || tx.vout[0].nValue != 0) {
return false;
}
uint8_t hasAdditionalOpcodes{HasForks::None};
bool hasAdditionalOpcodes{false};
const auto result = ParseScriptByMarker(tx.vout[0].scriptPubKey, DfAnchorFinalizeTxMarkerPlus, metadata, hasAdditionalOpcodes);
if ((hasForks & HasForks::FortCanning && !(hasForks & HasForks::GreatWorld) && hasAdditionalOpcodes & HasForks::FortCanning) ||
(hasForks & HasForks::GreatWorld && hasAdditionalOpcodes & HasForks::GreatWorld)) {
if (fortCanning && hasAdditionalOpcodes) {
return false;
}
return result;
Expand All @@ -140,7 +125,7 @@ bool IsTokenSplitTx(CTransaction const & tx, std::vector<unsigned char> & metada
if (!tx.IsCoinBase() || tx.vout.size() != 1 || tx.vout[0].nValue != 0) {
return false;
}
uint8_t hasAdditionalOpcodes{HasForks::None};
bool hasAdditionalOpcodes{false};
const auto result = ParseScriptByMarker(tx.vout[0].scriptPubKey, DfTokenSplitMarker, metadata, hasAdditionalOpcodes);
if (hasAdditionalOpcodes) {
return false;
Expand Down
13 changes: 2 additions & 11 deletions src/consensus/tx_check.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,25 @@
* belongs in tx_verify.h/cpp instead.
*/

#include <cstdint>
#include <vector>

extern const std::vector<unsigned char> DfTxMarker;
extern const std::vector<unsigned char> DfAnchorFinalizeTxMarker;
extern const std::vector<unsigned char> DfAnchorFinalizeTxMarkerPlus;
extern const std::vector<unsigned char> DfTokenSplitMarker;

struct CExpirationAndVersion;
class CScript;
class CTransaction;
class CValidationState;

enum HasForks : uint8_t {
None = 0,
FortCanning = 1 << 0,
GreatWorld = 1 << 1,
};

bool CheckTransaction(const CTransaction& tx, CValidationState& state, bool fCheckDuplicateInputs=true);

bool ParseScriptByMarker(CScript const & script,
const std::vector<unsigned char> & marker,
std::vector<unsigned char> & metadata,
uint8_t& hasAdditionalOpcodes,
CExpirationAndVersion* customTxParams = nullptr);
bool& hasAdditionalOpcodes);
bool IsAnchorRewardTx(CTransaction const & tx, std::vector<unsigned char> & metadata, bool fortCanning = false);
bool IsAnchorRewardTxPlus(CTransaction const & tx, std::vector<unsigned char> & metadata, uint8_t hasForks = HasForks::None);
bool IsAnchorRewardTxPlus(CTransaction const & tx, std::vector<unsigned char> & metadata, bool fortCanning = false);
bool IsTokenSplitTx(CTransaction const & tx, std::vector<unsigned char> & metadata, bool fortCanningCrunch = true);

#endif // DEFI_CONSENSUS_TX_CHECK_H
4 changes: 0 additions & 4 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,6 @@ void SetupServerArgs()
gArgs.AddArg("-dbbatchsize", strprintf("Maximum database write batch size in bytes (default: %u)", nDefaultDbBatchSize), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::OPTIONS);
gArgs.AddArg("-dbcache=<n>", strprintf("Maximum database cache size <n> MiB (%d to %d, default: %d). In addition, unused mempool memory is shared for this cache (see -maxmempool).", nMinDbCache, nMaxDbCache, nDefaultDbCache), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
gArgs.AddArg("-debuglogfile=<file>", strprintf("Specify location of debug log file. Relative paths will be prefixed by a net-specific datadir location. (-nodebuglogfile to disable; default: %s)", DEFAULT_DEBUGLOGFILE), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
gArgs.AddArg("-customtxexpiration=<n>", strprintf("Number of blocks ahead of tip locally created transaction will expire (default: %u)", DEFAULT_CUSTOM_TX_EXPIRATION), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
gArgs.AddArg("-feefilter", strprintf("Tell other nodes to filter invs to us by our mempool min fee (default: %u)", DEFAULT_FEEFILTER), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::OPTIONS);
gArgs.AddArg("-includeconf=<file>", "Specify additional configuration file, relative to the -datadir path (only useable from configuration file, not command line)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
gArgs.AddArg("-loadblock=<file>", "Imports blocks from external blk000??.dat file on startup", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
Expand Down Expand Up @@ -1729,9 +1728,6 @@ bool AppInitMain(InitInterfaces& interfaces)
// Ensure we are on latest DB version
pcustomcsview->SetDbVersion(CCustomCSView::DbVersion);

// Set custom Tx expiration
pcustomcsview->SetGlobalCustomTxExpiration(gArgs.GetArg("-customtxexpiration", DEFAULT_CUSTOM_TX_EXPIRATION));

// make account history db
paccountHistoryDB.reset();
if (gArgs.GetBoolArg("-acindex", DEFAULT_ACINDEX)) {
Expand Down
10 changes: 0 additions & 10 deletions src/masternodes/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,16 +744,6 @@ CCustomCSView::CCustomCSView(CCustomCSView & other)
CheckPrefixes();
}

void CCustomCSView::SetGlobalCustomTxExpiration(const uint32_t height)
{
globalCustomTxExpiration = height;
}

uint32_t CCustomCSView::GetGlobalCustomTxExpiration() const
{
return globalCustomTxExpiration;
}

int CCustomCSView::GetDbVersion() const
{
int version;
Expand Down
7 changes: 0 additions & 7 deletions src/masternodes/masternodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ enum class UpdateMasternodeType : uint8_t
};

constexpr uint8_t SUBNODE_COUNT{4};
constexpr uint32_t DEFAULT_CUSTOM_TX_EXPIRATION{120};

class CMasternode
{
Expand Down Expand Up @@ -437,9 +436,6 @@ class CCustomCSView

std::unique_ptr<CAccountHistoryStorage> accHistoryStore;
std::unique_ptr<CVaultHistoryStorage> vauHistoryStore;

uint32_t globalCustomTxExpiration{DEFAULT_CUSTOM_TX_EXPIRATION};

public:
// Increase version when underlaying tables are changed
static constexpr const int DbVersion = 1;
Expand Down Expand Up @@ -484,9 +480,6 @@ class CCustomCSView

int GetDbVersion() const;

void SetGlobalCustomTxExpiration(const uint32_t height);
uint32_t GetGlobalCustomTxExpiration() const;

uint256 MerkleRoot();

// we construct it as it
Expand Down
25 changes: 4 additions & 21 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ static ResVal<CBalances> MintedTokens(CTransaction const & tx, uint32_t mintingO
return {balances, Res::Ok()};
}

CCustomTxMessage customTypeToMessage(CustomTxType txType, uint8_t version) {
CCustomTxMessage customTypeToMessage(CustomTxType txType) {
switch (txType)
{
case CustomTxType::CreateMasternode: return CCreateMasterNodeMessage{};
Expand Down Expand Up @@ -3691,40 +3691,23 @@ void PopulateVaultHistoryData(CHistoryWriters* writers, CAccountsHistoryWriter&
}
}

Res ApplyCustomTx(CCustomCSView& mnview, const CCoinsViewCache& coins, const CTransaction& tx, const Consensus::Params& consensus, uint32_t height, uint64_t time, uint256* canSpend, uint32_t* customTxExpiration, uint32_t txn, CHistoryWriters* writers) {
Res ApplyCustomTx(CCustomCSView& mnview, const CCoinsViewCache& coins, const CTransaction& tx, const Consensus::Params& consensus, uint32_t height, uint64_t time, uint256* canSpend, uint32_t txn, CHistoryWriters* writers) {
auto res = Res::Ok();
if (tx.IsCoinBase() && height > 0) { // genesis contains custom coinbase txs
return res;
}
std::vector<unsigned char> metadata;
const auto metadataValidation = height >= static_cast<uint32_t>(consensus.FortCanningHeight);

CExpirationAndVersion customTxParams;
auto txType = GuessCustomTxType(tx, metadata, metadataValidation, height, &customTxParams);
auto txType = GuessCustomTxType(tx, metadata, metadataValidation);
if (txType == CustomTxType::None) {
return res;
}

if (metadataValidation && txType == CustomTxType::Reject) {
return Res::ErrCode(CustomTxErrCodes::Fatal, "Invalid custom transaction");
}

if (height >= static_cast<uint32_t>(consensus.GreatWorldHeight)) {
if (customTxParams.expiration == 0) {
return Res::ErrCode(CustomTxErrCodes::Fatal, "Invalid transaction expiration set");
}
if (customTxParams.version > static_cast<uint8_t>(MetadataVersion::Two)) {
return Res::ErrCode(CustomTxErrCodes::Fatal, "Invalid transaction version set");
}
if (height > customTxParams.expiration) {
return Res::ErrCode(CustomTxErrCodes::Fatal, "Transaction has expired");
}
if (customTxExpiration) {
*customTxExpiration = customTxParams.expiration;
}
}

auto txMessage = customTypeToMessage(txType, customTxParams.version);
auto txMessage = customTypeToMessage(txType);
CAccountsHistoryWriter view(mnview, height, txn, tx.GetHash(), uint8_t(txType), writers);
if ((res = CustomMetadataParse(height, consensus, metadata, txMessage))) {
if (pvaultHistoryDB && writers) {
Expand Down
27 changes: 8 additions & 19 deletions src/masternodes/mn_checks.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,6 @@ enum class CustomTxType : uint8_t
TokenSplit = 'P',
};

enum class MetadataVersion : uint8_t {
None = 0,
One = 1,
Two = 2,
};

inline CustomTxType CustomTxCodeToType(uint8_t ch) {
auto type = static_cast<CustomTxType>(ch);
switch(type) {
Expand Down Expand Up @@ -353,11 +347,11 @@ using CCustomTxMessage = std::variant<
CAuctionBidMessage
>;

CCustomTxMessage customTypeToMessage(CustomTxType txType, uint8_t version);
CCustomTxMessage customTypeToMessage(CustomTxType txType);
bool IsMempooledCustomTxCreate(const CTxMemPool& pool, const uint256& txid);
Res RpcInfo(const CTransaction& tx, uint32_t height, CustomTxType& type, UniValue& results);
Res CustomMetadataParse(uint32_t height, const Consensus::Params& consensus, const std::vector<unsigned char>& metadata, CCustomTxMessage& txMessage);
Res ApplyCustomTx(CCustomCSView& mnview, const CCoinsViewCache& coins, const CTransaction& tx, const Consensus::Params& consensus, uint32_t height, uint64_t time = 0, uint256* canSpend = nullptr, uint32_t* customTxExpiration = nullptr, uint32_t txn = 0, CHistoryWriters* writers = nullptr);
Res ApplyCustomTx(CCustomCSView& mnview, const CCoinsViewCache& coins, const CTransaction& tx, const Consensus::Params& consensus, uint32_t height, uint64_t time = 0, uint256* canSpend = nullptr, uint32_t txn = 0, CHistoryWriters* writers = nullptr);
Res CustomTxVisit(CCustomCSView& mnview, const CCoinsViewCache& coins, const CTransaction& tx, uint32_t height, const Consensus::Params& consensus, const CCustomTxMessage& txMessage, uint64_t time, uint32_t txn = 0);
ResVal<uint256> ApplyAnchorRewardTx(CCustomCSView& mnview, const CTransaction& tx, int height, const uint256& prevStakeModifier, const std::vector<unsigned char>& metadata, const Consensus::Params& consensusParams);
ResVal<uint256> ApplyAnchorRewardTxPlus(CCustomCSView& mnview, const CTransaction& tx, int height, const std::vector<unsigned char>& metadata, const Consensus::Params& consensusParams);
Expand All @@ -381,8 +375,7 @@ inline bool OraclePriceFeed(CCustomCSView& view, const CTokenCurrencyPair& price
/*
* Checks if given tx is probably one of 'CustomTx', returns tx type and serialized metadata in 'data'
*/
inline CustomTxType GuessCustomTxType(CTransaction const & tx, std::vector<unsigned char> & metadata, bool metadataValidation = false,
uint32_t height = 0, CExpirationAndVersion* customTxParams = nullptr){
inline CustomTxType GuessCustomTxType(CTransaction const & tx, std::vector<unsigned char> & metadata, bool metadataValidation = false){
if (tx.vout.empty()) {
return CustomTxType::None;
}
Expand All @@ -391,25 +384,21 @@ inline CustomTxType GuessCustomTxType(CTransaction const & tx, std::vector<unsig
if (metadataValidation) {
for (size_t i{1}; i < tx.vout.size(); ++i) {
std::vector<unsigned char> dummydata;
uint8_t dummyOpcodes{HasForks::None};
bool dummyOpcodes{false};
if (ParseScriptByMarker(tx.vout[i].scriptPubKey, DfTxMarker, dummydata, dummyOpcodes)) {
return CustomTxType::Reject;
}
}
}

uint8_t hasAdditionalOpcodes{HasForks::None};
if (!ParseScriptByMarker(tx.vout[0].scriptPubKey, DfTxMarker, metadata, hasAdditionalOpcodes, customTxParams)) {
bool hasAdditionalOpcodes{false};
if (!ParseScriptByMarker(tx.vout[0].scriptPubKey, DfTxMarker, metadata, hasAdditionalOpcodes)) {
return CustomTxType::None;
}

// If metadata contains additional opcodes mark as Reject.
if (metadataValidation) {
if (height < static_cast<uint32_t>(Params().GetConsensus().GreatWorldHeight) && hasAdditionalOpcodes & HasForks::FortCanning) {
return CustomTxType::Reject;
} else if (height >= static_cast<uint32_t>(Params().GetConsensus().GreatWorldHeight) && hasAdditionalOpcodes & HasForks::GreatWorld) {
return CustomTxType::Reject;
}
if (metadataValidation && hasAdditionalOpcodes) {
return CustomTxType::Reject;
}

auto txType = CustomTxCodeToType(metadata[0]);
Expand Down
44 changes: 2 additions & 42 deletions src/masternodes/mn_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,8 @@ std::vector<CTxIn> GetAuthInputsSmart(CWalletCoinsUnlocker& pwallet, int32_t txV

void execTestTx(const CTransaction& tx, uint32_t height, CTransactionRef optAuthTx) {
std::vector<unsigned char> metadata;
CExpirationAndVersion customTxParams;
auto txType = GuessCustomTxType(tx, metadata, false, 0, &customTxParams);
auto txMessage = customTypeToMessage(txType, customTxParams.version);
auto txType = GuessCustomTxType(tx, metadata);
auto txMessage = customTypeToMessage(txType);
auto res = CustomMetadataParse(height, Params().GetConsensus(), metadata, txMessage);
if (res) {
LOCK(cs_main);
Expand Down Expand Up @@ -950,44 +949,6 @@ static UniValue clearmempool(const JSONRPCRequest& request)
return removed;
}

UniValue setcustomtxexpiration(const JSONRPCRequest& request) {
RPCHelpMan{"setcustomtxexpiration",
"\nSet the expiration in blocks of locally created transactions. This expiration is to be\n"
"added to the current block height at the point of transaction creation. Once the chain reaches the\n"
"combined height if the transaction has not been added to a block it will be removed from the mempool\n"
"and can no longer be added to a block\n",
{
{"blockCount", RPCArg::Type::NUM, RPCArg::Optional::NO, ""}
},
RPCResults{},
RPCExamples{
HelpExampleCli("setcustomtxexpiration", "10")
+ HelpExampleRpc("setcustomtxexpiration", "10")
},
}.Check(request);

RPCTypeCheck(request.params, {UniValue::VNUM}, false);

LOCK(cs_main);

pcustomcsview->SetGlobalCustomTxExpiration(request.params[0].get_int());

return {};
}

void AddVersionAndExpiration(CScript& metaData, const uint32_t height, const MetadataVersion version)
{
if (height < static_cast<uint32_t>(Params().GetConsensus().GreatWorldHeight)) {
return;
}

CExpirationAndVersion customTxParams{height + pcustomcsview->GetGlobalCustomTxExpiration(), static_cast<uint8_t>(version)};

CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << customTxParams;

metaData << ToByteVector(stream);
}

static const CRPCCommand commands[] =
{
Expand All @@ -1000,7 +961,6 @@ static const CRPCCommand commands[] =
{"blockchain", "isappliedcustomtx", &isappliedcustomtx, {"txid", "blockHeight"}},
{"blockchain", "listsmartcontracts", &listsmartcontracts, {}},
{"blockchain", "clearmempool", &clearmempool, {} },
{"blockchain", "setcustomtxexpiration", &setcustomtxexpiration, {"blockHeight"}},
};

void RegisterMNBlockchainRPCCommands(CRPCTable& tableRPC) {
Expand Down
1 change: 0 additions & 1 deletion src/masternodes/mn_rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,5 @@ void execTestTx(const CTransaction& tx, uint32_t height, CTransactionRef optAuth
CScript CreateScriptForHTLC(const JSONRPCRequest& request, uint32_t &blocks, std::vector<unsigned char>& image);
CPubKey PublickeyFromString(const std::string &pubkey);
std::optional<FutureSwapHeightInfo> GetFuturesBlock(const uint32_t typeId);
void AddVersionAndExpiration(CScript& metadata, const uint32_t height, const MetadataVersion version = MetadataVersion::One);

#endif // DEFI_MASTERNODES_MN_RPC_H
5 changes: 2 additions & 3 deletions src/masternodes/rpc_customtx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,12 +483,11 @@ class CCustomTxRpcVisitor

Res RpcInfo(const CTransaction& tx, uint32_t height, CustomTxType& txType, UniValue& results) {
std::vector<unsigned char> metadata;
CExpirationAndVersion customTxParams;
txType = GuessCustomTxType(tx, metadata, false, 0, &customTxParams);
txType = GuessCustomTxType(tx, metadata);
if (txType == CustomTxType::None) {
return Res::Ok();
}
auto txMessage = customTypeToMessage(txType, customTxParams.version);
auto txMessage = customTypeToMessage(txType);
auto res = CustomMetadataParse(height, Params().GetConsensus(), metadata, txMessage);
if (res) {
CCustomCSView mnview(*pcustomcsview);
Expand Down
Loading