-
Notifications
You must be signed in to change notification settings - Fork 627
feat(cache and trace api): Add redis cache in database and add trace api in bridge.l2backend. #228
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 all commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
549eb3c
Add redis cache.
mask-pp d907513
Merge branch 'staging' into cache_redis
mask-pp 4d87025
upgrade struct
mask-pp 1d95be5
go mod tidy
mask-pp 99e86aa
Merge branch 'staging' into cache_redis
mask-pp a91401f
merge staging branch and fix conflict
mask-pp 343d75f
fix ci and update dependence
mask-pp 5daef83
Add redis docker
mask-pp 8f36f18
change redis config
mask-pp 98c8fbe
fix test case
mask-pp 0ea2e43
Update dependence
mask-pp 6f11ca8
Update version
mask-pp 9adaac9
fix ci
mask-pp c15e267
fix ci
mask-pp 7d06a5d
fix ci
mask-pp dcbcb3f
fix ci
mask-pp 14c1c1d
change api
mask-pp fdc1a70
change api
mask-pp 886ddf2
change config
mask-pp e08e1e7
Update database/orm_test.go
mask-pp 8800061
change config
mask-pp 8b4ded0
change config
mask-pp 946bdc7
change config
mask-pp eaf2294
change config
mask-pp 54a6c22
fix bug
mask-pp 4232ed7
merge staging branch and fix conflict
mask-pp 927492c
upgrade api
mask-pp f84d23d
Update coordinator/manager.go
mask-pp 27143cb
fix ci
mask-pp 751513a
fix ci
mask-pp 17d83bc
fix comments
mask-pp 8dd41e8
fix comments and add init function in l2 watcher.
mask-pp 2b5c361
Merge branch 'staging' into cache_redis
mask-pp 2fa4d8a
fix bug
mask-pp dbd0b1a
Add test case in redis.
mask-pp d1db7d3
Apply suggestions from code review
colinlyguo ffadba8
fix comments
mask-pp d1c636e
fix comments
mask-pp 0e80078
fix comments
mask-pp 79ba063
Merge branch 'staging' into cache_redis
mask-pp 456b98b
Merge branch 'staging' into cache_redis
mask-pp 86b9e5c
Merge branch 'staging' into cache_redis
mask-pp 0748558
Update database/config.go
mask-pp 049cac3
Update bridge/l1/l1_test.go
mask-pp 38e942f
fix comments
mask-pp a5aef78
Merge branch 'staging' into cache_redis
mask-pp c3fe80a
Merge branch 'staging' into cache_redis
colinlyguo 8689c6d
trigger ci
colinlyguo c0b7a9b
upgrade redis cache
mask-pp 6cf5d76
Merge branch 'staging' into cache_redis
mask-pp fbd9d84
upgrade redis cache
mask-pp b858f12
fix comments
mask-pp 7abcdb4
fix ci
mask-pp b54d15f
Fill pending batch traces.
mask-pp 5823282
Update version.go
0xmountaintop 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
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,5 +1,21 @@ | ||
package l2 | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/scroll-tech/go-ethereum/core/types" | ||
) | ||
|
||
// WatcherAPI watcher api service | ||
type WatcherAPI interface { | ||
GetTracesByBatchIndex(ctx context.Context, index uint64) ([]*types.BlockTrace, error) | ||
} | ||
|
||
// GetTracesByBatchIndex get traces by batch_id. | ||
func (w *WatcherClient) GetTracesByBatchIndex(ctx context.Context, index uint64) ([]*types.BlockTrace, error) { | ||
id, err := w.orm.GetBatchIDByIndex(index) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return w.orm.GetBlockTraces(map[string]interface{}{"batch_id": id}) | ||
colinlyguo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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,120 @@ | ||
package l2 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/big" | ||
"runtime" | ||
|
||
"github.com/scroll-tech/go-ethereum/log" | ||
"golang.org/x/sync/errgroup" | ||
|
||
"scroll-tech/database/cache" | ||
"scroll-tech/database/orm" | ||
) | ||
|
||
func (w *WatcherClient) initCache(ctx context.Context) error { | ||
var ( | ||
// Use at most half of the system threads. | ||
parallel = (runtime.GOMAXPROCS(0) + 1) / 2 | ||
db = w.orm | ||
) | ||
|
||
mask-pp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Fill unsigned block traces. | ||
for { | ||
batches, err := db.GetBlockBatches( | ||
map[string]interface{}{"proving_status": orm.ProvingTaskUnassigned}, | ||
fmt.Sprintf("ORDER BY index ASC LIMIT %d;", parallel), | ||
) | ||
if err != nil { | ||
log.Error("failed to get block batch", "err", err) | ||
return err | ||
} | ||
if len(batches) == 0 { | ||
break | ||
} | ||
|
||
var eg errgroup.Group | ||
for _, batch := range batches { | ||
batch := batch | ||
eg.Go(func() error { | ||
return w.fillTraceByNumber(ctx, batch.StartBlockNumber, batch.EndBlockNumber) | ||
}) | ||
} | ||
if err = eg.Wait(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// Fill assigned and under proofing block traces into cache. | ||
ids, err := w.orm.GetAssignedBatchIDs() | ||
if err != nil { | ||
return err | ||
} | ||
for _, id := range ids { | ||
err = w.fillTraceByID(ctx, id) | ||
if err != nil { | ||
log.Error("failed to fill traces by id", "id", id, "err", err) | ||
return err | ||
} | ||
} | ||
|
||
// Fill pending block traces into cache. | ||
for { | ||
ids, err = w.orm.GetPendingBatches(uint64(parallel)) | ||
if err != nil { | ||
log.Error("failed to get pending batch ids", "err", err) | ||
return err | ||
} | ||
if len(ids) == 0 { | ||
return nil | ||
} | ||
for _, id := range ids { | ||
err = w.fillTraceByID(ctx, id) | ||
if err != nil { | ||
log.Error("failed to fill traces by id", "id", id, "err", err) | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
|
||
// fillTraceByID Fill block traces by batch id. | ||
func (w *WatcherClient) fillTraceByID(ctx context.Context, id string) error { | ||
batches, err := w.orm.GetBlockBatches(map[string]interface{}{"id": id}) | ||
if err != nil || len(batches) == 0 { | ||
return err | ||
} | ||
batch := batches[0] | ||
err = w.fillTraceByNumber(ctx, batch.StartBlockNumber, batch.EndBlockNumber) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (w *WatcherClient) fillTraceByNumber(ctx context.Context, start, end uint64) error { | ||
var ( | ||
rdb = w.orm.(cache.Cache) | ||
client = w.Client | ||
) | ||
for height := start; height <= end; height++ { | ||
number := big.NewInt(0).SetUint64(height) | ||
exist, err := rdb.ExistTrace(ctx, number) | ||
if err != nil { | ||
return err | ||
} | ||
if exist { | ||
continue | ||
} | ||
trace, err := client.GetBlockTraceByNumber(ctx, number) | ||
if err != nil { | ||
return err | ||
} | ||
err = rdb.SetBlockTrace(ctx, trace) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
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
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.