Skip to content

Commit afe3ae9

Browse files
committed
remove duplicated code
1 parent afe1a77 commit afe3ae9

File tree

4 files changed

+83
-93
lines changed

4 files changed

+83
-93
lines changed

coordinator/internal/logic/provertask/batch_prover_task.go

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package provertask
33
import (
44
"context"
55
"encoding/json"
6-
"errors"
76
"fmt"
87
"time"
98

@@ -56,20 +55,6 @@ func NewBatchProverTask(cfg *config.Config, chainCfg *params.ChainConfig, db *go
5655
return bp
5756
}
5857

59-
// proverHardForkSanityCheck check the prover task's hard-fork name
60-
// and prover-task's hard-fork name is the same
61-
func (bp *BatchProverTask) hardForkSanityCheck(ctx *gin.Context, taskCtx *proverTaskContext, batchTask *orm.Batch) (string, error) {
62-
hardForkName, getHardForkErr := bp.hardForkName(ctx, batchTask)
63-
if getHardForkErr != nil {
64-
return "", getHardForkErr
65-
}
66-
67-
if _, ok := taskCtx.HardForkNames[hardForkName]; !ok {
68-
return "", errors.New("hard-fork name is not the same as the batch's hard-fork name")
69-
}
70-
return hardForkName, nil
71-
}
72-
7358
// Assign load and assign batch tasks
7459
func (bp *BatchProverTask) Assign(ctx *gin.Context, getTaskParameter *coordinatorType.GetTaskParameter) (*coordinatorType.GetTaskSchema, error) {
7560
taskCtx, err := bp.checkParameter(ctx)
@@ -117,8 +102,11 @@ func (bp *BatchProverTask) Assign(ctx *gin.Context, getTaskParameter *coordinato
117102
return nil, nil
118103
}
119104

105+
taskCtx.taskType = message.ProofTypeBatch
106+
taskCtx.batchTask = tmpBatchTask
107+
120108
var checkErr error
121-
hardForkName, checkErr = bp.hardForkSanityCheck(ctx, taskCtx, tmpBatchTask)
109+
hardForkName, checkErr = bp.hardForkSanityCheck(ctx, taskCtx)
122110
if checkErr != nil {
123111
log.Debug("hard fork sanity check failed", "height", getTaskParameter.ProverHeight, "err", checkErr)
124112
return nil, nil
@@ -195,20 +183,6 @@ func (bp *BatchProverTask) Assign(ctx *gin.Context, getTaskParameter *coordinato
195183
return taskMsg, nil
196184
}
197185

198-
func (bp *BatchProverTask) hardForkName(ctx *gin.Context, batchTask *orm.Batch) (string, error) {
199-
startChunk, getChunkErr := bp.chunkOrm.GetChunkByHash(ctx, batchTask.StartChunkHash)
200-
if getChunkErr != nil {
201-
return "", getChunkErr
202-
}
203-
204-
l2Block, getBlockErr := bp.blockOrm.GetL2BlockByNumber(ctx.Copy(), startChunk.StartBlockNumber)
205-
if getBlockErr != nil {
206-
return "", getBlockErr
207-
}
208-
hardForkName := encoding.GetHardforkName(bp.chainCfg, l2Block.Number, l2Block.BlockTimestamp)
209-
return hardForkName, nil
210-
}
211-
212186
func (bp *BatchProverTask) formatProverTask(ctx context.Context, task *orm.ProverTask, batch *orm.Batch, hardForkName string) (*coordinatorType.GetTaskSchema, error) {
213187
// get chunk from db
214188
chunks, err := bp.chunkOrm.GetChunksByBatchHash(ctx, task.TaskID)

coordinator/internal/logic/provertask/bundle_prover_task.go

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ package provertask
33
import (
44
"context"
55
"encoding/json"
6-
"errors"
76
"fmt"
87
"time"
98

109
"github.com/gin-gonic/gin"
1110
"github.com/prometheus/client_golang/prometheus"
1211
"github.com/prometheus/client_golang/prometheus/promauto"
13-
"github.com/scroll-tech/da-codec/encoding"
1412
"github.com/scroll-tech/go-ethereum/log"
1513
"github.com/scroll-tech/go-ethereum/params"
1614
"gorm.io/gorm"
@@ -56,20 +54,6 @@ func NewBundleProverTask(cfg *config.Config, chainCfg *params.ChainConfig, db *g
5654
return bp
5755
}
5856

59-
// proverHardForkSanityCheck check the prover task's hard-fork name
60-
// and prover-task's hard-fork name is the same
61-
func (bp *BundleProverTask) hardForkSanityCheck(ctx *gin.Context, taskCtx *proverTaskContext, bundleTask *orm.Bundle) (string, error) {
62-
hardForkName, getHardForkErr := bp.hardForkName(ctx, bundleTask)
63-
if getHardForkErr != nil {
64-
return "", getHardForkErr
65-
}
66-
67-
if _, ok := taskCtx.HardForkNames[hardForkName]; !ok {
68-
return "", errors.New("prover task's hard-fork name is not the same as the bundle's hard-fork name")
69-
}
70-
return hardForkName, nil
71-
}
72-
7357
// Assign load and assign batch tasks
7458
func (bp *BundleProverTask) Assign(ctx *gin.Context, getTaskParameter *coordinatorType.GetTaskParameter) (*coordinatorType.GetTaskSchema, error) {
7559
taskCtx, err := bp.checkParameter(ctx)
@@ -117,8 +101,11 @@ func (bp *BundleProverTask) Assign(ctx *gin.Context, getTaskParameter *coordinat
117101
return nil, nil
118102
}
119103

104+
taskCtx.taskType = message.ProofTypeBundle
105+
taskCtx.bundleTask = tmpBundleTask
106+
120107
var checkErr error
121-
hardForkName, checkErr = bp.hardForkSanityCheck(ctx, taskCtx, tmpBundleTask)
108+
hardForkName, checkErr = bp.hardForkSanityCheck(ctx, taskCtx)
122109
if checkErr != nil {
123110
log.Debug("hard fork sanity check failed", "height", getTaskParameter.ProverHeight, "err", checkErr)
124111
return nil, nil
@@ -195,26 +182,6 @@ func (bp *BundleProverTask) Assign(ctx *gin.Context, getTaskParameter *coordinat
195182
return taskMsg, nil
196183
}
197184

198-
func (bp *BundleProverTask) hardForkName(ctx *gin.Context, bundleTask *orm.Bundle) (string, error) {
199-
startBatch, getBatchErr := bp.batchOrm.GetBatchByHash(ctx, bundleTask.StartBatchHash)
200-
if getBatchErr != nil {
201-
return "", getBatchErr
202-
}
203-
204-
startChunk, getChunkErr := bp.chunkOrm.GetChunkByHash(ctx, startBatch.StartChunkHash)
205-
if getChunkErr != nil {
206-
return "", getChunkErr
207-
}
208-
209-
l2Block, getBlockErr := bp.blockOrm.GetL2BlockByNumber(ctx.Copy(), startChunk.StartBlockNumber)
210-
if getBlockErr != nil {
211-
return "", getBlockErr
212-
}
213-
214-
hardForkName := encoding.GetHardforkName(bp.chainCfg, l2Block.Number, l2Block.BlockTimestamp)
215-
return hardForkName, nil
216-
}
217-
218185
func (bp *BundleProverTask) formatProverTask(ctx context.Context, task *orm.ProverTask, hardForkName string) (*coordinatorType.GetTaskSchema, error) {
219186
// get bundle from db
220187
batches, err := bp.batchOrm.GetBatchesByBundleHash(ctx, task.TaskID)

coordinator/internal/logic/provertask/chunk_prover_task.go

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ package provertask
33
import (
44
"context"
55
"encoding/json"
6-
"errors"
76
"fmt"
87
"time"
98

109
"github.com/gin-gonic/gin"
1110
"github.com/prometheus/client_golang/prometheus"
1211
"github.com/prometheus/client_golang/prometheus/promauto"
13-
"github.com/scroll-tech/da-codec/encoding"
1412
"github.com/scroll-tech/go-ethereum/log"
1513
"github.com/scroll-tech/go-ethereum/params"
1614
"gorm.io/gorm"
@@ -54,20 +52,6 @@ func NewChunkProverTask(cfg *config.Config, chainCfg *params.ChainConfig, db *go
5452
return cp
5553
}
5654

57-
// proverHardForkSanityCheck check the prover task's hard-fork name
58-
// and prover-task's hard-fork name is the same
59-
func (cp *ChunkProverTask) hardForkSanityCheck(ctx *gin.Context, taskCtx *proverTaskContext, chunkTask *orm.Chunk) (string, error) {
60-
hardForkName, getHardForkErr := cp.hardForkName(ctx, chunkTask)
61-
if getHardForkErr != nil {
62-
return "", getHardForkErr
63-
}
64-
65-
if _, ok := taskCtx.HardForkNames[hardForkName]; !ok {
66-
return "", errors.New("prover task's hard-fork name is not the same as the chunk's hard-fork name")
67-
}
68-
return hardForkName, nil
69-
}
70-
7155
// Assign the chunk proof which need to prove
7256
func (cp *ChunkProverTask) Assign(ctx *gin.Context, getTaskParameter *coordinatorType.GetTaskParameter) (*coordinatorType.GetTaskSchema, error) {
7357
taskCtx, err := cp.checkParameter(ctx)
@@ -115,8 +99,10 @@ func (cp *ChunkProverTask) Assign(ctx *gin.Context, getTaskParameter *coordinato
11599
return nil, nil
116100
}
117101

102+
taskCtx.taskType = message.ProofTypeChunk
103+
taskCtx.chunkTask = tmpChunkTask
118104
var checkErr error
119-
hardForkName, checkErr = cp.hardForkSanityCheck(ctx, taskCtx, tmpChunkTask)
105+
hardForkName, checkErr = cp.hardForkSanityCheck(ctx, taskCtx)
120106
if checkErr != nil {
121107
log.Debug("hard fork sanity check failed", "height", getTaskParameter.ProverHeight, "err", checkErr)
122108
return nil, nil
@@ -192,15 +178,6 @@ func (cp *ChunkProverTask) Assign(ctx *gin.Context, getTaskParameter *coordinato
192178
return taskMsg, nil
193179
}
194180

195-
func (cp *ChunkProverTask) hardForkName(ctx *gin.Context, chunkTask *orm.Chunk) (string, error) {
196-
l2Block, getBlockErr := cp.blockOrm.GetL2BlockByNumber(ctx.Copy(), chunkTask.StartBlockNumber)
197-
if getBlockErr != nil {
198-
return "", getBlockErr
199-
}
200-
hardForkName := encoding.GetHardforkName(cp.chainCfg, l2Block.Number, l2Block.BlockTimestamp)
201-
return hardForkName, nil
202-
}
203-
204181
func (cp *ChunkProverTask) formatProverTask(ctx context.Context, task *orm.ProverTask, hardForkName string) (*coordinatorType.GetTaskSchema, error) {
205182
// Get block hashes.
206183
blockHashes, dbErr := cp.blockOrm.GetL2BlockHashesByChunkHash(ctx, task.TaskID)

coordinator/internal/logic/provertask/prover_task.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import (
99
"github.com/gin-gonic/gin"
1010
"github.com/prometheus/client_golang/prometheus"
1111
"github.com/prometheus/client_golang/prometheus/promauto"
12+
"github.com/scroll-tech/da-codec/encoding"
1213
"github.com/scroll-tech/go-ethereum/params"
1314
"gorm.io/gorm"
1415

16+
"scroll-tech/common/types/message"
1517
"scroll-tech/coordinator/internal/config"
1618
"scroll-tech/coordinator/internal/orm"
1719
coordinatorType "scroll-tech/coordinator/internal/types"
@@ -52,6 +54,76 @@ type proverTaskContext struct {
5254
ProverVersion string
5355
ProverProviderType uint8
5456
HardForkNames map[string]struct{}
57+
58+
taskType message.ProofType
59+
chunkTask *orm.Chunk
60+
batchTask *orm.Batch
61+
bundleTask *orm.Bundle
62+
}
63+
64+
// hardForkName get the chunk/batch/bundle hard fork name
65+
func (b *BaseProverTask) hardForkName(ctx *gin.Context, taskCtx *proverTaskContext) (string, error) {
66+
switch {
67+
case taskCtx.taskType == message.ProofTypeChunk:
68+
if taskCtx.chunkTask == nil {
69+
return "", errors.New("chunk task is nil")
70+
}
71+
l2Block, getBlockErr := b.blockOrm.GetL2BlockByNumber(ctx.Copy(), taskCtx.chunkTask.StartBlockNumber)
72+
if getBlockErr != nil {
73+
return "", getBlockErr
74+
}
75+
hardForkName := encoding.GetHardforkName(b.chainCfg, l2Block.Number, l2Block.BlockTimestamp)
76+
return hardForkName, nil
77+
78+
case taskCtx.taskType == message.ProofTypeBatch:
79+
if taskCtx.batchTask == nil {
80+
return "", errors.New("batch task is nil")
81+
}
82+
startChunk, getChunkErr := b.chunkOrm.GetChunkByHash(ctx, taskCtx.batchTask.StartChunkHash)
83+
if getChunkErr != nil {
84+
return "", getChunkErr
85+
}
86+
l2Block, getBlockErr := b.blockOrm.GetL2BlockByNumber(ctx.Copy(), startChunk.StartBlockNumber)
87+
if getBlockErr != nil {
88+
return "", getBlockErr
89+
}
90+
hardForkName := encoding.GetHardforkName(b.chainCfg, l2Block.Number, l2Block.BlockTimestamp)
91+
return hardForkName, nil
92+
93+
case taskCtx.taskType == message.ProofTypeBundle:
94+
if taskCtx.bundleTask == nil {
95+
return "", errors.New("bundle task is nil")
96+
}
97+
startBatch, getBatchErr := b.batchOrm.GetBatchByHash(ctx, taskCtx.bundleTask.StartBatchHash)
98+
if getBatchErr != nil {
99+
return "", getBatchErr
100+
}
101+
startChunk, getChunkErr := b.chunkOrm.GetChunkByHash(ctx, startBatch.StartChunkHash)
102+
if getChunkErr != nil {
103+
return "", getChunkErr
104+
}
105+
l2Block, getBlockErr := b.blockOrm.GetL2BlockByNumber(ctx.Copy(), startChunk.StartBlockNumber)
106+
if getBlockErr != nil {
107+
return "", getBlockErr
108+
}
109+
hardForkName := encoding.GetHardforkName(b.chainCfg, l2Block.Number, l2Block.BlockTimestamp)
110+
return hardForkName, nil
111+
default:
112+
return "", errors.New("illegal task type")
113+
}
114+
}
115+
116+
// and prover-task's hard-fork name is the same
117+
func (b *BaseProverTask) hardForkSanityCheck(ctx *gin.Context, taskCtx *proverTaskContext) (string, error) {
118+
hardForkName, getHardForkErr := b.hardForkName(ctx, taskCtx)
119+
if getHardForkErr != nil {
120+
return "", getHardForkErr
121+
}
122+
123+
if _, ok := taskCtx.HardForkNames[hardForkName]; !ok {
124+
return "", errors.New("to be assigned prover task's hard-fork name is not the same as prover")
125+
}
126+
return hardForkName, nil
55127
}
56128

57129
// checkParameter check the prover task parameter illegal

0 commit comments

Comments
 (0)