2018-12-14 15:30:59 +01:00
|
|
|
// Copyright (c) 2017-2017 The Bitcoin Core developers
|
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
|
|
#include <pegins.h>
|
|
|
|
|
|
|
|
|
|
#include <arith_uint256.h>
|
|
|
|
|
#include <block_proof.h>
|
|
|
|
|
#include <chainparams.h>
|
|
|
|
|
#include <crypto/hmac_sha256.h>
|
|
|
|
|
#include <consensus/consensus.h>
|
|
|
|
|
#include <consensus/validation.h>
|
|
|
|
|
#include <mainchainrpc.h>
|
2018-12-20 12:32:29 +01:00
|
|
|
#include <merkleblock.h>
|
2018-12-14 15:30:59 +01:00
|
|
|
#include <pow.h>
|
|
|
|
|
#include <primitives/transaction.h>
|
|
|
|
|
#include <primitives/bitcoin/merkleblock.h>
|
|
|
|
|
#include <secp256k1.h>
|
|
|
|
|
#include <script/interpreter.h>
|
|
|
|
|
#include <script/standard.h>
|
|
|
|
|
#include <streams.h>
|
2019-05-01 20:51:19 +01:00
|
|
|
#include <util/system.h>
|
2019-05-31 11:30:10 -04:00
|
|
|
#include <dynafed.h>
|
2018-12-14 15:30:59 +01:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// ELEMENTS
|
|
|
|
|
//
|
|
|
|
|
|
2023-09-29 12:04:59 -07:00
|
|
|
#include <validation.h>
|
|
|
|
|
|
2018-12-14 15:30:59 +01:00
|
|
|
namespace {
|
|
|
|
|
static secp256k1_context* secp256k1_ctx_validation;
|
|
|
|
|
|
|
|
|
|
class Secp256k1Ctx
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Secp256k1Ctx() {
|
|
|
|
|
assert(secp256k1_ctx_validation == NULL);
|
|
|
|
|
secp256k1_ctx_validation = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN);
|
|
|
|
|
assert(secp256k1_ctx_validation != NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~Secp256k1Ctx() {
|
|
|
|
|
assert(secp256k1_ctx_validation != NULL);
|
|
|
|
|
secp256k1_context_destroy(secp256k1_ctx_validation);
|
|
|
|
|
secp256k1_ctx_validation = NULL;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
static Secp256k1Ctx instance_of_secp256k1ctx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GetAmountFromParentChainPegin(CAmount& amount, const Sidechain::Bitcoin::CTransaction& txBTC, unsigned int nOut)
|
|
|
|
|
{
|
|
|
|
|
amount = txBTC.vout[nOut].nValue;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GetAmountFromParentChainPegin(CAmount& amount, const CTransaction& txBTC, unsigned int nOut)
|
|
|
|
|
{
|
2019-03-19 20:26:39 +00:00
|
|
|
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();
|
2018-12-14 15:30:59 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Takes federation redeem script and adds HMAC_SHA256(pubkey, scriptPubKey) as a tweak to each pubkey
|
2019-01-15 10:22:45 -05:00
|
|
|
CScript calculate_contract(const CScript& federation_script, const CScript& scriptPubKey) {
|
2018-12-14 15:30:59 +01:00
|
|
|
CScript scriptDestination;
|
2019-05-31 11:29:02 -04:00
|
|
|
|
2019-03-19 20:31:58 +00:00
|
|
|
bool is_liquidv1_watchman = MatchLiquidWatchman(federation_script);
|
2018-12-14 15:30:59 +01:00
|
|
|
|
2019-03-19 20:31:58 +00:00
|
|
|
CScript::const_iterator sdpc = federation_script.begin();
|
|
|
|
|
std::vector<unsigned char> vch;
|
|
|
|
|
opcodetype opcodeTmp;
|
|
|
|
|
bool liquid_op_else_found = false;
|
|
|
|
|
while (federation_script.GetOp(sdpc, opcodeTmp, vch))
|
2018-12-14 15:30:59 +01:00
|
|
|
{
|
2019-05-31 11:29:02 -04:00
|
|
|
|
|
|
|
|
// For liquidv1 initial watchman template, don't tweak emergency keys
|
2019-03-19 20:31:58 +00:00
|
|
|
if (is_liquidv1_watchman && opcodeTmp == OP_ELSE) {
|
|
|
|
|
liquid_op_else_found = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t pub_len = 33;
|
|
|
|
|
if (vch.size() == pub_len && !liquid_op_else_found)
|
2018-12-14 15:30:59 +01:00
|
|
|
{
|
2019-03-19 20:31:58 +00:00
|
|
|
unsigned char tweak[32];
|
|
|
|
|
CHMAC_SHA256(vch.data(), pub_len).Write(scriptPubKey.data(), scriptPubKey.size()).Finalize(tweak);
|
|
|
|
|
int ret;
|
|
|
|
|
secp256k1_pubkey watchman;
|
|
|
|
|
secp256k1_pubkey tweaked;
|
|
|
|
|
ret = secp256k1_ec_pubkey_parse(secp256k1_ctx_validation, &watchman, vch.data(), pub_len);
|
|
|
|
|
assert(ret == 1);
|
|
|
|
|
ret = secp256k1_ec_pubkey_parse(secp256k1_ctx_validation, &tweaked, vch.data(), pub_len);
|
|
|
|
|
assert(ret == 1);
|
|
|
|
|
// If someone creates a tweak that makes this fail, they broke SHA256
|
|
|
|
|
ret = secp256k1_ec_pubkey_tweak_add(secp256k1_ctx_validation, &tweaked, tweak);
|
|
|
|
|
assert(ret == 1);
|
|
|
|
|
unsigned char new_pub[33];
|
|
|
|
|
ret = secp256k1_ec_pubkey_serialize(secp256k1_ctx_validation, new_pub, &pub_len, &tweaked, SECP256K1_EC_COMPRESSED);
|
|
|
|
|
assert(ret == 1);
|
|
|
|
|
assert(pub_len == 33);
|
|
|
|
|
|
|
|
|
|
// push tweaked pubkey
|
|
|
|
|
std::vector<unsigned char> pub_vec(new_pub, new_pub + pub_len);
|
|
|
|
|
scriptDestination << pub_vec;
|
|
|
|
|
|
|
|
|
|
// Sanity checks to reduce pegin risk. If the tweaked
|
|
|
|
|
// value flips a bit, we may lose pegin funds irretrievably.
|
|
|
|
|
// We take the tweak, derive its pubkey and check that
|
|
|
|
|
// `tweaked - watchman = tweak` to check the computation
|
|
|
|
|
// two different ways
|
|
|
|
|
secp256k1_pubkey tweaked2;
|
|
|
|
|
ret = secp256k1_ec_pubkey_create(secp256k1_ctx_validation, &tweaked2, tweak);
|
|
|
|
|
assert(ret);
|
|
|
|
|
ret = secp256k1_ec_pubkey_negate(secp256k1_ctx_validation, &watchman);
|
|
|
|
|
assert(ret);
|
|
|
|
|
secp256k1_pubkey* pubkey_combined[2];
|
|
|
|
|
pubkey_combined[0] = &watchman;
|
|
|
|
|
pubkey_combined[1] = &tweaked;
|
|
|
|
|
secp256k1_pubkey maybe_tweaked2;
|
|
|
|
|
ret = secp256k1_ec_pubkey_combine(secp256k1_ctx_validation, &maybe_tweaked2, pubkey_combined, 2);
|
|
|
|
|
assert(ret);
|
|
|
|
|
assert(!memcmp(&maybe_tweaked2, &tweaked2, 64));
|
|
|
|
|
} else {
|
|
|
|
|
// add to script untouched
|
|
|
|
|
if (vch.size() > 0) {
|
|
|
|
|
scriptDestination << vch;
|
2018-12-14 15:30:59 +01:00
|
|
|
} else {
|
2019-03-19 20:31:58 +00:00
|
|
|
scriptDestination << opcodeTmp;
|
2018-12-14 15:30:59 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return scriptDestination;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2019-06-17 16:35:52 -04:00
|
|
|
static bool CheckPeginTx(const std::vector<unsigned char>& tx_data, T& pegtx, const COutPoint& prevout, const CAmount claim_amount, const CScript& claim_script, const std::vector<std::pair<CScript, CScript>>& fedpegscripts)
|
2018-12-14 15:30:59 +01:00
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
CDataStream pegtx_stream(tx_data, SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
|
pegtx_stream >> pegtx;
|
|
|
|
|
if (!pegtx_stream.empty()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-09-28 17:24:47 -07:00
|
|
|
} catch (std::exception&) {
|
2018-12-14 15:30:59 +01:00
|
|
|
// Invalid encoding of transaction
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that transaction matches txid
|
|
|
|
|
if (pegtx->GetHash() != prevout.hash) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (prevout.n >= pegtx->vout.size()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
CAmount amount = 0;
|
|
|
|
|
if (!GetAmountFromParentChainPegin(amount, *pegtx, prevout.n)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Check the transaction nout/value matches
|
|
|
|
|
if (claim_amount != amount) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 16:35:52 -04:00
|
|
|
// Check that the witness program matches the p2ch on the (p2sh-)p2wsh
|
|
|
|
|
// transaction output. We support multiple scripts as a grace period for peg-in users
|
|
|
|
|
for (const auto& scripts : fedpegscripts) {
|
2019-06-18 14:56:05 -04:00
|
|
|
int fedpeg_version = 0;
|
|
|
|
|
std::vector<unsigned char> fedpeg_program;
|
|
|
|
|
scripts.first.IsWitnessProgram(fedpeg_version, fedpeg_program);
|
|
|
|
|
// We immediately return true if any fedpegscripts are unencumbered
|
|
|
|
|
// by currently-known parent chain segwit versions.
|
|
|
|
|
// TODO: Refactor for future versionbits deployment of parent-segwit version
|
|
|
|
|
if (fedpeg_version > 0) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-06-17 16:35:52 -04:00
|
|
|
CScript tweaked_fedpegscript = calculate_contract(scripts.second, claim_script);
|
|
|
|
|
// TODO: Remove script/standard.h dep for GetScriptFor*
|
2020-11-27 21:29:55 +00:00
|
|
|
CScript expected_script(GetScriptForDestination(WitnessV0ScriptHash(tweaked_fedpegscript)));
|
2019-06-17 16:35:52 -04:00
|
|
|
if (scripts.first.IsPayToScriptHash()) {
|
|
|
|
|
expected_script = GetScriptForDestination(ScriptHash(expected_script));
|
|
|
|
|
}
|
2019-05-31 13:43:14 -04:00
|
|
|
if (pegtx->vout[prevout.n].scriptPubKey == expected_script) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-12-14 15:30:59 +01:00
|
|
|
}
|
2019-05-31 13:43:14 -04:00
|
|
|
return false;
|
2018-12-14 15:30:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2019-03-25 12:11:03 -04:00
|
|
|
static bool GetBlockAndTxFromMerkleBlock(uint256& block_hash, uint256& tx_hash, unsigned int& tx_index, T& merkle_block, const std::vector<unsigned char>& merkle_block_raw)
|
2018-12-14 15:30:59 +01:00
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
std::vector<uint256> tx_hashes;
|
|
|
|
|
std::vector<unsigned int> tx_indices;
|
2019-05-31 11:31:02 -04:00
|
|
|
CDataStream merkle_block_stream(merkle_block_raw, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
|
2018-12-14 15:30:59 +01:00
|
|
|
merkle_block_stream >> merkle_block;
|
|
|
|
|
block_hash = merkle_block.header.GetHash();
|
|
|
|
|
|
|
|
|
|
if (!merkle_block_stream.empty()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (merkle_block.txn.ExtractMatches(tx_hashes, tx_indices) != merkle_block.header.hashMerkleRoot || tx_hashes.size() != 1) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
tx_hash = tx_hashes[0];
|
2019-03-25 12:11:03 -04:00
|
|
|
tx_index = tx_indices[0];
|
2023-09-27 12:01:23 +00:00
|
|
|
} catch (std::exception&) {
|
2018-12-14 15:30:59 +01:00
|
|
|
// Invalid encoding of merkle block
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CheckParentProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
|
|
|
|
|
{
|
|
|
|
|
bool fNegative;
|
|
|
|
|
bool fOverflow;
|
|
|
|
|
arith_uint256 bnTarget;
|
|
|
|
|
|
|
|
|
|
bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
|
|
|
|
|
|
|
|
|
|
// Check range
|
|
|
|
|
if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.parentChainPowLimit))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Check proof of work matches claimed amount
|
|
|
|
|
if (UintToArith256(hash) > bnTarget)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
When validation is waiting for parent chain daemon, "stall".
Currently, if -validatepegin is given, and block validation can't proceed
because the parent chain is not synced, we mark the block invalid and put
it in a queue to be "revalidated" later. Unfortunately, marking a block
invalid has downstream consequences, in particular causing descendant blocks
to be marked invalid, which are not currently fixed by the queue.
Instead, we'll use a different strategy: if the mainchain daemon isn't
sufficiently synced to validate a block, we will "stall" connecting that
block to the chain, and have ActivateBestChain simply keep the tip at the
previous block until we're ready.
We can still download and validate (partly) blocks past this point while
we're waiting. They will be connected once the parent chain daemon catches
up.
2021-09-07 17:01:33 +00:00
|
|
|
bool IsValidPeginWitness(const CScriptWitness& pegin_witness, const std::vector<std::pair<CScript, CScript>>& fedpegscripts, const COutPoint& prevout, std::string& err_msg, bool check_depth, bool* depth_failed) {
|
|
|
|
|
if (depth_failed) {
|
|
|
|
|
*depth_failed = false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-14 15:30:59 +01:00
|
|
|
// 0) Return false if !consensus.has_parent_chain
|
|
|
|
|
if (!Params().GetConsensus().has_parent_chain) {
|
2019-01-29 19:00:06 +00:00
|
|
|
err_msg = "Parent chain is not enabled on this network.";
|
2018-12-14 15:30:59 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Format on stack is as follows:
|
|
|
|
|
// 1) value - the value of the pegin output
|
|
|
|
|
// 2) asset type - the asset type being pegged in
|
|
|
|
|
// 3) genesis blockhash - genesis block of the parent chain
|
|
|
|
|
// 4) claim script - script to be evaluated for spend authorization
|
|
|
|
|
// 5) serialized transaction - serialized bitcoin transaction
|
|
|
|
|
// 6) txout proof - merkle proof connecting transaction to header
|
|
|
|
|
//
|
|
|
|
|
// First 4 values(plus prevout) are enough to validate a peg-in without any internal knowledge
|
|
|
|
|
// of Bitcoin serialization. This is useful for further abstraction by outsourcing
|
|
|
|
|
// the other validity checks to RPC calls.
|
|
|
|
|
|
|
|
|
|
const std::vector<std::vector<unsigned char> >& stack = pegin_witness.stack;
|
|
|
|
|
// Must include all elements
|
|
|
|
|
if (stack.size() != 6) {
|
2019-01-29 19:00:06 +00:00
|
|
|
err_msg = "Not enough stack items.";
|
2018-12-14 15:30:59 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CDataStream stream(stack[0], SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
|
CAmount value;
|
|
|
|
|
try {
|
|
|
|
|
stream >> value;
|
|
|
|
|
} catch (...) {
|
2019-01-29 19:00:06 +00:00
|
|
|
err_msg = "Could not deserialize value.";
|
2018-12-14 15:30:59 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!MoneyRange(value)) {
|
2019-01-29 19:00:06 +00:00
|
|
|
err_msg = "Value was not in valid value range.";
|
2018-12-14 15:30:59 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get asset type
|
|
|
|
|
if (stack[1].size() != 32) {
|
2019-01-29 19:00:06 +00:00
|
|
|
err_msg = "Asset type was not 32 bytes.";
|
2018-12-14 15:30:59 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-03-19 20:26:39 +00:00
|
|
|
CAsset asset(stack[1]);
|
2018-12-14 15:30:59 +01:00
|
|
|
|
|
|
|
|
// Get genesis blockhash
|
|
|
|
|
if (stack[2].size() != 32) {
|
2019-01-29 19:00:06 +00:00
|
|
|
err_msg = "Parent genesis blockchaash was not 32 bytes.";
|
2018-12-14 15:30:59 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
uint256 gen_hash(stack[2]);
|
|
|
|
|
|
|
|
|
|
// Get claim_script, sanity check size
|
|
|
|
|
CScript claim_script(stack[3].begin(), stack[3].end());
|
|
|
|
|
if (claim_script.size() > 100) {
|
2019-01-29 19:00:06 +00:00
|
|
|
err_msg = "Claim script is too large.";
|
2018-12-14 15:30:59 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint256 block_hash;
|
|
|
|
|
uint256 tx_hash;
|
|
|
|
|
int num_txs;
|
2019-03-25 12:11:03 -04:00
|
|
|
unsigned int tx_index = 0;
|
2018-12-14 15:30:59 +01:00
|
|
|
// Get txout proof
|
|
|
|
|
if (Params().GetConsensus().ParentChainHasPow()) {
|
|
|
|
|
Sidechain::Bitcoin::CMerkleBlock merkle_block_pow;
|
2019-03-25 12:11:03 -04:00
|
|
|
if (!GetBlockAndTxFromMerkleBlock(block_hash, tx_hash, tx_index, merkle_block_pow, stack[5])) {
|
2019-01-29 19:00:06 +00:00
|
|
|
err_msg = "Could not extract block and tx from merkleblock.";
|
2018-12-14 15:30:59 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!CheckParentProofOfWork(block_hash, merkle_block_pow.header.nBits, Params().GetConsensus())) {
|
2019-01-29 19:00:06 +00:00
|
|
|
err_msg = "Parent proof of work is invalid or insufficient.";
|
2018-12-14 15:30:59 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Sidechain::Bitcoin::CTransactionRef pegtx;
|
2019-05-31 13:43:14 -04:00
|
|
|
if (!CheckPeginTx(stack[4], pegtx, prevout, value, claim_script, fedpegscripts)) {
|
2019-01-29 19:00:06 +00:00
|
|
|
err_msg = "Peg-in tx is invalid.";
|
2018-12-14 15:30:59 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
num_txs = merkle_block_pow.txn.GetNumTransactions();
|
|
|
|
|
} else {
|
2018-12-20 12:32:29 +01:00
|
|
|
CMerkleBlock merkle_block;
|
2019-03-25 12:11:03 -04:00
|
|
|
if (!GetBlockAndTxFromMerkleBlock(block_hash, tx_hash, tx_index, merkle_block, stack[5])) {
|
2019-01-29 19:00:06 +00:00
|
|
|
err_msg = "Could not extract block and tx from merkleblock.";
|
2018-12-20 12:32:29 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!CheckProofSignedParent(merkle_block.header, Params().GetConsensus())) {
|
2019-01-29 19:00:06 +00:00
|
|
|
err_msg = "Parent signed block is invalid.";
|
2018-12-20 12:32:29 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CTransactionRef pegtx;
|
2019-05-31 13:43:14 -04:00
|
|
|
if (!CheckPeginTx(stack[4], pegtx, prevout, value, claim_script, fedpegscripts)) {
|
2019-01-29 19:00:06 +00:00
|
|
|
err_msg = "Peg-in tx is invalid.";
|
2018-12-20 12:32:29 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
num_txs = merkle_block.txn.GetNumTransactions();
|
2018-12-14 15:30:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that the merkle proof corresponds to the txid
|
|
|
|
|
if (prevout.hash != tx_hash) {
|
2019-01-29 19:00:06 +00:00
|
|
|
err_msg = "Merkle proof and txid mismatch.";
|
2018-12-14 15:30:59 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check the genesis block corresponds to a valid peg (only one for now)
|
|
|
|
|
if (gen_hash != Params().ParentGenesisBlockHash()) {
|
2019-01-29 19:00:06 +00:00
|
|
|
err_msg = "Parent genesis block mismatch.";
|
2018-12-14 15:30:59 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-19 20:26:39 +00:00
|
|
|
// Check the asset type corresponds to a valid pegged asset (only one for now)
|
|
|
|
|
if (asset != Params().GetConsensus().pegged_asset) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-12-14 15:30:59 +01:00
|
|
|
|
|
|
|
|
// Finally, validate peg-in via rpc call
|
2019-03-21 14:39:45 +00:00
|
|
|
if (check_depth && gArgs.GetBoolArg("-validatepegin", Params().GetConsensus().has_parent_chain)) {
|
2019-03-25 12:11:03 -04:00
|
|
|
unsigned int required_depth = Params().GetConsensus().pegin_min_depth;
|
|
|
|
|
// Don't allow coinbase output claims before coinbase maturity
|
|
|
|
|
if (tx_index == 0) {
|
|
|
|
|
required_depth = std::max(required_depth, (unsigned int)COINBASE_MATURITY);
|
|
|
|
|
}
|
|
|
|
|
if (!IsConfirmedBitcoinBlock(block_hash, required_depth, num_txs)) {
|
2019-01-29 19:00:06 +00:00
|
|
|
err_msg = "Needs more confirmations.";
|
When validation is waiting for parent chain daemon, "stall".
Currently, if -validatepegin is given, and block validation can't proceed
because the parent chain is not synced, we mark the block invalid and put
it in a queue to be "revalidated" later. Unfortunately, marking a block
invalid has downstream consequences, in particular causing descendant blocks
to be marked invalid, which are not currently fixed by the queue.
Instead, we'll use a different strategy: if the mainchain daemon isn't
sufficiently synced to validate a block, we will "stall" connecting that
block to the chain, and have ActivateBestChain simply keep the tip at the
previous block until we're ready.
We can still download and validate (partly) blocks past this point while
we're waiting. They will be connected once the parent chain daemon catches
up.
2021-09-07 17:01:33 +00:00
|
|
|
if (depth_failed) {
|
|
|
|
|
*depth_failed = true;
|
|
|
|
|
}
|
2018-12-14 15:30:59 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 10:22:45 -05:00
|
|
|
bool MatchLiquidWatchman(const CScript& script)
|
|
|
|
|
{
|
|
|
|
|
CScript::const_iterator it = script.begin();
|
|
|
|
|
std::vector<unsigned char> data;
|
|
|
|
|
opcodetype opcode;
|
|
|
|
|
|
|
|
|
|
// Stack depth check for branch choice
|
|
|
|
|
if (!script.GetOp(it, opcode, data) || opcode != OP_DEPTH) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Take in value, then check equality
|
|
|
|
|
if (!script.GetOp(it, opcode, data) ||
|
|
|
|
|
!script.GetOp(it, opcode, data) ||
|
|
|
|
|
opcode != OP_EQUAL) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// IF EQUAL
|
|
|
|
|
if (!script.GetOp(it, opcode, data) || opcode != OP_IF) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Take in value k, make sure minimally encoded number from 1 to 16
|
|
|
|
|
if (!script.GetOp(it, opcode, data) ||
|
|
|
|
|
opcode > OP_16 ||
|
|
|
|
|
(opcode < OP_1NEGATE && !CheckMinimalPush(data, opcode))) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
opcodetype opcode2 = opcode;
|
|
|
|
|
std::vector<unsigned char> num = data;
|
|
|
|
|
// Iterate through multisig stuff until ELSE is hit
|
|
|
|
|
while (opcode != OP_ELSE) {
|
|
|
|
|
if (!script.GetOp(it, opcode, data)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Take minimally-encoded CSV push number k'
|
|
|
|
|
if (!script.GetOp(it, opcode, data) ||
|
|
|
|
|
opcode > OP_16 || (opcode < OP_1NEGATE && !CheckMinimalPush(data, opcode))) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// CSV
|
|
|
|
|
if (!script.GetOp(it, opcode, data) || opcode != OP_CHECKSEQUENCEVERIFY) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Drop the CSV number
|
|
|
|
|
if (!script.GetOp(it, opcode, data) || opcode != OP_DROP) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Take the minimally-encoded n of k-of-n multisig arg
|
|
|
|
|
if (!script.GetOp(it, opcode, data) ||
|
|
|
|
|
opcode > OP_16 || (opcode < OP_1NEGATE && !CheckMinimalPush(data, opcode)) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The two multisig k-numbers must not match, otherwise ELSE branch can not be reached
|
|
|
|
|
if (opcode == opcode2 && num == data) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find the ENDIF
|
|
|
|
|
while (opcode != OP_ENDIF) {
|
|
|
|
|
if (!script.GetOp(it, opcode, data)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// CHECKMULTISIG
|
|
|
|
|
if (!script.GetOp(it, opcode, data) || opcode != OP_CHECKMULTISIG) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// No more pushes
|
2019-03-19 20:31:58 +00:00
|
|
|
return (it == script.end());
|
2019-01-15 10:22:45 -05:00
|
|
|
}
|
2019-05-31 11:30:10 -04:00
|
|
|
|
2019-06-17 16:35:52 -04:00
|
|
|
std::vector<std::pair<CScript, CScript>> GetValidFedpegScripts(const CBlockIndex* pblockindex, const Consensus::Params& params, bool nextblock_validation)
|
2019-05-31 11:30:10 -04:00
|
|
|
{
|
|
|
|
|
assert(pblockindex);
|
|
|
|
|
|
2019-06-17 16:35:52 -04:00
|
|
|
std::vector<std::pair<CScript, CScript>> fedpegscripts;
|
2019-05-31 11:30:10 -04:00
|
|
|
|
2020-12-04 15:20:42 +00:00
|
|
|
const int32_t epoch_length = (int32_t) params.dynamic_epoch_length;
|
2019-05-31 11:30:10 -04:00
|
|
|
const int32_t epoch_age = pblockindex->nHeight % epoch_length;
|
|
|
|
|
const int32_t epoch_start_height = pblockindex->nHeight - epoch_age;
|
|
|
|
|
|
|
|
|
|
// In mempool and general "enforced next block" RPC we need to look ahead one block
|
|
|
|
|
// to see if we're on a boundary. If so, put that epoch's fedpegscript in place
|
|
|
|
|
if (nextblock_validation && epoch_age == epoch_length - 1) {
|
2019-06-17 16:35:52 -04:00
|
|
|
DynaFedParamEntry next_param = ComputeNextBlockFullCurrentParameters(pblockindex, params);
|
|
|
|
|
fedpegscripts.push_back(std::make_pair(next_param.m_fedpeg_program, next_param.m_fedpegscript));
|
2019-05-31 11:30:10 -04:00
|
|
|
}
|
|
|
|
|
|
2019-08-19 15:21:30 -04:00
|
|
|
// Next we walk backwards up to M epoch starts
|
2020-12-14 14:58:05 +00:00
|
|
|
for (int32_t i = 0; i < (int32_t) params.total_valid_epochs; i++) {
|
|
|
|
|
// We are within total_valid_epochs of the genesis
|
|
|
|
|
if (i * epoch_length > epoch_start_height) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-05-31 11:30:10 -04:00
|
|
|
|
2019-08-19 15:21:30 -04:00
|
|
|
const CBlockIndex* p_epoch_start = pblockindex->GetAncestor(epoch_start_height-i*epoch_length);
|
|
|
|
|
|
|
|
|
|
// We're done here, for whatever reason.
|
|
|
|
|
if (!p_epoch_start) {
|
|
|
|
|
break;
|
2019-05-31 11:30:10 -04:00
|
|
|
}
|
|
|
|
|
|
2024-05-27 09:25:31 +00:00
|
|
|
if (node::fTrimHeaders) {
|
2024-05-08 05:53:49 +00:00
|
|
|
LOCK(cs_main);
|
|
|
|
|
ForceUntrimHeader(p_epoch_start);
|
|
|
|
|
}
|
2022-11-07 23:49:08 -08:00
|
|
|
if (!p_epoch_start->dynafed_params().IsNull()) {
|
|
|
|
|
fedpegscripts.push_back(std::make_pair(p_epoch_start->dynafed_params().m_current.m_fedpeg_program, p_epoch_start->dynafed_params().m_current.m_fedpegscript));
|
2019-05-31 11:30:10 -04:00
|
|
|
} else {
|
2019-06-17 16:35:52 -04:00
|
|
|
fedpegscripts.push_back(std::make_pair(GetScriptForDestination(ScriptHash(GetScriptForDestination(WitnessV0ScriptHash(params.fedpegScript)))), params.fedpegScript));
|
2019-05-31 11:30:10 -04:00
|
|
|
}
|
|
|
|
|
}
|
2019-09-25 21:01:55 -04:00
|
|
|
// Only return up to the latest total_valid_epochs fedpegscripts, which are enforced
|
2019-08-19 15:21:30 -04:00
|
|
|
fedpegscripts.resize(std::min(fedpegscripts.size(), params.total_valid_epochs));
|
2019-05-31 11:30:10 -04:00
|
|
|
return fedpegscripts;
|
|
|
|
|
}
|
2019-10-14 12:22:17 -04:00
|
|
|
|
|
|
|
|
template<typename T_tx_ref, typename T_merkle_block>
|
|
|
|
|
CScriptWitness CreatePeginWitnessInner(const CAmount& value, const CAsset& asset, const uint256& genesis_hash, const CScript& claim_script, const T_tx_ref& tx_ref, const T_merkle_block& merkle_block)
|
|
|
|
|
{
|
|
|
|
|
std::vector<unsigned char> value_bytes;
|
|
|
|
|
CVectorWriter ss_val(0, 0, value_bytes, 0);
|
|
|
|
|
try {
|
|
|
|
|
ss_val << value;
|
|
|
|
|
} catch (...) {
|
|
|
|
|
throw std::ios_base::failure("Amount serialization is invalid.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Strip witness data for proof inclusion since only TXID-covered fields matters
|
|
|
|
|
CDataStream ss_tx(SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
|
|
|
|
|
ss_tx << tx_ref;
|
2023-06-28 18:50:02 +00:00
|
|
|
const auto* ss_tx_ptr = UCharCast(ss_tx.data());
|
|
|
|
|
std::vector<unsigned char> tx_data_stripped(ss_tx_ptr, ss_tx_ptr + ss_tx.size());
|
2019-10-14 12:22:17 -04:00
|
|
|
|
|
|
|
|
// Serialize merkle block
|
|
|
|
|
CDataStream ss_txout_proof(SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
|
|
|
|
|
ss_txout_proof << merkle_block;
|
2023-06-28 18:50:02 +00:00
|
|
|
const auto* ss_txout_ptr = UCharCast(ss_txout_proof.data());
|
|
|
|
|
std::vector<unsigned char> txout_proof_bytes(ss_txout_ptr, ss_txout_ptr + ss_txout_proof.size());
|
2019-10-14 12:22:17 -04:00
|
|
|
|
|
|
|
|
// Construct pegin proof
|
|
|
|
|
CScriptWitness pegin_witness;
|
2023-06-28 18:50:02 +00:00
|
|
|
std::vector<std::vector<unsigned char>>& stack = pegin_witness.stack;
|
2019-10-14 12:22:17 -04:00
|
|
|
stack.push_back(value_bytes);
|
|
|
|
|
stack.push_back(std::vector<unsigned char>(asset.begin(), asset.end()));
|
|
|
|
|
stack.push_back(std::vector<unsigned char>(genesis_hash.begin(), genesis_hash.end()));
|
|
|
|
|
stack.push_back(std::vector<unsigned char>(claim_script.begin(), claim_script.end()));
|
|
|
|
|
stack.push_back(tx_data_stripped);
|
|
|
|
|
stack.push_back(txout_proof_bytes);
|
|
|
|
|
return pegin_witness;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CScriptWitness CreatePeginWitness(const CAmount& value, const CAsset& asset, const uint256& genesis_hash, const CScript& claim_script, const CTransactionRef& tx_ref, const CMerkleBlock& merkle_block)
|
|
|
|
|
{
|
|
|
|
|
return CreatePeginWitnessInner(value, asset, genesis_hash, claim_script, tx_ref, merkle_block);
|
|
|
|
|
}
|
|
|
|
|
CScriptWitness CreatePeginWitness(const CAmount& value, const CAsset& asset, const uint256& genesis_hash, const CScript& claim_script, const Sidechain::Bitcoin::CTransactionRef& tx_ref, const Sidechain::Bitcoin::CMerkleBlock& merkle_block)
|
|
|
|
|
{
|
|
|
|
|
return CreatePeginWitnessInner(value, asset, genesis_hash, claim_script, tx_ref, merkle_block);
|
|
|
|
|
}
|
2021-05-04 17:14:46 -04:00
|
|
|
|
2021-09-04 15:19:42 +00:00
|
|
|
bool DecomposePeginWitness(const CScriptWitness& witness, CAmount& value, CAsset& asset, uint256& genesis_hash, CScript& claim_script, std::variant<std::monostate, Sidechain::Bitcoin::CTransactionRef, CTransactionRef>& tx, std::variant<std::monostate, Sidechain::Bitcoin::CMerkleBlock, CMerkleBlock>& merkle_block)
|
2021-05-04 17:14:46 -04:00
|
|
|
{
|
|
|
|
|
const auto& stack = witness.stack;
|
|
|
|
|
|
|
|
|
|
if (stack.size() < 5) return false;
|
|
|
|
|
|
|
|
|
|
CDataStream stream(stack[0], SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
|
stream >> value;
|
|
|
|
|
|
|
|
|
|
CAsset tmp_asset(stack[1]);
|
|
|
|
|
asset = tmp_asset;
|
|
|
|
|
|
|
|
|
|
uint256 gh(stack[2]);
|
|
|
|
|
genesis_hash = gh;
|
|
|
|
|
|
|
|
|
|
CScript s(stack[3].begin(), stack[3].end());
|
|
|
|
|
claim_script = s;
|
|
|
|
|
|
|
|
|
|
CDataStream ss_tx(stack[4], SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
|
if (Params().GetConsensus().ParentChainHasPow()) {
|
|
|
|
|
Sidechain::Bitcoin::CTransactionRef btc_tx;
|
|
|
|
|
ss_tx >> btc_tx;
|
|
|
|
|
tx = btc_tx;
|
|
|
|
|
} else {
|
|
|
|
|
CTransactionRef elem_tx;
|
|
|
|
|
ss_tx >> elem_tx;
|
|
|
|
|
tx = elem_tx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CDataStream ss_proof(stack[5], SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
|
if (Params().GetConsensus().ParentChainHasPow()) {
|
|
|
|
|
Sidechain::Bitcoin::CMerkleBlock tx_proof;
|
|
|
|
|
ss_proof >> tx_proof;
|
|
|
|
|
merkle_block = tx_proof;
|
|
|
|
|
} else {
|
|
|
|
|
CMerkleBlock tx_proof;
|
|
|
|
|
ss_proof >> tx_proof;
|
|
|
|
|
merkle_block = tx_proof;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|