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
/
commitment.go
91 lines (81 loc) · 2.91 KB
/
commitment.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
package storage
import (
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/enums/batchtype"
"github.com/Worldcoin/hubble-commander/models/stored"
"github.com/pkg/errors"
bh "github.com/timshannon/badgerhold/v4"
)
func (s *CommitmentStorage) AddCommitment(commitment models.Commitment) error {
switch commitment.GetCommitmentBase().Type {
case batchtype.Transfer, batchtype.Create2Transfer:
return s.addTxCommitment(commitment.ToTxCommitment())
case batchtype.MassMigration:
return s.addMMCommitment(commitment.ToMMCommitment())
case batchtype.Deposit:
return s.addDepositCommitment(commitment.ToDepositCommitment())
default:
panic("invalid commitment type")
}
}
func (s *CommitmentStorage) GetCommitment(id *models.CommitmentID) (models.Commitment, error) {
commitment, err := s.getStoredCommitment(id)
if err != nil {
return nil, err
}
switch commitment.Type {
case batchtype.Transfer, batchtype.Create2Transfer:
return commitment.ToTxCommitment(), nil
case batchtype.MassMigration:
return commitment.ToMMCommitment(), nil
case batchtype.Deposit:
return commitment.ToDepositCommitment(), nil
default:
panic("invalid commitment type")
}
}
func (s *CommitmentStorage) GetCommitmentsByBatchID(batchID models.Uint256) ([]models.Commitment, error) {
storedCommitments, err := s.getStoredCommitmentsByBatchID(batchID)
if err != nil {
return nil, err
}
commitments := make([]models.Commitment, 0, len(storedCommitments))
for i := range storedCommitments {
switch storedCommitments[i].Type {
case batchtype.Transfer, batchtype.Create2Transfer:
commitments = append(commitments, storedCommitments[i].ToTxCommitment())
case batchtype.MassMigration:
commitments = append(commitments, storedCommitments[i].ToMMCommitment())
case batchtype.Deposit:
commitments = append(commitments, storedCommitments[i].ToDepositCommitment())
default:
panic("invalid commitment type")
}
}
return commitments, nil
}
func (s *CommitmentStorage) UpdateCommitments(commitments []models.Commitment) error {
return s.database.ExecuteInTransaction(TxOptions{}, func(txDatabase *Database) error {
for i := range commitments {
var commitment stored.Commitment
switch commitments[i].GetCommitmentBase().Type {
case batchtype.Transfer, batchtype.Create2Transfer:
commitment = stored.MakeCommitmentFromTxCommitment(commitments[i].ToTxCommitment())
case batchtype.MassMigration:
commitment = stored.MakeCommitmentFromMMCommitment(commitments[i].ToMMCommitment())
case batchtype.Deposit:
commitment = stored.MakeCommitmentFromDepositCommitment(commitments[i].ToDepositCommitment())
default:
panic("invalid commitment type")
}
err := txDatabase.Badger.Update(commitments[i].GetCommitmentBase().ID, commitment)
if errors.Is(err, bh.ErrNotFound) {
return NewNotFoundError("commitment")
}
if err != nil {
return err
}
}
return nil
})
}