Skip to content

Commit

Permalink
feat(blobstorage): covering tests for BlockMetaRepo, BlobHashRepo and…
Browse files Browse the repository at this point in the history
… DatabaseTransactions
  • Loading branch information
k-kaddal committed Apr 22, 2024
1 parent ac93ace commit c2816e6
Show file tree
Hide file tree
Showing 13 changed files with 674 additions and 16 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/taikoxyz/taiko-mono
go 1.21

require (
github.com/DATA-DOG/go-sqlmock v1.5.2
github.com/buildkite/terminal-to-html/v3 v3.8.0
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/cenkalti/backoff/v4 v4.3.0
Expand Down Expand Up @@ -35,6 +36,7 @@ require (
gopkg.in/yaml.v3 v3.0.1
gorm.io/datatypes v1.2.0
gorm.io/driver/mysql v1.5.6
gorm.io/driver/sqlite v1.4.3
gorm.io/gorm v1.25.9
gotest.tools v2.2.0+incompatible
)
Expand Down Expand Up @@ -129,6 +131,7 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-sqlite3 v1.14.15 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/microcosm-cc/bluemonday v1.0.21 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
Expand Down Expand Up @@ -158,6 +161,7 @@ require (
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.2.1 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ github.com/ClickHouse/ch-go v0.58.2 h1:jSm2szHbT9MCAB1rJ3WuCJqmGLi5UTjlNu+f530UT
github.com/ClickHouse/ch-go v0.58.2/go.mod h1:Ap/0bEmiLa14gYjCiRkYGbXvbe8vwdrfTYWhsuQ99aw=
github.com/ClickHouse/clickhouse-go/v2 v2.17.1 h1:ZCmAYWpu75IyEi7+Yrs/uaAjiCGY5wfW5kXo64exkX4=
github.com/ClickHouse/clickhouse-go/v2 v2.17.1/go.mod h1:rkGTvFDTLqLIm0ma+13xmcCfr/08Gvs7KmFt1tgiWHQ=
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
github.com/DataDog/zstd v1.5.2 h1:vUG4lAyuPCXO0TLbXvPv7EB7cNK1QV/luu55UHLrrn8=
github.com/DataDog/zstd v1.5.2/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
Expand Down Expand Up @@ -339,6 +341,7 @@ github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJS
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 h1:rp+c0RAYOWj8l6qbCUTSiRLG/iKnW3K3/QfPPuSsBt4=
Expand All @@ -354,6 +357,7 @@ github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
github.com/klauspost/compress v1.11.8/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.11.12/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
Expand Down Expand Up @@ -551,6 +555,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down Expand Up @@ -853,6 +859,7 @@ gorm.io/driver/sqlserver v1.4.1 h1:t4r4r6Jam5E6ejqP7N82qAJIJAht27EGT41HyPfXRw0=
gorm.io/driver/sqlserver v1.4.1/go.mod h1:DJ4P+MeZbc5rvY58PnmN1Lnyvb5gw5NPzGshHDnJLig=
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.21.3/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.9 h1:wct0gxZIELDk8+ZqF/MVnHLkA1rvYlBWUMv2EdsK1g8=
gorm.io/gorm v1.25.9/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
Expand Down
3 changes: 0 additions & 3 deletions packages/blobstorage/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ func (i *Indexer) setInitialIndexingBlock(
ctx context.Context,
) error {
// get most recently processed block height from the DB
// latest, err := i.blobHashRepo.FindLatestBlockID()
latest, err := i.repositories.BlockMetaRepo.FindLatestBlockID()
// latest, err := i.blockMetaRepo.FindLatestBlockID()
if err != nil {
return err
}
Expand Down Expand Up @@ -271,7 +269,6 @@ func calculateBlobHash(commitmentStr string) common.Hash {
}

func (i *Indexer) checkReorg(ctx context.Context, event *taikol1.TaikoL1BlockProposed) error {
// n, err := i.blockMetaRepo.FindLatestBlockID()
n, err := i.repositories.BlockMetaRepo.FindLatestBlockID()
if err != nil {
return err
Expand Down
26 changes: 26 additions & 0 deletions packages/blobstorage/pkg/mocks/blob_hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package mocks

import (
"github.com/stretchr/testify/mock"
"github.com/taikoxyz/taiko-mono/packages/blobstorage"
)

type MockBlobHashRepo struct {
mock.Mock
}

func (m *MockBlobHashRepo) FirstByBlobHash(blobHash string) (*blobstorage.BlobHash, error) {
args := m.Called(blobHash)
if item, ok := args.Get(0).(*blobstorage.BlobHash); ok {
return item, args.Error(1)
}
return nil, args.Error(1)
}

func (m *MockBlobHashRepo) Save(opts blobstorage.SaveBlobHashOpts) error {
return m.Called(opts).Error(0)
}

func (m *MockBlobHashRepo) DeleteAllAfterBlockID(blockID uint64) error {
return m.Called(blockID).Error(0)
}
23 changes: 23 additions & 0 deletions packages/blobstorage/pkg/mocks/block_meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package mocks

import (
"github.com/stretchr/testify/mock"
blobstorage "github.com/taikoxyz/taiko-mono/packages/blobstorage"
)

type MockBlockMetaRepo struct {
mock.Mock
}

func (m *MockBlockMetaRepo) Save(opts blobstorage.SaveBlockMetaOpts) error {
return m.Called(opts).Error(0)
}

func (m *MockBlockMetaRepo) FindLatestBlockID() (uint64, error) {
args := m.Called()
return args.Get(0).(uint64), args.Error(1)
}

func (m *MockBlockMetaRepo) DeleteAllAfterBlockID(blockID uint64) error {
return m.Called(blockID).Error(0)
}
62 changes: 62 additions & 0 deletions packages/blobstorage/pkg/mocks/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package mocks

import (
"database/sql"
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/mock"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

type MockDB struct {
mock.Mock
gormDB *gorm.DB
}

func (m *MockDB) DB() (*sql.DB, error) {
args := m.Called()
return args.Get(0).(*sql.DB), args.Error(1)
}

func (m *MockDB) GormDB() *gorm.DB {
args := m.Called()
return args.Get(0).(*gorm.DB)
}

func (db *MockDB) Begin() *gorm.DB {
args := db.Called()
return args.Get(0).(*gorm.DB)
}

func (db *MockDB) Commit() error {
args := db.Called()
return args.Error(0)
}

func (db *MockDB) Rollback() error {
args := db.Called()
return args.Error(0)
}

func NewMockDB(t *testing.T) (*MockDB, sqlmock.Sqlmock, *gorm.DB, func()) {
sqlDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("newMockDB: %v", err)
}

mock.ExpectQuery("select sqlite_version()").
WillReturnRows(sqlmock.NewRows([]string{"version"}).
AddRow("3.31.1"))

gormDB, err := gorm.Open(sqlite.Dialector{Conn: sqlDB}, &gorm.Config{})
if err != nil {
t.Fatalf("newMockDB: %v", err)
}

mockDB := &MockDB{gormDB: gormDB}
closeDB := func() { sqlDB.Close() }

return mockDB, mock, gormDB, closeDB
}
4 changes: 3 additions & 1 deletion packages/blobstorage/pkg/repo/blob_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (r *BlobHashRepository) startQuery() *gorm.DB {
return r.db.GormDB().Table("blob_hashes")
}

/* Save a blob_hash record */
func (r *BlobHashRepository) Save(opts blobstorage.SaveBlobHashOpts) error {
b := &blobstorage.BlobHash{
BlobHash: opts.BlobHash,
Expand All @@ -36,6 +37,7 @@ func (r *BlobHashRepository) Save(opts blobstorage.SaveBlobHashOpts) error {
return nil
}

/* Get a block_hash record by a blobHash */
func (r *BlobHashRepository) FirstByBlobHash(blobHash string) (*blobstorage.BlobHash, error) {
var b blobstorage.BlobHash

Expand All @@ -46,7 +48,7 @@ func (r *BlobHashRepository) FirstByBlobHash(blobHash string) (*blobstorage.Blob
return &b, nil
}

// DeleteAllAfterBlockID is used when a reorg is detected
/* Delete all records from block_hash tables starting from a blockID */
func (r *BlobHashRepository) DeleteAllAfterBlockID(blockID uint64) error {
query := `
DELETE FROM blob_hashes
Expand Down
Loading

0 comments on commit c2816e6

Please sign in to comment.