Skip to content

Commit 4ff073f

Browse files
committed
Merge #238: Replace static BITCOINID with asset-derivation style generation
b76b0b2 Change network settings commitment to sha2, include in asset type def (Gregory Sanders) 8c1ba39 Replace static BITCOINID with generated pegged_asset using standard issuance derivation (Gregory Sanders) ae1f058 Only use chainparams to access BITCOINID as pegged_asset (Gregory Sanders) 000ab24 Add pegged_asset field to consensus params (Gregory Sanders)
2 parents 8df0f0e + b76b0b2 commit 4ff073f

22 files changed

+96
-76
lines changed

contrib/assets_tutorial/assets_tutorial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def sync_all(e1, e2):
211211
# You can also filter calls using specific asset hex or labels:
212212
e1.getwalletinfo("bitcoin")
213213
# bitcoin's hex asset type
214-
e1.getwalletinfo("09f663de96be771f50cab5ded00256ffe63773e2eaa9a604092951cc3d7c6621")
214+
e1.getwalletinfo("b2e15d0d7a0c94e4e2ce0fe6e8691b9e451377f6e46e8045a86f7c4b5d4f0f23")
215215

216216
# We can also issue our own assets, 1 asset and 1 reissuance token in this case
217217
issue = e1.issueasset(1, 1)

contrib/assets_tutorial/assets_tutorial.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ e1-cli dumpassetlabels
144144
# You can also filter calls using specific asset hex or labels:
145145
e1-cli getwalletinfo bitcoin
146146
# bitcoin's hex asset type
147-
e1-cli getwalletinfo 09f663de96be771f50cab5ded00256ffe63773e2eaa9a604092951cc3d7c6621
147+
e1-cli getwalletinfo b2e15d0d7a0c94e4e2ce0fe6e8691b9e451377f6e46e8045a86f7c4b5d4f0f23
148148

149149
# We can also issue our own assets, 1 asset and 1 reissuance token in this case
150150
ISSUE=$(e1-cli issueasset 1 1)

src/amount.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ struct CAsset {
6969
}
7070
};
7171

72-
/** The sha256 of Bitcoin genesis block, for easy reference **/
73-
static const CAsset BITCOINID(uint256S("09f663de96be771f50cab5ded00256ffe63773e2eaa9a604092951cc3d7c6621"));
74-
7572
/** Used for consensus fee and general wallet accounting*/
7673
typedef std::map<CAsset, CAmount> CAmountMap;
7774

src/assetsdir.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include "assetsdir.h"
6+
#include "chainparams.h"
67

78
#include "tinyformat.h"
89
#include "utilstrencodings.h"
@@ -47,7 +48,7 @@ void CAssetsDir::InitFromStrings(const std::vector<std::string>& assetsToInit)
4748
SetHex(vAssets[0], vAssets[1]);
4849
}
4950
// Set "bitcoin" to the pegged asset for tests
50-
Set(BITCOINID, AssetMetadata("bitcoin"));
51+
Set(Params().GetConsensus().pegged_asset, AssetMetadata("bitcoin"));
5152
}
5253

5354
CAsset CAssetsDir::GetAsset(const std::string& label) const

src/bench/coin_selection.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include "bench.h"
6+
#include "chainparams.h"
67
#include "wallet/wallet.h"
78

89
#include <boost/foreach.hpp>
@@ -51,10 +52,10 @@ static void CoinSelection(benchmark::State& state)
5152
std::set<std::pair<const CWalletTx*, unsigned int> > setCoinsRet;
5253
CAmountMap nValueRet;
5354
CAmountMap mapValue;
54-
mapValue[BITCOINID] = 1003 * COIN;
55+
mapValue[Params().GetConsensus().pegged_asset] = 1003 * COIN;
5556
bool success = wallet.SelectCoinsMinConf(mapValue, 1, 6, 0, vCoins, setCoinsRet, nValueRet);
5657
assert(success);
57-
assert(nValueRet[BITCOINID] == 1003 * COIN);
58+
assert(nValueRet[Params().GetConsensus().pegged_asset] == 1003 * COIN);
5859
assert(setCoinsRet.size() == 2);
5960
}
6061
}

src/bitcoin-tx.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ static void MutateTxAddOutPubKey(CMutableTransaction& tx, const std::string& str
321321
}
322322

323323
// construct TxOut, append to transaction output list
324-
CTxOut txout(BITCOINID, value, scriptPubKey);
324+
CTxOut txout(Params().GetConsensus().pegged_asset, value, scriptPubKey);
325325
tx.vout.push_back(txout);
326326
}
327327

@@ -388,7 +388,7 @@ static void MutateTxAddOutMultiSig(CMutableTransaction& tx, const std::string& s
388388
}
389389

390390
// construct TxOut, append to transaction output list
391-
CTxOut txout(BITCOINID, value, scriptPubKey);
391+
CTxOut txout(Params().GetConsensus().pegged_asset, value, scriptPubKey);
392392
tx.vout.push_back(txout);
393393
}
394394

@@ -415,7 +415,7 @@ static void MutateTxAddOutData(CMutableTransaction& tx, const std::string& strIn
415415

416416
std::vector<unsigned char> data = ParseHex(strData);
417417

418-
CTxOut txout(BITCOINID, value, CScript() << OP_RETURN << data);
418+
CTxOut txout(Params().GetConsensus().pegged_asset, value, CScript() << OP_RETURN << data);
419419
tx.vout.push_back(txout);
420420
}
421421

src/chainparams.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55

66
#include "chainparams.h"
77
#include "consensus/merkle.h"
8+
#include "issuance.h"
89

910
#include "tinyformat.h"
1011
#include "util.h"
1112
#include "utilstrencodings.h"
1213
#include "amount.h"
13-
#include "crypto/ripemd160.h"
14+
#include "crypto/sha256.h"
1415

1516
#include <assert.h>
1617

@@ -21,13 +22,13 @@
2122
// Safer for users if they load incorrect parameters via arguments.
2223
static std::vector<unsigned char> CommitToArguments(const Consensus::Params& params, const std::string& networkID, const CScript& signblockscript)
2324
{
24-
CRIPEMD160 ripemd;
25-
unsigned char commitment[20];
26-
ripemd.Write((const unsigned char*)networkID.c_str(), networkID.length());
27-
ripemd.Write((const unsigned char*)HexStr(params.fedpegScript).c_str(), HexStr(params.fedpegScript).length());
28-
ripemd.Write((const unsigned char*)HexStr(signblockscript).c_str(), HexStr(signblockscript).length());
29-
ripemd.Finalize(commitment);
30-
return std::vector<unsigned char>(commitment, commitment + 20);
25+
CSHA256 sha2;
26+
unsigned char commitment[32];
27+
sha2.Write((const unsigned char*)networkID.c_str(), networkID.length());
28+
sha2.Write((const unsigned char*)HexStr(params.fedpegScript).c_str(), HexStr(params.fedpegScript).length());
29+
sha2.Write((const unsigned char*)HexStr(signblockscript).c_str(), HexStr(signblockscript).length());
30+
sha2.Finalize(commitment);
31+
return std::vector<unsigned char>(commitment, commitment + 32);
3132
}
3233

3334
static CScript StrHexToScriptWithDefault(std::string strScript, const CScript defaultScript)
@@ -49,9 +50,9 @@ static CBlock CreateGenesisBlock(const Consensus::Params& params, const std::str
4950
CMutableTransaction txNew;
5051
txNew.nVersion = 1;
5152
txNew.vin.resize(1);
53+
txNew.vout.resize(rewardShards);
5254
// Any consensus-related values that are command-line set can be added here for anti-footgun
5355
txNew.vin[0].scriptSig = CScript(CommitToArguments(params, networkID, scriptChallenge));
54-
txNew.vout.resize(rewardShards);
5556
for (unsigned int i = 0; i < rewardShards; i++) {
5657
txNew.vout[i].nValue = genesisReward/rewardShards;
5758
txNew.vout[i].nAsset = asset;
@@ -96,6 +97,7 @@ class CElementsParams : public CChainParams {
9697
consensus.BIP65Height = 388381; // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0
9798
consensus.BIP66Height = 363725; // 00000000000000000379eaa19dce8c9b722d46ae6a57c2f1a988119488b50931
9899
consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
100+
99101
// Peg-ins Bitcoin headers must have higher difficulty target than this field
100102
// This value must be sufficiently small to not preclude realistic parent
101103
// chain difficulty during network lifespan yet sufficiently large to
@@ -141,8 +143,15 @@ class CElementsParams : public CChainParams {
141143
nPruneAfterHeight = 100000;
142144

143145
parentGenesisBlockHash = uint256S("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943");
146+
147+
// Generate pegged Bitcoin asset
148+
std::vector<unsigned char> commit = CommitToArguments(consensus, strNetworkID, genesisChallengeScript);
149+
uint256 entropy;
150+
GenerateAssetEntropy(entropy, COutPoint(uint256(commit), 0), parentGenesisBlockHash);
151+
CalculateAsset(consensus.pegged_asset, entropy);
152+
144153
CScript scriptDestination(CScript() << std::vector<unsigned char>(parentGenesisBlockHash.begin(), parentGenesisBlockHash.end()) << OP_WITHDRAWPROOFVERIFY);
145-
genesis = CreateGenesisBlock(consensus, strNetworkID, scriptDestination, 1231006505, genesisChallengeScript, 1, MAX_MONEY, 100, BITCOINID);
154+
genesis = CreateGenesisBlock(consensus, strNetworkID, scriptDestination, 1231006505, genesisChallengeScript, 1, MAX_MONEY, 100, consensus.pegged_asset);
146155
consensus.hashGenesisBlock = genesis.GetHash();
147156

148157
scriptCoinbaseDestination = CScript() << ParseHex("0229536c4c83789f59c30b93eb40d4abbd99b8dcc99ba8bd748f29e33c1d279e3c") << OP_CHECKSIG;
@@ -243,10 +252,17 @@ class CRegTestParams : public CChainParams {
243252
nDefaultPort = 7042;
244253
nPruneAfterHeight = 1000;
245254

246-
genesis = CreateGenesisBlock(consensus, strNetworkID, defaultRegtestScript, 1296688602, genesisChallengeScript, 1, MAX_MONEY, 100, BITCOINID);
255+
parentGenesisBlockHash = uint256S("0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206");
256+
257+
// Generate pegged Bitcoin asset
258+
std::vector<unsigned char> commit = CommitToArguments(consensus, strNetworkID, genesisChallengeScript);
259+
uint256 entropy;
260+
GenerateAssetEntropy(entropy, COutPoint(uint256(commit), 0), parentGenesisBlockHash);
261+
CalculateAsset(consensus.pegged_asset, entropy);
262+
263+
genesis = CreateGenesisBlock(consensus, strNetworkID, defaultRegtestScript, 1296688602, genesisChallengeScript, 1, MAX_MONEY, 100, consensus.pegged_asset);
247264
consensus.hashGenesisBlock = genesis.GetHash();
248265

249-
parentGenesisBlockHash = uint256S("0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206");
250266

251267
scriptCoinbaseDestination = CScript(); // Allow any coinbase destination
252268

src/consensus/params.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "script/script.h"
1010
#include "uint256.h"
11+
#include "amount.h"
1112
#include <map>
1213
#include <string>
1314

@@ -65,6 +66,7 @@ struct Params {
6566
int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; }
6667
uint256 nMinimumChainWork;
6768
CScript fedpegScript;
69+
CAsset pegged_asset;
6870
uint256 defaultAssumeValid;
6971
};
7072
} // namespace Consensus

src/init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ std::string HelpMessage(HelpMessageMode mode)
460460
strUsage += HelpMessageOpt("-maxsigcachesize=<n>", strprintf("Limit size of signature cache to <n> MiB (default: %u)", DEFAULT_MAX_SIG_CACHE_SIZE));
461461
strUsage += HelpMessageOpt("-maxtipage=<n>", strprintf("Maximum tip age in seconds to consider node in initial block download (default: %u)", DEFAULT_MAX_TIP_AGE));
462462
}
463-
strUsage += HelpMessageOpt("-feeasset=<hex>", strprintf(_("Asset ID (hex) for mempool/relay fees (default: %s)"), DEFAULT_FEE_ASSET));
463+
strUsage += HelpMessageOpt("-feeasset=<hex>", strprintf(_("Asset ID (hex) for mempool/relay fees (default: %s)"), defaultChainParams->GetConsensus().pegged_asset.GetHex()));
464464
strUsage += HelpMessageOpt("-minrelaytxfee=<amt>", strprintf(_("Fees (in %s/kB) smaller than this are considered zero fee for relaying, mining and transaction creation (default: %s)"),
465465
CURRENCY_UNIT, FormatMoney(DEFAULT_MIN_RELAY_TX_FEE)));
466466
strUsage += HelpMessageOpt("-maxtxfee=<amt>", strprintf(_("Maximum total fees (in %s) to use in a single wallet transaction or raw transaction; setting this too low may abort large transactions (default: %s)"),
@@ -1011,7 +1011,7 @@ bool AppInitParameterInteraction()
10111011
if (nConnectTimeout <= 0)
10121012
nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
10131013

1014-
policyAsset = CAsset(uint256S(GetArg("-feeasset", DEFAULT_FEE_ASSET)));
1014+
policyAsset = CAsset(uint256S(GetArg("-feeasset", chainparams.GetConsensus().pegged_asset.GetHex())));
10151015

10161016
// Fee-per-kilobyte amount considered the same as "free"
10171017
// If you are mining, be careful setting this:

src/policy/policy.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class CCoinsViewCache;
1717
/** The sha256 of Bitcoin genesis block, for easy reference **/
1818
extern CAsset policyAsset;
1919

20-
static const std::string DEFAULT_FEE_ASSET = "09f663de96be771f50cab5ded00256ffe63773e2eaa9a604092951cc3d7c6621";
2120
/** Default for -blockmaxsize, which controls the maximum size of block the mining code will create **/
2221
static const unsigned int DEFAULT_BLOCK_MAX_SIZE = 1000000;
2322
/** Default for -blockprioritysize, maximum space for zero/low-fee transactions **/

0 commit comments

Comments
 (0)