Skip to content

Commit 298c2fb

Browse files
committed
Merge #538: Enforce coinbase maturity on bitcoin peg-ins
5acc42e feature_fedpeg.py: remove the spam output (Gregory Sanders) 870e416 Enforce coinbase maturity on bitcoin peg-ins (Gregory Sanders) Pull request description: resolves #14 Technically a softfork but not really since the elements chain would eventually become valid anyways. Tree-SHA512: bfdf404f7056e6b16adde3a82b0e11839b921fd08063ae6415d207b553e1a19c5f5a76cb308af1ca63164c001eb59b5874fe33160f7bf87061a6a4a507aa9241
2 parents 09e20ab + 5acc42e commit 298c2fb

3 files changed

Lines changed: 54 additions & 19 deletions

File tree

src/pegins.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ static bool CheckPeginTx(const std::vector<unsigned char>& tx_data, T& pegtx, co
189189
}
190190

191191
template<typename T>
192-
static bool GetBlockAndTxFromMerkleBlock(uint256& block_hash, uint256& tx_hash, T& merkle_block, const std::vector<unsigned char>& merkle_block_raw)
192+
static bool GetBlockAndTxFromMerkleBlock(uint256& block_hash, uint256& tx_hash, unsigned int& tx_index, T& merkle_block, const std::vector<unsigned char>& merkle_block_raw)
193193
{
194194
try {
195195
std::vector<uint256> tx_hashes;
@@ -205,6 +205,7 @@ static bool GetBlockAndTxFromMerkleBlock(uint256& block_hash, uint256& tx_hash,
205205
return false;
206206
}
207207
tx_hash = tx_hashes[0];
208+
tx_index = tx_indices[0];
208209
} catch (std::exception& e) {
209210
// Invalid encoding of merkle block
210211
return false;
@@ -295,10 +296,11 @@ bool IsValidPeginWitness(const CScriptWitness& pegin_witness, const COutPoint& p
295296
uint256 block_hash;
296297
uint256 tx_hash;
297298
int num_txs;
299+
unsigned int tx_index = 0;
298300
// Get txout proof
299301
if (Params().GetConsensus().ParentChainHasPow()) {
300302
Sidechain::Bitcoin::CMerkleBlock merkle_block_pow;
301-
if (!GetBlockAndTxFromMerkleBlock(block_hash, tx_hash, merkle_block_pow, stack[5])) {
303+
if (!GetBlockAndTxFromMerkleBlock(block_hash, tx_hash, tx_index, merkle_block_pow, stack[5])) {
302304
err_msg = "Could not extract block and tx from merkleblock.";
303305
return false;
304306
}
@@ -316,7 +318,7 @@ bool IsValidPeginWitness(const CScriptWitness& pegin_witness, const COutPoint& p
316318
num_txs = merkle_block_pow.txn.GetNumTransactions();
317319
} else {
318320
CMerkleBlock merkle_block;
319-
if (!GetBlockAndTxFromMerkleBlock(block_hash, tx_hash, merkle_block, stack[5])) {
321+
if (!GetBlockAndTxFromMerkleBlock(block_hash, tx_hash, tx_index, merkle_block, stack[5])) {
320322
err_msg = "Could not extract block and tx from merkleblock.";
321323
return false;
322324
}
@@ -354,7 +356,13 @@ bool IsValidPeginWitness(const CScriptWitness& pegin_witness, const COutPoint& p
354356

355357
// Finally, validate peg-in via rpc call
356358
if (check_depth && gArgs.GetBoolArg("-validatepegin", Params().GetConsensus().has_parent_chain)) {
357-
if (!IsConfirmedBitcoinBlock(block_hash, Params().GetConsensus().pegin_min_depth, num_txs)) {
359+
unsigned int required_depth = Params().GetConsensus().pegin_min_depth;
360+
// Don't allow coinbase output claims before coinbase maturity
361+
if (tx_index == 0) {
362+
required_depth = std::max(required_depth, (unsigned int)COINBASE_MATURITY);
363+
}
364+
LogPrintf("Required depth: %d\n", required_depth);
365+
if (!IsConfirmedBitcoinBlock(block_hash, required_depth, num_txs)) {
358366
err_msg = "Needs more confirmations.";
359367
return false;
360368
}

src/wallet/rpcwallet.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5240,7 +5240,11 @@ static UniValue createrawpegin(const JSONRPCRequest& request, T_tx_ref& txBTCRef
52405240

52415241
// Additional block lee-way to avoid bitcoin block races
52425242
if (gArgs.GetBoolArg("-validatepegin", Params().GetConsensus().has_parent_chain)) {
5243-
ret.pushKV("mature", IsConfirmedBitcoinBlock(merkleBlock.header.GetHash(), Params().GetConsensus().pegin_min_depth+2, merkleBlock.txn.GetNumTransactions()));
5243+
unsigned int required_depth = Params().GetConsensus().pegin_min_depth + 2;
5244+
if (txIndices[0] == 0) {
5245+
required_depth = std::max(required_depth, (unsigned int)COINBASE_MATURITY+2);
5246+
}
5247+
ret.pushKV("mature", IsConfirmedBitcoinBlock(merkleBlock.header.GetHash(), required_depth, merkleBlock.txn.GetNumTransactions()));
52445248
}
52455249

52465250
return ret;

test/functional/feature_fedpeg.py

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
get_datadir_path,
1111
rpc_port,
1212
p2p_port,
13+
assert_raises_rpc_error,
14+
assert_equal,
1315
)
1416
from decimal import Decimal
1517

@@ -62,9 +64,10 @@ def setup_network(self, split=False):
6264
else:
6365
extra_args.extend([
6466
"-validatepegin=0",
65-
"-initialfreecoins=2100000000000000",
67+
"-initialfreecoins=0",
6668
"-anyonecanspendaremine",
6769
"-signblockscript=51", # OP_TRUE
70+
'-con_blocksubsidy=5000000000',
6871
])
6972

7073
self.add_nodes(1, [extra_args], chain=[parent_chain], binary=parent_binary, chain_in_args=[not self.options.parent_bitcoin])
@@ -73,7 +76,6 @@ def setup_network(self, split=False):
7376

7477
connect_nodes_bi(self.nodes, 0, 1)
7578
self.parentgenesisblockhash = self.nodes[0].getblockhash(0)
76-
print('parentgenesisblockhash', self.parentgenesisblockhash)
7779
if not self.options.parent_bitcoin:
7880
parent_pegged_asset = self.nodes[0].getsidechaininfo()['pegged_asset']
7981

@@ -158,24 +160,20 @@ def run_test(self):
158160

159161
addrs = sidechain.getpeginaddress()
160162
addr = addrs["mainchain_address"]
161-
print('addrs', addrs)
162-
print(parent.getaddressinfo(addr))
163163
txid1 = parent.sendtoaddress(addr, 24)
164164
# 10+2 confirms required to get into mempool and confirm
165165
parent.generate(1)
166166
time.sleep(2)
167167
proof = parent.gettxoutproof([txid1])
168168

169169
raw = parent.getrawtransaction(txid1)
170-
print('raw', parent.getrawtransaction(txid1, True))
171170

172-
print("Attempting peg-in")
171+
print("Attempting peg-ins")
173172
# First attempt fails the consensus check but gives useful result
174173
try:
175174
pegtxid = sidechain.claimpegin(raw, proof)
176175
raise Exception("Peg-in should not be mature enough yet, need another block.")
177176
except JSONRPCException as e:
178-
print('RPC ERROR:', e.error['message'])
179177
assert("Peg-in Bitcoin transaction needs more confirmations to be sent." in e.error["message"])
180178

181179
# Second attempt simply doesn't hit mempool bar
@@ -184,14 +182,12 @@ def run_test(self):
184182
pegtxid = sidechain.claimpegin(raw, proof)
185183
raise Exception("Peg-in should not be mature enough yet, need another block.")
186184
except JSONRPCException as e:
187-
print('RPC ERROR:', e.error['message'])
188185
assert("Peg-in Bitcoin transaction needs more confirmations to be sent." in e.error["message"])
189186

190187
try:
191188
pegtxid = sidechain.createrawpegin(raw, proof, 'AEIOU')
192189
raise Exception("Peg-in with non-hex claim_script should fail.")
193190
except JSONRPCException as e:
194-
print('RPC ERROR:', e.error['message'])
195191
assert("Given claim_script is not hex." in e.error["message"])
196192

197193
# Should fail due to non-matching wallet address
@@ -200,7 +196,6 @@ def run_test(self):
200196
pegtxid = sidechain.claimpegin(raw, proof, scriptpubkey)
201197
raise Exception("Peg-in with non-matching claim_script should fail.")
202198
except JSONRPCException as e:
203-
print('RPC ERROR:', e.error['message'])
204199
assert("Given claim_script does not match the given Bitcoin transaction." in e.error["message"])
205200

206201
# 12 confirms allows in mempool
@@ -219,7 +214,6 @@ def run_test(self):
219214

220215
tx1 = sidechain.gettransaction(pegtxid1)
221216

222-
print('tx1', tx1)
223217
if "confirmations" in tx1 and tx1["confirmations"] == 6:
224218
print("Peg-in is confirmed: Success!")
225219
else:
@@ -233,7 +227,6 @@ def run_test(self):
233227
vsize = decoded["vsize"]
234228
fee_output = decoded["vout"][1]
235229
fallbackfee_pervbyte = Decimal("0.00001")/Decimal("1000")
236-
print("fee_output", fee_output)
237230
assert fee_output["scriptPubKey"]["type"] == "fee"
238231
assert fee_output["value"] >= fallbackfee_pervbyte*vsize
239232

@@ -252,7 +245,7 @@ def run_test(self):
252245
# Do multiple claims in mempool
253246
n_claims = 6
254247

255-
print("Flooding mempool with many small claims")
248+
print("Flooding mempool with a few claims")
256249
pegtxs = []
257250
sidechain.generate(101)
258251

@@ -352,7 +345,6 @@ def run_test(self):
352345

353346
print("Restarting parent2")
354347
self.start_node(1)
355-
#parent2 = self.nodes[1]
356348
connect_nodes_bi(self.nodes, 0, 1)
357349

358350
# Don't make a block, race condition when pegin-invalid block
@@ -401,10 +393,41 @@ def run_test(self):
401393
# Make sure balance went down
402394
assert(bal_2 + 1 < bal_1)
403395

396+
# Send rest of coins using subtractfee from output arg
404397
sidechain.sendtomainchain(some_btc_addr, bal_2, True)
405398

406399
assert(sidechain.getwalletinfo()["balance"]['bitcoin'] == 0)
407400

401+
print('Test coinbase peg-in maturity rules')
402+
403+
# Have bitcoin output go directly into a claim output
404+
pegin_info = sidechain.getpeginaddress()
405+
mainchain_addr = pegin_info["mainchain_address"]
406+
# Watch the address so we can get tx without txindex
407+
parent.importaddress(mainchain_addr)
408+
claim_block = parent.generatetoaddress(50, mainchain_addr)[0]
409+
block_coinbase = parent.getblock(claim_block, 2)["tx"][0]
410+
claim_txid = block_coinbase["txid"]
411+
claim_tx = block_coinbase["hex"]
412+
claim_proof = parent.gettxoutproof([claim_txid], claim_block)
413+
414+
# Can't claim something even though it has 50 confirms since it's coinbase
415+
assert_raises_rpc_error(-8, "Peg-in Bitcoin transaction needs more confirmations to be sent.", sidechain.claimpegin, claim_tx, claim_proof)
416+
# If done via raw API, still doesn't work
417+
coinbase_pegin = sidechain.createrawpegin(claim_tx, claim_proof)
418+
assert_equal(coinbase_pegin["mature"], False)
419+
signed_pegin = sidechain.signrawtransactionwithwallet(coinbase_pegin["hex"])["hex"]
420+
assert_raises_rpc_error(-26, "bad-pegin-witness, Needs more confirmations.", sidechain.sendrawtransaction, signed_pegin)
421+
422+
# 50 more blocks to allow wallet to make it succeed by relay and consensus
423+
parent.generatetoaddress(50, parent.getnewaddress())
424+
# Wallet still doesn't want to for 2 more confirms
425+
assert_equal(sidechain.createrawpegin(claim_tx, claim_proof)["mature"], False)
426+
# But we can just shoot it off
427+
claim_txid = sidechain.sendrawtransaction(signed_pegin)
428+
sidechain.generatetoaddress(1, sidechain.getnewaddress())
429+
assert_equal(sidechain.gettransaction(claim_txid)["confirmations"], 1)
430+
408431
print('Success!')
409432

410433
# Manually stop sidechains first, then the parent chains.

0 commit comments

Comments
 (0)