diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 4f666c7916..8f4cc2fc0f 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -48,10 +48,8 @@ static CBlock CreateGenesisBlock(const Consensus::Params& params, const CScript& CMutableTransaction txNew; txNew.nVersion = 1; txNew.vin.resize(1); - txNew.vout.resize(1); txNew.vin[0].scriptSig = genesisScriptSig; - txNew.vout[0].nValue = genesisReward; - txNew.vout[0].scriptPubKey = genesisOutputScript; + txNew.vout.push_back(CTxOut(CAsset(), genesisReward, genesisOutputScript)); CBlock genesis; genesis.nTime = nTime; @@ -86,23 +84,6 @@ static CBlock CreateGenesisBlock(uint32_t nTime, uint32_t nNonce, uint32_t nBits return CreateGenesisBlock(params, genesisScriptSig, genesisOutputScript, nTime, nNonce, nBits, nVersion, genesisReward); } -/** Add an issuance transaction to the genesis block. Typically used to pre-issue - * the policyAsset of a blockchain. The genesis block is not actually validated, - * so this transaction simply has to match issuance structure. */ -static void AppendInitialIssuance(CBlock& genesis_block, const COutPoint& prevout, const int64_t asset_values, const CScript& issuance_destination) { - - // Note: Genesis block isn't actually validated, outputs are entered into utxo db only - CMutableTransaction txNew; - txNew.nVersion = 1; - txNew.vin.resize(1); - txNew.vin[0].prevout = prevout; - - txNew.vout.push_back(CTxOut(asset_values, issuance_destination)); - - genesis_block.vtx.push_back(MakeTransactionRef(std::move(txNew))); - genesis_block.hashMerkleRoot = BlockMerkleRoot(genesis_block); -} - /** * Main network */ @@ -623,8 +604,8 @@ class CCustomParams : public CRegTestParams { // Intended compatibility with Liquid v1 and elements-0.14.1 std::vector commit = CommitToArguments(consensus, strNetworkID); genesis = CreateGenesisBlock(consensus, CScript(commit), CScript(OP_RETURN), 1296688602, 2, 0x207fffff, 1, 0); - if (initialFreeCoins != 0) { - AppendInitialIssuance(genesis, COutPoint(uint256(commit), 0), initialFreeCoins, CScript() << OP_TRUE); + if (initialFreeCoins != 0 || initial_reissuance_tokens != 0) { + AppendInitialIssuance(genesis, COutPoint(uint256(commit), 0), parentGenesisBlockHash, (initialFreeCoins > 0) ? 1 : 0, initialFreeCoins, (initial_reissuance_tokens > 0) ? 1 : 0, initial_reissuance_tokens, CScript() << OP_TRUE); } } else { throw std::runtime_error(strprintf("Invalid -genesis_style (%s)", consensus.genesis_style)); diff --git a/src/coins.cpp b/src/coins.cpp index 672912b2fd..2aa90b18ab 100644 --- a/src/coins.cpp +++ b/src/coins.cpp @@ -124,7 +124,16 @@ bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) { return true; } -static const Coin coinEmpty; +// ELEMENTS: +// Because g_con_elementsmode is only set after the moment coinEmpty is initialized, +// we have to force set it to an empty coin without the default asset commitment. +Coin generateEmptyCoin() { + Coin coin; + coin.out.nValue.vchCommitment.clear(); + coin.out.nAsset.vchCommitment.clear(); + return coin; +} +static const Coin coinEmpty = generateEmptyCoin(); const Coin& CCoinsViewCache::AccessCoin(const COutPoint &outpoint) const { CCoinsMap::const_iterator it = FetchCoin(outpoint); @@ -306,7 +315,8 @@ CAmount CCoinsViewCache::GetValueIn(const CTransaction& tx) const CAmount nResult = 0; for (unsigned int i = 0; i < tx.vin.size(); i++) - nResult += AccessCoin(tx.vin[i].prevout).out.nValue; + // ELEMENTS: this method is for tests only, just naively add amounts + nResult += AccessCoin(tx.vin[i].prevout).out.nValue.GetAmount(); return nResult; } diff --git a/src/compressor.h b/src/compressor.h index c1eda503c8..b3c44addfe 100644 --- a/src/compressor.h +++ b/src/compressor.h @@ -95,14 +95,42 @@ class CTxOutCompressor template inline void SerializationOp(Stream& s, Operation ser_action) { - if (!ser_action.ForRead()) { - uint64_t nVal = CompressAmount(txout.nValue); - READWRITE(VARINT(nVal)); + if (g_con_elementsmode) { + if (!ser_action.ForRead()) { + if (txout.nValue.IsExplicit()) { + uint8_t b = 0; + READWRITE(b); + uint64_t nVal = CompressAmount(txout.nValue.GetAmount()); + READWRITE(VARINT(nVal)); + } else { + uint8_t b = 1; + READWRITE(b); + READWRITE(txout.nValue); + } + } else { + uint8_t type = 0; + READWRITE(type); + if (type == 0) { + uint64_t nVal = 0; + READWRITE(VARINT(nVal)); + txout.nValue = DecompressAmount(nVal); + } else { + READWRITE(txout.nValue); + } + } + READWRITE(txout.nAsset); } else { - uint64_t nVal = 0; - READWRITE(VARINT(nVal)); - txout.nValue = DecompressAmount(nVal); + if (!ser_action.ForRead()) { + assert(txout.nValue.IsExplicit()); + uint64_t nVal = CompressAmount(txout.nValue.GetAmount()); + READWRITE(VARINT(nVal)); + } else { + uint64_t nVal = 0; + READWRITE(VARINT(nVal)); + txout.nValue = DecompressAmount(nVal); + } } + CScriptCompressor cscript(REF(txout.scriptPubKey)); READWRITE(cscript); } diff --git a/src/consensus/merkle.cpp b/src/consensus/merkle.cpp index febeef938c..e38d8dcdf4 100644 --- a/src/consensus/merkle.cpp +++ b/src/consensus/merkle.cpp @@ -77,13 +77,17 @@ uint256 BlockWitnessMerkleRoot(const CBlock& block, bool* mutated) { std::vector leaves; leaves.resize(block.vtx.size()); - leaves[0].SetNull(); // The witness hash of the coinbase is 0. - for (size_t s = 1; s < block.vtx.size(); s++) { - if (g_con_elementsmode) { + if (g_con_elementsmode) { + // Coinbase witness hash for inputs is just CTxInWitness().GetHash() + for (size_t s = 0; s < block.vtx.size(); s++) { leaves[s] = block.vtx[s]->GetWitnessOnlyHash(); - } else { + } + return ComputeFastMerkleRoot(std::move(leaves)); + } else { + leaves[0].SetNull(); // The witness hash of the coinbase is 0. + for (size_t s = 1; s < block.vtx.size(); s++) { leaves[s] = block.vtx[s]->GetWitnessHash(); } + return ComputeMerkleRoot(std::move(leaves), mutated); } - return ComputeMerkleRoot(std::move(leaves), mutated); } diff --git a/src/core_io.h b/src/core_io.h index d53a45c0cb..a10d748217 100644 --- a/src/core_io.h +++ b/src/core_io.h @@ -6,6 +6,7 @@ #define BITCOIN_CORE_IO_H #include +#include #include #include diff --git a/src/core_write.cpp b/src/core_write.cpp index f09472e61b..49be842f3b 100644 --- a/src/core_write.cpp +++ b/src/core_write.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include