|
| 1 | +import time |
| 2 | + |
| 3 | +import flexitest |
| 4 | +from bitcoinlib.services.bitcoind import BitcoindClient |
| 5 | + |
| 6 | +from constants import ( |
| 7 | + ROLLUP_PARAMS_FOR_DEPOSIT_TX, |
| 8 | + SEQ_PUBLISH_BATCH_INTERVAL_SECS, |
| 9 | +) |
| 10 | +from utils import wait_for_proof_with_time_out, wait_until |
| 11 | + |
| 12 | +EVM_WAIT_TIME = 2 |
| 13 | +SATS_TO_WEI = 10**10 |
| 14 | + |
| 15 | +withdrawal_intent_event_abi = { |
| 16 | + "anonymous": False, |
| 17 | + "inputs": [ |
| 18 | + {"indexed": False, "internalType": "uint64", "name": "amount", "type": "uint64"}, |
| 19 | + {"indexed": False, "internalType": "bytes", "name": "dest_pk", "type": "bytes32"}, |
| 20 | + ], |
| 21 | + "name": "WithdrawalIntentEvent", |
| 22 | + "type": "event", |
| 23 | +} |
| 24 | +event_signature_text = "WithdrawalIntentEvent(uint64,bytes32)" |
| 25 | + |
| 26 | + |
| 27 | +@flexitest.register |
| 28 | +class BridgeDepositTest(flexitest.Test): |
| 29 | + def __init__(self, ctx: flexitest.InitContext): |
| 30 | + ctx.set_env("prover") |
| 31 | + |
| 32 | + def main(self, ctx: flexitest.RunContext): |
| 33 | + evm_addr = "deedf001900dca3ebeefdeadf001900dca3ebeef" |
| 34 | + |
| 35 | + btc = ctx.get_service("bitcoin") |
| 36 | + btcrpc: BitcoindClient = btc.create_rpc() |
| 37 | + |
| 38 | + # Do deposit and collect the L1 and L2 where the deposit transaction was included |
| 39 | + l2_block_num, l1_deposit_txn_id = self.do_deposit(ctx, evm_addr) |
| 40 | + l1_deposit_txn_block_info = btcrpc.proxy.gettransaction(l1_deposit_txn_id) |
| 41 | + deposit_txn_block_num = l1_deposit_txn_block_info["blockheight"] |
| 42 | + |
| 43 | + # Init the prover client |
| 44 | + prover_client = ctx.get_service("prover_client") |
| 45 | + prover_client_rpc = prover_client.create_rpc() |
| 46 | + time.sleep(60) |
| 47 | + |
| 48 | + # Dispatch the prover task |
| 49 | + # Proving task with with few L1 and L2 blocks including the deposit transaction |
| 50 | + l1_range = (deposit_txn_block_num - 1, deposit_txn_block_num + 1) |
| 51 | + l2_range = (l2_block_num - 1, l2_block_num + 1) |
| 52 | + task_id = prover_client_rpc.dev_strata_proveCheckpointRaw(0, l1_range, l2_range) |
| 53 | + print("got proving task_id ", task_id) |
| 54 | + assert task_id is not None |
| 55 | + |
| 56 | + time_out = 30 * 60 |
| 57 | + wait_for_proof_with_time_out(prover_client_rpc, task_id, time_out=time_out) |
| 58 | + |
| 59 | + def do_deposit(self, ctx: flexitest.RunContext, evm_addr: str): |
| 60 | + btc = ctx.get_service("bitcoin") |
| 61 | + seq = ctx.get_service("sequencer") |
| 62 | + |
| 63 | + seqrpc = seq.create_rpc() |
| 64 | + btcrpc: BitcoindClient = btc.create_rpc() |
| 65 | + |
| 66 | + amount_to_send = ROLLUP_PARAMS_FOR_DEPOSIT_TX["deposit_amount"] / 10**8 |
| 67 | + name = ROLLUP_PARAMS_FOR_DEPOSIT_TX["rollup_name"].encode("utf-8").hex() |
| 68 | + |
| 69 | + addr = "bcrt1pzupt5e8eqvt995r57jmmylxlswqfddsscrrq7njygrkhej3e7q2qur0c76" |
| 70 | + outputs = [{addr: amount_to_send}, {"data": f"{name}{evm_addr}"}] |
| 71 | + |
| 72 | + options = {"changePosition": 2} |
| 73 | + |
| 74 | + psbt_result = btcrpc.proxy.walletcreatefundedpsbt([], outputs, 0, options) |
| 75 | + psbt = psbt_result["psbt"] |
| 76 | + |
| 77 | + signed_psbt = btcrpc.proxy.walletprocesspsbt(psbt) |
| 78 | + |
| 79 | + finalized_psbt = btcrpc.proxy.finalizepsbt(signed_psbt["psbt"]) |
| 80 | + deposit_tx = finalized_psbt["hex"] |
| 81 | + |
| 82 | + original_num_deposits = len(seqrpc.strata_getCurrentDeposits()) |
| 83 | + print(f"Original deposit count: {original_num_deposits}") |
| 84 | + |
| 85 | + reth = ctx.get_service("reth") |
| 86 | + rethrpc = reth.create_rpc() |
| 87 | + |
| 88 | + original_balance = int(rethrpc.eth_getBalance(f"0x{evm_addr}"), 16) |
| 89 | + print(f"Balance before deposit: {original_balance}") |
| 90 | + |
| 91 | + btc_txn_id = btcrpc.sendrawtransaction(deposit_tx)["txid"] |
| 92 | + |
| 93 | + # check if we are getting deposits |
| 94 | + wait_until( |
| 95 | + lambda: len(seqrpc.strata_getCurrentDeposits()) > original_num_deposits, |
| 96 | + error_with="seem not be getting deposits", |
| 97 | + timeout=SEQ_PUBLISH_BATCH_INTERVAL_SECS, |
| 98 | + ) |
| 99 | + |
| 100 | + current_block_num = int(rethrpc.eth_blockNumber(), base=16) |
| 101 | + print(f"Current reth block num: {current_block_num}") |
| 102 | + |
| 103 | + wait_until( |
| 104 | + lambda: int(rethrpc.eth_getBalance(f"0x{evm_addr}"), 16) > original_balance, |
| 105 | + error_with="eth balance did not update", |
| 106 | + timeout=EVM_WAIT_TIME, |
| 107 | + ) |
| 108 | + |
| 109 | + deposit_amount = ROLLUP_PARAMS_FOR_DEPOSIT_TX["deposit_amount"] * SATS_TO_WEI |
| 110 | + |
| 111 | + balance = int(rethrpc.eth_getBalance(f"0x{evm_addr}"), 16) |
| 112 | + print(f"Balance after deposit: {balance}") |
| 113 | + |
| 114 | + net_balance = balance - original_balance |
| 115 | + assert net_balance == deposit_amount, f"invalid deposit amount: {net_balance}" |
| 116 | + |
| 117 | + wait_until( |
| 118 | + lambda: int(rethrpc.eth_blockNumber(), base=16) > current_block_num, |
| 119 | + error_with="not building blocks", |
| 120 | + timeout=EVM_WAIT_TIME * 2, |
| 121 | + ) |
| 122 | + |
| 123 | + balance = int(rethrpc.eth_getBalance(f"0x{evm_addr}"), 16) |
| 124 | + net_balance = balance - original_balance |
| 125 | + assert ( |
| 126 | + net_balance == deposit_amount |
| 127 | + ), f"deposit processed multiple times, extra: {balance - original_balance - deposit_amount}" |
| 128 | + |
| 129 | + # Scan the L2 blocks where the deposits were included |
| 130 | + start_block = 1 |
| 131 | + end_block = int(rethrpc.eth_blockNumber(), base=16) + 1 |
| 132 | + for block_num in range(start_block, end_block): |
| 133 | + block = rethrpc.eth_getBlockByNumber(hex(block_num), False) |
| 134 | + withdrawals = block.get("withdrawals", None) |
| 135 | + if withdrawals is not None and len(withdrawals) != 0: |
| 136 | + return block_num, btc_txn_id |
| 137 | + |
| 138 | + return None, btc_txn_id |
0 commit comments