Skip to content

Commit

Permalink
wrap up historydb tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Intizar-T committed Nov 15, 2024
1 parent 5706d12 commit 3f8f1d8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 65 deletions.
14 changes: 7 additions & 7 deletions sequencer/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ tasks:
test-historydb:
dotenv: [".env"]
cmds:
- go test ./database/historydb -v
- go test ./database/historydb -v -count=1

test-til:
dotenv: [".env"]
cmds:
- go test ./test/til -v
- go test ./test/til -v -count=1

test-synchronizer:
dotenv: [".env"]
cmds:
- go test ./synchronizer -v
- go test ./synchronizer -v -count=1

test-sequencer:
deps:
- test-historydb
- test-til
- test-synchronizer
cmds:
- task: test-synchronizer
- task: test-historydb
- task: test-til
88 changes: 34 additions & 54 deletions sequencer/database/historydb/historydb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"tokamak-sybil-resistance/test/til"

ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -35,15 +34,11 @@ var Block0 common.Block = common.Block{
Timestamp: time.Date(2015, time.July, 30, 3, 26, 13, 0, time.UTC), // 2015-07-30 03:26:13
}

// WipeDB redo all the migrations of the SQL DB (HistoryDB and L2DB),
// efectively recreating the original state
func WipeDB(db *sqlx.DB) {
if err := database.MigrationsDown(db.DB, 0); err != nil {
panic(err)
}
if err := database.MigrationsUp(db.DB); err != nil {
panic(err)
}
func setup(db *HistoryDB, t *testing.T) {
test.MigrationsUpTest(db.DB())
t.Cleanup(func() {
test.MigrationsDownTest(db.DB())
})
}

func TestMain(m *testing.M) {
Expand All @@ -56,8 +51,7 @@ func TestMain(m *testing.M) {
apiConnCon := database.NewAPIConnectionController(1, time.Second)
historyDBWithACC = NewHistoryDB(db, db, apiConnCon)

// Reset DB
WipeDB(historyDB.DB())
test.MigrationsDownTest(historyDB.DB())

// Run tests
result := m.Run()
Expand All @@ -69,6 +63,8 @@ func TestMain(m *testing.M) {
}

func TestBlocks(t *testing.T) {
setup(historyDB, t)

var fromBlock, toBlock int64
fromBlock = 0
toBlock = 7
Expand Down Expand Up @@ -122,12 +118,11 @@ func TestBlocks(t *testing.T) {
lastBlock, err := historyDB.GetLastBlock()
assert.NoError(t, err)
assertEqualBlock(t, &blocks[len(blocks)-1].Block, lastBlock)

// Reset DB
WipeDB(historyDB.DB())
}

func TestBatches(t *testing.T) {
setup(historyDB, t)

// Generate batches using til (and blocks for foreign key)
set := `
Type: Blockchain
Expand Down Expand Up @@ -205,12 +200,11 @@ func TestBatches(t *testing.T) {
assert.Equal(t, &batches[0], fetchedBatch)
_, err = historyDB.GetBatch(common.BatchNum(len(batches) + 1))
assert.Equal(t, sql.ErrNoRows, common.Unwrap(err))

// Reset DB
WipeDB(historyDB.DB())
}

func TestAccounts(t *testing.T) {
setup(historyDB, t)

const fromBlock int64 = 1
const toBlock int64 = 5

Expand Down Expand Up @@ -255,12 +249,11 @@ func TestAccounts(t *testing.T) {
fetchedAccBalances, err := historyDB.GetAllAccountUpdates()
require.NoError(t, err)
assert.Equal(t, accUpdates, fetchedAccBalances)

// Reset DB
test.WipeDB(historyDB.DB())
}

func TestTxs(t *testing.T) {
setup(historyDB, t)

set := `
Type: Blockchain
Expand Down Expand Up @@ -484,12 +477,11 @@ func TestTxs(t *testing.T) {

// Amount
assert.Equal(t, big.NewInt(10), dbL2Txs[2].Amount)

// Reset DB
test.WipeDB(historyDB.DB())
}

func TestExitTree(t *testing.T) {
setup(historyDB, t)

nBatches := 17

blocks := setTestBlocks(1, 10)
Expand All @@ -504,12 +496,11 @@ func TestExitTree(t *testing.T) {
exitTree := test.GenExitTree(nBatches, batches, accs, blocks)
err = historyDB.AddExitTree(exitTree)
assert.NoError(t, err)

// Reset DB
test.WipeDB(historyDB.DB())
}

func TestGetUnforgedL1UserTxs(t *testing.T) {
setup(historyDB, t)

set := `
Type: Blockchain
Expand Down Expand Up @@ -569,9 +560,6 @@ func TestGetUnforgedL1UserTxs(t *testing.T) {
l1UserTxs, err = historyDB.GetUnforgedL1UserTxs(3)
require.NoError(t, err)
assert.Equal(t, 0, len(l1UserTxs))

// Reset DB
test.WipeDB(historyDB.DB())
}

func exampleInitSCVars() *common.RollupVariables { // *common.AuctionVariables,
Expand Down Expand Up @@ -613,6 +601,8 @@ func exampleInitSCVars() *common.RollupVariables { // *common.AuctionVariables,
}

func TestSetInitialSCVars(t *testing.T) {
setup(historyDB, t)

_, err := historyDB.GetSCVars()
assert.Equal(t, sql.ErrNoRows, err)
rollup := exampleInitSCVars()
Expand All @@ -621,12 +611,11 @@ func TestSetInitialSCVars(t *testing.T) {
dbRollup, err := historyDB.GetSCVars()
require.NoError(t, err)
require.Equal(t, rollup, dbRollup)

// Reset DB
test.WipeDB(historyDB.DB())
}

func TestSetExtraInfoForgedL1UserTxs(t *testing.T) {
setup(historyDB, t)

set := `
Type: Blockchain
Expand Down Expand Up @@ -697,12 +686,11 @@ func TestSetExtraInfoForgedL1UserTxs(t *testing.T) {
assert.Equal(t, big.NewInt(0), tx.EffectiveAmount)
}
}

// Reset DB
test.WipeDB(historyDB.DB())
}

func TestUpdateExitTree(t *testing.T) {
setup(historyDB, t)

set := `
Type: Blockchain
Expand Down Expand Up @@ -801,12 +789,11 @@ func TestUpdateExitTree(t *testing.T) {
// dbExitsByIdx[dbExit.AccountIdx] = dbExit
// }
// require.Equal(t, block.Block.Num, dbExitsByIdx[257].DelayedWithdrawn)

// Reset DB
test.WipeDB(historyDB.DB())
}

func TestAddBucketUpdates(t *testing.T) {
setup(historyDB, t)

const fromBlock int64 = 1
const toBlock int64 = 5 + 1
setTestBlocks(fromBlock, toBlock)
Expand All @@ -830,28 +817,25 @@ func TestAddBucketUpdates(t *testing.T) {
dbBucketUpdates, err := historyDB.GetAllBucketUpdates()
require.NoError(t, err)
assert.Equal(t, bucketUpdates, dbBucketUpdates)

// Reset DB
test.WipeDB(historyDB.DB())
}

func TestGetLastL1TxsNum(t *testing.T) {
setup(historyDB, t)

_, err := historyDB.GetLastL1TxsNum()
assert.NoError(t, err)

// Reset DB
test.WipeDB(historyDB.DB())
}

func TestGetLastTxsPosition(t *testing.T) {
setup(historyDB, t)

_, err := historyDB.GetLastTxsPosition(0)
assert.Equal(t, sql.ErrNoRows.Error(), err.Error())

// Reset DB
test.WipeDB(historyDB.DB())
}

func TestGetFirstBatchBlockNumBySlot(t *testing.T) {
setup(historyDB, t)

set := `
Type: Blockchain
Expand Down Expand Up @@ -911,12 +895,11 @@ func TestGetFirstBatchBlockNumBySlot(t *testing.T) {
bn2, err := historyDB.GetFirstBatchBlockNumBySlot(2)
require.NoError(t, err)
assert.Equal(t, int64(10), bn2)

// Reset DB
test.WipeDB(historyDB.DB())
}

func TestTxItemID(t *testing.T) {
setup(historyDB, t)

testUsersLen := 10
var set []til.Instruction
for user := 0; user < testUsersLen; user++ {
Expand Down Expand Up @@ -988,9 +971,6 @@ func TestTxItemID(t *testing.T) {
assert.Equal(t, position, tx.Position)
position++
}

// Reset DB
test.WipeDB(historyDB.DB())
}

func assertEqualBlock(t *testing.T, expected *common.Block, actual *common.Block) {
Expand Down
4 changes: 0 additions & 4 deletions sequencer/synchronizer/synchronizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,6 @@ var chainID uint16 = 0
var deleteme = []string{}

func TestMain(m *testing.M) {
// TODO: implement WipeDB

exitVal := m.Run()
for _, dir := range deleteme {
if err := os.RemoveAll(dir); err != nil {
Expand All @@ -265,8 +263,6 @@ func newTestModules(t *testing.T) (*statedb.StateDB, *historydb.HistoryDB, *l2db
db, err := dbUtils.InitTestSQLDB()
require.NoError(t, err)
historyDB := historydb.NewHistoryDB(db, db, nil)
// Clear DB
test.WipeDB(historyDB.DB())

// Init L2 DB
l2DB := l2db.NewL2DB(db, db, 10, 100, 0.0, 1000.0, 24*time.Hour, nil)
Expand Down
12 changes: 12 additions & 0 deletions sequencer/test/dbUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ func WipeDB(db *sqlx.DB) {
panic(err)
}
}

func MigrationsDownTest(db *sqlx.DB) {
if err := dbUtils.MigrationsDown(db.DB, 0); err != nil {
panic(err)
}
}

func MigrationsUpTest(db *sqlx.DB) {
if err := dbUtils.MigrationsUp(db.DB); err != nil {
panic(err)
}
}

0 comments on commit 3f8f1d8

Please sign in to comment.