Skip to content

Commit

Permalink
Update block, transaction and uint256 (add uint512)
Browse files Browse the repository at this point in the history
  • Loading branch information
akyo8 committed Feb 18, 2021
1 parent 850f5c6 commit 749652f
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/primitives/block.cpp
Expand Up @@ -10,7 +10,9 @@

uint256 CBlockHeader::GetHash() const
{
return SerializeHash(*this);
uint256 thash = HashX13(BEGIN(nVersion), END(nNonce));
return thash;
//return SerializeHash(*this);
}

std::string CBlock::ToString() const
Expand Down
16 changes: 16 additions & 0 deletions src/primitives/block.h
Expand Up @@ -21,6 +21,7 @@ class CBlockHeader
{
public:
// header
static const int32_t CURRENT_VERSION = 4;
int32_t nVersion;
uint256 hashPrevBlock;
uint256 hashMerkleRoot;
Expand Down Expand Up @@ -64,6 +65,8 @@ class CBlock : public CBlockHeader
public:
// network and disk
std::vector<CTransactionRef> vtx;
std::vector<unsigned char> vchBlockSig;


// memory only
mutable bool fChecked;
Expand All @@ -83,6 +86,8 @@ class CBlock : public CBlockHeader
{
READWRITEAS(CBlockHeader, obj);
READWRITE(obj.vtx);
READWRITE(vchBlockSig);

}

void SetNull()
Expand All @@ -105,6 +110,17 @@ class CBlock : public CBlockHeader
}

std::string ToString() const;

bool IsProofOfStake() const
{
return (vtx.size() > 1 && vtx[1]->IsCoinStake());
}

bool IsProofOfWork() const
{
return !IsProofOfStake();
}

};

/** Describes a place in the block chain to another node such that if the
Expand Down
17 changes: 10 additions & 7 deletions src/primitives/transaction.cpp
Expand Up @@ -56,8 +56,8 @@ std::string CTxOut::ToString() const
return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
}

CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime) {}
CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nTime(0), nLockTime(0) {}
CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin), vout(tx.vout), nTime(tx.nTime), nVersion(tx.nVersion), nLockTime(tx.nLockTime) {}

uint256 CMutableTransaction::GetHash() const
{
Expand All @@ -78,9 +78,11 @@ uint256 CTransaction::ComputeWitnessHash() const
}

/* For backward compatibility, the hash is initialized to 0. TODO: remove the need for this default constructor entirely. */
CTransaction::CTransaction() : vin(), vout(), nVersion(CTransaction::CURRENT_VERSION), nLockTime(0), hash{}, m_witness_hash{} {}
CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
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()} {}

CTransaction::CTransaction() : vin(), vout(), nVersion(CTransaction::CURRENT_VERSION), nLockTime(0), nTime(0), hash{}, m_witness_hash{} {}
CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), nTime(tx.nTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
CTransaction::CTransaction(CMutableTransaction&& tx) : vin(std::move(tx.vin)), vout(std::move(tx.vout)), nVersion(tx.nVersion), nLockTime(tx.nLockTime), nTime(tx.nTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}


CAmount CTransaction::GetValueOut() const
{
Expand All @@ -102,11 +104,12 @@ unsigned int CTransaction::GetTotalSize() const
std::string CTransaction::ToString() const
{
std::string str;
str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
GetHash().ToString().substr(0,10),
str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nTime=%u, nLockTime=%u)\n",
GetHash().ToString().substr(0, 10),
nVersion,
vin.size(),
vout.size(),
nTime,
nLockTime);
for (const auto& tx_in : vin)
str += " " + tx_in.ToString() + "\n";
Expand Down
46 changes: 41 additions & 5 deletions src/primitives/transaction.h
Expand Up @@ -31,8 +31,8 @@ class COutPoint

static constexpr uint32_t NULL_INDEX = std::numeric_limits<uint32_t>::max();

COutPoint(): n(NULL_INDEX) { }
COutPoint(const uint256& hashIn, uint32_t nIn): hash(hashIn), n(nIn) { }
COutPoint() : n((uint32_t)-1) {}
COutPoint(const uint256& hashIn, uint32_t nIn) : hash(hashIn), n(nIn) {}

SERIALIZE_METHODS(COutPoint, obj) { READWRITE(obj.hash, obj.n); }

Expand Down Expand Up @@ -157,6 +157,17 @@ class CTxOut
a.scriptPubKey == b.scriptPubKey);
}

void SetEmpty()
{
nValue = 0;
scriptPubKey.clear();
}

bool IsEmpty() const
{
return (nValue == 0 && scriptPubKey.empty());
}

friend bool operator!=(const CTxOut& a, const CTxOut& b)
{
return !(a == b);
Expand Down Expand Up @@ -189,6 +200,8 @@ inline void UnserializeTransaction(TxType& tx, Stream& s) {
const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);

s >> tx.nVersion;
s << tx.nTime;

unsigned char flags = 0;
tx.vin.clear();
tx.vout.clear();
Expand Down Expand Up @@ -260,7 +273,7 @@ class CTransaction
{
public:
// Default transaction version.
static const int32_t CURRENT_VERSION=2;
static const int32_t CURRENT_VERSION=1;

// Changing the default transaction version requires a two step process: first
// adapting relay policy by bumping MAX_STANDARD_VERSION, and then later date
Expand All @@ -277,6 +290,7 @@ class CTransaction
const std::vector<CTxOut> vout;
const int32_t nVersion;
const uint32_t nLockTime;
const uint32_t nTime;

private:
/** Memory only. */
Expand Down Expand Up @@ -321,9 +335,15 @@ class CTransaction
*/
unsigned int GetTotalSize() const;

bool IsCoinBase() const
bool IsCoinBase() const
{
return (vin.size() == 1 && vin[0].prevout.IsNull() && vout.size() >= 1);
}

bool IsCoinStake() const
{
return (vin.size() == 1 && vin[0].prevout.IsNull());
// ppcoin: the coin stake transaction is marked with the first output empty
return (vin.size() > 0 && (!vin[0].prevout.IsNull()) && vout.size() >= 2 && vout[0].IsEmpty());
}

friend bool operator==(const CTransaction& a, const CTransaction& b)
Expand Down Expand Up @@ -376,6 +396,22 @@ struct CMutableTransaction
Unserialize(s);
}

bool IsCoinBase() const
{
return (vin.size() == 1 && vin[0].prevout.IsNull() && vout.size() >= 1);
}

bool IsCoinStake() const
{
// ppcoin: the coin stake transaction is marked with the first output empty
return (vin.size() > 0 && (!vin[0].prevout.IsNull()) && vout.size() >= 2 && vout[0].IsEmpty());
}

bool IsCoinBaseOrStake() const
{
return (IsCoinBase() || IsCoinStake());
}

/** Compute the hash of this CMutableTransaction. This is computed on the
* fly, as opposed to GetHash() in CTransaction, which uses a cached result.
*/
Expand Down
1 change: 1 addition & 0 deletions src/uint256.cpp
Expand Up @@ -80,5 +80,6 @@ template std::string base_blob<256>::ToString() const;
template void base_blob<256>::SetHex(const char*);
template void base_blob<256>::SetHex(const std::string&);


const uint256 uint256::ZERO(0);
const uint256 uint256::ONE(1);
33 changes: 33 additions & 0 deletions src/uint256.h
Expand Up @@ -104,6 +104,9 @@ class base_blob
{
s.read((char*)m_data, sizeof(m_data));
}
friend class uint160;
friend class uint256;
friend class uint512;
};

/** 160-bit opaque blob.
Expand Down Expand Up @@ -151,4 +154,34 @@ inline uint256 uint256S(const std::string& str)
return rv;
}

/** 512-bit opaque blob.
* @note This type is called uint256 for historical reasons only. It is an
* opaque blob of 512 bits and has no integer operations. Use arith_uint256 if
* those are required.
*/
class uint512 : public base_blob<512>
{
public:
uint512() {}
uint512(const base_blob<512>& b) : base_blob<512>(b) {}
explicit uint512(const std::vector<unsigned char>& vch) : base_blob<512>(vch) {}
/** A cheap hash function that just returns 64 bits from the result, it can be
* used when the contents are considered uniformly random. It is not appropriate
* when the value can easily be influenced from outside as e.g. a network adversary could
* provide values to trigger worst-case behavior.
*/
uint64_t GetCheapHash() const
{
return ReadLE64(data);
}
uint256 trim256() const
{
uint256 ret;
for (unsigned int i = 0; i < uint256::WIDTH; i++) {
ret.data[i] = data[i];
}
return ret;
}
};

#endif // BITCOIN_UINT256_H

0 comments on commit 749652f

Please sign in to comment.