-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
584 lines (505 loc) · 21 KB
/
main.go
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package main
import (
"bytes"
"context"
"database/sql"
"encoding/hex"
"encoding/json"
"fmt"
"juicerkle/tree"
"log"
"math/big"
"net/http"
"os"
"strconv"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
_ "github.com/mattn/go-sqlite3"
)
// An item in config.json
type NetworkConfig struct {
Name string `json:"name"`
ChainId *big.Int `json:"chainId"`
RpcUrl string `json:"rpcUrl"`
}
// A BPLeaf as stored in sqlite
type DBLeaf struct {
ChainId string `db:"chain_id"`
ContractAddress string `db:"contract_address"`
TokenAddress string `db:"token_address"`
Index string `db:"leaf_index"`
Beneficiary string `db:"leaf_beneficiary"`
ProjectTokenAmount string `db:"leaf_project_token_amount"`
TerminalTokenAmount string `db:"leaf_terminal_token_amount"`
LeafHash string `db:"leaf_hash"`
IsClaimed bool `db:"is_claimed"`
}
// A specification for an inbox tree on a sucker contract
type InboxTree struct {
ChainId *big.Int
SuckerAddress common.Address
TokenAddress common.Address
Root [32]byte
}
// Schema for incoming claims requests
type ClaimsRequest struct {
ChainId *big.Int `json:"chainId"` // The chain ID of the sucker contract
Sucker common.Address `json:"sucker"` // The sucker contract address
Token common.Address `json:"token"` // The token address of the inbox tree being claimed from
Beneficiary common.Address `json:"beneficiary"` // The address of the beneficiary to get the claims for
}
// Map of chain IDs (as strings) to ETH clients
var clients = make(map[string]*ethclient.Client)
func main() {
// Read networks
var networks []NetworkConfig
configFile, err := os.ReadFile("config.json")
if err != nil {
log.Fatalf("Could not read config.json: %v\n", err)
}
if err := json.Unmarshal(configFile, &networks); err != nil {
log.Fatalf("Failed to unmarshal config.json: %v\n", err)
}
// Set up ETH clients
for _, network := range networks {
client, err := ethclient.Dial(network.RpcUrl)
if err != nil {
log.Fatalf("Failed to connect to the %s network: %v\n", network.Name, err)
}
clients[network.ChainId.String()] = client
}
// Set up DB
if err := initDb(); err != nil {
log.Fatalf("Failed to initialize database: %v\n", err)
}
defer db.Close()
// Default to 8080 if no port is specified
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.HandleFunc("POST /claims", claims)
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Juicerkle running. Send claims requests to /claims."))
})
log.Printf("Listening on http://localhost:%s\n", port)
http.ListenAndServe(":"+port, nil)
}
var (
MaxIndex = big.NewInt(1 << 32)
ZeroRoot = make([]byte, 32)
)
func claims(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
var toClaim ClaimsRequest
err := json.NewDecoder(req.Body).Decode(&toClaim)
if err != nil {
errStr := fmt.Sprintf("Failed to parse claims request body: %v", err)
log.Println(errStr)
http.Error(w, errStr, http.StatusBadRequest)
return
}
// Set up context
// ctx, cancel := context.WithTimeout(req.Context(), 15*time.Second)
ctx, cancel := context.WithCancel(req.Context()) // Use cancel instead of timeout for debugging
defer cancel()
logDescription := fmt.Sprintf("beneficiary %s; inbox %s of sucker %s on chain %s",
toClaim.Beneficiary, toClaim.Token, toClaim.Sucker, toClaim.ChainId)
client, ok := clients[toClaim.ChainId.String()]
if !ok {
errStr := fmt.Sprintf("No RPC for chain ID %s. Contact the developer to have it added.", toClaim.ChainId)
log.Println(errStr + " For " + logDescription)
http.Error(w, errStr, http.StatusNotFound)
return
}
localSucker, err := NewBPSucker(toClaim.Sucker, client)
if err != nil {
errStr := fmt.Sprintf("Failed to instantiate a BPSucker for %s: %v", logDescription, err)
log.Println(errStr)
http.Error(w, errStr, http.StatusInternalServerError)
return
}
// callOpts with our context
callOpts := &bind.CallOpts{Context: ctx}
localInboxTree, err := localSucker.Inbox(callOpts, toClaim.Token)
if err != nil {
errStr := fmt.Sprintf("Failed to get inbox tree root for %s: %v", logDescription, err)
log.Println(errStr)
http.Error(w, errStr, http.StatusInternalServerError)
return
}
// Check if the inbox tree root is empty.
if bytes.Equal(localInboxTree.Root[:], ZeroRoot) {
errStr := fmt.Sprintf("Inbox tree root for %s is empty", logDescription)
log.Println(errStr)
http.Error(w, errStr, http.StatusBadRequest)
return
}
// Get the claims
claims, err := dbClaims(ctx, toClaim.Beneficiary, InboxTree{
ChainId: toClaim.ChainId,
SuckerAddress: toClaim.Sucker,
TokenAddress: toClaim.Token,
Root: localInboxTree.Root,
})
if err != nil {
errStr := fmt.Sprintf("Failed to get claims for %s: %v", logDescription, err)
log.Println(errStr)
http.Error(w, errStr, http.StatusInternalServerError)
return
}
b, err := json.Marshal(claims)
if err != nil {
errStr := fmt.Sprintf("Failed to marshal claims for %s: %v", logDescription, err)
log.Println(errStr)
http.Error(w, errStr, http.StatusInternalServerError)
return
}
w.Write(b)
}
// Get the currently available BPClaims for a beneficiary from the database.
// If the database is out of date for the given inbox tree, update it.
func dbClaims(ctx context.Context, beneficiary common.Address, inboxTree InboxTree) ([]BPClaim, error) {
logDescription := fmt.Sprintf("inbox %s of sucker %s on chain %s",
inboxTree.TokenAddress, inboxTree.SuckerAddress, inboxTree.ChainId)
// Get the current root from the database (which may be out of date)
row := db.QueryRowContext(ctx, `SELECT current_root FROM trees
WHERE chain_id = ? AND contract_address = ? AND token_address = ?`,
inboxTree.ChainId.String(), inboxTree.SuckerAddress.String(), inboxTree.TokenAddress.String())
var dbRoot string
if err := row.Scan(&dbRoot); err != nil && err != sql.ErrNoRows {
return nil, fmt.Errorf("failed to query root from database for %s: %v", logDescription, err)
}
dbRootBytes, err := hex.DecodeString(dbRoot)
// This should never happen, but just in case:
if err != nil {
return nil, fmt.Errorf("failed to decode dbRoot '%s' for %s: %v", dbRoot, logDescription, err)
}
// If no rows were found, or the roots don't match, we need to update the database.
if err == sql.ErrNoRows || !bytes.Equal(dbRootBytes, inboxTree.Root[:]) {
log.Printf("Updating the database for %s", logDescription)
if err := updateLeaves(ctx, inboxTree); err != nil {
return nil, fmt.Errorf("failed to update leaves for %s: %v", logDescription, err)
}
}
// Update the database with the latest claims for this inbox tree.
if err := updateClaims(ctx, inboxTree); err != nil {
return nil, fmt.Errorf("failed to update claims for %s: %v", logDescription, err)
}
rows, err := db.QueryContext(ctx, `SELECT leaf_hash, leaf_index, leaf_beneficiary,
leaf_project_token_amount, leaf_terminal_token_amount, is_claimed
FROM leaves WHERE chain_id = ? AND contract_address = ? AND token_address = ?
ORDER BY leaf_index ASC`,
inboxTree.ChainId.String(), inboxTree.SuckerAddress.String(), inboxTree.TokenAddress.String())
if err != nil {
// Note: this includes sql.ErrNoRows
return nil, fmt.Errorf("failed to read leaves for %s from the database: %v", logDescription, err)
}
// Build the tree and claims from the database leaves
claims := make([]BPClaim, 0)
leaves := make([][]byte, 0)
// TODO: Check whether the leaf has already been claimed.
defer rows.Close()
for rows.Next() {
var leafHash, index, leafBeneficiary, projectTokenAmount, terminalTokenAmount string
var isClaimed bool
err := rows.Scan(&leafHash, &index, &leafBeneficiary, &projectTokenAmount, &terminalTokenAmount, &isClaimed)
if err != nil {
return nil, fmt.Errorf("failed to scan leaf hash %s for %s: %v", leafHash, logDescription, err)
}
h, err := hex.DecodeString(leafHash)
if err != nil {
return nil, fmt.Errorf("failed to decode leaf hash %s for %s: %v", leafHash, logDescription, err)
}
leaves = append(leaves, h)
// If this leaf has already been claimed, move on.
if isClaimed {
continue
}
// Check if the leaf is for the beneficiary we're looking for, and if so, add it to the claims
if !common.IsHexAddress(leafBeneficiary) {
return nil, fmt.Errorf("db leaf beneficiary %s is not a valid address for %s", leafBeneficiary, logDescription)
}
b := common.HexToAddress(leafBeneficiary)
if beneficiary.Cmp(b) == 0 {
idx, success := big.NewInt(0).SetString(index, 10)
if !success {
return nil, fmt.Errorf("failed to parse index %s for %s: %v", index, logDescription, err)
}
pt, success := big.NewInt(0).SetString(projectTokenAmount, 10)
if !success {
return nil, fmt.Errorf("failed to parse projectTokenAmount %s for %s: %v", projectTokenAmount, logDescription, err)
}
tt, success := big.NewInt(0).SetString(terminalTokenAmount, 10)
if !success {
return nil, fmt.Errorf("failed to parse terminalTokenAmount %s for %s: %v", terminalTokenAmount, logDescription, err)
}
claims = append(claims, BPClaim{
Token: inboxTree.TokenAddress,
Leaf: BPLeaf{
Index: idx,
Beneficiary: beneficiary,
ProjectTokenAmount: pt,
TerminalTokenAmount: tt,
},
})
}
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("failed while scanning leaves for %s from the database: %v", logDescription, err)
}
// Construct a tree with the retrieved leaves
t := tree.NewTree(leaves)
treeRoot := t.Root()
// Sanity check the tree root
if !bytes.Equal(treeRoot, inboxTree.Root[:]) {
return nil, fmt.Errorf("constructed tree has root %s, does not match onchain tree root %s for %s",
hex.EncodeToString(treeRoot), hex.EncodeToString(inboxTree.Root[:]), logDescription)
}
// Add the proofs to the claims
for i := range claims {
// We know the index is within uint bounds for 32/64-bit platforms because there can only be 2^32 leaves
proofIndex := uint(claims[i].Leaf.Index.Uint64())
p, err := t.Proof(proofIndex)
if err != nil {
return nil, fmt.Errorf("failed to get proof at index %d for %s: %v", proofIndex, logDescription, err)
}
proofArray, err := proofSliceToArray(p)
if err != nil {
return nil, fmt.Errorf("failed to convert proof to arrays for %s: %v", logDescription, err)
}
claims[i].Proof = proofArray
}
return claims, nil
}
// Update the leaves in the database for a specific inbox tree
func updateLeaves(ctx context.Context, inboxTree InboxTree) error {
logDescription := fmt.Sprintf("inbox %s of sucker %s on chain %s",
inboxTree.TokenAddress, inboxTree.SuckerAddress, inboxTree.ChainId)
client := clients[inboxTree.ChainId.String()] // chain was checked in the handler function
// Get the latest leaf hash for the tree from the db
row := db.QueryRowContext(ctx, `SELECT leaf_hash FROM leaves
WHERE chain_id = ? AND contract_address = ? AND token_address = ?
ORDER BY leaf_index DESC LIMIT 1`,
inboxTree.ChainId.String(), inboxTree.SuckerAddress.String(), inboxTree.TokenAddress.String())
var latestHash string
err := row.Scan(&latestHash)
if err != nil && err != sql.ErrNoRows {
return fmt.Errorf("failed to query latest leaf from database for %s: %v", logDescription, err)
}
seenLatestDbLeaf := false
var latestHashBytes []byte
if err == sql.ErrNoRows {
// Start parsing events from the beginning if our query didn't return any leaf
seenLatestDbLeaf = true
} else {
if latestHashBytes, err = hex.DecodeString(latestHash); err != nil {
return fmt.Errorf("failed to decode leaf hash %s from db for %s: %v", latestHash, logDescription, err)
}
}
// Instantiate the local sucker
localSucker, err := NewBPSucker(inboxTree.SuckerAddress, client)
if err != nil {
return fmt.Errorf("failed to instantiate the sucker contract for %s: %v", logDescription, err)
}
// TODO: We can parallelize some of these reads if we need to reduce latency.
// We need to read the InsertToOutboxTree events from the peer sucker.
// First, get the peer sucker's chain ID and address.
callOpts := &bind.CallOpts{Context: ctx}
peerSuckerChainId, err := localSucker.PeerChainID(callOpts)
if err != nil {
return fmt.Errorf("failed to get peer chain ID for %s: %v", logDescription, err)
}
peerSuckerAddr, err := localSucker.PEER(callOpts)
if err != nil {
return fmt.Errorf("failed to get peer address for %s: %v", logDescription, err)
}
peerClient, ok := clients[peerSuckerChainId.String()]
if !ok {
return fmt.Errorf("no RPC for peer chain %d, peer for %s; contact the developer to have it added", peerSuckerChainId, logDescription)
}
// Instantiate the peer sucker
peerSucker, err := NewBPSucker(peerSuckerAddr, peerClient)
if err != nil {
return fmt.Errorf("failed to instantiate the peer sucker at %s on chain %d for %s: %v", peerSuckerAddr, peerSuckerChainId, logDescription, err)
}
// Add peer information to further logging
logDescription += fmt.Sprintf(" with peer sucker %s on chain %d", peerSuckerAddr, peerSuckerChainId)
// Make sure the peers match
peerAddressOfPeerSucker, err := peerSucker.PEER(callOpts)
if err != nil {
return fmt.Errorf("failed to get peer address of peer sucker: %v", err)
}
if peerAddressOfPeerSucker.Cmp(inboxTree.SuckerAddress) != 0 {
return fmt.Errorf("peer address of peer sucker %s on chain %d is %s, which does not match local sucker %s on chain %d",
peerSuckerAddr, peerSuckerChainId, peerAddressOfPeerSucker, inboxTree.SuckerAddress, inboxTree.ChainId)
}
// We also need to know what peer outbox token address the local inbox tree corresponds to
remoteToken, err := localSucker.RemoteTokenFor(callOpts, inboxTree.TokenAddress)
if err != nil {
return fmt.Errorf("failed to get remote token for %s: %v", logDescription, err)
}
// Iterate through insertions to the peer sucker's outbox
outboxIterator, err := peerSucker.FilterInsertToOutboxTree(
&bind.FilterOpts{Context: ctx},
nil,
[]common.Address{remoteToken.Addr}, // Only get logs where the terminal token matches the correct remote token
)
if err != nil {
return fmt.Errorf("failed to instantiate peer outbox iterator for peer in request for %s: %v", logDescription, err)
}
leavesToInsert := make([]DBLeaf, 0)
defer outboxIterator.Close()
for outboxIterator.Next() {
// TODO: Remove this once we know it's working
log.Printf("Leaf index: %s; project token amount: %s; terminal token amount: %s",
outboxIterator.Event.Index.String(), outboxIterator.Event.ProjectTokenAmount.String(), outboxIterator.Event.TerminalTokenAmount.String())
// Keep skipping until we pass the latest hash
if !seenLatestDbLeaf {
if bytes.Equal(outboxIterator.Event.Hashed[:], latestHashBytes) {
seenLatestDbLeaf = true
}
continue
}
// Add the remaining leaves to the list to insert
// Leaves are associated with their inbox tree
leavesToInsert = append(leavesToInsert, DBLeaf{
ChainId: inboxTree.ChainId.String(),
ContractAddress: inboxTree.SuckerAddress.String(),
TokenAddress: inboxTree.TokenAddress.String(),
Index: outboxIterator.Event.Index.String(),
Beneficiary: outboxIterator.Event.Beneficiary.String(),
ProjectTokenAmount: outboxIterator.Event.ProjectTokenAmount.String(),
TerminalTokenAmount: outboxIterator.Event.TerminalTokenAmount.String(),
LeafHash: hex.EncodeToString(outboxIterator.Event.Hashed[:]),
IsClaimed: false,
})
// If we've gotten to the latest inbox root, break
if inboxTree.Root == outboxIterator.Event.Root {
break
}
}
if err := outboxIterator.Error(); err != nil {
return fmt.Errorf("failed while iterating through outbox insertions for %s: %v", logDescription, err)
}
if len(leavesToInsert) == 0 {
log.Printf("found no leaves to insert for %s", logDescription)
return nil
}
// Start sqlite transaction to insert leaves
tx, err := db.Begin()
if err != nil {
return fmt.Errorf("failed to start sqlite transaction for %s: %v", logDescription, err)
}
stmt, err := tx.PrepareContext(ctx, `INSERT INTO leaves
(chain_id, contract_address, token_address, leaf_index, leaf_beneficiary,
leaf_project_token_amount, leaf_terminal_token_amount, leaf_hash, is_claimed)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`)
if err != nil {
return fmt.Errorf("failed to prepare sqlite statement: %v", err)
}
defer stmt.Close()
// Insert the leaves
for _, leaf := range leavesToInsert {
_, err := stmt.ExecContext(ctx, leaf.ChainId, leaf.ContractAddress, leaf.TokenAddress, leaf.Index, leaf.Beneficiary,
leaf.ProjectTokenAmount, leaf.TerminalTokenAmount, leaf.LeafHash, leaf.IsClaimed)
if err != nil {
tx.Rollback() // Rollback the transaction if an error occurs
return fmt.Errorf("failed to insert leaf into sqlite for %s: %v", logDescription, err)
}
}
// Update the inbox tree root
if _, err = tx.ExecContext(ctx, `INSERT OR REPLACE INTO trees(chain_id, contract_address, token_address, current_root, block_claims_last_updated)
VALUES (?, ?, ?, ?, ?)`,
inboxTree.ChainId.String(), inboxTree.SuckerAddress.String(), inboxTree.TokenAddress.String(), hex.EncodeToString(inboxTree.Root[:]), "0"); err != nil {
tx.Rollback()
return fmt.Errorf("failed to update inbox tree root for %s: %v", logDescription, err)
}
// Commit the transaction
if err := tx.Commit(); err != nil {
return fmt.Errorf("failed to commit sqlite transaction for %s: %v", logDescription, err)
}
return nil
}
// Updates the database with the latest claims for a specific inbox tree.
func updateClaims(ctx context.Context, inboxTree InboxTree) error {
logDescription := fmt.Sprintf("inbox %s of sucker %s on chain %s",
inboxTree.TokenAddress, inboxTree.SuckerAddress, inboxTree.ChainId)
// Read the latest block that we've checked for claims in from the db.
row := db.QueryRowContext(ctx, `SELECT block_claims_last_updated FROM trees
WHERE chain_id = ? AND contract_address = ? AND token_address = ?`,
inboxTree.ChainId.String(), inboxTree.SuckerAddress.String(), inboxTree.TokenAddress.String())
var lastUpdatedBlock string
err := row.Scan(&lastUpdatedBlock)
if err != nil && err != sql.ErrNoRows {
return fmt.Errorf("failed to query block_claims_last_updated from database for %s: %v", logDescription, err)
}
if err == sql.ErrNoRows {
lastUpdatedBlock = "0"
}
startBlock, err := strconv.ParseUint(lastUpdatedBlock, 10, 64)
if err != nil {
return fmt.Errorf("failed to parse last updated block %s for %s: %v", lastUpdatedBlock, logDescription, err)
}
// Get the sucker and an iterator for Claimed events.
client := clients[inboxTree.ChainId.String()]
sucker, err := NewBPSucker(inboxTree.SuckerAddress, client)
if err != nil {
return fmt.Errorf("failed to instantiate local sucker contract for %s: %v", logDescription, err)
}
claimIterator, err := sucker.FilterClaimed(&bind.FilterOpts{Context: ctx, Start: startBlock})
if err != nil {
return fmt.Errorf("failed to instantiate Claimed iterator for %s: %v", logDescription, err)
}
defer claimIterator.Close()
// Iterate through the claimed events and update the leaves in the db accordingly
tx, err := db.Begin()
if err != nil {
return fmt.Errorf("failed to start sqlite transaction for %s: %v", logDescription, err)
}
for claimIterator.Next() {
// If this event is for a different inbox tree, skip it
if claimIterator.Event.Token.Cmp(inboxTree.TokenAddress) != 0 {
continue
}
_, err := tx.ExecContext(ctx, `UPDATE leaves SET is_claimed = 1
WHERE chain_id = ? AND contract_address = ? AND token_address = ? AND leaf_index = ?`,
inboxTree.ChainId.String(), inboxTree.SuckerAddress.String(), inboxTree.TokenAddress.String(), claimIterator.Event.Index.String())
if err != nil {
tx.Rollback()
return fmt.Errorf("failed while executing update sqlite transaction for %s: %v", logDescription, err)
}
}
if err := claimIterator.Error(); err != nil {
return fmt.Errorf("failed while iterating through claimed events for %s: %v", logDescription, err)
}
// Update the last block that we've checked for claims in
tx.ExecContext(ctx, `UPDATE trees SET block_claims_last_updated = ?
WHERE chain_id = ? AND contract_address = ? AND token_address = ?`,
strconv.FormatUint(claimIterator.Event.Raw.BlockNumber, 10),
inboxTree.ChainId.String(), inboxTree.SuckerAddress.String(), inboxTree.TokenAddress.String())
if err != nil {
tx.Rollback()
return fmt.Errorf("failed while updating block_claims_last_updated for %s: %v", logDescription, err)
}
// Commit the transaction
if err := tx.Commit(); err != nil {
return fmt.Errorf("failed to commit sqlite transaction for %s: %v", logDescription, err)
}
return nil
}
// Convert a proof from slices to arrays, and make sure it's the right length.
func proofSliceToArray(input [][]byte) ([32][32]byte, error) {
var output [32][32]byte
if len(input) != 32 {
return output, fmt.Errorf("input does not have exactly 32 elements")
}
for i, slice := range input {
if len(slice) != 32 {
return output, fmt.Errorf("slice %d is not exactly 32 bytes long", i)
}
copy(output[i][:], slice)
}
return output, nil
}