This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdeposits.go
161 lines (127 loc) · 3.93 KB
/
deposits.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
package commander
import (
"context"
"github.com/Worldcoin/hubble-commander/contracts/depositmanager"
"github.com/Worldcoin/hubble-commander/eth"
"github.com/Worldcoin/hubble-commander/metrics"
"github.com/Worldcoin/hubble-commander/models"
st "github.com/Worldcoin/hubble-commander/storage"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/prometheus/client_golang/prometheus"
)
func (c *Commander) syncDeposits(ctx context.Context, start, end uint64) error {
var depositSubtrees []models.PendingDepositSubtree
_, span := newBlockTracer.Start(ctx, "syncDeposits")
defer span.End()
duration, err := metrics.MeasureDuration(func() error {
var err error
err = c.syncQueuedDeposits(start, end)
if err != nil {
return err
}
depositSubtrees, err = c.fetchDepositSubtrees(start, end)
if err != nil {
return err
}
if len(depositSubtrees) > 0 {
return c.saveSyncedSubtrees(depositSubtrees)
}
return nil
})
if err != nil {
return err
}
metrics.SaveHistogramMeasurement(duration, c.metrics.SyncingMethodDuration, prometheus.Labels{
"method": metrics.SyncDepositsMethod,
})
return nil
}
func (c *Commander) syncQueuedDeposits(start, end uint64) error {
it, err := c.getDepositQueuedIterator(start, end)
if err != nil {
return err
}
defer func() { _ = it.Close() }()
for it.Next() {
deposit := models.PendingDeposit{
ID: models.DepositID{
SubtreeID: models.MakeUint256FromBig(*it.Event.SubtreeID),
DepositIndex: models.MakeUint256FromBig(*it.Event.DepositID),
},
ToPubKeyID: uint32(it.Event.PubkeyID.Uint64()),
TokenID: models.MakeUint256FromBig(*it.Event.TokenID),
L2Amount: models.MakeUint256FromBig(*it.Event.L2Amount),
}
err = c.storage.AddPendingDeposit(&deposit)
if err != nil {
return err
}
}
return it.Error()
}
func (c *Commander) fetchDepositSubtrees(start, end uint64) ([]models.PendingDepositSubtree, error) {
it, err := c.getDepositSubtreeReadyIterator(start, end)
if err != nil {
return nil, err
}
defer func() { _ = it.Close() }()
depositSubtrees := make([]models.PendingDepositSubtree, 0, 1)
for it.Next() {
subtree := models.PendingDepositSubtree{
ID: models.MakeUint256FromBig(*it.Event.SubtreeID),
Root: it.Event.SubtreeRoot,
}
depositSubtrees = append(depositSubtrees, subtree)
}
return depositSubtrees, it.Error()
}
func (c *Commander) getDepositQueuedIterator(start, end uint64) (*depositmanager.DepositQueuedIterator, error) {
it := &depositmanager.DepositQueuedIterator{}
err := c.client.FilterLogs(c.client.DepositManager.BoundContract, eth.DepositQueuedEvent, &bind.FilterOpts{
Start: start,
End: &end,
}, it)
if err != nil {
return nil, err
}
return it, nil
}
func (c *Commander) getDepositSubtreeReadyIterator(start, end uint64) (*depositmanager.DepositSubTreeReadyIterator, error) {
it := &depositmanager.DepositSubTreeReadyIterator{}
err := c.client.FilterLogs(c.client.DepositManager.BoundContract, eth.DepositSubTreeReadyEvent, &bind.FilterOpts{
Start: start,
End: &end,
}, it)
if err != nil {
return nil, err
}
return it, nil
}
func (c *Commander) saveSyncedSubtrees(subtrees []models.PendingDepositSubtree) error {
maxDepositSubtreeDepth, err := c.client.GetMaxSubtreeDepthParam()
if err != nil {
return err
}
subtreeLeavesAmount := 1 << *maxDepositSubtreeDepth
for i := range subtrees {
err := c.saveSingleSubtree(&subtrees[i], subtreeLeavesAmount)
if err != nil {
return err
}
}
return nil
}
func (c *Commander) saveSingleSubtree(subtree *models.PendingDepositSubtree, subtreeLeavesAmount int) error {
return c.storage.ExecuteInTransaction(st.TxOptions{}, func(txStorage *st.Storage) error {
deposits, err := txStorage.GetFirstPendingDeposits(subtreeLeavesAmount)
if err != nil {
return err
}
subtree.Deposits = deposits
err = txStorage.AddPendingDepositSubtree(subtree)
if err != nil {
return err
}
return txStorage.RemovePendingDeposits(deposits)
})
}