-
Notifications
You must be signed in to change notification settings - Fork 628
[feat] Integration e2e test tools #1694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
41caf08
local e2e test for prover and coordinator
noel2004 687b903
update e2e db with blocks in scroll sepolia
noel2004 ff21b68
readme
noel2004 2cd648b
fixing according to review
noel2004 5b59166
Merge remote-tracking branch 'origin/develop' into integration_test
noel2004 7171c2c
Merge branch 'develop' into integration_test
noel2004 50dda3f
Update typos in README.md
noel2004 b4c72cb
typo
noel2004 41a6d0e
Merge branch 'develop' into integration_test
noel2004 516e299
fix according to reviews
noel2004 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "math/rand" | ||
| "sort" | ||
| "strings" | ||
|
|
||
| "gorm.io/gorm" | ||
|
|
||
| "github.com/scroll-tech/da-codec/encoding" | ||
| "github.com/scroll-tech/go-ethereum/common" | ||
| "github.com/scroll-tech/go-ethereum/log" | ||
|
|
||
| "scroll-tech/common/database" | ||
|
|
||
| "scroll-tech/rollup/internal/orm" | ||
| "scroll-tech/rollup/internal/utils" | ||
| ) | ||
|
|
||
| type importRecord struct { | ||
| Chunk []string `json:"chunks"` | ||
| Batch []string `json:"batches"` | ||
| Bundle []string `json:"bundles"` | ||
| } | ||
|
|
||
| func randomPickKfromN(n, k int, rng *rand.Rand) []int { | ||
| ret := make([]int, n-1) | ||
| for i := 1; i < n; i++ { | ||
| ret[i-1] = i | ||
| } | ||
|
|
||
| rng.Shuffle(len(ret), func(i, j int) { | ||
| ret[i], ret[j] = ret[j], ret[i] | ||
| }) | ||
|
|
||
| ret = ret[:k-1] | ||
| sort.Ints(ret) | ||
|
|
||
| return ret | ||
| } | ||
|
|
||
| func importData(ctx context.Context, beginBlk, endBlk uint64, chkNum, batchNum, bundleNum int, seed int64) (*importRecord, error) { | ||
|
|
||
| db, err := database.InitDB(cfg.DBConfig) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| ret := &importRecord{} | ||
| // Create a new random source with the provided seed | ||
| source := rand.NewSource(seed) | ||
| //nolint:all | ||
| rng := rand.New(source) | ||
|
|
||
| chkSepIdx := randomPickKfromN(int(endBlk-beginBlk)+1, chkNum, rng) | ||
| chkSep := make([]uint64, len(chkSepIdx)) | ||
| for i, ind := range chkSepIdx { | ||
| chkSep[i] = beginBlk + uint64(ind) | ||
| } | ||
| chkSep = append(chkSep, endBlk+1) | ||
|
|
||
| log.Info("separated chunk", "border", chkSep) | ||
| head := beginBlk | ||
| lastMsgHash := common.Hash{} | ||
|
|
||
| ormChks := make([]*orm.Chunk, 0, chkNum) | ||
| encChks := make([]*encoding.Chunk, 0, chkNum) | ||
| for _, edBlk := range chkSep { | ||
| ormChk, chk, err := importChunk(ctx, db, head, edBlk-1, lastMsgHash) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| lastMsgHash = chk.PostL1MessageQueueHash | ||
| ormChks = append(ormChks, ormChk) | ||
| encChks = append(encChks, chk) | ||
| head = edBlk | ||
| } | ||
|
|
||
| for _, chk := range ormChks { | ||
| ret.Chunk = append(ret.Chunk, chk.Hash) | ||
| } | ||
|
|
||
| batchSep := randomPickKfromN(chkNum, batchNum, rng) | ||
| batchSep = append(batchSep, chkNum) | ||
| log.Info("separated batch", "border", batchSep) | ||
|
|
||
| headChk := int(0) | ||
| batches := make([]*orm.Batch, 0, batchNum) | ||
| var lastBatch *orm.Batch | ||
| for _, endChk := range batchSep { | ||
| batch, err := importBatch(ctx, db, ormChks[headChk:endChk], encChks[headChk:endChk], lastBatch) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| lastBatch = batch | ||
| batches = append(batches, batch) | ||
| headChk = endChk | ||
| } | ||
|
|
||
| for _, batch := range batches { | ||
| ret.Batch = append(ret.Batch, batch.Hash) | ||
| } | ||
|
|
||
| bundleSep := randomPickKfromN(batchNum, bundleNum, rng) | ||
| bundleSep = append(bundleSep, batchNum) | ||
| log.Info("separated bundle", "border", bundleSep) | ||
|
|
||
| headBatch := int(0) | ||
| for _, endBatch := range bundleSep { | ||
| hash, err := importBundle(ctx, db, batches[headBatch:endBatch]) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| ret.Bundle = append(ret.Bundle, hash) | ||
| headBatch = endBatch | ||
| } | ||
|
|
||
| return ret, nil | ||
| } | ||
|
|
||
| func importChunk(ctx context.Context, db *gorm.DB, beginBlk, endBlk uint64, prevMsgQueueHash common.Hash) (*orm.Chunk, *encoding.Chunk, error) { | ||
| nblk := int(endBlk-beginBlk) + 1 | ||
| blockOrm := orm.NewL2Block(db) | ||
|
|
||
| blks, err := blockOrm.GetL2BlocksGEHeight(ctx, beginBlk, nblk) | ||
|
|
||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| postHash, err := encoding.MessageQueueV2ApplyL1MessagesFromBlocks(prevMsgQueueHash, blks) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| theChunk := &encoding.Chunk{ | ||
| Blocks: blks, | ||
| PrevL1MessageQueueHash: prevMsgQueueHash, | ||
| PostL1MessageQueueHash: postHash, | ||
| } | ||
| chunkOrm := orm.NewChunk(db) | ||
|
|
||
| dbChk, err := chunkOrm.InsertChunk(ctx, theChunk, codecCfg, utils.ChunkMetrics{}) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| err = blockOrm.UpdateChunkHashInRange(ctx, beginBlk, endBlk, dbChk.Hash) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| log.Info("insert chunk", "From", beginBlk, "To", endBlk, "hash", dbChk.Hash) | ||
| return dbChk, theChunk, nil | ||
| } | ||
|
|
||
| func importBatch(ctx context.Context, db *gorm.DB, chks []*orm.Chunk, encChks []*encoding.Chunk, last *orm.Batch) (*orm.Batch, error) { | ||
|
|
||
| batchOrm := orm.NewBatch(db) | ||
| if last == nil { | ||
| var err error | ||
| last, err = batchOrm.GetLatestBatch(ctx) | ||
| if err != nil && !strings.Contains(err.Error(), "record not found") { | ||
| return nil, err | ||
| } else if last != nil { | ||
| log.Info("start from last batch", "index", last.Index) | ||
| } | ||
| } | ||
|
|
||
| index := uint64(0) | ||
| var parentHash common.Hash | ||
| if last != nil { | ||
| index = last.Index + 1 | ||
| parentHash = common.HexToHash(last.Hash) | ||
| } | ||
|
|
||
| var blks []*encoding.Block | ||
| for _, chk := range encChks { | ||
| blks = append(blks, chk.Blocks...) | ||
| } | ||
|
|
||
| batch := &encoding.Batch{ | ||
| Index: index, | ||
| TotalL1MessagePoppedBefore: chks[0].TotalL1MessagesPoppedBefore, | ||
| ParentBatchHash: parentHash, | ||
| Chunks: encChks, | ||
| Blocks: blks, | ||
| } | ||
|
|
||
| dbBatch, err := batchOrm.InsertBatch(ctx, batch, codecCfg, utils.BatchMetrics{}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| err = orm.NewChunk(db).UpdateBatchHashInRange(ctx, chks[0].Index, chks[len(chks)-1].Index, dbBatch.Hash) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| log.Info("insert batch", "index", index) | ||
| return dbBatch, nil | ||
| } | ||
|
|
||
| func importBundle(ctx context.Context, db *gorm.DB, batches []*orm.Batch) (string, error) { | ||
|
|
||
| bundleOrm := orm.NewBundle(db) | ||
| bundle, err := bundleOrm.InsertBundle(ctx, batches, codecCfg) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| err = orm.NewBatch(db).UpdateBundleHashInRange(ctx, batches[0].Index, batches[len(batches)-1].Index, bundle.Hash) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| log.Info("insert bundle", "hash", bundle.Hash) | ||
| return bundle.Hash, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.