Skip to content

Commit

Permalink
Merge pull request #25 from Crowndev/elements
Browse files Browse the repository at this point in the history
quick match
  • Loading branch information
BlockMechanic authored Feb 14, 2022
2 parents d07eb88 + 5cb844b commit 406edcd
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/assetdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CAssetData::CAssetData(const CAsset& _asset, const CTransactionRef& assetTx, con
asset = _asset;
nTime = _nTime;
txhash = assetTx->GetHash();
const CTxOut& txout = assetTx->vout[nOut];
CTxOutAsset txout = assetTx->vpout[nOut];
issuingAddress = txout.scriptPubKey;
if(txout.nValue)
issuedAmount = txout.nValue;
Expand Down
4 changes: 2 additions & 2 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ class CTestNetParams : public CChainParams {
consensus.BIP34Hash = uint256S("0x0000000023b3a96d3484e5abb3755c413e7d41500f8e2a5c3f0dd01299cd8ef8");
consensus.BIP65Height = 581885; // 00000000007f6655f22f98e72ed80d8b06dc761d5da09df0fa1dc4be4f861eb6
consensus.BIP66Height = 330776; // 000000002104c8c45e99a8853285a3b592602a3ccde2b832481da85e9e4ba182
consensus.CSVHeight = 4000; // 00000000025e930139bac5c6c31a403776da130831ab85be56578f3fa75369bb
consensus.SegwitHeight = 4000; // 00000000002b980fcd729daaa248fd9316a5200e9b367f4ff2c42453e84201ca
consensus.CSVHeight = 1; // 00000000025e930139bac5c6c31a403776da130831ab85be56578f3fa75369bb
consensus.SegwitHeight = 1; // 00000000002b980fcd729daaa248fd9316a5200e9b367f4ff2c42453e84201ca
consensus.MinBIP9WarningHeight = 836640; // segwit activation height + miner confirmation window
consensus.powLimit = uint256S("00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nPowTargetTimespan = 60; // two weeks
Expand Down
8 changes: 3 additions & 5 deletions src/consensus/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,16 @@ inline int GetWitnessCommitmentIndex(const CBlock& block)
{
int commitpos = NO_WITNESS_COMMITMENT;
if (!block.vtx.empty()) {

for(unsigned int i = 0; i < (block.vtx[0]->nVersion >= TX_ELE_VERSION ? block.vtx[0]->vpout.size() : block.vtx[0]->vout.size()) ; i++){
const CTxOutAsset& vout = (block.vtx[0]->nVersion >= TX_ELE_VERSION ? block.vtx[0]->vpout[i] : block.vtx[0]->vout[i]);

for(size_t o = 0; o < (block.vtx[0]->nVersion >= TX_ELE_VERSION ? block.vtx[0]->vpout.size() : block.vtx[0]->vout.size()) ; o++){
const CTxOutAsset& vout = (block.vtx[0]->nVersion >= TX_ELE_VERSION ? block.vtx[0]->vpout[o] : block.vtx[0]->vout[o]);
if (vout.scriptPubKey.size() >= MINIMUM_WITNESS_COMMITMENT &&
vout.scriptPubKey[0] == OP_RETURN &&
vout.scriptPubKey[1] == 0x24 &&
vout.scriptPubKey[2] == 0xaa &&
vout.scriptPubKey[3] == 0x21 &&
vout.scriptPubKey[4] == 0xa9 &&
vout.scriptPubKey[5] == 0xed) {
commitpos = i;
commitpos = o;
}
}
}
Expand Down
23 changes: 20 additions & 3 deletions src/core_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,11 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
entry.pushKV("data", vdata);

UniValue vout(UniValue::VARR);
for(unsigned int k = 0; k < (tx.nVersion >= TX_ELE_VERSION ? tx.vpout.size() : tx.vout.size()) ; k++){
CTxOutAsset txout = (tx.nVersion >= TX_ELE_VERSION ? tx.vpout[k] : tx.vout[k]);
for(unsigned int k = 0; k < tx.vout.size(); k++){
CTxOut txout = tx.vout[k];

UniValue out(UniValue::VOBJ);
out.pushKV("value", ValueFromAmount(txout.nValue));
out.pushKV("asset", txout.nAsset.getName());
out.pushKV("n", (int64_t)k);

UniValue o(UniValue::VOBJ);
Expand All @@ -309,6 +308,24 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
}
entry.pushKV("vout", vout);

if(tx.nVersion >= TX_ELE_VERSION ){
UniValue vpout(UniValue::VARR);
for(unsigned int k = 0; k < tx.vpout.size(); k++){
CTxOutAsset txout = tx.vpout[k];

UniValue out(UniValue::VOBJ);
out.pushKV("value", ValueFromAmount(txout.nValue));
out.pushKV("asset", txout.nAsset.getName());
out.pushKV("n", (int64_t)k);

UniValue o(UniValue::VOBJ);
ScriptPubKeyToUniv(txout.scriptPubKey, o, true);
out.pushKV("scriptPubKey", o);
vout.push_back(out);
}
entry.pushKV("vpout", vout);
}

if (!tx.extraPayload.empty()) {
entry.pushKV("extraPayloadSize", (int)tx.extraPayload.size());
entry.pushKV("extraPayload", HexStr(tx.extraPayload));
Expand Down
6 changes: 3 additions & 3 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc

// Create coinbase transaction.
CMutableTransaction coinbaseTx;
if(nHeight >= 4000)
if(nHeight >= 1)
coinbaseTx.nVersion = TX_ELE_VERSION;
coinbaseTx.vin.resize(1);
coinbaseTx.vin[0].prevout.SetNull();
coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;

if(nHeight <= 4000){
if(Params().NetworkIDString() == CBaseChainParams::TESTNET && nHeight < 1){
coinbaseTx.vout.resize(1);
coinbaseTx.vout[0].scriptPubKey = scriptPubKeyIn;
coinbaseTx.vout[0].nValue = nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus());
Expand Down Expand Up @@ -194,7 +194,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
}
int64_t nTime2 = GetTimeMicros();

LogPrint(BCLog::BENCH, "CreateNewBlock() packages: %.2fms (%d packages, %d updated descendants), validity: %.2fms (total %.2fms)\n", 0.001 * (nTime1 - nTimeStart), nPackagesSelected, nDescendantsUpdated, 0.001 * (nTime2 - nTime1), 0.001 * (nTime2 - nTimeStart));
LogPrint(BCLog::BENCH, "CreateNewBlock() packages: %.2fms (%d packages, %d updated descendants), validity: %.2fms (total %.2fms)\n %s \n", 0.001 * (nTime1 - nTimeStart), nPackagesSelected, nDescendantsUpdated, 0.001 * (nTime2 - nTime1), 0.001 * (nTime2 - nTimeStart), pblock->ToString());

return std::move(pblocktemplate);
}
Expand Down
54 changes: 27 additions & 27 deletions src/primitives/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,23 +345,6 @@ inline void UnserializeTransaction(TxType& tx, Stream& s) {
else
s >> tx.vout;
}

if ((flags & 1) && fAllowWitness) {
/* The witness flag is present, and we support witnesses. */
flags ^= 1;
for (size_t i = 0; i < tx.vin.size(); i++) {
s >> tx.vin[i].scriptWitness.stack;
}
if (!tx.HasWitness()) {
/* It's illegal to encode witnesses when all witness stacks are empty. */
throw std::ios_base::failure("Superfluous witness record");
}
}
if (flags) {
/* Unknown flag in the serialization */
throw std::ios_base::failure("Unknown transaction optional data");
}
s >> tx.nLockTime;
if (tx.nVersion >= TX_ELE_VERSION) {
size_t nOutputs = ReadCompactSize(s);
tx.vdata.reserve(nOutputs);
Expand All @@ -381,7 +364,25 @@ inline void UnserializeTransaction(TxType& tx, Stream& s) {
tx.vdata[k]->nVersion = bv;
s >> *tx.vdata[k];
}
} else if (tx.nVersion >= 3 && tx.nType != TRANSACTION_NORMAL) {
}

if ((flags & 1) && fAllowWitness) {
/* The witness flag is present, and we support witnesses. */
flags ^= 1;
for (size_t i = 0; i < tx.vin.size(); i++) {
s >> tx.vin[i].scriptWitness.stack;
}
if (!tx.HasWitness()) {
/* It's illegal to encode witnesses when all witness stacks are empty. */
throw std::ios_base::failure("Superfluous witness record");
}
}
if (flags) {
/* Unknown flag in the serialization */
throw std::ios_base::failure("Unknown transaction optional data");
}
s >> tx.nLockTime;
if (tx.nVersion == 3 && tx.nType != TRANSACTION_NORMAL) {
s >> tx.extraPayload;
}
}
Expand All @@ -408,26 +409,25 @@ inline void SerializeTransaction(const TxType& tx, Stream& s) {
s << flags;
}
s << tx.vin;

if (tx.nVersion >= TX_ELE_VERSION)
s << tx.vpout;
else
s << tx.vout;

if (flags & 1) {
for (size_t i = 0; i < tx.vin.size(); i++) {
s << tx.vin[i].scriptWitness.stack;
}
}
s << tx.nLockTime;

if (tx.nVersion >= TX_ELE_VERSION) {
WriteCompactSize(s, tx.vdata.size());
for (size_t k = 0; k < tx.vdata.size(); ++k) {
s << tx.vdata[k]->nVersion;
s << *tx.vdata[k];
}
} else if (tx.nVersion >= 3 && tx.nType != TRANSACTION_NORMAL) {
}
if (flags & 1) {
for (size_t i = 0; i < tx.vin.size(); i++) {
s << tx.vin[i].scriptWitness.stack;
}
}
s << tx.nLockTime;
if (tx.nVersion == 3 && tx.nType != TRANSACTION_NORMAL) {
s << tx.extraPayload;
}
}
Expand Down
29 changes: 14 additions & 15 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,7 @@ CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
}

int halvings = nHeight / Params().GetConsensus().nSubsidyHalvingInterval;
if (Params().NetworkIDString() == CBaseChainParams::TESTNET && nHeight < 4000)
if (Params().NetworkIDString() == CBaseChainParams::TESTNET && nHeight < 1000)
nSubsidy = 100 * COIN;

// Subsidy is cut in half every 2,100,000 blocks which will occur approximately every 4 years.
Expand All @@ -1360,7 +1360,7 @@ CAmount GetBlockValue(int nHeight, const CAmount &nFees)

int64_t budgetValue = nSubsidy * 0.25;
if (Params().NetworkIDString() == CBaseChainParams::TESTNET) {
if (nHeight >= 4000)
if (nHeight >= 1000)
nSubsidy -= budgetValue;
} else {
if (nHeight > 1265000)
Expand Down Expand Up @@ -4055,21 +4055,17 @@ std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBloc
{
std::vector<unsigned char> commitment;
int commitpos = GetWitnessCommitmentIndex(block);
LogPrintf("COMMIT POSE %d\n", commitpos);

std::vector<unsigned char> ret(32, 0x00);
if (consensusParams.SegwitHeight != std::numeric_limits<int>::max()) {
if (commitpos == NO_WITNESS_COMMITMENT) {
CMutableTransaction tx0(*block.vtx[0]);
if(block.vtx[0]->nVersion == TX_ELE_VERSION)
tx0.vpout.push_back(CTxOutAsset());
else
tx0.vout.push_back(CTxOut());
block.vtx[0] = MakeTransactionRef(std::move(tx0));
uint256 witnessroot = BlockWitnessMerkleRoot(block, nullptr);
CHash256().Write(witnessroot).Write(ret).Finalize(witnessroot);
CTxOutAsset out;
out.nValue = 0;
if(block.vtx[0]->nVersion == TX_ELE_VERSION)
out.nAsset = block.vtx[0]->vpout[0].nAsset;
out.nAsset = consensusParams.subsidy_asset;
out.scriptPubKey.resize(MINIMUM_WITNESS_COMMITMENT);
out.scriptPubKey[0] = OP_RETURN;
out.scriptPubKey[1] = 0x24;
Expand All @@ -4081,9 +4077,9 @@ std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBloc
commitment = std::vector<unsigned char>(out.scriptPubKey.begin(), out.scriptPubKey.end());
CMutableTransaction tx(*block.vtx[0]);
if(block.vtx[0]->nVersion == TX_ELE_VERSION)
tx.vpout.back() = out;
tx.vpout.push_back(out);
else
tx.vout.back() = out;
tx.vout.push_back(out);
block.vtx[0] = MakeTransactionRef(std::move(tx));
}
}
Expand Down Expand Up @@ -4176,7 +4172,6 @@ static bool ContextualCheckBlock(const CBlock& block, BlockValidationState& stat
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-nonfinal", "non-final transaction");
}
}

// Enforce rule that the coinbase starts with serialized block height
if (nHeight >= consensusParams.BIP34Height)
{
Expand All @@ -4186,7 +4181,6 @@ static bool ContextualCheckBlock(const CBlock& block, BlockValidationState& stat
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-height", "block height mismatch in coinbase");
}
}

// Validation for witness commitments.
// * We compute the witness hash (which is the hash including witnesses) of all the block's transactions, except the
// coinbase (where 0x0000....0000 is used instead).
Expand All @@ -4199,21 +4193,26 @@ static bool ContextualCheckBlock(const CBlock& block, BlockValidationState& stat
if (nHeight >= consensusParams.SegwitHeight) {

int commitpos = GetWitnessCommitmentIndex(block);
LogPrintf("COMMIT POSE2 %d\n", commitpos);

if (commitpos != NO_WITNESS_COMMITMENT) {
bool malleated = false;
uint256 hashWitness = BlockWitnessMerkleRoot(block, &malleated);
LogPrintf("TESTING 0 \n %s \n", block.ToString());
// The malleation check is ignored; as the transaction tree itself
// already does not permit it, it is impossible to trigger in the
// witness tree.
LogPrintf("TESTING 1 %d \n", block.vtx[0]->vin[0].scriptWitness.stack[0].size());
if (block.vtx[0]->vin[0].scriptWitness.stack.size() != 1 || block.vtx[0]->vin[0].scriptWitness.stack[0].size() != 32) {
return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "bad-witness-nonce-size", strprintf("%s : invalid witness reserved value size", __func__));
return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "bad-witness-nonce-size",
strprintf("%s : %d %d invalid witness reserved value size", __func__, block.vtx[0]->vin[0].scriptWitness.stack.size(), block.vtx[0]->vin[0].scriptWitness.stack[0].size()));
}
LogPrintf("TESTING 2 %d \n", block.vtx[0]->vin[0].scriptWitness.stack.size());
CHash256().Write(hashWitness).Write(block.vtx[0]->vin[0].scriptWitness.stack[0]).Finalize(hashWitness);

if (memcmp(hashWitness.begin(),(block.vtx[0]->nVersion == TX_ELE_VERSION ? &block.vtx[0]->vpout[commitpos].scriptPubKey[6] : &block.vtx[0]->vout[commitpos].scriptPubKey[6]), 32)) {
return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "bad-witness-merkle-match", strprintf("%s : witness merkle commitment mismatch", __func__));
}

fHaveWitness = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ static RPCHelpMan sendtoaddress()
}

if (asset.IsNull()) {
throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Unknown label and invalid asset hex: %s", asset.GetHex()));
throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Unknown label and invalid asset hex: %s %s", asset.GetHex(), asset.getName()));
}

// Wallet comments
Expand Down

0 comments on commit 406edcd

Please sign in to comment.