add scripts

This commit is contained in:
colinlyguo
2025-03-07 21:57:32 +08:00
parent b30f4d0b00
commit 1c284ec01c
4 changed files with 248 additions and 0 deletions

View File

@@ -84,6 +84,7 @@ type ChunkProof interface {
func NewChunkProof(hardForkName string) ChunkProof {
switch hardForkName {
case euclidFork:
fmt.Println("it is euclid fork")
return &OpenVMChunkProof{}
default:
return &Halo2ChunkProof{}

View File

@@ -78,6 +78,18 @@ func (*Batch) TableName() string {
return "batch"
}
// GetBatchHashByIndex retrieves the hash of the batch given its index.
// used for the script.
func (o *Batch) GetBatchHashByIndex(ctx context.Context, index uint64) (string, error) {
db := o.db.WithContext(ctx)
var batch Batch
err := db.Select("hash").Where("index = ?", index).First(&batch).Error
if err != nil {
return "", fmt.Errorf("Batch.GetBatchHashByIndex error: %w, index: %v", err, index)
}
return batch.Hash, nil
}
// GetUnassignedBatch retrieves unassigned batch based on the specified limit.
// The returned batches are sorted in ascending order by their index.
func (o *Batch) GetUnassignedBatch(ctx context.Context, maxActiveAttempts, maxTotalAttempts uint8) (*Batch, error) {

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,154 @@
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"github.com/scroll-tech/da-codec/encoding"
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/log"
"gorm.io/gorm"
"scroll-tech/common/database"
"scroll-tech/common/types/message"
"scroll-tech/coordinator/internal/orm"
)
func main() {
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.LogfmtFormat()))
glogger.Verbosity(log.LvlInfo)
log.Root().SetHandler(glogger)
if len(os.Args) < 2 {
log.Crit("no batch index range provided")
return
}
indexRange := os.Args[1]
indices := strings.Split(indexRange, "-")
if len(indices) != 2 {
log.Crit("invalid batch index range format. Use start-end", "providedRange", indexRange)
return
}
startIndex, err := strconv.Atoi(indices[0])
endIndex, err2 := strconv.Atoi(indices[1])
if err != nil || err2 != nil || startIndex > endIndex {
log.Crit("invalid batch index range", "start", indices[0], "end", indices[1], "err", err, "err2", err2)
return
}
db, err := database.InitDB(&database.Config{
DriverName: "postgres",
DSN: os.Getenv("DB_DSN"),
MaxOpenNum: 200,
MaxIdleNum: 20,
})
if err != nil {
log.Crit("failed to init db", "err", err)
}
defer func() {
if deferErr := database.CloseDB(db); deferErr != nil {
log.Error("failed to close db", "err", err)
}
}()
for i := startIndex; i <= endIndex; i++ {
batchIndex := uint64(i)
resultBytes, err := getBatchTask(db, batchIndex)
if err != nil {
log.Crit("failed to get batch task", "batchIndex", batchIndex, "err", err)
continue
}
outputFilename := fmt.Sprintf("batch_task_%d.json", batchIndex)
if err = os.WriteFile(outputFilename, resultBytes, 0644); err != nil {
log.Crit("failed to write output file", "filename", outputFilename, "err", err)
}
}
}
func getBatchTask(db *gorm.DB, batchIndex uint64) ([]byte, error) {
batch, err := orm.NewBatch(db).GetBatchByIndex(context.Background(), batchIndex)
if err != nil {
err = fmt.Errorf("failed to get batch hash by index: %d err: %w ", batchIndex, err)
return nil, err
}
chunks, err := orm.NewChunk(db).GetChunksByBatchHash(context.Background(), batch.Hash)
if err != nil {
err = fmt.Errorf("failed to get chunk proofs for batch task id: %s err: %w ", batch.Hash, err)
return nil, err
}
var chunkProofs []message.ChunkProof
var chunkInfos []*message.ChunkInfo
for _, chunk := range chunks {
fmt.Println("chunk index: ", chunk.Index)
fmt.Print("chunk proof: ", chunk.Proof)
proof := message.NewChunkProof("euclid")
if encodeErr := json.Unmarshal(chunk.Proof, &proof); encodeErr != nil {
return nil, fmt.Errorf("Chunk.GetProofsByBatchHash unmarshal proof error: %w, batch hash: %v, chunk hash: %v", encodeErr, batch.Hash, chunk.Hash)
}
chunkProofs = append(chunkProofs, proof)
chunkInfo := message.ChunkInfo{
ChainID: 534352,
PrevStateRoot: common.HexToHash(chunk.ParentChunkStateRoot),
PostStateRoot: common.HexToHash(chunk.StateRoot),
WithdrawRoot: common.HexToHash(chunk.WithdrawRoot),
DataHash: common.HexToHash(chunk.Hash),
IsPadding: false,
}
if haloProot, ok := proof.(*message.Halo2ChunkProof); ok {
if haloProot.ChunkInfo != nil {
chunkInfo.TxBytes = haloProot.ChunkInfo.TxBytes
}
}
chunkInfos = append(chunkInfos, &chunkInfo)
}
taskDetail, err := getBatchTaskDetail(batch, chunkInfos, chunkProofs)
if err != nil {
return nil, fmt.Errorf("failed to get batch task detail, taskID:%s err:%w", batch.Hash, err)
}
chunkProofsBytes, err := json.MarshalIndent(taskDetail, "", " ")
if err != nil {
return nil, fmt.Errorf("failed to marshal chunk proofs, taskID:%s err:%w", batch.Hash, err)
}
return chunkProofsBytes, nil
}
func getBatchTaskDetail(dbBatch *orm.Batch, chunkInfos []*message.ChunkInfo, chunkProofs []message.ChunkProof) (*message.BatchTaskDetail, error) {
taskDetail := &message.BatchTaskDetail{
ChunkInfos: chunkInfos,
ChunkProofs: chunkProofs,
}
dbBatchCodecVersion := encoding.CodecVersion(dbBatch.CodecVersion)
switch dbBatchCodecVersion {
case encoding.CodecV3, encoding.CodecV4, encoding.CodecV6:
default:
return taskDetail, nil
}
codec, err := encoding.CodecFromVersion(encoding.CodecVersion(dbBatch.CodecVersion))
if err != nil {
return nil, fmt.Errorf("failed to get codec from version %d, err: %w", dbBatch.CodecVersion, err)
}
batchHeader, decodeErr := codec.NewDABatchFromBytes(dbBatch.BatchHeader)
if decodeErr != nil {
return nil, fmt.Errorf("failed to decode batch header version %d: %w", dbBatch.CodecVersion, decodeErr)
}
taskDetail.BatchHeader = batchHeader
taskDetail.BlobBytes = dbBatch.BlobBytes
return taskDetail, nil
}