Skip to content

Commit

Permalink
BatchTaskDetail (#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmountaintop authored Aug 1, 2023
1 parent 0fb3ba8 commit fa899d8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
4 changes: 2 additions & 2 deletions common/types/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ type ChunkTaskDetail struct {

// BatchTaskDetail is a type containing BatchTask detail.
type BatchTaskDetail struct {
ChunkInfos []*ChunkInfo `json:"chunk_infos"`
SubProofs []*ChunkProof `json:"sub_proofs"`
ChunkInfos []*ChunkInfo `json:"chunk_infos"`
ChunkProofs []*ChunkProof `json:"chunk_proofs"`
}

// ProofDetail is the message received from provers that contains zk proof, the status of
Expand Down
28 changes: 25 additions & 3 deletions coordinator/internal/logic/collector/batch_proof_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package collector

import (
"context"
"encoding/json"
"fmt"

"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/log"
"gorm.io/gorm"

Expand Down Expand Up @@ -101,17 +103,37 @@ func (bp *BatchProofCollector) Collect(ctx context.Context) error {
}

func (bp *BatchProofCollector) sendTask(ctx context.Context, hash string) ([]*coordinatorType.ProverStatus, error) {
// get chunk proofs from db
chunkProofs, err := bp.chunkOrm.GetProofsByBatchHash(ctx, hash)
// get chunks from db
chunks, err := bp.chunkOrm.GetChunksByBatchHash(ctx, hash)
if err != nil {
err = fmt.Errorf("failed to get chunk proofs for batch task id:%s err:%w ", hash, err)
return nil, err
}

taskDetail := &message.BatchTaskDetail{}
for _, chunk := range chunks {
chunkInfo := &message.ChunkInfo{
ChainID: bp.cfg.L2Config.ChainID,
PrevStateRoot: common.HexToHash(chunk.ParentChunkStateRoot),
PostStateRoot: common.HexToHash(chunk.StateRoot),
WithdrawRoot: common.HexToHash(chunk.WithdrawRoot),
DataHash: common.HexToHash(chunk.Hash),
IsPadding: false,
}
taskDetail.ChunkInfos = append(taskDetail.ChunkInfos, chunkInfo)

chunkProof := &message.ChunkProof{}
if err := json.Unmarshal(chunk.Proof, chunkProof); err != nil {
return nil, fmt.Errorf("json Unmarshal ChunkProof error: %w, chunk hash: %v", err, chunk.Hash)
}
taskDetail.ChunkProofs = append(taskDetail.ChunkProofs, chunkProof)
}

taskMsg := &message.TaskMsg{
ID: hash,
Type: message.ProofTypeBatch,
ChunkTaskDetail: nil,
BatchTaskDetail: &message.BatchTaskDetail{SubProofs: chunkProofs},
BatchTaskDetail: taskDetail,
}
return bp.BaseCollector.sendTask(taskMsg)
}
15 changes: 15 additions & 0 deletions coordinator/internal/orm/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ func (o *Chunk) GetUnassignedChunks(ctx context.Context, limit int) ([]*Chunk, e
return chunks, nil
}

// GetChunksByBatchHash retrieves the chunks associated with a specific batch hash.
// The returned chunks are sorted in ascending order by their associated chunk index.
func (o *Chunk) GetChunksByBatchHash(ctx context.Context, batchHash string) ([]*Chunk, error) {
db := o.db.WithContext(ctx)
db = db.Model(&Chunk{})
db = db.Where("batch_hash", batchHash)
db = db.Order("index ASC")

var chunks []*Chunk
if err := db.Find(&chunks).Error; err != nil {
return nil, fmt.Errorf("Chunk.GetChunksByBatchHash error: %w, batch hash: %v", err, batchHash)
}
return chunks, nil
}

// GetProofsByBatchHash retrieves the proofs associated with a specific batch hash.
// It returns a slice of decoded proofs (message.ChunkProof) obtained from the database.
// The returned proofs are sorted in ascending order by their associated chunk index.
Expand Down
1 change: 1 addition & 0 deletions coordinator/test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func setupCoordinator(t *testing.T, proversPerSession uint8, wsURL string, reset
}

conf := config.Config{
L2Config: &config.L2Config{ChainID: 111},
ProverManagerConfig: &config.ProverManagerConfig{
ProversPerSession: proversPerSession,
Verifier: &config.VerifierConfig{MockMode: true},
Expand Down
2 changes: 1 addition & 1 deletion prover/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (r *Prover) proveBatch(task *store.ProvingTask) (*message.BatchProof, error
if task.Task.BatchTaskDetail == nil {
return nil, errors.New("BatchTaskDetail is empty")
}
return r.proverCore.ProveBatch(task.Task.ID, task.Task.BatchTaskDetail.ChunkInfos, task.Task.BatchTaskDetail.SubProofs)
return r.proverCore.ProveBatch(task.Task.ID, task.Task.BatchTaskDetail.ChunkInfos, task.Task.BatchTaskDetail.ChunkProofs)
}

func (r *Prover) signAndSubmitProof(msg *message.ProofDetail) {
Expand Down

0 comments on commit fa899d8

Please sign in to comment.