Skip to content

Commit 6427ac9

Browse files
committed
Implement new fields for the type and version of transaction
Signed-off-by: Dmitry Saveliev <[email protected]>
1 parent 886f0ab commit 6427ac9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1043
-1006
lines changed

src/bench/verify_script.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
static CMutableTransaction BuildCreditingTransaction(const CScript& scriptPubKey)
1919
{
2020
CMutableTransaction txCredit;
21-
txCredit.nVersion = 1;
21+
txCredit.version = 1;
2222
txCredit.nLockTime = 0;
2323
txCredit.vin.resize(1);
2424
txCredit.vout.resize(1);
@@ -35,7 +35,7 @@ static CMutableTransaction BuildCreditingTransaction(const CScript& scriptPubKey
3535
static CMutableTransaction BuildSpendingTransaction(const CScript& scriptSig, const CMutableTransaction& txCredit)
3636
{
3737
CMutableTransaction txSpend;
38-
txSpend.nVersion = 1;
38+
txSpend.version = 1;
3939
txSpend.nLockTime = 0;
4040
txSpend.vin.resize(1);
4141
txSpend.vout.resize(1);

src/compressor.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class CScriptCompressor
3838
private:
3939
/**
4040
* make this static for now (there are only 6 special scripts defined)
41-
* this can potentially be extended together with a new nVersion for
42-
* transactions, in which case this value becomes dependent on nVersion
41+
* this can potentially be extended together with a new version for
42+
* transactions, in which case this value becomes dependent on version
4343
* and nHeight of the enclosing transaction.
4444
*/
4545
static const unsigned int nSpecialScripts = 6;

src/consensus/tx_verify.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ std::pair<int, int64_t> CalculateSequenceLocks(const CTransaction &tx, int flags
4242
int nMinHeight = -1;
4343
int64_t nMinTime = -1;
4444

45-
// tx.nVersion is signed integer so requires cast to unsigned otherwise
46-
// we would be doing a signed comparison and half the range of nVersion
45+
// tx.version is signed integer so requires cast to unsigned otherwise
46+
// we would be doing a signed comparison and half the range of version
4747
// wouldn't support BIP 68.
48-
bool fEnforceBIP68 = static_cast<uint32_t>(tx.nVersion) >= 2
49-
&& flags & LOCKTIME_VERIFY_SEQUENCE;
48+
bool fEnforceBIP68 = tx.version >= 2 && flags & LOCKTIME_VERIFY_SEQUENCE;
5049

5150
// Do not enforce sequence numbers as a relative lock time
5251
// unless we have been instructed to

src/esperanza/walletextension.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,7 @@ bool WalletExtension::SetMasterKeyFromSeed(const key::mnemonic::Seed &seed,
248248
}
249249

250250
bool WalletExtension::BackupWallet() {
251-
const std::string wallet_file_name = m_enclosing_wallet.GetName().empty() ?
252-
"wallet.dat" : m_enclosing_wallet.GetName();
251+
const std::string wallet_file_name = m_enclosing_wallet.GetName().empty() ? "wallet.dat" : m_enclosing_wallet.GetName();
253252
const int64_t current_time = GetTime();
254253

255254
const std::string backup_wallet_filename =
@@ -612,7 +611,6 @@ void WalletExtension::VoteIfNeeded() {
612611
return;
613612
}
614613

615-
616614
const CWalletTx *prev_tx = m_enclosing_wallet.GetWalletTx(validator->m_last_transaction_hash);
617615
assert(prev_tx);
618616

src/policy/policy.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ bool IsStandardTx(const CTransaction& tx, std::string& reason)
9393
return true;
9494
}
9595

96-
if (tx.nVersion > CTransaction::MAX_STANDARD_VERSION || tx.nVersion < 1) {
96+
if (tx.version > CTransaction::MAX_STANDARD_VERSION || tx.version < 1) {
9797
reason = "version";
9898
return false;
9999
}

src/policy/policy.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ static const unsigned int DEFAULT_BLOCK_MAX_WEIGHT = MAX_BLOCK_WEIGHT - 4000;
2222
static const unsigned int DEFAULT_BLOCK_MIN_TX_FEE = 1000;
2323
/** The maximum weight for transactions we're willing to relay/mine */
2424
static const unsigned int MAX_STANDARD_TX_WEIGHT = 400000;
25-
/** The minimum non-witness size for transactions we're willing to relay/mine (1 segwit input + 1 P2WPKH output = 82 bytes) */
26-
static const unsigned int MIN_STANDARD_TX_NONWITNESS_SIZE = 82;
25+
/** The minimum non-witness size for transactions we're willing to relay/mine (1 segwit input + 1 P2WPKH output = 80 bytes) */
26+
static const unsigned int MIN_STANDARD_TX_NONWITNESS_SIZE = 80;
2727
/** Maximum number of signature check operations in an IsStandard() P2SH script */
2828
static const unsigned int MAX_P2SH_SIGOPS = 15;
2929
/** The maximum number of sigops we're willing to relay/mine in a single tx */

src/primitives/transaction.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ std::string CTxOut::ToString() const
5555
return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / UNIT, nValue % UNIT, HexStr(scriptPubKey).substr(0, 30));
5656
}
5757

58-
CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
59-
CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime) {}
58+
CMutableTransaction::CMutableTransaction() : version(CTransaction::CURRENT_VERSION), type(TxType::REGULAR), nLockTime(0) {}
59+
CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), version(tx.version), type(tx.type), nLockTime(tx.nLockTime) {}
6060

6161
void CMutableTransaction::SetType(TxType type) {
62-
nVersion = (nVersion & 0x0000FFFF) | (static_cast<uint16_t>(type) << 16);
62+
this->type = static_cast<uint8_t>(type);
6363
}
6464

65-
void CMutableTransaction::SetVersion(uint16_t type) {
66-
nVersion = (nVersion & 0xFFFF0000) | (static_cast<uint16_t>(type));
65+
void CMutableTransaction::SetVersion(uint8_t version) {
66+
this->version = version;
6767
}
6868

6969
uint256 CMutableTransaction::GetHash() const
@@ -85,9 +85,9 @@ uint256 CTransaction::ComputeWitnessHash() const
8585
}
8686

8787
/* For backward compatibility, the hash is initialized to 0. TODO: remove the need for this default constructor entirely. */
88-
CTransaction::CTransaction() : vin(), vout(), nVersion(CTransaction::CURRENT_VERSION), nLockTime(0), hash{}, m_witness_hash{} {}
89-
CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
90-
CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
88+
CTransaction::CTransaction() : vin(), vout(), version(CTransaction::CURRENT_VERSION), type(TxType::REGULAR), nLockTime(0), hash{}, m_witness_hash{} {}
89+
CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), version(tx.version), type(tx.type), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
90+
CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), version(tx.version), type(tx.type), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
9191

9292
CAmount CTransaction::GetValueOut() const
9393
{
@@ -110,7 +110,7 @@ std::string CTransaction::ToString() const
110110
std::string str;
111111
str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
112112
GetHash().ToString().substr(0,10),
113-
nVersion,
113+
version,
114114
vin.size(),
115115
vout.size(),
116116
nLockTime);

src/primitives/transaction.h

+18-14
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,15 @@ struct CMutableTransaction;
180180

181181
/**
182182
* Basic transaction serialization format:
183-
* - int32_t nVersion
183+
* - uint8_t type
184+
* - uint8_t version
184185
* - std::vector<CTxIn> vin
185186
* - std::vector<CTxOut> vout
186187
* - uint32_t nLockTime
187188
*
188189
* Extended transaction serialization format:
189-
* - int32_t nVersion
190+
* - uint8_t type
191+
* - uint8_t version
190192
* - unsigned char dummy = 0x00
191193
* - unsigned char flags (!= 0)
192194
* - std::vector<CTxIn> vin
@@ -199,7 +201,8 @@ template<typename Stream, typename TxType>
199201
inline void UnserializeTransaction(TxType& tx, Stream& s) {
200202
const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
201203

202-
s >> tx.nVersion;
204+
s >> tx.type;
205+
s >> tx.version;
203206
unsigned char flags = 0;
204207
tx.vin.clear();
205208
tx.vout.clear();
@@ -234,7 +237,8 @@ template<typename Stream, typename TxType>
234237
inline void SerializeTransaction(const TxType& tx, Stream& s) {
235238
const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
236239

237-
s << tx.nVersion;
240+
s << tx.type;
241+
s << tx.version;
238242
unsigned char flags = 0;
239243
// Consistency check
240244
if (fAllowWitness) {
@@ -264,14 +268,12 @@ class TransactionBase
264268
{
265269
public:
266270

267-
//! Returns the version of this transaction (considers only the two lower bytes of nVersion).
268-
uint16_t GetVersion() const {
269-
return static_cast<uint16_t>(GetDerived()->nVersion);
271+
uint8_t GetVersion() const {
272+
return GetDerived()->version;
270273
}
271274

272-
//! Returns the transaction type (TxType) of this transaction (stored in the two upper bytes of the nVersion field).
273275
TxType GetType() const {
274-
return TxType::_from_index_unchecked(GetDerived()->nVersion >> 16);
276+
return TxType::_from_index_unchecked(GetDerived()->type);
275277
}
276278

277279
bool IsRegular() const {
@@ -345,13 +347,13 @@ class CTransaction : public TransactionBase<CTransaction>
345347
{
346348
public:
347349
// Default transaction version.
348-
static const int32_t CURRENT_VERSION=2;
350+
static const uint8_t CURRENT_VERSION=2;
349351

350352
// Changing the default transaction version requires a two step process: first
351353
// adapting relay policy by bumping MAX_STANDARD_VERSION, and then later date
352354
// bumping the default CURRENT_VERSION at which point both CURRENT_VERSION and
353355
// MAX_STANDARD_VERSION will be equal.
354-
static const int32_t MAX_STANDARD_VERSION=2;
356+
static const uint8_t MAX_STANDARD_VERSION=2;
355357

356358
// The local variables are made const to prevent unintended modification
357359
// without updating the cached hash value. However, CTransaction is not
@@ -369,7 +371,8 @@ class CTransaction : public TransactionBase<CTransaction>
369371
* Thus the upper two bytes are used to piggy back that type (also uint16_t) onto
370372
* the version field.
371373
*/
372-
const uint32_t nVersion;
374+
const uint8_t version;
375+
const uint8_t type;
373376
const uint32_t nLockTime;
374377

375378
private:
@@ -435,7 +438,8 @@ struct CMutableTransaction : public TransactionBase<CMutableTransaction>
435438
{
436439
std::vector<CTxIn> vin;
437440
std::vector<CTxOut> vout;
438-
uint32_t nVersion;
441+
uint8_t version;
442+
uint8_t type;
439443
uint32_t nLockTime;
440444

441445
CMutableTransaction();
@@ -457,7 +461,7 @@ struct CMutableTransaction : public TransactionBase<CMutableTransaction>
457461
Unserialize(s);
458462
}
459463

460-
void SetVersion(uint16_t version);
464+
void SetVersion(uint8_t version);
461465

462466
void SetType(TxType type);
463467

src/proposer/proposer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ class ProposerImpl : public Proposer {
120120
continue;
121121
}
122122
const EligibleCoin &coin = winning_ticket.get();
123-
LogPrint(BCLog::PROPOSING, "Proposing... (wallet=%s, coin=%s)\n",
124-
wallet_name, util::to_string(coin.utxo));
123+
LogPrint(BCLog::PROPOSING, "Proposing... (wallet=%s, coin=%d)\n",
124+
wallet_name, coin.utxo.GetOutputIndex());
125125
staking::TransactionPicker::PickTransactionsParameters parameters{};
126126
staking::TransactionPicker::PickTransactionsResult result =
127127
m_transaction_picker->PickTransactions(parameters);

src/script/interpreter.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -1333,8 +1333,10 @@ class CTransactionSignatureSerializer
13331333
/** Serialize txTo */
13341334
template<typename S>
13351335
void Serialize(S &s) const {
1336-
// Serialize nVersion
1337-
::Serialize(s, txTo.nVersion);
1336+
// Serialize type
1337+
::Serialize(s, txTo.type);
1338+
// Serialize version
1339+
::Serialize(s, txTo.version);
13381340
// Serialize vin
13391341
unsigned int nInputs = fAnyoneCanPay ? 1 : txTo.vin.size();
13401342
::WriteCompactSize(s, nInputs);
@@ -1433,8 +1435,10 @@ uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn
14331435
}
14341436

14351437
CHashWriter ss(SER_GETHASH, 0);
1438+
// Type
1439+
ss << txTo.type;
14361440
// Version
1437-
ss << txTo.nVersion;
1441+
ss << txTo.version;
14381442
// Input prevouts/nSequence (none/all, depending on flags)
14391443
ss << hashPrevouts;
14401444
ss << hashSequence;
@@ -1549,7 +1553,7 @@ bool GenericTransactionSignatureChecker<T>::CheckSequence(const CScriptNum& nSeq
15491553

15501554
// Fail if the transaction's version number is not set high
15511555
// enough to trigger BIP 68 rules.
1552-
if (static_cast<uint32_t>(txTo->nVersion) < 2)
1556+
if (txTo->version < 2)
15531557
return false;
15541558

15551559
// Sequence numbers with their most significant bit set are not

0 commit comments

Comments
 (0)