-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7652c3
commit d1ea327
Showing
13 changed files
with
202 additions
and
129 deletions.
There are no files selected for viewing
This file contains 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 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
2 changes: 1 addition & 1 deletion
2
...ller/messagefetcher/l2_message_fetcher.go → ...ntroller/eventfetcher/l2_event_fetcher.go
This file contains 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,4 +1,4 @@ | ||
package messagefetcher | ||
package eventfetcher | ||
|
||
import ( | ||
"context" | ||
|
This file contains 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 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 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 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 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,107 @@ | ||
package orm | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
// RollupStatusType represents the status of a rollup. | ||
type RollupStatusType int | ||
|
||
// Constants for RollupStatusType. | ||
const ( | ||
RollupStatusTypeUncommitted RollupStatusType = iota | ||
RollupStatusTypeCommitted | ||
RollupStatusTypeFinalized // only batch finalized status is used. | ||
) | ||
|
||
// BatchStatusType represents the type of batch status. | ||
type BatchStatusType int | ||
|
||
// Constants for BatchStatusType. | ||
const ( | ||
BatchStatusTypeUnknown BatchStatusType = iota | ||
BatchStatusTypeCommitted | ||
BatchStatusTypeReverted | ||
BatchStatusTypeFinalized | ||
) | ||
|
||
// BatchEvent represents a batch event. | ||
type BatchEvent struct { | ||
db *gorm.DB `gorm:"column:-"` | ||
|
||
ID uint64 `json:"id" gorm:"column:id;primary_key"` | ||
BatchStatus int `json:"batch_status" gorm:"column:batch_status"` | ||
BatchIndex uint64 `json:"batch_index" gorm:"column:batch_index"` | ||
BatchHash string `json:"batch_hash" gorm:"column:batch_hash"` | ||
StartBlockNumber uint64 `json:"start_block_number" gorm:"column:start_block_number"` | ||
EndBlockNumber uint64 `json:"end_block_number" gorm:"column:end_block_number"` | ||
CreatedAt time.Time `json:"created_at" gorm:"column:created_at"` | ||
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"` | ||
DeletedAt *time.Time `json:"deleted_at" gorm:"column:deleted_at"` | ||
} | ||
|
||
// TableName returns the table name for the BatchEvent model. | ||
func (*BatchEvent) TableName() string { | ||
return "batch_event" | ||
} | ||
|
||
// NewBatchEvent returns a new instance of BatchEvent. | ||
func NewBatchEvent(db *gorm.DB) *BatchEvent { | ||
return &BatchEvent{db: db} | ||
} | ||
|
||
// GetBatchByBlock retrieves the batch that contains the given block number. | ||
func (c *BatchEvent) GetBatchByBlock(ctx context.Context, blockNumber uint64) (*BatchEvent, error) { | ||
var batchEvent BatchEvent | ||
db := c.db.WithContext(ctx) | ||
db = db.Model(&BatchEvent{}) | ||
db = db.Where("start_block_number <= ? AND end_block_number >= ?", blockNumber, blockNumber) | ||
if err := db.First(&batchEvent).Error; err != nil { | ||
if err == gorm.ErrRecordNotFound { | ||
return nil, nil | ||
} | ||
return nil, fmt.Errorf("failed to get batch by block number, block number: %d, error: %w", blockNumber, err) | ||
} | ||
return &batchEvent, nil | ||
} | ||
|
||
// InsertOrUpdateBatchEvents inserts a new batch event or updates an existing one based on the BatchStatusType. | ||
func (c *BatchEvent) InsertOrUpdateBatchEvents(ctx context.Context, l1BatchEvents []*BatchEvent, dbTX ...*gorm.DB) error { | ||
originalDB := c.db | ||
if len(dbTX) > 0 && dbTX[0] != nil { | ||
originalDB = dbTX[0] | ||
} | ||
originalDB = originalDB.WithContext(ctx) | ||
originalDB = originalDB.Model(&CrossMessage{}) | ||
|
||
for _, l1BatchEvent := range l1BatchEvents { | ||
db := originalDB | ||
db = db.Model(BatchEvent{}) | ||
updateFields := make(map[string]interface{}) | ||
switch BatchStatusType(l1BatchEvent.BatchStatus) { | ||
case BatchStatusTypeCommitted: | ||
if err := db.Create(l1BatchEvent).Error; err != nil { | ||
return fmt.Errorf("failed to insert batch event, batch: %+v, error: %w", l1BatchEvent, err) | ||
} | ||
case BatchStatusTypeFinalized: | ||
updateFields["batch_status"] = BatchStatusTypeFinalized | ||
if err := db.Updates(updateFields).Error; err != nil { | ||
return fmt.Errorf("failed to update batch event, batch: %+v, error: %w", l1BatchEvent, err) | ||
} | ||
case BatchStatusTypeReverted: | ||
updateFields["batch_status"] = BatchStatusTypeReverted | ||
if err := db.Updates(updateFields).Error; err != nil { | ||
return fmt.Errorf("failed to update batch event, batch: %+v, error: %w", l1BatchEvent, err) | ||
} | ||
// Soft delete the batch event | ||
if err := db.Delete(l1BatchEvent).Error; err != nil { | ||
return fmt.Errorf("failed to soft delete batch event, batch: %+v, error: %w", l1BatchEvent, err) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.