-
Notifications
You must be signed in to change notification settings - Fork 411
2WP: Allow other elements chains as parent chains for pegs #362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| #include "crypto/hmac_sha256.h" | ||
| #include "init.h" | ||
| #include "issuance.h" | ||
| #include "merkleblock.h" | ||
| #include "policy/fees.h" | ||
| #include "policy/policy.h" | ||
| #include "pow.h" | ||
|
|
@@ -2374,6 +2375,21 @@ bool GetAmountFromParentChainPegin(CAmount& amount, const Sidechain::Bitcoin::CT | |
| return true; | ||
| } | ||
|
|
||
| bool GetAmountFromParentChainPegin(CAmount& amount, const CTransaction& txBTC, unsigned int nOut) | ||
| { | ||
| if (!txBTC.vout[nOut].nValue.IsExplicit()) { | ||
| return false; | ||
| } | ||
| if (!txBTC.vout[nOut].nAsset.IsExplicit()) { | ||
| return false; | ||
| } | ||
| if (txBTC.vout[nOut].nAsset.GetAsset() != Params().GetConsensus().parent_pegged_asset) { | ||
| return false; | ||
| } | ||
| amount = txBTC.vout[nOut].nValue.GetAmount(); | ||
| return true; | ||
| } | ||
|
|
||
| template<typename T> | ||
| static bool GetBlockAndTxFromMerkleBlock(uint256& block_hash, uint256& tx_hash, T& merkle_block, const std::vector<unsigned char>& merkle_block_raw) | ||
| { | ||
|
|
@@ -2497,20 +2513,36 @@ bool IsValidPeginWitness(const CScriptWitness& pegin_witness, const COutPoint& p | |
|
|
||
| uint256 block_hash; | ||
| uint256 tx_hash; | ||
|
|
||
| // Get txout proof | ||
| Sidechain::Bitcoin::CMerkleBlock merkle_block; | ||
| if (!GetBlockAndTxFromMerkleBlock(block_hash, tx_hash, merkle_block, stack[5])) { | ||
| return false; | ||
| } | ||
| if (!CheckBitcoinProof(block_hash, merkle_block.header.nBits)) { | ||
| return false; | ||
| } | ||
| if (Params().GetConsensus().ParentChainHasPow()) { | ||
|
|
||
| // Get serialized transaction | ||
| Sidechain::Bitcoin::CTransactionRef pegtx; | ||
| if (!CheckPeginTx(stack[4], pegtx, prevout, value, claim_script)) { | ||
| return false; | ||
| Sidechain::Bitcoin::CMerkleBlock merkle_block_pow; | ||
| if (!GetBlockAndTxFromMerkleBlock(block_hash, tx_hash, merkle_block_pow, stack[5])) { | ||
| return false; | ||
| } | ||
| if (!CheckBitcoinProof(block_hash, merkle_block_pow.header.nBits)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now we have two very similar things with different naming conventions, Can we just abstract this out to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I just wanted to avoid changing CheckProof since it is called a few times. I could use a boolean that defaults to false, but I tend to dislike default arguments in C++. I find
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fair enough, perhaps a minor renaming then to make them obviously related
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or heck just comment what it's doing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed to CheckProofSignedParent as discussed on chat. |
||
| return false; | ||
| } | ||
|
|
||
| Sidechain::Bitcoin::CTransactionRef pegtx; | ||
| if (!CheckPeginTx(stack[4], pegtx, prevout, value, claim_script)) { | ||
| return false; | ||
| } | ||
| } else { | ||
|
|
||
| CMerkleBlock merkle_block; | ||
| if (!GetBlockAndTxFromMerkleBlock(block_hash, tx_hash, merkle_block, stack[5])) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!CheckProofSignedParent(merkle_block.header, Params().GetConsensus())) { | ||
| return false; | ||
| } | ||
|
|
||
| CTransactionRef pegtx; | ||
| if (!CheckPeginTx(stack[4], pegtx, prevout, value, claim_script)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // Check that the merkle proof corresponds to the txid | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2082,7 +2082,7 @@ UniValue gettransaction(const JSONRPCRequest& request) | |
| " \"fee\": x.xxx, (numeric) The amount of the fee in " + CURRENCY_UNIT + ". This is negative and only available for the \n" | ||
| " 'send' category of transactions.\n" | ||
| " \"abandoned\": xxx (bool) 'true' if the transaction has been abandoned (inputs are respendable). Only available for the \n" | ||
| " 'send' category of transactions.\n" | ||
| " 'send' category of transactions.\n" | ||
| " }\n" | ||
| " ,...\n" | ||
| " ],\n" | ||
|
|
@@ -3512,7 +3512,8 @@ UniValue sendtomainchain(const JSONRPCRequest& request) | |
| extern UniValue signrawtransaction(const JSONRPCRequest& request); | ||
| extern UniValue sendrawtransaction(const JSONRPCRequest& request); | ||
|
|
||
| unsigned int GetPeginTxnOutputIndex(const Sidechain::Bitcoin::CTransaction& txn, const CScript& witnessProgram) | ||
| template<typename T_tx> | ||
| unsigned int GetPeginTxnOutputIndex(const T_tx& txn, const CScript& witnessProgram) | ||
| { | ||
| unsigned int nOut = 0; | ||
| //Call contracthashtool | ||
|
|
@@ -3523,7 +3524,8 @@ unsigned int GetPeginTxnOutputIndex(const Sidechain::Bitcoin::CTransaction& txn, | |
| return nOut; | ||
| } | ||
|
|
||
| UniValue createrawpegin(const JSONRPCRequest& request) | ||
| template<typename T_tx_ref, typename T_tx, typename T_merkle_block> | ||
| static UniValue createrawpegin(const JSONRPCRequest& request, T_tx_ref& txBTCRef, T_tx& tx_aux, T_merkle_block& merkleBlock) | ||
| { | ||
| if (request.fHelp || request.params.size() < 2 || request.params.size() > 3) | ||
| throw std::runtime_error( | ||
|
|
@@ -3552,27 +3554,26 @@ UniValue createrawpegin(const JSONRPCRequest& request) | |
|
|
||
| std::vector<unsigned char> txData = ParseHex(request.params[0].get_str()); | ||
| CDataStream ssTx(txData, SER_NETWORK, PROTOCOL_VERSION); | ||
| Sidechain::Bitcoin::CTransactionRef txBTCRef; | ||
| try { | ||
| ssTx >> txBTCRef; | ||
| } | ||
| catch (...) { | ||
| throw JSONRPCError(RPC_TYPE_ERROR, "The included bitcoinTx is malformed. Are you sure that is the whole string?"); | ||
| } | ||
| Sidechain::Bitcoin::CTransaction txBTC(*txBTCRef); | ||
| T_tx txBTC(*txBTCRef); | ||
|
|
||
| std::vector<unsigned char> txOutProofData = ParseHex(request.params[1].get_str()); | ||
| CDataStream ssTxOutProof(txOutProofData, SER_NETWORK, PROTOCOL_VERSION); | ||
| Sidechain::Bitcoin::CMerkleBlock merkleBlock; | ||
| try { | ||
| ssTxOutProof >> merkleBlock; | ||
| } | ||
| catch (...) { | ||
| throw JSONRPCError(RPC_TYPE_ERROR, "The included txoutproof is malformed. Are you sure that is the whole string?"); | ||
| } | ||
|
|
||
| if (!ssTxOutProof.empty() || !CheckBitcoinProof(merkleBlock.header.GetHash(), merkleBlock.header.nBits)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we dropping this check?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if (!ssTxOutProof.empty()) { | ||
| throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid tx out proof"); | ||
| } | ||
|
|
||
| std::vector<uint256> txHashes; | ||
| std::vector<unsigned int> txIndices; | ||
|
|
@@ -3622,7 +3623,11 @@ UniValue createrawpegin(const JSONRPCRequest& request) | |
| throw JSONRPCError(RPC_INVALID_PARAMETER, "Given or recovered script is not a witness program."); | ||
| } | ||
|
|
||
| CAmount value = txBTC.vout[nOut].nValue; | ||
| CAmount value = 0; | ||
| if (!GetAmountFromParentChainPegin(value, txBTC, nOut)) { | ||
| throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Amounts to pegin must be explicit and asset must be %s", | ||
| Params().GetConsensus().parent_pegged_asset.GetHex())); | ||
| } | ||
|
|
||
| CDataStream stream(0, 0); | ||
| try { | ||
|
|
@@ -3703,6 +3708,29 @@ UniValue createrawpegin(const JSONRPCRequest& request) | |
| return ret; | ||
| } | ||
|
|
||
| UniValue createrawpegin(const JSONRPCRequest& request) | ||
| { | ||
| UniValue ret(UniValue::VOBJ); | ||
| if (Params().GetConsensus().ParentChainHasPow()) { | ||
| Sidechain::Bitcoin::CTransactionRef txBTCRef; | ||
| Sidechain::Bitcoin::CTransaction tx_aux; | ||
| Sidechain::Bitcoin::CMerkleBlock merkleBlock; | ||
| ret = createrawpegin(request, txBTCRef, tx_aux, merkleBlock); | ||
| if (!CheckBitcoinProof(merkleBlock.header.GetHash(), merkleBlock.header.nBits)) { | ||
| throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid tx out proof"); | ||
| } | ||
| } else { | ||
| CTransactionRef txBTCRef; | ||
| CTransaction tx_aux; | ||
| CMerkleBlock merkleBlock; | ||
| ret = createrawpegin(request, txBTCRef, tx_aux, merkleBlock); | ||
| if (!CheckProofSignedParent(merkleBlock.header, Params().GetConsensus())) { | ||
| throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid tx out proof"); | ||
| } | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| UniValue claimpegin(const JSONRPCRequest& request) | ||
| { | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should expose this via
getsidechaininfo, including a binary saying if it's PoW for ease of readingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed