From 248eb9157b1b98967afd3c3ef779774974ada6bb Mon Sep 17 00:00:00 2001 From: Gregory Sanders Date: Mon, 26 Mar 2018 18:11:38 -0400 Subject: [PATCH] add better reject reason for immature peg-in, with test --- qa/rpc-tests/pegging.py | 12 +++++++++++- src/wallet/rpcwallet.cpp | 6 +++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/qa/rpc-tests/pegging.py b/qa/rpc-tests/pegging.py index 2714c3985f..84aba1fe52 100755 --- a/qa/rpc-tests/pegging.py +++ b/qa/rpc-tests/pegging.py @@ -169,12 +169,22 @@ try: addrs = sidechain.getpeginaddress() txid1 = bitcoin.sendtoaddress(addrs["mainchain_address"], 24) # 10+2 confirms required to get into mempool and confirm - bitcoin.generate(11) + bitcoin.generate(1) time.sleep(2) proof = bitcoin.gettxoutproof([txid1]) raw = bitcoin.getrawtransaction(txid1) print("Attempting peg-in") + # First attempt fails the consensus check but gives useful result + try: + pegtxid = sidechain.claimpegin(raw, proof) + raise Exception("Peg-in should not mature enough yet, need another block.") + except JSONRPCException as e: + assert("Peg-in Bitcoin transaction needs more confirmations to be sent." in e.error["message"]) + pass + + # Second attempt simply doesn't hit mempool bar + bitcoin.generate(10) try: pegtxid = sidechain.claimpegin(raw, proof) raise Exception("Peg-in should not mature enough yet, need another block.") diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 739713da39..aa0f7218b6 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -3660,7 +3660,11 @@ UniValue createrawpegin(const JSONRPCRequest& request) stack.push_back(txData); stack.push_back(txOutProofData); - if (!IsValidPeginWitness(pegin_witness, mtx.vin[0].prevout)) { + // Peg-in witness isn't valid, even though the block header is(without depth check) + // We re-check depth before returning with more descriptive result + if (GetBoolArg("-validatepegin", DEFAULT_VALIDATE_PEGIN) && + !IsConfirmedBitcoinBlock(merkleBlock.header.GetHash(), 0) && + !IsValidPeginWitness(pegin_witness, mtx.vin[0].prevout)) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Constructed peg-in witness is invalid."); }