-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_runes_testserver.py
194 lines (156 loc) · 5.66 KB
/
start_runes_testserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""
Quick and dirty testserver for UI dev
"""
import threading
import logging
import sys
import time
from bridge.common.btc.rpc import BitcoinRPC
from tests.services import BitcoindService
sys.path.extend("bridge_node")
from tests.integration.fixtures.harness import IntegrationTestHarness # noqa
from tests import services # noqa
WATCHED_CONTAINERS = [
"alice-bridge",
"bob-bridge",
]
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
RUNE_BRIDGE_ADDRESS = "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9"
RUNE_NAME = "MYRUNEISGOODER"
BTC_SLEEP_TIME = 5
BTC_BLOCK_INTERVAL = 15
def create_ord(bitcoind):
service = services.OrdService(bitcoind=bitcoind)
assert service.is_running()
return service
def create_alice_ord_wallet(alice_ord, bitcoin_rpc):
wallet = services.OrdWallet(
ord=alice_ord,
name="alice-ord-test",
)
wallets = bitcoin_rpc.call("listwallets")
if wallet.name not in wallets:
logger.info("Creating alice-ord-test wallet")
wallet.create()
balances = wallet.cli("balance")
if balances["cardinal"] < 100:
logger.info("Funding alice-ord-test wallet")
address = wallet.cli("receive")["addresses"][0]
logger.info("ALICE ORD ADDRESS: %s", address)
bitcoin_rpc.mine_blocks(101, address, sleep=BTC_SLEEP_TIME)
if RUNE_NAME not in balances["runes"]:
wallet.etch_test_rune(RUNE_NAME)
bitcoin_rpc.mine_blocks(1, sleep=BTC_SLEEP_TIME)
return wallet
def create_user_ord_wallet(user_ord, bitcoin_rpc, alice_ord_wallet):
wallet = services.OrdWallet(
ord=user_ord,
name="user-ord-test",
)
wallets = bitcoin_rpc.call("listwallets")
address = None
if wallet.name not in wallets:
logger.info("Creating user-ord-test wallet")
wallet.create()
balances = wallet.cli("balance")
if balances["cardinal"] < 1000:
logger.info("Funding user-ord-test wallet")
address = wallet.cli("receive")["addresses"][0]
logger.info("USER ORD ADDRESS: %s", address)
bitcoin_rpc.mine_blocks(101, address, sleep=BTC_SLEEP_TIME)
if balances["runes"].get(RUNE_NAME, 0) < 1000 * 10**18:
if address is None:
address = wallet.cli("receive")["address"]
logger.info("USER ORD ADDRESS: %s", address)
alice_ord_wallet.cli(
"send",
"--fee-rate",
"1",
address,
f"100000:{RUNE_NAME}",
)
bitcoin_rpc.mine_blocks(1, sleep=BTC_SLEEP_TIME)
return wallet
def init_runes(bitcoin_rpc: BitcoinRPC):
bitcoind = BitcoindService()
assert bitcoind.is_running()
ord = create_ord(bitcoind=bitcoind)
alice_ord_wallet = create_alice_ord_wallet(ord, bitcoin_rpc)
create_user_ord_wallet(ord, bitcoin_rpc, alice_ord_wallet)
# harness takes care of the bridge wallets
# setup_bridge_wallet(bitcoin_rpc)
bitcoin_mining = False
def start_bitcoin_mining(bitcoin_rpc: BitcoinRPC):
global bitcoin_mining
logger.info("Starting bitcoin mining")
assert not bitcoin_mining
bitcoin_mining = True
def mine_bitcoin_blocks():
while bitcoin_mining:
bitcoin_rpc.mine_blocks(1, sleep=0.2)
start = time.time()
while time.time() - start < BTC_BLOCK_INTERVAL and bitcoin_mining:
time.sleep(1)
mining_thread = threading.Thread(target=mine_bitcoin_blocks)
mining_thread.start()
return mining_thread
def stop_bitcoin_mining(bitcoin_mining_thread: threading.Thread):
global bitcoin_mining
if not bitcoin_mining:
return
logger.info("Stopping bitcoin mining")
bitcoin_mining = False
if bitcoin_mining_thread:
bitcoin_mining_thread.join()
def print_info():
print("")
print("[INFO]")
print("Hardhat network (for metamask, etc): http://localhost:18545")
print("ORD explorer: http://localhost:3080/runes")
print("Bridge API: http://localhost:8181")
print(f"RuneBridge contract {RUNE_BRIDGE_ADDRESS}")
print(f"Test rune name: {RUNE_NAME}")
print("")
print(
"To generate a deposit address: (replace 0x1111111111111111111111111111111111111111 with user's EVM address)"
)
print(
"""
curl -X POST -H 'Content-Type: application/json' -d '{"evm_address": "0x1111111111111111111111111111111111111111"}' http://localhost:8181/api/v1/runes/deposit-addresses/
"""
)
print("To read balances of the user")
print(
"""
local_dev/bin/ord wallet --name user-ord-test balance
"""
)
print(
"To send runes to the address (replace bcrt1qtxysk2megp39dnpw9va32huk5fesrlvutl0zdpc29asar4hfkrlqs2kzv5 with generated deposit address):"
)
print(
f"""
local_dev/bin/ord wallet --name user-ord-test send --fee-rate 1 bcrt1qtxysk2megp39dnpw9va32huk5fesrlvutl0zdpc29asar4hfkrlqs2kzv5 "123 {RUNE_NAME}"
"""
)
def main():
harness = IntegrationTestHarness(verbose=True)
bitcoin_mining_thread = None
try:
harness.start()
print("Harness started, initing runes")
bitcoin_rpc = BitcoinRPC("http://polaruser:polarpass@localhost:18443")
init_runes(bitcoin_rpc)
bitcoin_mining_thread = start_bitcoin_mining(bitcoin_rpc)
print_info()
print("Press [enter] to view logs or Ctrl-C to quit")
input()
harness._run_docker_compose_command("logs", "-f", *WATCHED_CONTAINERS)
except KeyboardInterrupt:
print("Stopping")
finally:
stop_bitcoin_mining(bitcoin_mining_thread)
harness.stop()
if __name__ == "__main__":
main()