fix(rollup-relayer): add transaction support to insert l2 blocks (#1674)

This commit is contained in:
colin
2025-06-05 21:27:27 +08:00
committed by GitHub
parent 5204ad50e0
commit 82fb15de3b
3 changed files with 8 additions and 4 deletions

View File

@@ -5,7 +5,7 @@ import (
"runtime/debug"
)
var tag = "v4.5.20"
var tag = "v4.5.21"
var commit = func() string {
if info, ok := debug.ReadBuildInfo(); ok {

View File

@@ -220,7 +220,7 @@ func (r *Layer2Relayer) initializeGenesis() error {
chunk := &encoding.Chunk{Blocks: []*encoding.Block{{Header: genesis}}}
err = r.db.Transaction(func(dbTX *gorm.DB) error {
if err = r.l2BlockOrm.InsertL2Blocks(r.ctx, chunk.Blocks); err != nil {
if err = r.l2BlockOrm.InsertL2Blocks(r.ctx, chunk.Blocks, dbTX); err != nil {
return fmt.Errorf("failed to insert genesis block: %v", err)
}

View File

@@ -173,7 +173,7 @@ func (o *L2Block) GetL2BlocksInRange(ctx context.Context, startBlockNumber uint6
}
// InsertL2Blocks inserts l2 blocks into the "l2_block" table.
func (o *L2Block) InsertL2Blocks(ctx context.Context, blocks []*encoding.Block) error {
func (o *L2Block) InsertL2Blocks(ctx context.Context, blocks []*encoding.Block, dbTX ...*gorm.DB) error {
var l2Blocks []L2Block
for _, block := range blocks {
header, err := json.Marshal(block.Header)
@@ -203,7 +203,11 @@ func (o *L2Block) InsertL2Blocks(ctx context.Context, blocks []*encoding.Block)
l2Blocks = append(l2Blocks, l2Block)
}
db := o.db.WithContext(ctx)
db := o.db
if len(dbTX) > 0 && dbTX[0] != nil {
db = dbTX[0]
}
db = db.WithContext(ctx)
db = db.Model(&L2Block{})
if err := db.Create(&l2Blocks).Error; err != nil {