-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_minivault.py
193 lines (129 loc) · 6.61 KB
/
test_minivault.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
from typing import Tuple
import pytest
from examples.vault.minivault_contracts import Vault, Unvaulting
from matt.btctools import key
from matt.btctools.auth_proxy import AuthServiceProxy, JSONRPCException
from matt.btctools.messages import CTxOut
from matt.contracts import OpaqueP2TR
from matt.manager import ContractManager, SchnorrSigner
from matt.utils import format_tx_markdown
from test_utils import mine_blocks
unvault_priv_key = key.ExtendedKey.deserialize(
"tprv8ZgxMBicQKsPdpwA4vW8DcSdXzPn7GkS2RdziGXUX8k86bgDQLKhyXtB3HMbJhPFd2vKRpChWxgPe787WWVqEtjy8hGbZHqZKeRrEwMm3SN")
recover_priv_key = key.ExtendedKey.deserialize(
"tprv8ZgxMBicQKsPeDvaW4xxmiMXxqakLgvukT8A5GR6mRwBwjsDJV1jcZab8mxSerNcj22YPrusm2Pz5oR8LTw9GqpWT51VexTNBzxxm49jCZZ")
locktime = 10
MiniVaultSpecs = Tuple[str, Vault]
V_full: MiniVaultSpecs = (
"MiniVault",
Vault(None, locktime, recover_priv_key.pubkey[1:], unvault_priv_key.pubkey[1:])
)
V_no_partial_revault: MiniVaultSpecs = (
"MiniVault [no partial revault]",
Vault(None, locktime, recover_priv_key.pubkey[1:], unvault_priv_key.pubkey[1:], has_partial_revault=False)
)
V_no_early_recover: MiniVaultSpecs = (
"MiniVault [no early recover]",
Vault(None, locktime, recover_priv_key.pubkey[1:], unvault_priv_key.pubkey[1:], has_early_recover=False)
)
V_light: MiniVaultSpecs = (
"MiniVault [lightweight - no partial revault, no early recover]",
Vault(None, locktime, recover_priv_key.pubkey[1:], unvault_priv_key.pubkey[1:],
has_partial_revault=False, has_early_recover=False)
)
@pytest.mark.parametrize("minivault_specs", [V_full])
def test_minivault_recover(minivault_specs: MiniVaultSpecs, manager: ContractManager, report):
vault_description, vault_contract = minivault_specs
amount = 20_000
V_inst = manager.fund_instance(vault_contract, amount)
out_instances = V_inst("recover")(out_i=0)
out: CTxOut = V_inst.spending_tx.vout[0]
assert out.nValue == amount
assert out.scriptPubKey == OpaqueP2TR(recover_priv_key.pubkey[1:]).get_tr_info().scriptPubKey
report.write(vault_description, format_tx_markdown(V_inst.spending_tx, "Recovery from vault, 1 input"))
assert len(out_instances) == 0
@pytest.mark.parametrize("minivault_specs", [V_full, V_no_partial_revault, V_no_early_recover, V_light])
def test_minivault_trigger_and_recover(minivault_specs: MiniVaultSpecs, manager: ContractManager, report):
vault_description, vault_contract = minivault_specs
amount = 49999900
V_inst = manager.fund_instance(vault_contract, amount)
withdrawal_pk = bytes.fromhex("0981368165440d4fe866f84d75ae53a95b192aa45155735d4cb2a8894b340b8f")
[U_inst] = V_inst("trigger", signer=SchnorrSigner(unvault_priv_key))(
out_i=0,
withdrawal_pk=withdrawal_pk
)
report.write(vault_description, format_tx_markdown(V_inst.spending_tx, "Trigger"))
out_instances = U_inst("recover")(out_i=0)
assert len(out_instances) == 0
report.write(vault_description, format_tx_markdown(U_inst.spending_tx, "Recovery from trigger"))
@pytest.mark.parametrize("minivault_specs", [V_full, V_no_partial_revault, V_no_early_recover, V_light])
def test_minivault_trigger_and_withdraw(minivault_specs: MiniVaultSpecs, rpc: AuthServiceProxy, manager: ContractManager, report):
vault_description, vault_contract = minivault_specs
signer = SchnorrSigner(unvault_priv_key)
amount = 49999900
V_inst = manager.fund_instance(vault_contract, amount)
withdrawal_pk = bytes.fromhex("0981368165440d4fe866f84d75ae53a95b192aa45155735d4cb2a8894b340b8f")
[U_inst] = V_inst("trigger", signer=signer)(
out_i=0,
withdrawal_pk=withdrawal_pk
)
spend_tx, _ = manager.get_spend_tx(
(U_inst, "withdraw", {"withdrawal_pk": withdrawal_pk})
)
spend_tx.wit.vtxinwit = [manager.get_spend_wit(
U_inst,
"withdraw",
{"withdrawal_pk": withdrawal_pk}
)]
spend_tx.vin[0].nSequence = locktime
with pytest.raises(JSONRPCException):
manager.spend_and_wait(U_inst, spend_tx)
mine_blocks(rpc, locktime - 1)
manager.spend_and_wait(U_inst, spend_tx)
report.write(vault_description, format_tx_markdown(U_inst.spending_tx, "Withdraw"))
@pytest.mark.parametrize("minivault_specs", [V_full, V_no_early_recover])
def test_minivault_trigger_with_revault_and_withdraw(minivault_specs: MiniVaultSpecs, rpc: AuthServiceProxy, manager: ContractManager, report):
# get coins on 3 different Vaults, then trigger with partial withdrawal
# one of the vault uses "trigger_with_revault", the others us normal "trigger"
vault_description, vault_contract = minivault_specs
amount = 49_999_900
V_inst_1 = manager.fund_instance(vault_contract, amount)
V_inst_2 = manager.fund_instance(vault_contract, amount)
V_inst_3 = manager.fund_instance(vault_contract, amount)
withdrawal_pk = bytes.fromhex("0981368165440d4fe866f84d75ae53a95b192aa45155735d4cb2a8894b340b8f")
revault_amount = 20_000_000
spends = [
(V_inst_1, "trigger_and_revault", {"out_i": 0, "revault_out_i": 1, "withdrawal_pk": withdrawal_pk}),
(V_inst_2, "trigger", {"out_i": 0, "withdrawal_pk": withdrawal_pk}),
(V_inst_3, "trigger", {"out_i": 0, "withdrawal_pk": withdrawal_pk}),
]
spend_tx, sighashes = manager.get_spend_tx(spends, output_amounts={1: revault_amount})
spend_tx.wit.vtxinwit = []
sigs = [key.sign_schnorr(unvault_priv_key.privkey, sighash) for sighash in sighashes]
for i, (V_inst_i, action, args) in enumerate(spends):
spend_tx.wit.vtxinwit.append(manager.get_spend_wit(
V_inst_i,
action,
{**args, "sig": sigs[i]}
))
[U_inst, V_revault_inst] = manager.spend_and_wait([V_inst_1, V_inst_2, V_inst_3], spend_tx)
assert isinstance(U_inst.contract, Unvaulting)
assert isinstance(V_revault_inst.contract, Vault)
assert manager.instances.index(U_inst) >= 0
assert manager.instances.index(V_revault_inst) >= 0
report.write(vault_description, format_tx_markdown(spend_tx, "Trigger (with revault) [3 vault inputs]"))
spend_tx, _ = manager.get_spend_tx(
(U_inst, "withdraw", {"withdrawal_pk": withdrawal_pk})
)
# TODO: get_spend_wit does not fill the transaction
# according to the template (which the manager doesn't know)
# Figure out a better way to let the framework handle this
spend_tx.wit.vtxinwit = [manager.get_spend_wit(
U_inst,
"withdraw",
{"withdrawal_pk": withdrawal_pk}
)]
spend_tx.nVersion = 2
spend_tx.vin[0].nSequence = locktime
mine_blocks(rpc, locktime - 1)
manager.spend_and_wait(U_inst, spend_tx)