-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(blobstorage): isolating tables for no blob data duplication (#16702
) Co-authored-by: jeff <[email protected]>
- Loading branch information
1 parent
e25ad9f
commit 55426ef
Showing
8 changed files
with
340 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,19 @@ | ||
package blobstorage | ||
|
||
type BlobHash struct { | ||
BlobHash string | ||
KzgCommitment string | ||
BlobData string | ||
BlockID uint64 | ||
EmittedBlockID uint64 | ||
BlobHash string | ||
KzgCommitment string | ||
BlobData string | ||
} | ||
|
||
type SaveBlobHashOpts struct { | ||
BlobHash string | ||
KzgCommitment string | ||
BlobData string | ||
BlockID uint64 | ||
EmittedBlockID uint64 | ||
BlobHash string | ||
KzgCommitment string | ||
BlobData string | ||
} | ||
|
||
type BlobHashRepository interface { | ||
Save(opts SaveBlobHashOpts) error | ||
FirstByBlobHash(blobHash string) (*BlobHash, error) | ||
FindLatestBlockID() (uint64, error) | ||
DeleteAllAfterBlockID(blockID uint64) error | ||
} |
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,19 @@ | ||
package blobstorage | ||
|
||
type BlockMeta struct { | ||
BlobHash string | ||
BlockID uint64 | ||
EmittedBlockID uint64 | ||
} | ||
|
||
type SaveBlockMetaOpts struct { | ||
BlobHash string | ||
BlockID uint64 | ||
EmittedBlockID uint64 | ||
} | ||
|
||
type BlockMetaRepository interface { | ||
Save(opts SaveBlockMetaOpts) error | ||
FindLatestBlockID() (uint64, error) | ||
DeleteAllAfterBlockID(blockID uint64) error | ||
} |
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
27 changes: 27 additions & 0 deletions
27
packages/blobstorage/migrations/16670_create_blocks_meta_table.sql
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,27 @@ | ||
-- +goose Up | ||
-- create blocks_meta table | ||
CREATE TABLE IF NOT EXISTS blocks_meta ( | ||
block_id BIGINT NOT NULL PRIMARY KEY, | ||
blob_hash VARCHAR(100) NOT NULL, | ||
emitted_block_id BIGINT NOT NULL, | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | ||
FOREIGN KEY (blob_hash) REFERENCES blob_hashes(blob_hash) | ||
); | ||
|
||
-- migrate data into blocks_meta | ||
INSERT INTO blocks_meta (block_id, blob_hash, emitted_block_id, created_at, updated_at) | ||
SELECT block_id, blob_hash, emitted_block_id, created_at, updated_at | ||
FROM blob_hashes; | ||
|
||
-- update indexes | ||
DROP INDEX block_id_index on blob_hashes; | ||
ALTER TABLE blocks_meta ADD INDEX `block_id_index` (`block_id`); | ||
|
||
-- +goose Down | ||
-- reverse indexes | ||
DROP INDEX block_id_index on blocks_meta; | ||
ALTER TABLE blob_hashes ADD INDEX `block_id_index` (`block_id`); | ||
|
||
-- drop blocks_meta table | ||
DROP TABLE IF EXISTS blocks_meta; |
68 changes: 68 additions & 0 deletions
68
packages/blobstorage/migrations/16671_create_unique_blob_hashes_table.sql
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,68 @@ | ||
-- +goose Up | ||
-- create a blob_hashes_temp with constraint blob_hash | ||
CREATE TABLE IF NOT EXISTS blob_hashes_temp ( | ||
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, | ||
blob_hash VARCHAR(100) NOT NULL UNIQUE, | ||
kzg_commitment LONGTEXT NOT NULL, | ||
blob_data LONGTEXT NOT NULL, | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | ||
); | ||
|
||
-- migrate data to blob_hashes_temp | ||
INSERT IGNORE INTO blob_hashes_temp (id, blob_hash, kzg_commitment, blob_data, created_at, updated_at) | ||
SELECT id, blob_hash, kzg_commitment, blob_data, created_at, updated_at | ||
FROM blob_hashes; | ||
|
||
-- Update blob_hash references and indexes | ||
UPDATE blocks_meta bm | ||
JOIN blob_hashes_temp bh ON bm.blob_hash = bh.blob_hash | ||
SET bm.blob_hash = bh.blob_hash; | ||
|
||
ALTER TABLE blocks_meta DROP FOREIGN KEY blocks_meta_ibfk_1; | ||
|
||
ALTER TABLE `blob_hashes_temp` ADD INDEX `blob_hash_index` (`blob_hash`); | ||
|
||
-- make blob_hashes_temp the new blob_hashes | ||
DROP TABLE IF EXISTS blob_hashes; | ||
ALTER TABLE blob_hashes_temp RENAME TO blob_hashes; | ||
|
||
-- +goose Down | ||
-- create a blob_hashes_temp as original | ||
CREATE TABLE IF NOT EXISTS blob_hashes_temp ( | ||
id int NOT NULL PRIMARY KEY AUTO_INCREMENT, | ||
block_id BIGINT NOT NULL, | ||
emitted_block_id BIGINT NOT NULL, | ||
blob_hash VARCHAR(100) NOT NULL, | ||
kzg_commitment LONGTEXT NOT NULL, | ||
blob_data LONGTEXT NOT NULL, | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP , | ||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP | ||
); | ||
|
||
-- migrate data to blob_hashes_temp | ||
INSERT INTO blob_hashes_temp (block_id, emitted_block_id, blob_hash, kzg_commitment, blob_data, created_at, updated_at) | ||
SELECT | ||
bm.block_id, | ||
bm.emitted_block_id, | ||
bh.blob_hash, | ||
bh.kzg_commitment, | ||
bh.blob_data, | ||
bh.created_at, | ||
bh.updated_at | ||
FROM | ||
blocks_meta bm | ||
JOIN | ||
blob_hashes bh ON bm.blob_hash = bh.blob_hash; | ||
|
||
-- Update blob_hash references and indexes | ||
UPDATE blocks_meta bm | ||
JOIN blob_hashes_temp bh ON bm.blob_hash = bh.blob_hash | ||
SET bm.blob_hash = bh.blob_hash; | ||
|
||
-- create blob_hash_index | ||
ALTER TABLE `blob_hashes_temp` ADD INDEX `blob_hash_index` (`blob_hash`); | ||
|
||
-- make blob_hashes_temp the new blob_hashes | ||
DROP TABLE IF EXISTS blob_hashes; | ||
ALTER TABLE blob_hashes_temp RENAME TO blob_hashes; |
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,63 @@ | ||
package repo | ||
|
||
import ( | ||
blobstorage "github.com/taikoxyz/taiko-mono/packages/blobstorage" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type BlockMetaRepository struct { | ||
db DB | ||
} | ||
|
||
func NewBlockMetaRepository(db DB) (*BlockMetaRepository, error) { | ||
if db == nil { | ||
return nil, ErrNoDB | ||
} | ||
|
||
return &BlockMetaRepository{ | ||
db: db, | ||
}, nil | ||
} | ||
|
||
func (r *BlockMetaRepository) startQuery() *gorm.DB { | ||
return r.db.GormDB().Table("blocks_meta") | ||
} | ||
|
||
func (r *BlockMetaRepository) Save(opts blobstorage.SaveBlockMetaOpts) error { | ||
b := &blobstorage.BlockMeta{ | ||
BlobHash: opts.BlobHash, | ||
BlockID: opts.BlockID, | ||
EmittedBlockID: opts.EmittedBlockID, | ||
} | ||
if err := r.startQuery().Create(b).Error; err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *BlockMetaRepository) FindLatestBlockID() (uint64, error) { | ||
q := `SELECT COALESCE(MAX(emitted_block_id), 0) | ||
FROM blocks_meta` | ||
|
||
var b uint64 | ||
|
||
if err := r.startQuery().Raw(q).Scan(&b).Error; err != nil { | ||
return 0, err | ||
} | ||
|
||
return b, nil | ||
} | ||
|
||
// DeleteAllAfterBlockID is used when a reorg is detected | ||
func (r *BlockMetaRepository) DeleteAllAfterBlockID(blockID uint64) error { | ||
query := ` | ||
DELETE FROM blob_hashes | ||
WHERE block_id >= ?` | ||
|
||
if err := r.startQuery().Exec(query, blockID).Error; err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.