Skip to content

Commit f1bd75c

Browse files
committed
feat(coordinator): abstract proof types behind an interface
1 parent c591328 commit f1bd75c

File tree

18 files changed

+169
-131
lines changed

18 files changed

+169
-131
lines changed

common/types/message/message.go

Lines changed: 77 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@ import (
77
"github.com/scroll-tech/go-ethereum/common"
88
)
99

10-
// RespStatus represents status code from prover to scroll
11-
type RespStatus uint32
12-
13-
const (
14-
// StatusOk means generate proof success
15-
StatusOk RespStatus = iota
16-
// StatusProofError means generate proof failed
17-
StatusProofError
18-
)
19-
2010
// ProofType represents the type of task.
2111
type ProofType uint8
2212

@@ -51,15 +41,15 @@ type ChunkTaskDetail struct {
5141

5242
// BatchTaskDetail is a type containing BatchTask detail.
5343
type BatchTaskDetail struct {
54-
ChunkInfos []*ChunkInfo `json:"chunk_infos"`
55-
ChunkProofs []*ChunkProof `json:"chunk_proofs"`
56-
BatchHeader interface{} `json:"batch_header"`
57-
BlobBytes []byte `json:"blob_bytes"`
44+
ChunkInfos []*ChunkInfo `json:"chunk_infos"`
45+
ChunkProofs []ChunkProof `json:"chunk_proofs"`
46+
BatchHeader interface{} `json:"batch_header"`
47+
BlobBytes []byte `json:"blob_bytes"`
5848
}
5949

6050
// BundleTaskDetail consists of all the information required to describe the task to generate a proof for a bundle of batches.
6151
type BundleTaskDetail struct {
62-
BatchProofs []*BatchProof `json:"batch_proofs"`
52+
BatchProofs []BatchProof `json:"batch_proofs"`
6353
}
6454

6555
// ChunkInfo is for calculating pi_hash for chunk
@@ -79,11 +69,24 @@ type SubCircuitRowUsage struct {
7969
RowNumber uint64 `json:"row_number"`
8070
}
8171

82-
// ChunkProof includes the proof info that are required for chunk verification and rollup.
83-
type ChunkProof struct {
72+
// ChunkProof
73+
type ChunkProof interface {
74+
Proof() []byte
75+
}
76+
77+
// NewChunkProof creates a new ChunkProof instance.
78+
func NewChunkProof(hardForkName string) ChunkProof {
79+
switch hardForkName {
80+
default:
81+
return &Halo2ChunkProof{}
82+
}
83+
}
84+
85+
// Halo2ChunkProof includes the proof info that are required for chunk verification and rollup.
86+
type Halo2ChunkProof struct {
8487
StorageTrace []byte `json:"storage_trace,omitempty"`
8588
Protocol []byte `json:"protocol"`
86-
Proof []byte `json:"proof"`
89+
RawProof []byte `json:"proof"`
8790
Instances []byte `json:"instances"`
8891
Vk []byte `json:"vk"`
8992
// cross-reference between cooridinator computation and prover compution
@@ -92,29 +95,53 @@ type ChunkProof struct {
9295
RowUsages []SubCircuitRowUsage `json:"row_usages,omitempty"`
9396
}
9497

95-
// BatchProof includes the proof info that are required for batch verification and rollup.
96-
type BatchProof struct {
98+
// Proof returns the proof bytes of a ChunkProof
99+
func (ap *Halo2ChunkProof) Proof() []byte {
100+
return ap.RawProof
101+
}
102+
103+
// BatchProof
104+
type BatchProof interface {
105+
SanityCheck() error
106+
Proof() []byte
107+
}
108+
109+
// NewBatchProof creates a new BatchProof instance.
110+
func NewBatchProof(hardForkName string) BatchProof {
111+
switch hardForkName {
112+
default:
113+
return &Halo2BatchProof{}
114+
}
115+
}
116+
117+
// Halo2BatchProof includes the proof info that are required for batch verification and rollup.
118+
type Halo2BatchProof struct {
97119
Protocol []byte `json:"protocol"`
98-
Proof []byte `json:"proof"`
120+
RawProof []byte `json:"proof"`
99121
Instances []byte `json:"instances"`
100122
Vk []byte `json:"vk"`
101123
// cross-reference between cooridinator computation and prover compution
102124
BatchHash common.Hash `json:"batch_hash"`
103125
GitVersion string `json:"git_version,omitempty"`
104126
}
105127

128+
// Proof returns the proof bytes of a BatchProof
129+
func (ap *Halo2BatchProof) Proof() []byte {
130+
return ap.RawProof
131+
}
132+
106133
// SanityCheck checks whether a BatchProof is in a legal format
107-
func (ap *BatchProof) SanityCheck() error {
134+
func (ap *Halo2BatchProof) SanityCheck() error {
108135
if ap == nil {
109136
return errors.New("agg_proof is nil")
110137
}
111138

112-
if len(ap.Proof) == 0 {
139+
if len(ap.RawProof) == 0 {
113140
return errors.New("proof not ready")
114141
}
115142

116-
if len(ap.Proof)%32 != 0 {
117-
return fmt.Errorf("proof buffer length must be a multiple of 32, got: %d", len(ap.Proof))
143+
if len(ap.RawProof)%32 != 0 {
144+
return fmt.Errorf("proof buffer length must be a multiple of 32, got: %d", len(ap.RawProof))
118145
}
119146

120147
if len(ap.Instances) == 0 {
@@ -128,27 +155,46 @@ func (ap *BatchProof) SanityCheck() error {
128155
return nil
129156
}
130157

158+
// BundleProof
159+
type BundleProof interface {
160+
SanityCheck() error
161+
Proof() []byte
162+
}
163+
164+
// NewBundleProof creates a new BundleProof instance.
165+
func NewBundleProof(hardForkName string) BundleProof {
166+
switch hardForkName {
167+
default:
168+
return &Halo2BundleProof{}
169+
}
170+
}
171+
131172
// BundleProof includes the proof info that are required for verification of a bundle of batch proofs.
132-
type BundleProof struct {
133-
Proof []byte `json:"proof"`
173+
type Halo2BundleProof struct {
174+
RawProof []byte `json:"proof"`
134175
Instances []byte `json:"instances"`
135176
Vk []byte `json:"vk"`
136177
// cross-reference between cooridinator computation and prover compution
137178
GitVersion string `json:"git_version,omitempty"`
138179
}
139180

181+
// Proof returns the proof bytes of a BundleProof
182+
func (ap *Halo2BundleProof) Proof() []byte {
183+
return ap.RawProof
184+
}
185+
140186
// SanityCheck checks whether a BundleProof is in a legal format
141-
func (ap *BundleProof) SanityCheck() error {
187+
func (ap *Halo2BundleProof) SanityCheck() error {
142188
if ap == nil {
143189
return errors.New("agg_proof is nil")
144190
}
145191

146-
if len(ap.Proof) == 0 {
192+
if len(ap.RawProof) == 0 {
147193
return errors.New("proof not ready")
148194
}
149195

150-
if len(ap.Proof)%32 != 0 {
151-
return fmt.Errorf("proof buffer length must be a multiple of 32, got: %d", len(ap.Proof))
196+
if len(ap.RawProof)%32 != 0 {
197+
return fmt.Errorf("proof buffer length must be a multiple of 32, got: %d", len(ap.RawProof))
152198
}
153199

154200
if len(ap.Instances) == 0 {

coordinator/cmd/tool/tool.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ func action(ctx *cli.Context) error {
6262
return fmt.Errorf("failed to get batch proofs for bundle task id:%s, no batch found", taskID)
6363
}
6464

65-
var batchProofs []*message.BatchProof
65+
var batchProofs []message.BatchProof
6666
for _, batch := range batches {
67-
var proof message.BatchProof
67+
var proof message.BatchProof // todo: NewBatchProof
6868
if encodeErr := json.Unmarshal(batch.Proof, &proof); encodeErr != nil {
6969
log.Error("failed to unmarshal batch proof")
7070
return fmt.Errorf("failed to unmarshal proof: %w, bundle hash: %v, batch hash: %v", encodeErr, taskID, batch.Hash)
7171
}
72-
batchProofs = append(batchProofs, &proof)
72+
batchProofs = append(batchProofs, proof)
7373
}
7474

7575
taskDetail := message.BundleTaskDetail{

coordinator/internal/logic/provertask/batch_prover_task.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,14 @@ func (bp *BatchProverTask) formatProverTask(ctx context.Context, task *orm.Prove
215215
return nil, fmt.Errorf("no chunk found for batch task id:%s", task.TaskID)
216216
}
217217

218-
var chunkProofs []*message.ChunkProof
218+
var chunkProofs []message.ChunkProof
219219
var chunkInfos []*message.ChunkInfo
220220
for _, chunk := range chunks {
221-
var proof message.ChunkProof
221+
proof := message.NewChunkProof(hardForkName)
222222
if encodeErr := json.Unmarshal(chunk.Proof, &proof); encodeErr != nil {
223223
return nil, fmt.Errorf("Chunk.GetProofsByBatchHash unmarshal proof error: %w, batch hash: %v, chunk hash: %v", encodeErr, task.TaskID, chunk.Hash)
224224
}
225-
chunkProofs = append(chunkProofs, &proof)
225+
chunkProofs = append(chunkProofs, proof)
226226

227227
chunkInfo := message.ChunkInfo{
228228
ChainID: bp.cfg.L2.ChainID,
@@ -232,8 +232,10 @@ func (bp *BatchProverTask) formatProverTask(ctx context.Context, task *orm.Prove
232232
DataHash: common.HexToHash(chunk.Hash),
233233
IsPadding: false,
234234
}
235-
if proof.ChunkInfo != nil {
236-
chunkInfo.TxBytes = proof.ChunkInfo.TxBytes
235+
if haloProot, ok := proof.(*message.Halo2ChunkProof); ok {
236+
if haloProot.ChunkInfo != nil {
237+
chunkInfo.TxBytes = haloProot.ChunkInfo.TxBytes
238+
}
237239
}
238240
chunkInfos = append(chunkInfos, &chunkInfo)
239241
}
@@ -264,7 +266,7 @@ func (bp *BatchProverTask) recoverActiveAttempts(ctx *gin.Context, batchTask *or
264266
}
265267
}
266268

267-
func (bp *BatchProverTask) getBatchTaskDetail(dbBatch *orm.Batch, chunkInfos []*message.ChunkInfo, chunkProofs []*message.ChunkProof) (*message.BatchTaskDetail, error) {
269+
func (bp *BatchProverTask) getBatchTaskDetail(dbBatch *orm.Batch, chunkInfos []*message.ChunkInfo, chunkProofs []message.ChunkProof) (*message.BatchTaskDetail, error) {
268270
taskDetail := &message.BatchTaskDetail{
269271
ChunkInfos: chunkInfos,
270272
ChunkProofs: chunkProofs,

coordinator/internal/logic/provertask/bundle_prover_task.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,13 @@ func (bp *BundleProverTask) formatProverTask(ctx context.Context, task *orm.Prov
221221
return nil, fmt.Errorf("failed to get batch proofs for bundle task id:%s, no batch found", task.TaskID)
222222
}
223223

224-
var batchProofs []*message.BatchProof
224+
var batchProofs []message.BatchProof
225225
for _, batch := range batches {
226-
var proof message.BatchProof
226+
proof := message.NewBatchProof(hardForkName)
227227
if encodeErr := json.Unmarshal(batch.Proof, &proof); encodeErr != nil {
228228
return nil, fmt.Errorf("failed to unmarshal proof: %w, bundle hash: %v, batch hash: %v", encodeErr, task.TaskID, batch.Hash)
229229
}
230-
batchProofs = append(batchProofs, &proof)
230+
batchProofs = append(batchProofs, proof)
231231
}
232232

233233
taskDetail := message.BundleTaskDetail{

coordinator/internal/logic/submitproof/proof_receiver.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,23 +171,23 @@ func (m *ProofReceiverLogic) HandleZkProof(ctx *gin.Context, proofParameter coor
171171

172172
switch message.ProofType(proofParameter.TaskType) {
173173
case message.ProofTypeChunk:
174-
var chunkProof message.ChunkProof
174+
chunkProof := message.NewChunkProof(hardForkName)
175175
if unmarshalErr := json.Unmarshal([]byte(proofParameter.Proof), &chunkProof); unmarshalErr != nil {
176176
return unmarshalErr
177177
}
178-
success, verifyErr = m.verifier.VerifyChunkProof(&chunkProof, hardForkName)
178+
success, verifyErr = m.verifier.VerifyChunkProof(chunkProof, hardForkName)
179179
case message.ProofTypeBatch:
180-
var batchProof message.BatchProof
180+
batchProof := message.NewBatchProof(hardForkName)
181181
if unmarshalErr := json.Unmarshal([]byte(proofParameter.Proof), &batchProof); unmarshalErr != nil {
182182
return unmarshalErr
183183
}
184-
success, verifyErr = m.verifier.VerifyBatchProof(&batchProof, hardForkName)
184+
success, verifyErr = m.verifier.VerifyBatchProof(batchProof, hardForkName)
185185
case message.ProofTypeBundle:
186-
var bundleProof message.BundleProof
186+
bundleProof := message.NewBundleProof(hardForkName)
187187
if unmarshalErr := json.Unmarshal([]byte(proofParameter.Proof), &bundleProof); unmarshalErr != nil {
188188
return unmarshalErr
189189
}
190-
success, verifyErr = m.verifier.VerifyBundleProof(&bundleProof, hardForkName)
190+
success, verifyErr = m.verifier.VerifyBundleProof(bundleProof, hardForkName)
191191
}
192192

193193
if verifyErr != nil || !success {
@@ -265,7 +265,7 @@ func (m *ProofReceiverLogic) validator(ctx context.Context, proverTask *orm.Prov
265265
proofTime := time.Since(proverTask.CreatedAt)
266266
proofTimeSec := uint64(proofTime.Seconds())
267267

268-
if proofParameter.Status != int(message.StatusOk) {
268+
if proofParameter.Status != int(coordinatorType.StatusOk) {
269269
// Temporarily replace "panic" with "pa-nic" to prevent triggering the alert based on logs.
270270
failureMsg := strings.Replace(proofParameter.FailureMsg, "panic", "pa-nic", -1)
271271

coordinator/internal/logic/verifier/mock.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@ func NewVerifier(cfg *config.VerifierConfig) (*Verifier, error) {
1616
}
1717

1818
// VerifyChunkProof return a mock verification result for a ChunkProof.
19-
func (v *Verifier) VerifyChunkProof(proof *message.ChunkProof, forkName string) (bool, error) {
20-
if string(proof.Proof) == InvalidTestProof {
19+
func (v *Verifier) VerifyChunkProof(proof message.ChunkProof, forkName string) (bool, error) {
20+
if string(proof.Proof()) == InvalidTestProof {
2121
return false, nil
2222
}
2323
return true, nil
2424
}
2525

2626
// VerifyBatchProof return a mock verification result for a BatchProof.
27-
func (v *Verifier) VerifyBatchProof(proof *message.BatchProof, forkName string) (bool, error) {
28-
if string(proof.Proof) == InvalidTestProof {
27+
func (v *Verifier) VerifyBatchProof(proof message.BatchProof, forkName string) (bool, error) {
28+
if string(proof.Proof()) == InvalidTestProof {
2929
return false, nil
3030
}
3131
return true, nil
3232
}
3333

3434
// VerifyBundleProof return a mock verification result for a BundleProof.
35-
func (v *Verifier) VerifyBundleProof(proof *message.BundleProof, forkName string) (bool, error) {
36-
if string(proof.Proof) == InvalidTestProof {
35+
func (v *Verifier) VerifyBundleProof(proof message.BundleProof, forkName string) (bool, error) {
36+
if string(proof.Proof()) == InvalidTestProof {
3737
return false, nil
3838
}
3939
return true, nil

coordinator/internal/logic/verifier/verifier.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ func NewVerifier(cfg *config.VerifierConfig) (*Verifier, error) {
110110
}
111111

112112
// VerifyBatchProof Verify a ZkProof by marshaling it and sending it to the Halo2 Verifier.
113-
func (v *Verifier) VerifyBatchProof(proof *message.BatchProof, forkName string) (bool, error) {
113+
func (v *Verifier) VerifyBatchProof(proof message.BatchProof, forkName string) (bool, error) {
114114
if v.cfg.MockMode {
115115
log.Info("Mock mode, batch verifier disabled")
116-
if string(proof.Proof) == InvalidTestProof {
116+
if string(proof.Proof()) == InvalidTestProof {
117117
return false, nil
118118
}
119119
return true, nil
@@ -137,10 +137,10 @@ func (v *Verifier) VerifyBatchProof(proof *message.BatchProof, forkName string)
137137
}
138138

139139
// VerifyChunkProof Verify a ZkProof by marshaling it and sending it to the Halo2 Verifier.
140-
func (v *Verifier) VerifyChunkProof(proof *message.ChunkProof, forkName string) (bool, error) {
140+
func (v *Verifier) VerifyChunkProof(proof message.ChunkProof, forkName string) (bool, error) {
141141
if v.cfg.MockMode {
142142
log.Info("Mock mode, verifier disabled")
143-
if string(proof.Proof) == InvalidTestProof {
143+
if string(proof.Proof()) == InvalidTestProof {
144144
return false, nil
145145
}
146146
return true, nil
@@ -164,10 +164,10 @@ func (v *Verifier) VerifyChunkProof(proof *message.ChunkProof, forkName string)
164164
}
165165

166166
// VerifyBundleProof Verify a ZkProof for a bundle of batches, by marshaling it and verifying it via the EVM verifier.
167-
func (v *Verifier) VerifyBundleProof(proof *message.BundleProof, forkName string) (bool, error) {
167+
func (v *Verifier) VerifyBundleProof(proof message.BundleProof, forkName string) (bool, error) {
168168
if v.cfg.MockMode {
169169
log.Info("Mock mode, verifier disabled")
170-
if string(proof.Proof) == InvalidTestProof {
170+
if string(proof.Proof()) == InvalidTestProof {
171171
return false, nil
172172
}
173173
return true, nil

0 commit comments

Comments
 (0)