Skip to content

Commit f8d9fd8

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

File tree

18 files changed

+176
-128
lines changed

18 files changed

+176
-128
lines changed

common/types/message/message.go

Lines changed: 84 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,8 @@ 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-
1310
const (
14-
// StatusOk means generate proof success
15-
StatusOk RespStatus = iota
16-
// StatusProofError means generate proof failed
17-
StatusProofError
11+
darwinV2Fork = "darwinV2"
1812
)
1913

2014
// ProofType represents the type of task.
@@ -51,15 +45,15 @@ type ChunkTaskDetail struct {
5145

5246
// BatchTaskDetail is a type containing BatchTask detail.
5347
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"`
48+
ChunkInfos []*ChunkInfo `json:"chunk_infos"`
49+
ChunkProofs []ChunkProof `json:"chunk_proofs"`
50+
BatchHeader interface{} `json:"batch_header"`
51+
BlobBytes []byte `json:"blob_bytes"`
5852
}
5953

6054
// BundleTaskDetail consists of all the information required to describe the task to generate a proof for a bundle of batches.
6155
type BundleTaskDetail struct {
62-
BatchProofs []*BatchProof `json:"batch_proofs"`
56+
BatchProofs []BatchProof `json:"batch_proofs"`
6357
}
6458

6559
// ChunkInfo is for calculating pi_hash for chunk
@@ -79,11 +73,26 @@ type SubCircuitRowUsage struct {
7973
RowNumber uint64 `json:"row_number"`
8074
}
8175

82-
// ChunkProof includes the proof info that are required for chunk verification and rollup.
83-
type ChunkProof struct {
76+
// ChunkProof
77+
type ChunkProof interface {
78+
Proof() []byte
79+
}
80+
81+
// NewChunkProof creates a new ChunkProof instance.
82+
func NewChunkProof(hardForkName string) ChunkProof {
83+
switch hardForkName {
84+
case darwinV2Fork:
85+
return &Halo2ChunkProof{}
86+
default:
87+
return nil
88+
}
89+
}
90+
91+
// Halo2ChunkProof includes the proof info that are required for chunk verification and rollup.
92+
type Halo2ChunkProof struct {
8493
StorageTrace []byte `json:"storage_trace,omitempty"`
8594
Protocol []byte `json:"protocol"`
86-
Proof []byte `json:"proof"`
95+
RawProof []byte `json:"proof"`
8796
Instances []byte `json:"instances"`
8897
Vk []byte `json:"vk"`
8998
// cross-reference between cooridinator computation and prover compution
@@ -92,29 +101,55 @@ type ChunkProof struct {
92101
RowUsages []SubCircuitRowUsage `json:"row_usages,omitempty"`
93102
}
94103

95-
// BatchProof includes the proof info that are required for batch verification and rollup.
96-
type BatchProof struct {
104+
// Proof returns the proof bytes of a ChunkProof
105+
func (ap *Halo2ChunkProof) Proof() []byte {
106+
return ap.RawProof
107+
}
108+
109+
// BatchProof
110+
type BatchProof interface {
111+
SanityCheck() error
112+
Proof() []byte
113+
}
114+
115+
// NewBatchProof creates a new BatchProof instance.
116+
func NewBatchProof(hardForkName string) BatchProof {
117+
switch hardForkName {
118+
case darwinV2Fork:
119+
return &Halo2BatchProof{}
120+
default:
121+
return nil
122+
}
123+
}
124+
125+
// Halo2BatchProof includes the proof info that are required for batch verification and rollup.
126+
type Halo2BatchProof struct {
97127
Protocol []byte `json:"protocol"`
98-
Proof []byte `json:"proof"`
128+
RawProof []byte `json:"proof"`
99129
Instances []byte `json:"instances"`
100130
Vk []byte `json:"vk"`
101131
// cross-reference between cooridinator computation and prover compution
102132
BatchHash common.Hash `json:"batch_hash"`
103133
GitVersion string `json:"git_version,omitempty"`
104134
}
105135

136+
// Proof returns the proof bytes of a BatchProof
137+
func (ap *Halo2BatchProof) Proof() []byte {
138+
return ap.RawProof
139+
}
140+
106141
// SanityCheck checks whether a BatchProof is in a legal format
107-
func (ap *BatchProof) SanityCheck() error {
142+
func (ap *Halo2BatchProof) SanityCheck() error {
108143
if ap == nil {
109144
return errors.New("agg_proof is nil")
110145
}
111146

112-
if len(ap.Proof) == 0 {
147+
if len(ap.RawProof) == 0 {
113148
return errors.New("proof not ready")
114149
}
115150

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))
151+
if len(ap.RawProof)%32 != 0 {
152+
return fmt.Errorf("proof buffer length must be a multiple of 32, got: %d", len(ap.RawProof))
118153
}
119154

120155
if len(ap.Instances) == 0 {
@@ -128,27 +163,48 @@ func (ap *BatchProof) SanityCheck() error {
128163
return nil
129164
}
130165

166+
// BundleProof
167+
type BundleProof interface {
168+
SanityCheck() error
169+
Proof() []byte
170+
}
171+
172+
// NewBundleProof creates a new BundleProof instance.
173+
func NewBundleProof(hardForkName string) BundleProof {
174+
switch hardForkName {
175+
case darwinV2Fork:
176+
return &Halo2BundleProof{}
177+
default:
178+
return nil
179+
}
180+
}
181+
131182
// 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"`
183+
type Halo2BundleProof struct {
184+
RawProof []byte `json:"proof"`
134185
Instances []byte `json:"instances"`
135186
Vk []byte `json:"vk"`
136187
// cross-reference between cooridinator computation and prover compution
137188
GitVersion string `json:"git_version,omitempty"`
138189
}
139190

191+
// Proof returns the proof bytes of a BundleProof
192+
func (ap *Halo2BundleProof) Proof() []byte {
193+
return ap.RawProof
194+
}
195+
140196
// SanityCheck checks whether a BundleProof is in a legal format
141-
func (ap *BundleProof) SanityCheck() error {
197+
func (ap *Halo2BundleProof) SanityCheck() error {
142198
if ap == nil {
143199
return errors.New("agg_proof is nil")
144200
}
145201

146-
if len(ap.Proof) == 0 {
202+
if len(ap.RawProof) == 0 {
147203
return errors.New("proof not ready")
148204
}
149205

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))
206+
if len(ap.RawProof)%32 != 0 {
207+
return fmt.Errorf("proof buffer length must be a multiple of 32, got: %d", len(ap.RawProof))
152208
}
153209

154210
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)