Skip to content

Commit

Permalink
Merge pull request bitcoin#40 from CryptAxe/9517ScriptHeaders
Browse files Browse the repository at this point in the history
Update coinbase commitments and drivechain scripts
  • Loading branch information
psztorc committed Oct 4, 2017
2 parents 4fecdf0 + d1551e5 commit 61f4244
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 18 deletions.
25 changes: 12 additions & 13 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
pblock->vtx.push_back(MakeTransactionRef(std::move(wtx)));
}

// Add SidechainDB state
CTransaction stateTx = CreateSidechainStateTx();
for (const CTxOut& out : stateTx.vout) {
coinbaseTx.vout.push_back(out);
nSideFees += out.nValue;
if (scdb.HasState()) {
// Add SidechainDB hashMerkleRoot coinbase commitment
CTxOut scdbCommit = CreateSCDBHashMerkleRootCommit();
nSideFees += scdbCommit.nValue;
coinbaseTx.vout.push_back(scdbCommit);
}

coinbaseTx.vout[0].nValue = nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus());
Expand Down Expand Up @@ -445,15 +445,14 @@ CTransaction BlockAssembler::CreateSidechainWTJoinTx(uint8_t nSidechain)
return mtx;
}

CTransaction BlockAssembler::CreateSidechainStateTx()
CTxOut BlockAssembler::CreateSCDBHashMerkleRootCommit()
{
CMutableTransaction mtx;

CScript script = scdb.CreateStateScript(nHeight);
if (!script.empty())
mtx.vout.push_back(CTxOut(CENT, script));

return mtx;
// TODO this call will be replaced when the MT based update PR Is merged.
// For now just getting the old style SCDB hash and putting it into the
// commitment.
const uint256& hashMerkleRoot = scdb.GetSCDBHash();
CScript script = GenerateSCDBCoinbaseCommitment(hashMerkleRoot);
return CTxOut(CENT, script);
}

// Skip entries in mapTx that are already in a block or are present
Expand Down
4 changes: 2 additions & 2 deletions src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ class BlockAssembler
// SidechainDB
/** Returns a WT^ payout transaction for nSidechain if there is one */
CTransaction CreateSidechainWTJoinTx(uint8_t nSidechain);
/** Returns a SCDB state update transaction (update script in output) */
CTransaction CreateSidechainStateTx();
/** Returns an output with a commitment to the SCDB hashMerkleRoot */
CTxOut CreateSCDBHashMerkleRootCommit();
};

/** Modify the extranonce in a block */
Expand Down
38 changes: 37 additions & 1 deletion src/script/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ bool CScript::IsWitnessProgram(int& version, std::vector<unsigned char>& program
return false;
}

bool CScript::IsBribe() const
bool CScript::IsBribeHashCommit() const
{
// TODO
// Size must be at least:
Expand All @@ -273,6 +273,42 @@ bool CScript::IsBribe() const
return (this->Find(OP_BRIBE));
}

bool CScript::IsSCDBHashMerkleRootCommit() const
{
// Check script size
size_t size = this->size();
if (size != 38) // sha256 hash + opcodes
return false;

// Check script header
if ((*this)[0] != OP_RETURN ||
(*this)[1] != 0x43 ||
(*this)[2] != 0x50 ||
(*this)[3] != 0x50 ||
(*this)[4] != 0x53)
return false;

return true;
}

bool CScript::IsWTPrimeHashCommit() const
{
// Check script size
size_t size = this->size();
if (size < 39 || size > 41) // sha256 hash + nSidechain + opcodes
return false;

// Check script header
if ((*this)[0] != OP_RETURN ||
(*this)[1] != 0x53 ||
(*this)[2] != 0x50 ||
(*this)[3] != 0x50 ||
(*this)[4] != 0x43)
return false;

return true;
}

bool CScript::IsPushOnly(const_iterator pc) const
{
while (pc < end())
Expand Down
6 changes: 5 additions & 1 deletion src/script/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,11 @@ class CScript : public CScriptBase
bool IsPayToScriptHash() const;
bool IsPayToWitnessScriptHash() const;
bool IsWitnessProgram(int& version, std::vector<unsigned char>& program) const;
bool IsBribe() const;

/** Script formats for Drivechains */
bool IsBribeHashCommit() const;
bool IsSCDBHashMerkleRootCommit() const;
bool IsWTPrimeHashCommit() const;

/** Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical). */
bool IsPushOnly(const_iterator pc) const;
Expand Down
43 changes: 42 additions & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@ bool CScriptCheck::operator()() {
const CScriptWitness *witness = &ptxTo->vin[nIn].scriptWitness;

std::multimap<uint256, int> mapBMMLDCopy;
if (scriptPubKey.IsBribe())
if (scriptPubKey.IsBribeHashCommit())
mapBMMLDCopy = scdb.GetLinkingData();

return VerifyScript(scriptSig, scriptPubKey, witness, nFlags, CachingTransactionSignatureChecker(ptxTo, nIn, amount, cacheStore, *txdata, mapBMMLDCopy), &error);
Expand Down Expand Up @@ -2877,6 +2877,47 @@ std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBloc
return commitment;
}

CScript GenerateSCDBCoinbaseCommitment(const uint256& hashMerkleRoot)
{
// TODO
// check consensusParams.vDeployments[Consensus::DEPLOYMENT_DRIVECHAINS]
CScript script;

// Add script header
script << OP_RETURN;
script.push_back(0x43);
script.push_back(0x50);
script.push_back(0x50);
script.push_back(0x53);

// Add SCDB hashMerkleRoot
script << ToByteVector(hashMerkleRoot);

return script;
}

CScript GenerateBMMCriticalHashCommitment(int nHeight, const uint256& hashCritical)
{
// TODO
// check consensusParams.vDeployments[Consensus::DEPLOYMENT_DRIVECHAINS]
CScript script;

// Add script header
script << OP_RETURN;
script.push_back(0x53);
script.push_back(0x50);
script.push_back(0x50);
script.push_back(0x43);

// Add block number
script << CScriptNum(nHeight);

// Add h*
script << ToByteVector(hashCritical);

return script;
}

bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
{
assert(pindexPrev != NULL);
Expand Down
6 changes: 6 additions & 0 deletions src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,12 @@ void UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPr
/** Produce the necessary coinbase commitment for a block (modifies the hash, don't call for mined blocks). */
std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams);

/** Produce the SCDB hashMerkleRoot coinbase commitment for a block */
CScript GenerateSCDBCoinbaseCommitment(const uint256& hashMerkleRoot);

/** Produce a BMM h* coinbase commitment for a block */
CScript GeneratBMMCriticalHashCommitment(int nHeight, const uint256& hashCritical);

/** RAII wrapper for VerifyDB: Verify consistency of the block and coin databases */
class CVerifyDB {
public:
Expand Down

0 comments on commit 61f4244

Please sign in to comment.