Skip to content

Commit

Permalink
Merge pull request #15 from BitgesellOfficial/taproot-activation
Browse files Browse the repository at this point in the history
Taproot activation
  • Loading branch information
janus committed May 21, 2021
2 parents 7af3521 + 961196c commit d819a51
Show file tree
Hide file tree
Showing 22 changed files with 212 additions and 106 deletions.
4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
AC_PREREQ([2.69])
define(_CLIENT_VERSION_MAJOR, 0)
define(_CLIENT_VERSION_MINOR, 1)
define(_CLIENT_VERSION_REVISION, 4)
define(_CLIENT_VERSION_BUILD, 5)
define(_CLIENT_VERSION_REVISION, 1)
define(_CLIENT_VERSION_BUILD, 6)
define(_CLIENT_VERSION_RC, 0)
define(_CLIENT_VERSION_IS_RELEASE, false)
define(_COPYRIGHT_YEAR, 2021)
Expand Down
2 changes: 1 addition & 1 deletion src/addrdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ bool SerializeDB(Stream& stream, const Data& data)
{
// Write and commit header, data
try {
CHashWriter hasher(SER_DISK, CLIENT_VERSION);
CHashWriterKeccak hasher(SER_DISK, CLIENT_VERSION);
stream << Params().MessageStart() << data;
hasher << Params().MessageStart() << data;
stream << hasher.GetHash();
Expand Down
10 changes: 5 additions & 5 deletions src/addrman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

int CAddrInfo::GetTriedBucket(const uint256& nKey, const std::vector<bool> &asmap) const
{
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetKey()).GetCheapHash();
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup(asmap) << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetCheapHash();
uint64_t hash1 = (CHashWriterKeccak(SER_GETHASH, 0) << nKey << GetKey()).GetCheapHash();
uint64_t hash2 = (CHashWriterKeccak(SER_GETHASH, 0) << nKey << GetGroup(asmap) << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetCheapHash();
int tried_bucket = hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
uint32_t mapped_as = GetMappedAS(asmap);
LogPrint(BCLog::NET, "IP %s mapped to AS%i belongs to tried bucket %i\n", ToStringIP(), mapped_as, tried_bucket);
Expand All @@ -24,8 +24,8 @@ int CAddrInfo::GetTriedBucket(const uint256& nKey, const std::vector<bool> &asma
int CAddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src, const std::vector<bool> &asmap) const
{
std::vector<unsigned char> vchSourceGroupKey = src.GetGroup(asmap);
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup(asmap) << vchSourceGroupKey).GetCheapHash();
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetCheapHash();
uint64_t hash1 = (CHashWriterKeccak(SER_GETHASH, 0) << nKey << GetGroup(asmap) << vchSourceGroupKey).GetCheapHash();
uint64_t hash2 = (CHashWriterKeccak(SER_GETHASH, 0) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetCheapHash();
int new_bucket = hash2 % ADDRMAN_NEW_BUCKET_COUNT;
uint32_t mapped_as = GetMappedAS(asmap);
LogPrint(BCLog::NET, "IP %s mapped to AS%i belongs to new bucket %i\n", ToStringIP(), mapped_as, new_bucket);
Expand All @@ -34,7 +34,7 @@ int CAddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src, const std:

int CAddrInfo::GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const
{
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << (fNew ? 'N' : 'K') << nBucket << GetKey()).GetCheapHash();
uint64_t hash1 = (CHashWriterKeccak(SER_GETHASH, 0) << nKey << (fNew ? 'N' : 'K') << nBucket << GetKey()).GetCheapHash();
return hash1 % ADDRMAN_BUCKET_SIZE;
}

Expand Down
10 changes: 5 additions & 5 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class CMainParams : public CChainParams
consensus.nPowTargetSpacing = 10 * 60;
consensus.fPowAllowMinDifficultyBlocks = false;
consensus.fPowNoRetargeting = false;
consensus.nRuleChangeActivationThreshold = 1815; // 90% of 2016
consensus.nRuleChangeActivationThreshold = 1614; // 80% of 2016
consensus.nMinerConfirmationWindow = 2016; // nPowTargetTimespan / nPowTargetSpacing
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 1199145601; // January 1, 2008
Expand All @@ -95,9 +95,9 @@ class CMainParams : public CChainParams

// Deployment of Taproot (BIPs 340-342)
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].bit = 2;
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nStartTime = 1619222400; // April 24th, 2021
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nTimeout = 1628640000; // August 11th, 2021
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height = 709632; // Approximately November 12th, 2021
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nStartTime = 1621589357; // May 21th, 2021
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].nTimeout = 1623715200; // June 15th, 2021
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height = 66000; // Approximately July 1st, 2021

/**
* The message start string is designed to be unlikely to occur in normal data.
Expand Down Expand Up @@ -328,7 +328,7 @@ class SigNetParams : public CChainParams {
consensus.vDeployments[Consensus::DEPLOYMENT_TAPROOT].min_activation_height = 0; // No activation delay

// message start is defined as the first 4 bytes of the sha256d of the block script
CHashWriter h(SER_DISK, 0);
CHashWriterKeccak h(SER_DISK, 0);
h << consensus.signet_challenge;
uint256 hash = h.GetHash();
memcpy(pchMessageStart, hash.begin(), 4);
Expand Down
2 changes: 1 addition & 1 deletion src/clientversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* for both BGLd and BGL-qt, to make it harder for attackers to
* target servers or GUI users specifically.
*/
const std::string CLIENT_NAME("Satoshi");
const std::string CLIENT_NAME("SatoshiBGL");


#ifdef HAVE_BUILD_INFO
Expand Down
4 changes: 2 additions & 2 deletions src/consensus/consensus.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#include <stdint.h>

/** The maximum allowed size for a serialized block, in bytes (only for buffer size limits) */
static const unsigned int MAX_BLOCK_SERIALIZED_SIZE = 4000000;
static const unsigned int MAX_BLOCK_SERIALIZED_SIZE = 400000;
/** The maximum allowed weight for a block, see BIP 141 (network rule) */
static const unsigned int MAX_BLOCK_WEIGHT = 4000000;
static const unsigned int MAX_BLOCK_WEIGHT = 400000;
/** 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) */
Expand Down
75 changes: 65 additions & 10 deletions src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ class CHash256Single {
};

/** A SHA3 hasher class specifically for blocks and transactions of BGL. */
class CHash256BlockOrTransaction {
class CHash256Keccak {
private:
sha3_context sha3context;
public:
static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;

CHash256BlockOrTransaction() {
CHash256Keccak() {
sha3_Init256(&sha3context);
sha3_SetFlags(&sha3context, SHA3_FLAGS_KECCAK);
}
Expand All @@ -95,12 +95,12 @@ class CHash256BlockOrTransaction {
}
}

CHash256BlockOrTransaction& Write(const unsigned char *data, size_t len) {
CHash256Keccak& Write(const unsigned char *data, size_t len) {
sha3_Update(&sha3context, data, len);
return *this;
}

CHash256BlockOrTransaction& Reset() {
CHash256Keccak& Reset() {
sha3_Init256(&sha3context);
return *this;
}
Expand Down Expand Up @@ -158,16 +158,71 @@ inline uint160 Hash160(const T1& in1)
}

/** A writer stream (for serialization) that computes a 256-bit Keccak hash. */
class CHashWriter
class CHashWriterKeccak
{
private:
CHash256BlockOrTransaction ctx;
CHash256Keccak ctx;

const int nType;
const int nVersion;
public:

CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
CHashWriterKeccak(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}

int GetType() const { return nType; }
int GetVersion() const { return nVersion; }

void write(const char *pch, size_t size) {
ctx.Write((const unsigned char*)pch, size);
}

/** Compute the double-SHA256 hash of all data written to this object.
*
* Invalidates this object.
*/
uint256 GetHash() {
uint256 result;
ctx.Finalize((unsigned char*)&result);
return result;
}

/** Compute the SHA256 hash of all data written to this object.
*
* Invalidates this object.
*/
//uint256 GetSHA256() {
// uint256 result;
// ctx.Finalize(result.begin());
// return result;
//}

/**
* Returns the first 64 bits from the resulting hash.
*/
inline uint64_t GetCheapHash() {
unsigned char result[CHash256::OUTPUT_SIZE];
ctx.Finalize(result);
return ReadLE64(result);
}

template<typename T>
CHashWriterKeccak& operator<<(const T& obj) {
// Serialize to this stream
::Serialize(*this, obj);
return (*this);
}
};

class CHashWriterSHA256
{
private:
CHash256Single ctx;

const int nType;
const int nVersion;
public:

CHashWriterSHA256(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}

int GetType() const { return nType; }
int GetVersion() const { return nVersion; }
Expand Down Expand Up @@ -276,13 +331,13 @@ class CHashWriterSHA256

/** Reads data from an underlying stream, while hashing the read data. */
template<typename Source>
class CHashVerifier : public CHashWriter
class CHashVerifier : public CHashWriterKeccak
{
private:
Source* source;

public:
explicit CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}
explicit CHashVerifier(Source* source_) : CHashWriterKeccak(source_->GetType(), source_->GetVersion()), source(source_) {}

void read(char* pch, size_t nSize)
{
Expand Down Expand Up @@ -313,7 +368,7 @@ class CHashVerifier : public CHashWriter
template<typename T>
uint256 SerializeHashKeccak(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
{
CHashWriter ss(nType, nVersion);
CHashWriterKeccak ss(nType, nVersion);
ss << obj;
return ss.GetHash();
}
Expand Down
8 changes: 4 additions & 4 deletions src/node/coinstats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ CDataStream TxOutSer(const COutPoint& outpoint, const Coin& coin) {
//! It is also possible, though very unlikely, that a change in this
//! construction could cause a previously invalid (and potentially malicious)
//! UTXO snapshot to be considered valid.
static void ApplyHash(CHashWriter& ss, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
static void ApplyHash(CHashWriterKeccak& ss, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
{
for (auto it = outputs.begin(); it != outputs.end(); ++it) {
if (it == outputs.begin()) {
Expand Down Expand Up @@ -147,7 +147,7 @@ bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats& stats,
{
switch (stats.m_hash_type) {
case(CoinStatsHashType::HASH_SERIALIZED): {
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
CHashWriterKeccak ss(SER_GETHASH, PROTOCOL_VERSION);
return GetUTXOStats(view, blockman, stats, ss, interruption_point, pindex);
}
case(CoinStatsHashType::MUHASH): {
Expand All @@ -162,15 +162,15 @@ bool GetUTXOStats(CCoinsView* view, BlockManager& blockman, CCoinsStats& stats,
}

// The legacy hash serializes the hashBlock
static void PrepareHash(CHashWriter& ss, const CCoinsStats& stats)
static void PrepareHash(CHashWriterKeccak& ss, const CCoinsStats& stats)
{
ss << stats.hashBlock;
}
// MuHash does not need the prepare step
static void PrepareHash(MuHash3072& muhash, CCoinsStats& stats) {}
static void PrepareHash(std::nullptr_t, CCoinsStats& stats) {}

static void FinalizeHash(CHashWriter& ss, CCoinsStats& stats)
static void FinalizeHash(CHashWriterKeccak& ss, CCoinsStats& stats)
{
stats.hashSerialized = ss.GetHash();
}
Expand Down
16 changes: 8 additions & 8 deletions src/script/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,7 @@ class CTransactionSignatureSerializer
template <class T>
uint256 GetPrevoutsSHA256(const T& txTo)
{
CHashWriter ss(SER_GETHASH, 0);
CHashWriterKeccak ss(SER_GETHASH, 0);
for (const auto& txin : txTo.vin) {
ss << txin.prevout;
}
Expand All @@ -1378,7 +1378,7 @@ uint256 GetPrevoutsSHA256(const T& txTo)
template <class T>
uint256 GetSequencesSHA256(const T& txTo)
{
CHashWriter ss(SER_GETHASH, 0);
CHashWriterKeccak ss(SER_GETHASH, 0);
for (const auto& txin : txTo.vin) {
ss << txin.nSequence;
}
Expand All @@ -1389,7 +1389,7 @@ uint256 GetSequencesSHA256(const T& txTo)
template <class T>
uint256 GetOutputsSHA256(const T& txTo)
{
CHashWriter ss(SER_GETHASH, 0);
CHashWriterKeccak ss(SER_GETHASH, 0);
for (const auto& txout : txTo.vout) {
ss << txout;
}
Expand All @@ -1399,7 +1399,7 @@ uint256 GetOutputsSHA256(const T& txTo)
/** Compute the (single) SHA256 of the concatenation of all amounts spent by a tx. */
uint256 GetSpentAmountsSHA256(const std::vector<CTxOut>& outputs_spent)
{
CHashWriter ss(SER_GETHASH, 0);
CHashWriterKeccak ss(SER_GETHASH, 0);
for (const auto& txout : outputs_spent) {
ss << txout.nValue;
}
Expand All @@ -1409,7 +1409,7 @@ uint256 GetSpentAmountsSHA256(const std::vector<CTxOut>& outputs_spent)
/** Compute the (single) SHA256 of the concatenation of all scriptPubKeys spent by a tx. */
uint256 GetSpentScriptsSHA256(const std::vector<CTxOut>& outputs_spent)
{
CHashWriter ss(SER_GETHASH, 0);
CHashWriterKeccak ss(SER_GETHASH, 0);
for (const auto& txout : outputs_spent) {
ss << txout.scriptPubKey;
}
Expand Down Expand Up @@ -1610,12 +1610,12 @@ uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn
if ((nHashType & 0x1f) != SIGHASH_SINGLE && (nHashType & 0x1f) != SIGHASH_NONE) {
hashOutputs = cacheready ? cache->hashOutputs : GetOutputsSHA256(txTo); //SHA256Uint256(GetOutputsSHA256(txTo));
} else if ((nHashType & 0x1f) == SIGHASH_SINGLE && nIn < txTo.vout.size()) {
CHashWriter ss(SER_GETHASH, 0);
CHashWriterKeccak ss(SER_GETHASH, 0);
ss << txTo.vout[nIn];
hashOutputs = ss.GetHash();
}

CHashWriter ss(SER_GETHASH, 0);
CHashWriterKeccak ss(SER_GETHASH, 0);
// Version
ss << txTo.nVersion;
// Input prevouts/nSequence (none/all, depending on flags)
Expand Down Expand Up @@ -1650,7 +1650,7 @@ uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn
CTransactionSignatureSerializer<T> txTmp(txTo, scriptCode, nIn, nHashType);

// Serialize and hash
CHashWriter ss(SER_GETHASH, 0);
CHashWriterKeccak ss(SER_GETHASH, 0);
ss << txTmp << nHashType;
return ss.GetHash();
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ bool MessageSign(

uint256 MessageHash(const std::string& message)
{
CHashWriter hasher(SER_GETHASH, 0);
CHashWriterKeccak hasher(SER_GETHASH, 0);
hasher << MESSAGE_MAGIC << message;

return hasher.GetHash();
Expand Down
2 changes: 1 addition & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ static bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const
fileout << blockundo;

// calculate & write checksum
CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
CHashWriterKeccak hasher(SER_GETHASH, PROTOCOL_VERSION);
hasher << hashBlock;
hasher << blockundo;
fileout << hasher.GetHash();
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bool DumpWallet(CWallet& wallet, bilingual_str& error)
return false;
}

CHashWriter hasher(0, 0);
CHashWriterKeccak hasher(0, 0);

WalletDatabase& db = wallet.GetDatabase();
std::unique_ptr<DatabaseBatch> batch = db.MakeBatch();
Expand Down Expand Up @@ -123,7 +123,7 @@ bool CreateFromDump(const std::string& name, const fs::path& wallet_path, biling
fsbridge::ifstream dump_file(dump_path);

// Compute the checksum
CHashWriter hasher(0, 0);
CHashWriterKeccak hasher(0, 0);
uint256 checksum;

// Check the magic and version
Expand Down
4 changes: 2 additions & 2 deletions test/functional/test_framework/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
from .segwit_addr import encode_segwit_address
from .util import assert_equal, hex_str_to_bytes

ADDRESS_BCRT1_UNSPENDABLE = 'bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj'
ADDRESS_BCRT1_UNSPENDABLE = 'rgbl1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj'
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj)#juyq9d97'
# Coins sent to this address can be spent with a witness stack of just OP_TRUE
ADDRESS_BCRT1_P2WSH_OP_TRUE = 'bcrt1qft5p2uhsdcdc3l2ua4ap5qqfg4pjaqlp250x7us7a8qqhrxrxfsqseac85'
ADDRESS_BCRT1_P2WSH_OP_TRUE = 'rbgl1qft5p2uhsdcdc3l2ua4ap5qqfg4pjaqlp250x7us7a8qqhrxrxfsqseac85'


class AddressType(enum.Enum):
Expand Down
Loading

0 comments on commit d819a51

Please sign in to comment.