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 3
/
batch.go
191 lines (170 loc) · 5.08 KB
/
batch.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
package storage
import (
"github.com/Worldcoin/hubble-commander/db"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/stored"
bdg "github.com/dgraph-io/badger/v3"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
bh "github.com/timshannon/badgerhold/v4"
)
type BatchStorage struct {
database *Database
}
func NewBatchStorage(database *Database) (*BatchStorage, error) {
return &BatchStorage{
database: database,
}, nil
}
func (s *BatchStorage) copyWithNewDatabase(database *Database) *BatchStorage {
newBatchStorage := *s
newBatchStorage.database = database
return &newBatchStorage
}
func (s *BatchStorage) AddBatch(batch *models.Batch) error {
return s.database.Badger.Insert(batch.ID, *stored.NewBatchFromModelsBatch(batch))
}
func (s *BatchStorage) GetBatch(batchID models.Uint256) (*models.Batch, error) {
var storedBatch stored.Batch
err := s.database.Badger.Get(batchID, &storedBatch)
if errors.Is(err, bh.ErrNotFound) {
return nil, errors.WithStack(NewNotFoundError("batch"))
}
if err != nil {
return nil, err
}
return storedBatch.ToModelsBatch(), nil
}
func (s *BatchStorage) UpdateBatch(batch *models.Batch) error {
err := s.database.Badger.Update(batch.ID, *stored.NewBatchFromModelsBatch(batch))
if errors.Is(err, bh.ErrNotFound) {
return errors.WithStack(NewNotFoundError("batch"))
}
return err
}
func (s *BatchStorage) GetMinedBatch(batchID models.Uint256) (*models.Batch, error) {
batch, err := s.GetBatch(batchID)
if err != nil {
return nil, err
}
if batch.Hash == nil {
return nil, errors.WithStack(NewNotFoundError("batch"))
}
return batch, nil
}
func (s *BatchStorage) GetBatchByHash(batchHash common.Hash) (*models.Batch, error) {
var batch stored.Batch
err := s.database.Badger.FindOneUsingIndex(&batch, &batchHash, "Hash")
if errors.Is(err, bh.ErrNotFound) {
return nil, errors.WithStack(NewNotFoundError("batch"))
}
if err != nil {
return nil, err
}
return batch.ToModelsBatch(), nil
}
func (s *BatchStorage) GetPendingBatches() ([]models.Batch, error) {
var nilHash *common.Hash
storedBatches := make([]stored.Batch, 0)
err := s.database.Badger.Find(
&storedBatches,
bh.Where("Hash").Eq(nilHash).Index("Hash"),
)
if err != nil {
return nil, err
}
batches := make([]models.Batch, 0, len(storedBatches))
for i := range storedBatches {
batches = append(batches, *storedBatches[i].ToModelsBatch())
}
return batches, nil
}
func (s *BatchStorage) GetLatestSubmittedBatch() (*models.Batch, error) {
batch, err := s.reverseIterateBatches(func(batch *stored.Batch) bool {
return batch.Hash != nil
})
if err != nil {
return nil, err
}
return batch, nil
}
func (s *BatchStorage) GetNextBatchID() (*models.Uint256, error) {
batch, err := s.reverseIterateBatches(func(batch *stored.Batch) bool {
return true
})
if IsNotFoundError(err) {
return models.NewUint256(1), nil
}
if err != nil {
return nil, err
}
return batch.ID.AddN(1), nil
}
func (s *BatchStorage) GetLatestFinalisedBatch(currentBlockNumber uint32) (*models.Batch, error) {
batch, err := s.reverseIterateBatches(func(batch *stored.Batch) bool {
return batch.FinalisationBlock != nil && *batch.FinalisationBlock <= currentBlockNumber
})
if err != nil {
return nil, err
}
return batch, nil
}
func (s *BatchStorage) GetBatchesInRange(from, to *models.Uint256) ([]models.Batch, error) {
criteria := bh.Where(bh.Key)
var query *bh.Query
if from == nil && to == nil {
query = criteria.Ge(models.MakeUint256(0))
}
if from != nil && to != nil {
query = criteria.Ge(*from).And(bh.Key).Le(*to)
} else if from != nil {
query = criteria.Ge(*from)
} else if to != nil {
query = criteria.Le(*to)
}
storedBatches := make([]stored.Batch, 0, 32)
err := s.database.Badger.Find(&storedBatches, query)
if err != nil {
return nil, err
}
res := make([]models.Batch, 0, len(storedBatches))
for i := range storedBatches {
res = append(res, *storedBatches[i].ToModelsBatch())
}
return res, nil
}
// RemoveBatches uses for loop instead badgerhold.DeleteMatching because it's faster
func (s *BatchStorage) RemoveBatches(batchIDs ...models.Uint256) error {
return s.database.ExecuteInTransaction(TxOptions{}, func(txDatabase *Database) error {
storedBatch := stored.Batch{}
for i := range batchIDs {
err := txDatabase.Badger.Delete(batchIDs[i], storedBatch)
if errors.Is(err, bh.ErrNotFound) {
return errors.WithStack(NewNotFoundError("batch"))
}
if err != nil {
return err
}
}
return nil
})
}
func (s *BatchStorage) reverseIterateBatches(filter func(batch *stored.Batch) bool) (*models.Batch, error) {
var storedBatch stored.Batch
err := s.database.Badger.Iterator(stored.BatchPrefix, db.ReversePrefetchIteratorOpts, func(item *bdg.Item) (bool, error) {
err := item.Value(func(v []byte) error {
return db.Decode(v, &storedBatch)
})
if err != nil {
return false, err
}
return filter(&storedBatch), nil
})
if errors.Is(err, db.ErrIteratorFinished) {
return nil, errors.WithStack(NewNotFoundError("batch"))
}
if err != nil {
return nil, err
}
return storedBatch.ToModelsBatch(), nil
}