Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ler2block #61

Merged
merged 33 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
af021ee
wip
arnaubennassar Aug 29, 2024
f80e900
wip
arnaubennassar Aug 29, 2024
cace932
WIP
arnaubennassar Aug 29, 2024
8b90e2e
decoding direct and indeirecr assets and messages works
arnaubennassar Aug 30, 2024
de8d8a7
connect everything
arnaubennassar Aug 30, 2024
b4ad84e
fix building contract scripts
arnaubennassar Aug 30, 2024
f3d4af6
fix building contract scripts
arnaubennassar Aug 30, 2024
9ea9b29
wip
arnaubennassar Aug 30, 2024
eac43c8
WIP
arnaubennassar Sep 2, 2024
78a2aca
tree migrated to SQLite
arnaubennassar Sep 3, 2024
07d6571
wip
arnaubennassar Sep 3, 2024
d18914a
wip
arnaubennassar Sep 4, 2024
37cf940
bridgesync working with sqlite
arnaubennassar Sep 4, 2024
3083e5c
pass tests
arnaubennassar Sep 4, 2024
10b17f9
minor cleaning
arnaubennassar Sep 4, 2024
6e07803
add GetBlockByLER func
arnaubennassar Sep 4, 2024
a90926c
handle err not found
arnaubennassar Sep 4, 2024
30a7891
merge develop
arnaubennassar Sep 5, 2024
d0b035d
merge develop
arnaubennassar Sep 5, 2024
c245cfd
use memory for sqlite on the tests
arnaubennassar Sep 5, 2024
9842ad5
increase timestamp to pass UT
arnaubennassar Sep 5, 2024
a212233
review
arnaubennassar Sep 9, 2024
d17db59
add callbacks on db tx
arnaubennassar Sep 12, 2024
f05383e
fix conflicts
arnaubennassar Sep 12, 2024
3d5a2ab
lint
arnaubennassar Sep 12, 2024
f541a20
fix compilation
arnaubennassar Sep 12, 2024
76b5b44
fix linter II
arnaubennassar Sep 12, 2024
9b39da5
fix linter III
arnaubennassar Sep 12, 2024
4a9d124
fix linter
arnaubennassar Sep 12, 2024
3f28c15
increase linter TO
arnaubennassar Sep 12, 2024
1f2b35b
fix UTs and lint
arnaubennassar Sep 12, 2024
12904e5
increase linter TO
arnaubennassar Sep 12, 2024
34ad89d
add PR requests
arnaubennassar Sep 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bridgesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (p *processor) getLastProcessedBlockWithTx(tx db.DBer) (uint64, error) {
// Reorg triggers a purge and reset process on the processor to leaf it on a state
// as if the last block processed was firstReorgedBlock-1
func (p *processor) Reorg(ctx context.Context, firstReorgedBlock uint64) error {
tx, err := p.db.BeginTx(ctx, nil)
tx, err := db.NewTx(ctx, p.db)
if err != nil {
return err
}
Expand Down Expand Up @@ -222,7 +222,7 @@ func (p *processor) Reorg(ctx context.Context, firstReorgedBlock uint64) error {
// ProcessBlock process the events of the block to build the exit tree
// and updates the last processed block (can be called without events for that purpose)
func (p *processor) ProcessBlock(ctx context.Context, block sync.Block) error {
tx, err := p.db.BeginTx(ctx, nil)
tx, err := db.NewTx(ctx, p.db)
if err != nil {
return err
}
Expand Down
49 changes: 49 additions & 0 deletions db/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package db

import (
"context"
"database/sql"
)

type Tx struct {
*sql.Tx
rollbackCallbacks []func()
commitCallbacks []func()
}

func NewTx(ctx context.Context, db *sql.DB) (*Tx, error) {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
return &Tx{
Tx: tx,
}, nil
}

func (s *Tx) AddRollbackCallback(cb func()) {
s.rollbackCallbacks = append(s.rollbackCallbacks, cb)
}
func (s *Tx) AddCommitCallback(cb func()) {
s.commitCallbacks = append(s.commitCallbacks, cb)
}

func (s *Tx) Commit() error {
if err := s.Tx.Commit(); err != nil {
return err
}
for _, cb := range s.commitCallbacks {
cb()
}
return nil
}

func (s *Tx) Rollback() error {
if err := s.Tx.Rollback(); err != nil {
return err
}
for _, cb := range s.rollbackCallbacks {
cb()
}
return nil
}
4 changes: 2 additions & 2 deletions l1infotreesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (p *processor) getLastProcessedBlockWithTx(tx db.DBer) (uint64, error) {
// Reorg triggers a purge and reset process on the processor to leaf it on a state
// as if the last block processed was firstReorgedBlock-1
func (p *processor) Reorg(ctx context.Context, firstReorgedBlock uint64) error {
tx, err := p.db.BeginTx(ctx, nil)
tx, err := db.NewTx(ctx, p.db)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sugest that newTx belong to DB object

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but db is *sql.DB, I can't add this method there 😅

if err != nil {
return err
}
Expand Down Expand Up @@ -218,7 +218,7 @@ func (p *processor) Reorg(ctx context.Context, firstReorgedBlock uint64) error {
// ProcessBlock process the events of the block to build the rollup exit tree and the l1 info tree
// and updates the last processed block (can be called without events for that purpose)
func (p *processor) ProcessBlock(ctx context.Context, b sync.Block) error {
tx, err := p.db.BeginTx(ctx, nil)
tx, err := db.NewTx(ctx, p.db)
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions tree/appendonlytree.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"fmt"

"github.com/0xPolygon/cdk/db"
"github.com/0xPolygon/cdk/tree/types"
"github.com/ethereum/go-ethereum/common"
)
Expand All @@ -26,7 +27,7 @@ func NewAppendOnlyTree(db *sql.DB, dbPrefix string) *AppendOnlyTree {
}
}

func (t *AppendOnlyTree) AddLeaf(tx *sql.Tx, blockNum, blockPosition uint64, leaf types.Leaf) error {
func (t *AppendOnlyTree) AddLeaf(tx *db.Tx, blockNum, blockPosition uint64, leaf types.Leaf) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to use a interface instead real object, It allows to unittest more easy and have multiples implementation (as the case of zkevm-sync-l1 that have one implementation of posgtres and another for sqlite)

if int64(leaf.Index) != t.lastIndex+1 {
// rebuild cache
if err := t.initCache(tx); err != nil {
Expand Down Expand Up @@ -72,10 +73,11 @@ func (t *AppendOnlyTree) AddLeaf(tx *sql.Tx, blockNum, blockPosition uint64, lea
return err
}
t.lastIndex++
tx.AddRollbackCallback(func() { t.lastIndex-- })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you can't execute in parallel this code, isn't?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, there is a single writer, and it's sequential (it flows form ProcessBlock of each syncer)

return nil
}

func (t *AppendOnlyTree) initCache(tx *sql.Tx) error {
func (t *AppendOnlyTree) initCache(tx *db.Tx) error {
siblings := [types.DefaultHeight]common.Hash{}
lastRoot, err := t.getLastRootWithTx(tx)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func generateZeroHashes(height uint8) []common.Hash {
return zeroHashes
}

func (t *Tree) storeNodes(tx db.DBer, nodes []types.TreeNode) error {
func (t *Tree) storeNodes(tx *db.Tx, nodes []types.TreeNode) error {
for _, node := range nodes {
if err := meddler.Insert(tx, t.rhtTable, &node); err != nil {
if sqliteErr, ok := db.SQLiteErr(err); ok {
Expand All @@ -167,7 +167,7 @@ func (t *Tree) storeNodes(tx db.DBer, nodes []types.TreeNode) error {
return nil
}

func (t *Tree) storeRoot(tx db.DBer, root types.Root) error {
func (t *Tree) storeRoot(tx *db.Tx, root types.Root) error {
return meddler.Insert(tx, t.rootTable, &root)
}

Expand Down Expand Up @@ -241,7 +241,7 @@ func (t *Tree) GetLeaf(ctx context.Context, index uint32, root common.Hash) (com
}

// Reorg deletes all the data relevant from firstReorgedBlock (includded) and onwards
func (t *Tree) Reorg(tx db.DBer, firstReorgedBlock uint64) error {
func (t *Tree) Reorg(tx *db.Tx, firstReorgedBlock uint64) error {
_, err := tx.Exec(
fmt.Sprintf(`DELETE FROM %s WHERE block_num >= $1`, t.rootTable),
firstReorgedBlock,
Expand Down
16 changes: 8 additions & 8 deletions tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ func TestMTAddLeaf(t *testing.T) {
log.Debug("DB created at: ", dbPath)
err := migrations.RunMigrations(dbPath)
require.NoError(t, err)
db, err := db.NewSQLiteDB(dbPath)
treeDB, err := db.NewSQLiteDB(dbPath)
require.NoError(t, err)
_, err = db.Exec(`select * from root`)
_, err = treeDB.Exec(`select * from root`)
require.NoError(t, err)
merkletree := tree.NewAppendOnlyTree(db, "")
merkletree := tree.NewAppendOnlyTree(treeDB, "")

// Add exisiting leaves
tx, err := db.BeginTx(ctx, nil)
tx, err := db.NewTx(ctx, treeDB)
require.NoError(t, err)
for i, leaf := range testVector.ExistingLeaves {
err = merkletree.AddLeaf(tx, uint64(i), 0, types.Leaf{
Expand All @@ -57,7 +57,7 @@ func TestMTAddLeaf(t *testing.T) {
}

// Add new bridge
tx, err = db.BeginTx(ctx, nil)
tx, err = db.NewTx(ctx, treeDB)
require.NoError(t, err)
err = merkletree.AddLeaf(tx, uint64(len(testVector.ExistingLeaves)), 0, types.Leaf{
Index: uint32(len(testVector.ExistingLeaves)),
Expand Down Expand Up @@ -87,11 +87,11 @@ func TestMTGetProof(t *testing.T) {
dbPath := path.Join(t.TempDir(), "file::memory:?cache=shared")
err := migrations.RunMigrations(dbPath)
require.NoError(t, err)
db, err := db.NewSQLiteDB(dbPath)
treeDB, err := db.NewSQLiteDB(dbPath)
require.NoError(t, err)
tre := tree.NewAppendOnlyTree(db, "")
tre := tree.NewAppendOnlyTree(treeDB, "")

tx, err := db.BeginTx(ctx, nil)
tx, err := db.NewTx(ctx, treeDB)
require.NoError(t, err)
for li, leaf := range testVector.Deposits {
err = tre.AddLeaf(tx, uint64(li), 0, types.Leaf{
Expand Down
3 changes: 2 additions & 1 deletion tree/updatabletree.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tree
import (
"database/sql"

"github.com/0xPolygon/cdk/db"
"github.com/0xPolygon/cdk/tree/types"
"github.com/ethereum/go-ethereum/common"
)
Expand All @@ -21,7 +22,7 @@ func NewUpdatableTree(db *sql.DB, dbPrefix string) *UpdatableTree {
return ut
}

func (t *UpdatableTree) UpsertLeaf(tx *sql.Tx, blockNum, blockPosition uint64, leaf types.Leaf) error {
func (t *UpdatableTree) UpsertLeaf(tx *db.Tx, blockNum, blockPosition uint64, leaf types.Leaf) error {
var rootHash common.Hash
root, err := t.getLastRootWithTx(tx)
if err != nil {
Expand Down
Loading