Skip to content

Commit 5cfdf9e

Browse files
author
colinlyguo
committed
fix: add transaction support to insert l2 blocks
1 parent 5204ad5 commit 5cfdf9e

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

common/version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"runtime/debug"
66
)
77

8-
var tag = "v4.5.20"
8+
var tag = "v4.5.21"
99

1010
var commit = func() string {
1111
if info, ok := debug.ReadBuildInfo(); ok {

rollup/internal/controller/relayer/l2_relayer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (r *Layer2Relayer) initializeGenesis() error {
220220
chunk := &encoding.Chunk{Blocks: []*encoding.Block{{Header: genesis}}}
221221

222222
err = r.db.Transaction(func(dbTX *gorm.DB) error {
223-
if err = r.l2BlockOrm.InsertL2Blocks(r.ctx, chunk.Blocks); err != nil {
223+
if err = r.l2BlockOrm.InsertL2Blocks(r.ctx, chunk.Blocks, dbTX); err != nil {
224224
return fmt.Errorf("failed to insert genesis block: %v", err)
225225
}
226226

rollup/internal/orm/l2_block.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (o *L2Block) GetL2BlocksInRange(ctx context.Context, startBlockNumber uint6
173173
}
174174

175175
// InsertL2Blocks inserts l2 blocks into the "l2_block" table.
176-
func (o *L2Block) InsertL2Blocks(ctx context.Context, blocks []*encoding.Block) error {
176+
func (o *L2Block) InsertL2Blocks(ctx context.Context, blocks []*encoding.Block, dbTX ...*gorm.DB) error {
177177
var l2Blocks []L2Block
178178
for _, block := range blocks {
179179
header, err := json.Marshal(block.Header)
@@ -203,7 +203,11 @@ func (o *L2Block) InsertL2Blocks(ctx context.Context, blocks []*encoding.Block)
203203
l2Blocks = append(l2Blocks, l2Block)
204204
}
205205

206-
db := o.db.WithContext(ctx)
206+
db := o.db
207+
if len(dbTX) > 0 && dbTX[0] != nil {
208+
db = dbTX[0]
209+
}
210+
db = db.WithContext(ctx)
207211
db = db.Model(&L2Block{})
208212

209213
if err := db.Create(&l2Blocks).Error; err != nil {

0 commit comments

Comments
 (0)