-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbatch_continuation_test.go
More file actions
374 lines (324 loc) · 10.6 KB
/
Copy pathbatch_continuation_test.go
File metadata and controls
374 lines (324 loc) · 10.6 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package test
import (
"bytes"
"context"
"encoding/hex"
"testing"
arklib "github.com/arkade-os/arkd/pkg/ark-lib"
"github.com/arkade-os/arkd/pkg/ark-lib/intent"
"github.com/arkade-os/arkd/pkg/ark-lib/script"
"github.com/arkade-os/arkd/pkg/ark-lib/tree"
"github.com/arkade-os/arkd/pkg/ark-lib/txutils"
"github.com/arkade-os/emulator/pkg/arkade"
emulatorclient "github.com/arkade-os/emulator/pkg/client"
arksdk "github.com/arkade-os/go-sdk"
"github.com/arkade-os/go-sdk/client"
"github.com/arkade-os/go-sdk/explorer"
mempoolexplorer "github.com/arkade-os/go-sdk/explorer/mempool"
"github.com/arkade-os/go-sdk/types"
"github.com/arkade-os/go-sdk/wallet"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil/psbt"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/stretchr/testify/require"
)
// TestCounterContractBatchContinuation exercises carrying custom contract
// packets through a batch-swap intent.
//
// Semantics: each batch swap is a real contract transition. The intent proof
// spends the current counter VTXO, carries counter=N+1 in its output packets,
// and outputs the same value back to the same contract script. The emulator
// executes the arkade script against that intent proof and signs only because
// counter=N+1 is a valid transition from the current counter=N.
//
// arkd propagates the counter packet from the intent proof into the new batch
// leaf tx (see https://github.com/arkade-os/arkd/issues/1017), so a second
// increment can read the previous counter via OP_INSPECTINPUTPACKET and chain
// from the batch leaf VTXO.
func TestCounterContractBatchContinuation(t *testing.T) {
t.Skip("requires arkd PR https://github.com/arkade-os/arkd/pull/1022")
ctx := t.Context()
alice, aliceWallet, alicePubKey, grpcClient := setupArkSDKwithPublicKey(t)
t.Cleanup(func() {
grpcClient.Close()
})
aliceAddr := fundAndSettleAlice(t, ctx, alice, 50000)
emulatorClient, emulatorPubKey, conn := setupEmulatorClient(t, ctx)
t.Cleanup(func() {
//nolint:errcheck
conn.Close()
})
infos, err := grpcClient.GetInfo(ctx)
require.NoError(t, err)
checkpointScriptBytes, err := hex.DecodeString(infos.CheckpointTapscript)
require.NoError(t, err)
indexerSvc := setupIndexer(t)
explorer, err := mempoolexplorer.NewExplorer("http://localhost:3000/api", arklib.BitcoinRegTest)
require.NoError(t, err)
// =========================================================================
// Phase 1: Deploy the counter at counter=0 from Alice's wallet VTXO.
// =========================================================================
counterArkadeScript := counterContractArkadeScript(t)
counterVtxoScript := createVtxoScriptWithArkadeAndCSV(
alicePubKey,
aliceAddr.Signer,
emulatorPubKey,
arkade.ArkadeScriptHash(counterArkadeScript),
)
counterTapscript := onlyForfeitScript(t, counterVtxoScript)
counterPkScript := p2trScriptForVtxoScript(t, counterVtxoScript)
counterRevealedTapscripts, err := counterVtxoScript.Encode()
require.NoError(t, err)
deployTx := deployCounterFromWallet(
t,
ctx,
alice,
aliceWallet,
grpcClient,
indexerSvc,
alicePubKey,
aliceAddr.Signer,
uint32(infos.UnilateralExitDelay),
counterPkScript,
checkpointScriptBytes,
)
// =========================================================================
// Phase 2: Increment from counter=0 to counter=1 through a swap intent.
//
// The intent proof spends the counter VTXO, carries counter=1, and asks
// arkd to create the same contract output in the next batch.
// =========================================================================
counterVtxoAmount := deployTx.UnsignedTx.TxOut[0].Value
cosignerKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
signerSession := tree.NewTreeSignerSession(cosignerKey)
firstIntentPtx, firstMessage := buildCounterIncrementIntent(
t,
signerSession,
deployTx.UnsignedTx,
0,
counterVtxoScript,
counterTapscript,
counterPkScript,
counterArkadeScript,
1,
)
requireCounterPacket(t, firstIntentPtx.UnsignedTx, 1)
require.NoError(t, executeArkadeScripts(t, firstIntentPtx, nil, emulatorPubKey))
signedIntent := signAndSubmitCounterIntent(
t,
ctx,
aliceWallet,
explorer,
emulatorClient,
firstIntentPtx,
firstMessage,
)
intentId, err := grpcClient.RegisterIntent(ctx, signedIntent.Proof, signedIntent.Message)
require.NoError(t, err)
vtxo := client.TapscriptsVtxo{
Vtxo: types.Vtxo{
Outpoint: types.Outpoint{
Txid: deployTx.UnsignedTx.TxHash().String(),
VOut: 0,
},
Script: hex.EncodeToString(counterTapscript),
Amount: uint64(counterVtxoAmount),
},
Tapscripts: counterRevealedTapscripts,
}
batchHandler := &capturingBatchEventsHandler{
delegateBatchEventsHandler: &delegateBatchEventsHandler{
intentId: intentId,
intent: signedIntent,
vtxosToForfeit: []client.TapscriptsVtxo{vtxo},
signerSession: signerSession,
emulatorClient: emulatorClient,
wallet: aliceWallet,
client: grpcClient,
explorer: explorer,
},
}
topics := arksdk.GetEventStreamTopics(
[]types.Outpoint{vtxo.Outpoint},
[]tree.SignerSession{signerSession},
)
eventStream, stop, err := grpcClient.GetEventStream(ctx, topics)
require.NoError(t, err)
t.Cleanup(func() {
stop()
})
commitmentTxid, err := arksdk.JoinBatchSession(ctx, eventStream, batchHandler)
require.NoError(t, err)
require.NotEmpty(t, commitmentTxid)
require.NotNil(t, batchHandler.vtxoTree)
// =========================================================================
// Phase 3: Increment again from the newly created batch leaf VTXO.
//
// The leaf tx retains the counter=1 packet from the first swap intent, so
// OP_INSPECTINPUTPACKET can recover it and validate the 1->2 transition.
// =========================================================================
nextCounterTx, nextCounterVout := findCounterLeafOutput(
t, batchHandler.vtxoTree, counterPkScript, counterVtxoAmount,
)
requireCounterPacket(t, nextCounterTx, 1)
secondSignerKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
secondSignerSession := tree.NewTreeSignerSession(secondSignerKey)
secondIntentPtx, secondMessage := buildCounterIncrementIntent(
t,
secondSignerSession,
nextCounterTx,
nextCounterVout,
counterVtxoScript,
counterTapscript,
counterPkScript,
counterArkadeScript,
2,
)
requireCounterPacket(t, secondIntentPtx.UnsignedTx, 2)
require.NoError(t, executeArkadeScripts(t, secondIntentPtx, nil, emulatorPubKey))
signAndSubmitCounterIntent(
t,
ctx,
aliceWallet,
explorer,
emulatorClient,
secondIntentPtx,
secondMessage,
)
}
type capturingBatchEventsHandler struct {
*delegateBatchEventsHandler
vtxoTree *tree.TxTree
}
func (h *capturingBatchEventsHandler) OnBatchFinalization(
ctx context.Context,
event client.BatchFinalizationEvent,
vtxoTree, connectorTree *tree.TxTree,
) error {
h.vtxoTree = vtxoTree
return h.delegateBatchEventsHandler.OnBatchFinalization(ctx, event, vtxoTree, connectorTree)
}
func buildCounterIncrementIntent(
t *testing.T,
signerSession tree.SignerSession,
prevArkTx *wire.MsgTx,
prevVout uint32,
counterVtxoScript script.TapscriptsVtxoScript,
counterTapscript []byte,
counterPkScript []byte,
counterArkadeScript []byte,
nextCounterValue uint64,
) (*psbt.Packet, string) {
t.Helper()
message, err := intent.RegisterMessage{
BaseMessage: intent.BaseMessage{
Type: intent.IntentMessageTypeRegister,
},
OnchainOutputIndexes: nil,
ExpireAt: 0,
ValidAt: 0,
CosignersPublicKeys: []string{signerSession.GetPublicKey()},
}.Encode()
require.NoError(t, err)
require.True(t, prevVout < uint32(len(prevArkTx.TxOut)))
counterVtxoAmount := prevArkTx.TxOut[prevVout].Value
require.Equal(t, counterPkScript, prevArkTx.TxOut[prevVout].PkScript)
intentProof, err := intent.New(
message,
[]intent.Input{
{
OutPoint: &wire.OutPoint{
Hash: prevArkTx.TxHash(),
Index: prevVout,
},
Sequence: wire.MaxTxInSequenceNum,
WitnessUtxo: prevArkTx.TxOut[prevVout],
},
},
[]*wire.TxOut{
{
Value: counterVtxoAmount,
PkScript: counterPkScript,
},
},
)
require.NoError(t, err)
_, counterTapTree, err := counterVtxoScript.TapTree()
require.NoError(t, err)
counterMerkleProof, err := counterTapTree.GetTaprootMerkleProof(
txscript.NewBaseTapLeaf(counterTapscript).TapHash(),
)
require.NoError(t, err)
counterCtrlBlock, err := txscript.ParseControlBlock(counterMerkleProof.ControlBlock)
require.NoError(t, err)
counterCtrlBlockBytes, err := counterCtrlBlock.ToBytes()
require.NoError(t, err)
counterRevealedTapscripts, err := counterVtxoScript.Encode()
require.NoError(t, err)
taptreeField, err := txutils.VtxoTaprootTreeField.Encode(counterRevealedTapscripts)
require.NoError(t, err)
tapLeafScript := []*psbt.TaprootTapLeafScript{
{
LeafVersion: txscript.BaseLeafVersion,
ControlBlock: counterCtrlBlockBytes,
Script: counterMerkleProof.Script,
},
}
intentProof.Inputs[0].TaprootLeafScript = tapLeafScript
intentProof.Inputs[1].TaprootLeafScript = tapLeafScript
intentProof.Inputs[0].Unknowns = append(intentProof.Inputs[0].Unknowns, taptreeField)
intentProof.Inputs[1].Unknowns = append(intentProof.Inputs[1].Unknowns, taptreeField)
intentPtx := &intentProof.Packet
addCounterPacket(t, intentPtx, nextCounterValue)
addEmulatorPacket(t, intentPtx, []arkade.EmulatorEntry{
{Vin: 1, Script: counterArkadeScript},
})
require.NoError(t, txutils.SetArkPsbtField(
intentPtx, 1, arkade.PrevArkTxField, *prevArkTx,
))
return intentPtx, message
}
func signAndSubmitCounterIntent(
t *testing.T,
ctx context.Context,
walletSvc wallet.WalletService,
explorerSvc explorer.Explorer,
emulatorClient emulatorclient.TransportClient,
intentPtx *psbt.Packet,
message string,
) emulatorclient.Intent {
t.Helper()
encodedIntentProof, err := intentPtx.B64Encode()
require.NoError(t, err)
signedIntentProof, err := walletSvc.SignTransaction(ctx, explorerSvc, encodedIntentProof)
require.NoError(t, err)
approvedIntentProof, err := emulatorClient.SubmitIntent(ctx, emulatorclient.Intent{
Proof: signedIntentProof,
Message: message,
})
require.NoError(t, err)
return emulatorclient.Intent{
Proof: approvedIntentProof,
Message: message,
}
}
func findCounterLeafOutput(
t *testing.T,
vtxoTree *tree.TxTree,
counterPkScript []byte,
counterVtxoAmount int64,
) (*wire.MsgTx, uint32) {
t.Helper()
for _, leaf := range vtxoTree.Leaves() {
for vout, output := range leaf.UnsignedTx.TxOut {
if output.Value == counterVtxoAmount && bytes.Equal(output.PkScript, counterPkScript) {
return leaf.UnsignedTx, uint32(vout)
}
}
}
require.FailNow(t, "counter leaf output not found")
return nil, 0
}