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 pathget_mass_migration_commitment_proof.go
106 lines (89 loc) · 3.59 KB
/
get_mass_migration_commitment_proof.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
package api
import (
"fmt"
"github.com/Worldcoin/hubble-commander/commander/executor"
"github.com/Worldcoin/hubble-commander/encoder"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/dto"
"github.com/Worldcoin/hubble-commander/models/enums/batchtype"
"github.com/Worldcoin/hubble-commander/storage"
"github.com/Worldcoin/hubble-commander/utils/merkletree"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
)
var (
ErrOnlyMassMigrationCommitmentsForProofing = fmt.Errorf(
"mass migration commitment inclusion proof cannot be generated for different type of commitments",
)
APIErrOnlyMassMigrationCommitmentsForProofing = NewAPIError(
50009,
"mass migration commitment inclusion proof cannot be generated for different type of commitments",
)
APIErrCannotGenerateMMCommitmentProof = NewAPIError(50004, "mass migration commitment inclusion proof could not be generated")
getMassMigrationCommitmentProofAPIErrors = map[error]*APIError{
storage.AnyNotFoundError: APIErrCannotGenerateMMCommitmentProof,
ErrOnlyMassMigrationCommitmentsForProofing: APIErrOnlyMassMigrationCommitmentsForProofing,
}
)
func (a *API) GetMassMigrationCommitmentProof(commitmentID models.CommitmentID) (*dto.MassMigrationCommitmentProof, error) {
if !a.cfg.EnableProofMethods {
return nil, APIErrProofMethodsDisabled
}
commitmentInclusionProof, err := a.unsafeGetMassMigrationCommitmentProof(commitmentID)
if err != nil {
return nil, sanitizeError(err, getMassMigrationCommitmentProofAPIErrors)
}
return commitmentInclusionProof, nil
}
func (a *API) unsafeGetMassMigrationCommitmentProof(commitmentID models.CommitmentID) (*dto.MassMigrationCommitmentProof, error) {
batch, err := a.storage.GetBatch(commitmentID.BatchID)
if err != nil {
return nil, errors.WithStack(err)
}
if batch.Type != batchtype.MassMigration {
return nil, errors.WithStack(ErrOnlyMassMigrationCommitmentsForProofing)
}
commitments, err := a.storage.GetCommitmentsByBatchID(commitmentID.BatchID)
if err != nil {
return nil, errors.WithStack(err)
}
unsortedTransactions, err := a.storage.GetTransactionsByCommitmentID(commitmentID)
if err != nil {
return nil, errors.WithStack(err)
}
// TODO: I believe this time is now
// TODO remove when new primary key for transactions with transaction index is implement
txQueue := executor.NewTxQueue(unsortedTransactions)
massMigrations := txQueue.PickTxsForCommitment().ToMassMigrationArray()
serializedMassMigrations, err := encoder.SerializeMassMigrations(massMigrations)
if err != nil {
return nil, errors.WithStack(err)
}
commitment := commitments[commitmentID.IndexInBatch].ToMMCommitment()
leafHashes := make([]common.Hash, 0, len(commitments))
for i := range commitments {
leafHashes = append(leafHashes, commitments[i].LeafHash())
}
batchLeafTree, err := merkletree.NewMerkleTree(leafHashes)
if err != nil {
return nil, errors.WithStack(err)
}
proofBase := &dto.CommitmentInclusionProofBase{
StateRoot: commitments[commitmentID.IndexInBatch].GetPostStateRoot(),
Path: &dto.MerklePath{
Path: uint32(commitmentID.IndexInBatch),
Depth: batchLeafTree.Depth(),
},
Witness: batchLeafTree.GetWitness(uint32(commitmentID.IndexInBatch)),
}
return &dto.MassMigrationCommitmentProof{
CommitmentInclusionProofBase: *proofBase,
Body: &dto.MassMigrationBody{
AccountRoot: *batch.AccountTreeRoot,
Signature: commitment.CombinedSignature,
Meta: dto.NewMassMigrationMeta(commitment.Meta),
WithdrawRoot: commitment.WithdrawRoot,
Transactions: serializedMassMigrations,
},
}, nil
}