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

refactor: move CTransaction to primitives #2006

Merged
merged 5 commits into from
Apr 11, 2021
Merged
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
12 changes: 12 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ GRIDCOIN_CORE_H = \
compat/endian.h \
consensus/consensus.h \
consensus/params.h \
consensus/tx_verify.h \
crypter.h \
fs.h \
fwd.h \
Expand Down Expand Up @@ -127,6 +128,8 @@ GRIDCOIN_CORE_H = \
gridcoin/voting/result.h \
gridcoin/voting/vote.h \
hash.h \
index/disktxpos.h \
index/txindex.h \
init.h \
key.h \
keystore.h \
Expand All @@ -137,7 +140,10 @@ GRIDCOIN_CORE_H = \
netbase.h \
net.h \
pbkdf2.h \
policy/fees.h \
policy/policy.h \
prevector.h \
primitives/transaction.h \
protocol.h \
reverselock.h \
rpc/blockchain.h \
Expand Down Expand Up @@ -167,6 +173,7 @@ GRIDCOIN_CORE_H = \
util/threadnames.h \
util/time.h \
util.h \
validation.h \
version.h \
wallet/coincontrol.h \
wallet/db.h \
Expand All @@ -182,6 +189,7 @@ GRIDCOIN_CORE_CPP = addrdb.cpp \
chainparams.cpp \
chainparamsbase.cpp \
checkpoints.cpp \
consensus/tx_verify.cpp \
crypter.cpp \
fs.cpp \
gridcoin/appcache.cpp \
Expand All @@ -207,6 +215,7 @@ GRIDCOIN_CORE_CPP = addrdb.cpp \
gridcoin/superblock.cpp \
gridcoin/support/block_finder.cpp \
gridcoin/tally.cpp \
gridcoin/tx_message.cpp \
gridcoin/upgrade.cpp \
gridcoin/voting/builders.cpp \
gridcoin/voting/claims.cpp \
Expand All @@ -224,6 +233,8 @@ GRIDCOIN_CORE_CPP = addrdb.cpp \
net.cpp \
noui.cpp \
pbkdf2.cpp \
policy/policy.cpp \
primitives/transaction.cpp \
protocol.cpp \
rpc/blockchain.cpp \
rpc/client.cpp \
Expand All @@ -249,6 +260,7 @@ GRIDCOIN_CORE_CPP = addrdb.cpp \
util/threadnames.cpp \
util/time.cpp \
util.cpp \
validation.cpp \
version.cpp \
wallet/db.cpp \
wallet/rpcdump.cpp \
Expand Down
6 changes: 4 additions & 2 deletions src/consensus/consensus.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "amount.h"

static const int LAST_POW_BLOCK = 2050;
static const int CONSENSUS_LOOKBACK = 5; //Amount of blocks to go back from best block, to avoid counting forked blocks
static const int BLOCK_GRANULARITY = 10; //Consensus block divisor
Expand All @@ -20,6 +22,6 @@ static const unsigned int MAX_ORPHAN_TRANSACTIONS = MAX_BLOCK_SIZE/100;
/** The maximum number of entries in an 'inv' protocol message */
static const unsigned int MAX_INV_SZ = 50000;
/** Fees smaller than this (in satoshi) are considered zero fee (for transaction creation) */
static const int64_t MIN_TX_FEE = 10000;
static const CAmount MIN_TX_FEE = 10000;
/** Fees smaller than this (in satoshi) are considered zero fee (for relaying) */
static const int64_t MIN_RELAY_TX_FEE = MIN_TX_FEE;
static const CAmount MIN_RELAY_TX_FEE = MIN_TX_FEE;
54 changes: 54 additions & 0 deletions src/consensus/tx_verify.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2017-2020 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <consensus/tx_verify.h>

#include <primitives/transaction.h>

bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
{
AssertLockHeld(cs_main);
// Time based nLockTime implemented in 0.1.6
if (tx.nLockTime == 0)
return true;
if (nBlockHeight == 0)
nBlockHeight = nBestHeight;
if (nBlockTime == 0)
nBlockTime = GetAdjustedTime();
if ((int64_t)tx.nLockTime < ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD ? (int64_t)nBlockHeight : nBlockTime))
return true;
for (auto const& txin : tx.vin)
if (!txin.IsFinal())
return false;
return true;
}

unsigned int GetLegacySigOpCount(const CTransaction& tx)
{
unsigned int nSigOps = 0;
for (auto const& txin : tx.vin)
{
nSigOps += txin.scriptSig.GetSigOpCount(false);
}
for (auto const& txout : tx.vout)
{
nSigOps += txout.scriptPubKey.GetSigOpCount(false);
}
return nSigOps;
}

unsigned int GetP2SHSigOpCount(const CTransaction& tx, const MapPrevTx& inputs)
{
if (tx.IsCoinBase())
return 0;

unsigned int nSigOps = 0;
for (unsigned int i = 0; i < tx.vin.size(); i++)
{
const CTxOut& prevout = GetOutputFor(tx.vin[i], inputs);
if (prevout.scriptPubKey.IsPayToScriptHash())
nSigOps += prevout.scriptPubKey.GetSigOpCount(tx.vin[i].scriptSig);
}
return nSigOps;
}
33 changes: 33 additions & 0 deletions src/consensus/tx_verify.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2017-2020 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_CONSENSUS_TX_VERIFY_H
#define BITCOIN_CONSENSUS_TX_VERIFY_H

#include "main.h"

class CTransaction;

/** Count ECDSA signature operations the old-fashioned (pre-0.6) way
@param[in] tx The transaction to count
@return number of sigops tx's outputs will produce when spent
@see FetchInputs
*/
unsigned int GetLegacySigOpCount(const CTransaction& tx);

/** Count ECDSA signature operations in pay-to-script-hash inputs.
@param[in] tx The transaction to count
@param[in] mapInputs Map of previous transactions that have outputs tx is spending
@return maximum number of sigops required to validate tx's inputs
@see FetchInputs
*/
unsigned int GetP2SHSigOpCount(const CTransaction& tx, const MapPrevTx& inputs);

/**
* Check if transaction is final and can be included in a block with the
* specified height and time. Consensus critical.
*/
bool IsFinalTx(const CTransaction &tx, int nBlockHeight = 0, int64_t nBlockTime = 0);

#endif // BITCOIN_CONSENSUS_TX_VERIFY_H
27 changes: 27 additions & 0 deletions src/gridcoin/tx_message.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2014-2020 The Gridcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <string>

#include "gridcoin/support/xml.h"
#include "gridcoin/tx_message.h"

std::string GetMessage(const CTransaction& tx)
{
if (tx.nVersion <= 1) {
return ExtractXML(tx.hashBoinc, "<MESSAGE>", "</MESSAGE>");
}

if (tx.vContracts.empty()) {
return std::string();
}

if (tx.vContracts.front().m_type != GRC::ContractType::MESSAGE) {
return std::string();
}

const auto payload = tx.vContracts.front().SharePayloadAs<GRC::TxMessage>();

return payload->m_message;
}
6 changes: 6 additions & 0 deletions src/gridcoin/tx_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include "gridcoin/contract/payload.h"
#include "primitives/transaction.h"

#include <string>

Expand Down Expand Up @@ -103,3 +104,8 @@ class TxMessage : public IContractPayload
}
}; // TxMessage
}

//!
//! \brief Get the custom, user-supplied transaction message, if any.
//!
std::string GetMessage(const CTransaction& tx);
3 changes: 2 additions & 1 deletion src/gridcoin/voting/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "gridcoin/voting/vote.h"
#include "txdb.h"
#include "ui_interface.h"
#include "validation.h"

using namespace GRC;
using LogFlags = BCLog::LogFlags;
Expand Down Expand Up @@ -149,7 +150,7 @@ class PollClaimValidator

CTransaction tx;

if (!tx.ReadFromDisk(tx_index.pos)) {
if (!ReadTxFromDisk(tx, tx_index.pos)) {
throw InvalidPollError();
}

Expand Down
65 changes: 65 additions & 0 deletions src/index/disktxpos.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2019-2020 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_INDEX_DISKTXPOS_H
#define BITCOIN_INDEX_DISKTXPOS_H

#include <serialize.h>
#include "tinyformat.h"

/** Position on disk for a particular transaction. */
class CDiskTxPos
{
public:
unsigned int nFile;
unsigned int nBlockPos;
unsigned int nTxPos;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(nFile);
READWRITE(nBlockPos);
READWRITE(nTxPos);
}

CDiskTxPos()
{
SetNull();
}

CDiskTxPos(unsigned int nFileIn, unsigned int nBlockPosIn, unsigned int nTxPosIn)
{
nFile = nFileIn;
nBlockPos = nBlockPosIn;
nTxPos = nTxPosIn;
}

void SetNull() { nFile = (unsigned int) -1; nBlockPos = 0; nTxPos = 0; }
bool IsNull() const { return (nFile == (unsigned int) -1); }

friend bool operator==(const CDiskTxPos& a, const CDiskTxPos& b)
{
return (a.nFile == b.nFile &&
a.nBlockPos == b.nBlockPos &&
a.nTxPos == b.nTxPos);
}

friend bool operator!=(const CDiskTxPos& a, const CDiskTxPos& b)
{
return !(a == b);
}

std::string ToString() const
{
if (IsNull())
return "null";
else
return strprintf("(nFile=%u, nBlockPos=%u, nTxPos=%u)", nFile, nBlockPos, nTxPos);
}
};

#endif // BITCOIN_INDEX_DISKTXPOS_H
66 changes: 66 additions & 0 deletions src/index/txindex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2017-2018 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_INDEX_TXINDEX_H
#define BITCOIN_INDEX_TXINDEX_H

/** A txdb record that contains the disk location of a transaction and the
* locations of transactions that spend its outputs. vSpent is really only
* used as a flag, but having the location is very helpful for debugging.
*/
class CTxIndex
{
public:
CDiskTxPos pos;
std::vector<CDiskTxPos> vSpent;

CTxIndex()
{
SetNull();
}

CTxIndex(const CDiskTxPos& posIn, unsigned int nOutputs)
{
pos = posIn;
vSpent.resize(nOutputs);
}

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
if (!(s.GetType() & SER_GETHASH)) {
int nVersion = s.GetVersion();
READWRITE(nVersion);
}

READWRITE(pos);
READWRITE(vSpent);
}

void SetNull()
{
pos.SetNull();
vSpent.clear();
}

bool IsNull()
{
return pos.IsNull();
}

friend bool operator==(const CTxIndex& a, const CTxIndex& b)
{
return (a.pos == b.pos &&
a.vSpent == b.vSpent);
}

friend bool operator!=(const CTxIndex& a, const CTxIndex& b)
{
return !(a == b);
}
};

#endif // BITCOIN_INDEX_TXINDEX_H
Loading