Skip to content

Commit 2afb560

Browse files
committed
enable test evm_withdraw_indirect
1 parent 5788dd0 commit 2afb560

10 files changed

+116
-75
lines changed

functional-tests/factory/alpen_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,14 @@ def l1_address(self):
143143
# fmt: on
144144
return self._run_and_extract_with_re(cmd, r"\b(?:bc1|tb1|bcrt1)[0-9a-z]{25,59}\b")
145145

146-
def deposit(self) -> str | None:
146+
def deposit(self, alpen_address=None) -> str | None:
147147
# fmt: off
148148
cmd = [
149149
"alpen",
150150
"deposit",
151151
]
152+
if alpen_address:
153+
cmd.append(alpen_address)
152154
# fmt: on
153155
return self._run_and_extract_with_re(cmd, r"Transaction ID:\s*([0-9a-f]{64})")
154156

functional-tests/mixins/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def premain(self, ctx: flexitest.RunContext):
2929
w3 = self._new_w3()
3030
funded_acc = FundedAccount(w3)
3131
funded_acc.fund_me(genesis_account)
32+
self.funded_acc = funded_acc
3233
# Setting transactions api with default DEBUG level.
3334
self._txs = EthTransactions(funded_acc, self.debug)
3435
self._w3 = w3

functional-tests/mixins/bridge_mixin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def deposit(
5050
self.info(f"Initial EL balance: {initial_balance}")
5151

5252
# Make DRT (deposit request transaction)
53-
drt_tx_id, raw_drt_bytes = self.make_drt()
53+
drt_tx_id, raw_drt_bytes = self.make_drt(el_address)
5454
self.info(f"Deposit Request Transaction ID: {drt_tx_id}")
5555

5656
# Create managed DT (deposit transaction) with auto-incremented ID
@@ -178,7 +178,7 @@ def check_checkpoint_covers_withdrawal():
178178

179179
return l2_tx_hash, tx_receipt, total_gas_used
180180

181-
def make_drt(self) -> tuple[str, str]:
181+
def make_drt(self, el_address=None) -> tuple[str, str]:
182182
"""
183183
Creates and matures a Deposit Request Transaction (DRT).
184184
@@ -193,7 +193,7 @@ def make_drt(self) -> tuple[str, str]:
193193
self.btcrpc.proxy.sendtoaddress(addr, 10.01)
194194
self.btcrpc.proxy.generatetoaddress(1, seq_addr)
195195
# Create and send deposit request transaction
196-
drt_tx_id = self.alpen_cli.deposit()
196+
drt_tx_id = self.alpen_cli.deposit(el_address)
197197
current_height = self.btcrpc.proxy.getblockcount()
198198
# time to mature DRT
199199
self.btcrpc.proxy.generatetoaddress(6, seq_addr)

functional-tests/mixins/bridge_out_precompile_contract_mixin.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from mixins import bridge_mixin
66
from utils import get_bridge_pubkey
7+
from utils.transaction import SmartContracts
78

89

910
class BridgePrecompileMixin(bridge_mixin.BridgeMixin):
@@ -15,14 +16,27 @@ def premain(self, ctx: flexitest.InitContext):
1516

1617
self.withdraw_address = ctx.env.gen_ext_btc_address()
1718
self.bridge_pk = get_bridge_pubkey(self.seqrpc)
19+
self.w3.eth.default_account = self.w3.address
1820

1921
xonlypk = extract_p2tr_pubkey(self.withdraw_address)
2022
bosd = xonlypk_to_descriptor(xonlypk)
2123

2224
self.bosd = bytes.fromhex(bosd)
2325

26+
# Extract ABI for compatibility with existing tests
27+
self.abi, _ = SmartContracts.compile_contract(
28+
"IndirectWithdrawalProxy.sol", "WithdrawCaller"
29+
)
30+
2431
# Deploy contract.
2532
self.withdraw_contract_id = "withdraw_contract"
26-
self.txs.deploy_contract(
33+
contract_address, _ = self.txs.deploy_contract(
2734
"IndirectWithdrawalProxy.sol", "WithdrawCaller", self.withdraw_contract_id
2835
)
36+
37+
# Create a simple object to hold the contract address for compatibility
38+
class DeploymentReceipt:
39+
def __init__(self, contract_address):
40+
self.contractAddress = contract_address
41+
42+
self.deployed_contract_receipt = DeploymentReceipt(contract_address)

functional-tests/tests/bridge/bridge_test.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import os
2-
3-
import base58
41
import flexitest
52
from strata_utils import (
63
extract_p2tr_pubkey,
@@ -30,24 +27,7 @@ def __init__(self, ctx: flexitest.InitContext):
3027
)
3128

3229
def main(self, ctx: flexitest.RunContext):
33-
path = os.path.join(ctx.datadir_root, "_bridge_test", "_init")
34-
print(path)
35-
priv_keys = []
36-
opkeys = sorted(
37-
filter(lambda file: file.startswith("opkey"), os.listdir(path)),
38-
key=lambda x: int("".join(filter(str.isdigit, x))),
39-
)
40-
print(opkeys)
41-
for filename in opkeys:
42-
if not filename.startswith("op"):
43-
continue
44-
45-
full_path = os.path.join(path, filename)
46-
with open(full_path) as f:
47-
content = f.read().strip()
48-
decoded = base58.b58decode(content)[:-4] # remove checksum
49-
priv_keys.append(decoded)
50-
30+
priv_keys = get_priv_keys(ctx)
5131
el_address = self.alpen_cli.l2_address()
5232
print("-----------------------")
5333
print(el_address)

functional-tests/tests/el_bridge_precompile.py

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
1-
import os
2-
import time
3-
41
import flexitest
52
from web3 import Web3
63
from web3._utils.events import get_event_data
74

8-
from envs import testenv
5+
from envs import net_settings, testenv
6+
from mixins.bridge_out_precompile_contract_mixin import BridgePrecompileMixin
7+
from utils import *
98
from utils.constants import PRECOMPILE_BRIDGEOUT_ADDRESS
109

1110
withdrawal_intent_event_abi = {
1211
"anonymous": False,
1312
"inputs": [
1413
{"indexed": False, "internalType": "uint64", "name": "amount", "type": "uint64"},
15-
{"indexed": False, "internalType": "bytes", "name": "dest_pk", "type": "bytes32"},
14+
{"indexed": False, "internalType": "bytes", "name": "destination", "type": "bytes"},
1615
],
1716
"name": "WithdrawalIntentEvent",
1817
"type": "event",
1918
}
20-
event_signature_text = "WithdrawalIntentEvent(uint64,bytes32)"
19+
event_signature_text = "WithdrawalIntentEvent(uint64,bytes)"
2120

2221

2322
@flexitest.register
24-
class ElBridgePrecompileTest(testenv.StrataTestBase):
23+
class ElBridgePrecompileTest(BridgePrecompileMixin):
2524
def __init__(self, ctx: flexitest.InitContext):
26-
ctx.set_env("basic")
25+
ctx.set_env(
26+
testenv.BasicEnvConfig(
27+
101,
28+
prover_client_settings=ProverClientSettings.new_with_proving(),
29+
rollup_settings=net_settings.get_fast_batch_settings(),
30+
auto_generate_blocks=True,
31+
)
32+
)
2733

2834
def main(self, ctx: flexitest.RunContext):
29-
self.warning("SKIPPING TEST fn_el_bridge_precompile")
30-
return True
31-
32-
reth = ctx.get_service("reth")
33-
web3: Web3 = reth.create_web3()
35+
web3: Web3 = self.reth.create_web3()
3436

3537
source = web3.address
3638
dest = web3.to_checksum_address(PRECOMPILE_BRIDGEOUT_ADDRESS)
37-
# 64 bytes
38-
dest_pk = os.urandom(32).hex()
39-
self.debug(dest_pk)
39+
40+
priv_keys = get_priv_keys(ctx)
41+
self.deposit(ctx, self.deployed_contract_receipt.contractAddress, priv_keys)
4042

4143
assert web3.is_connected(), "cannot connect to reth"
4244

@@ -46,24 +48,36 @@ def main(self, ctx: flexitest.RunContext):
4648

4749
assert original_bridge_balance == 0
4850

49-
# 10 rollup btc as wei
50-
to_transfer_wei = 10_000_000_000_000_000_000
51+
cfg = ctx.env.rollup_cfg()
52+
deposit_amount = cfg.deposit_amount
53+
to_transfer_sats = deposit_amount * 10_000_000_000
54+
to_transfer_wei = to_transfer_sats # Same value in wei
55+
dest_pk = "0x04db4c79cc3ffca26f51e21241b9332d646b0772dd7e98de9c1de6b10990cab80b"
5156

5257
txid = web3.eth.send_transaction(
5358
{
5459
"to": dest,
55-
"value": hex(to_transfer_wei),
56-
"gas": hex(100000),
60+
"value": hex(to_transfer_sats),
5761
"from": source,
62+
"gas": hex(200000),
5863
"data": dest_pk,
5964
}
6065
)
61-
self.debug(txid.to_0x_hex())
6266

63-
# build block
64-
time.sleep(2)
65-
66-
receipt = web3.eth.get_transaction_receipt(txid)
67+
def check_transaction_and_blocks():
68+
try:
69+
receipt = web3.eth.get_transaction_receipt(txid)
70+
return receipt
71+
except Exception as e:
72+
return e
73+
74+
receipt = wait_until_with_value(
75+
check_transaction_and_blocks,
76+
lambda result: not isinstance(result, Exception),
77+
error_with="Transaction receipt for txid not available",
78+
timeout=60,
79+
step=2,
80+
)
6781

6882
assert receipt.status == 1, "precompile transaction failed"
6983
assert len(receipt.logs) == 1, "no logs or invalid logs"
@@ -74,11 +88,8 @@ def main(self, ctx: flexitest.RunContext):
7488
assert log.topics[0].hex() == event_signature_hash
7589
event_data = get_event_data(web3.codec, withdrawal_intent_event_abi, log)
7690

77-
# 1 rollup btc = 10**18 wei
78-
to_transfer_sats = to_transfer_wei // 10_000_000_000
79-
80-
assert event_data.args.amount == to_transfer_sats
81-
assert event_data.args.dest_pk.hex() == dest_pk
91+
assert event_data.args.amount == deposit_amount
92+
assert event_data.args.destination.hex() == dest_pk[2:] # Remove 0x prefix for comparison
8293

8394
final_block_no = web3.eth.block_number
8495
final_bridge_balance = web3.eth.get_balance(dest)

functional-tests/tests/evm/evm_withdraw_indirect_with_contract_balance.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
import logging
2-
31
import flexitest
42

53
from envs import net_settings, testenv
64
from mixins.bridge_out_precompile_contract_mixin import BridgePrecompileMixin
5+
from utils import ProverClientSettings, get_priv_keys
76

87

98
@flexitest.register
109
class ContractBridgeOutWithContractBalanceTest(BridgePrecompileMixin):
1110
def __init__(self, ctx: flexitest.InitContext):
12-
fast_batch_settings = net_settings.get_fast_batch_settings()
1311
ctx.set_env(
14-
testenv.BasicEnvConfig(pre_generate_blocks=101, rollup_settings=fast_batch_settings)
12+
testenv.BasicEnvConfig(
13+
101,
14+
prover_client_settings=ProverClientSettings.new_with_proving(),
15+
rollup_settings=net_settings.get_fast_batch_settings(),
16+
auto_generate_blocks=True,
17+
)
1518
)
1619

1720
def main(self, ctx: flexitest.RunContext):
18-
logging.warn("test temporarily disabled")
19-
return
20-
2121
# Deposit to contract Address
22-
self.deposit(ctx, self.deployed_contract_receipt.contractAddress, self.bridge_pk)
22+
priv_keys = get_priv_keys(ctx)
23+
self.deposit(ctx, self.deployed_contract_receipt.contractAddress, priv_keys)
24+
self.deposit(ctx, self.web3.address, priv_keys)
2325

2426
# withdraw
2527
# TODO: use self.txs.deploy and self.txs.call
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,44 @@
1-
import logging
2-
31
import flexitest
42

53
from envs import net_settings, testenv
64
from envs.rollup_params_cfg import RollupConfig
75
from mixins.bridge_out_precompile_contract_mixin import BridgePrecompileMixin
6+
from utils import ProverClientSettings, get_priv_keys
87
from utils.constants import SATS_TO_WEI
98

109

1110
@flexitest.register
1211
class ContractBridgeOutWithSenderValueTest(BridgePrecompileMixin):
1312
def __init__(self, ctx: flexitest.InitContext):
14-
fast_batch_settings = net_settings.get_fast_batch_settings()
1513
ctx.set_env(
16-
testenv.BasicEnvConfig(pre_generate_blocks=101, rollup_settings=fast_batch_settings)
14+
testenv.BasicEnvConfig(
15+
101,
16+
prover_client_settings=ProverClientSettings.new_with_proving(),
17+
rollup_settings=net_settings.get_fast_batch_settings(),
18+
auto_generate_blocks=True,
19+
)
1720
)
1821

1922
def main(self, ctx: flexitest.RunContext):
20-
logging.warn("test temporarily disabled")
21-
return
23+
priv_keys = get_priv_keys(ctx)
2224

23-
# deposit once
24-
self.deposit(ctx, self.bridge_eth_account.address, self.bridge_pk)
25+
# deposit twice
26+
self.deposit(ctx, self.web3.address, priv_keys)
27+
self.deposit(ctx, self.web3.address, priv_keys)
28+
print(self.web3.address)
2529

2630
cfg: RollupConfig = ctx.env.rollup_cfg()
2731
deposit_amount = cfg.deposit_amount
2832

2933
# Call the contract function
3034
# TODO: use self.txs.deploy and self.txs.call
31-
contract_instance = self.w3.eth.contract(
35+
# check balance
36+
contract_instance = self.web3.eth.contract(
3237
abi=self.abi, address=self.deployed_contract_receipt.contractAddress
3338
)
3439
tx_hash = contract_instance.functions.withdraw(self.bosd).transact(
3540
{"gas": 5_000_000, "value": deposit_amount * SATS_TO_WEI}
3641
)
3742

38-
tx_receipt = self.w3.eth.wait_for_transaction_receipt(tx_hash, timeout=30)
43+
tx_receipt = self.web3.eth.wait_for_transaction_receipt(tx_hash, timeout=30)
3944
assert tx_receipt.status == 1

functional-tests/tests/prover/prover_el_deposit_withdraw.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def main(self, ctx: flexitest.RunContext):
3434
self.warning("SKIPPING TEST prover_el_deposit_withdraw")
3535
return True
3636

37-
evm_addr = self.bridge_eth_account.address
37+
evm_addr = self.eth_account.address
3838
bridge_pk = get_bridge_pubkey(self.seqrpc)
3939

4040
# Init RPCs.

functional-tests/utils/utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from threading import Thread
99
from typing import Any, TypeVar
1010

11+
import base58
1112
from bitcoinlib.services.bitcoind import BitcoindClient
1213
from strata_utils import convert_to_xonly_pk, musig_aggregate_pks
1314

@@ -652,3 +653,28 @@ def check_initial_eth_balance(rethrpc, address, debug_fn=print):
652653
balance = int(rethrpc.eth_getBalance(address), 16)
653654
debug_fn(f"Strata Balance before deposits: {balance}")
654655
assert balance == 0, "Strata balance is not expected (should be zero initially)"
656+
657+
658+
def get_priv_keys(ctx, env=None):
659+
if env is None:
660+
path = os.path.join(ctx.datadir_root, f"_{ctx.name}", "_init")
661+
else:
662+
path = os.path.join(ctx.datadir_root, env, "_init")
663+
664+
print(path)
665+
priv_keys = []
666+
opkeys = sorted(
667+
filter(lambda file: file.startswith("opkey"), os.listdir(path)),
668+
key=lambda x: int("".join(filter(str.isdigit, x))),
669+
)
670+
print(opkeys)
671+
for filename in opkeys:
672+
if not filename.startswith("op"):
673+
continue
674+
675+
full_path = os.path.join(path, filename)
676+
with open(full_path) as f:
677+
content = f.read().strip()
678+
decoded = base58.b58decode(content)[:-4] # remove checksum
679+
priv_keys.append(decoded)
680+
return priv_keys

0 commit comments

Comments
 (0)