Skip to content

Commit 515b5b8

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

File tree

18 files changed

+165
-131
lines changed

18 files changed

+165
-131
lines changed

common/types/message/message.go

Lines changed: 83 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,26 @@ 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+
case "darwinV2":
81+
return &Halo2ChunkProof{}
82+
default:
83+
return nil
84+
}
85+
}
86+
87+
// Halo2ChunkProof includes the proof info that are required for chunk verification and rollup.
88+
type Halo2ChunkProof struct {
8489
StorageTrace []byte `json:"storage_trace,omitempty"`
8590
Protocol []byte `json:"protocol"`
86-
Proof []byte `json:"proof"`
91+
RawProof []byte `json:"proof"`
8792
Instances []byte `json:"instances"`
8893
Vk []byte `json:"vk"`
8994
// cross-reference between cooridinator computation and prover compution
@@ -92,29 +97,55 @@ type ChunkProof struct {
9297
RowUsages []SubCircuitRowUsage `json:"row_usages,omitempty"`
9398
}
9499

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

132+
// Proof returns the proof bytes of a BatchProof
133+
func (ap *Halo2BatchProof) Proof() []byte {
134+
return ap.RawProof
135+
}
136+
106137
// SanityCheck checks whether a BatchProof is in a legal format
107-
func (ap *BatchProof) SanityCheck() error {
138+
func (ap *Halo2BatchProof) SanityCheck() error {
108139
if ap == nil {
109140
return errors.New("agg_proof is nil")
110141
}
111142

112-
if len(ap.Proof) == 0 {
143+
if len(ap.RawProof) == 0 {
113144
return errors.New("proof not ready")
114145
}
115146

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

120151
if len(ap.Instances) == 0 {
@@ -128,27 +159,48 @@ func (ap *BatchProof) SanityCheck() error {
128159
return nil
129160
}
130161

162+
// BundleProof
163+
type BundleProof interface {
164+
SanityCheck() error
165+
Proof() []byte
166+
}
167+
168+
// NewBundleProof creates a new BundleProof instance.
169+
func NewBundleProof(hardForkName string) BundleProof {
170+
switch hardForkName {
171+
case "darwinV2":
172+
return &Halo2BundleProof{}
173+
default:
174+
return nil
175+
}
176+
}
177+
131178
// 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"`
179+
type Halo2BundleProof struct {
180+
RawProof []byte `json:"proof"`
134181
Instances []byte `json:"instances"`
135182
Vk []byte `json:"vk"`
136183
// cross-reference between cooridinator computation and prover compution
137184
GitVersion string `json:"git_version,omitempty"`
138185
}
139186

187+
// Proof returns the proof bytes of a BundleProof
188+
func (ap *Halo2BundleProof) Proof() []byte {
189+
return ap.RawProof
190+
}
191+
140192
// SanityCheck checks whether a BundleProof is in a legal format
141-
func (ap *BundleProof) SanityCheck() error {
193+
func (ap *Halo2BundleProof) SanityCheck() error {
142194
if ap == nil {
143195
return errors.New("agg_proof is nil")
144196
}
145197

146-
if len(ap.Proof) == 0 {
198+
if len(ap.RawProof) == 0 {
147199
return errors.New("proof not ready")
148200
}
149201

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

154206
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: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
package verifier
44

55
import (
6+
"scroll-tech/common/types"
67
"scroll-tech/common/types/message"
7-
88
"scroll-tech/coordinator/internal/config"
99
)
1010

@@ -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 types.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 types..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 types..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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/scroll-tech/go-ethereum/log"
2222

2323
"scroll-tech/common/types/message"
24-
2524
"scroll-tech/coordinator/internal/config"
2625
)
2726

@@ -110,10 +109,10 @@ func NewVerifier(cfg *config.VerifierConfig) (*Verifier, error) {
110109
}
111110

112111
// 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) {
112+
func (v *Verifier) VerifyBatchProof(proof message.BatchProof, forkName string) (bool, error) {
114113
if v.cfg.MockMode {
115114
log.Info("Mock mode, batch verifier disabled")
116-
if string(proof.Proof) == InvalidTestProof {
115+
if string(proof.Proof()) == InvalidTestProof {
117116
return false, nil
118117
}
119118
return true, nil
@@ -137,10 +136,10 @@ func (v *Verifier) VerifyBatchProof(proof *message.BatchProof, forkName string)
137136
}
138137

139138
// 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) {
139+
func (v *Verifier) VerifyChunkProof(proof message.ChunkProof, forkName string) (bool, error) {
141140
if v.cfg.MockMode {
142141
log.Info("Mock mode, verifier disabled")
143-
if string(proof.Proof) == InvalidTestProof {
142+
if string(proof.Proof()) == InvalidTestProof {
144143
return false, nil
145144
}
146145
return true, nil
@@ -164,10 +163,10 @@ func (v *Verifier) VerifyChunkProof(proof *message.ChunkProof, forkName string)
164163
}
165164

166165
// 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) {
166+
func (v *Verifier) VerifyBundleProof(proof message.BundleProof, forkName string) (bool, error) {
168167
if v.cfg.MockMode {
169168
log.Info("Mock mode, verifier disabled")
170-
if string(proof.Proof) == InvalidTestProof {
169+
if string(proof.Proof()) == InvalidTestProof {
171170
return false, nil
172171
}
173172
return true, nil

0 commit comments

Comments
 (0)