-
Notifications
You must be signed in to change notification settings - Fork 5
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
Ler2block #61
Changes from 1 commit
af021ee
f80e900
cace932
8b90e2e
de8d8a7
b4ad84e
f3d4af6
9ea9b29
eac43c8
78a2aca
07d6571
d18914a
37cf940
3083e5c
10b17f9
6e07803
a90926c
30a7891
d0b035d
c245cfd
9842ad5
a212233
d17db59
f05383e
3d5a2ab
f541a20
76b5b44
9b39da5
4a9d124
3f28c15
1f2b35b
12904e5
34ad89d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -72,10 +73,11 @@ func (t *AppendOnlyTree) AddLeaf(tx *sql.Tx, blockNum, blockPosition uint64, lea | |
return err | ||
} | ||
t.lastIndex++ | ||
tx.AddRollbackCallback(func() { t.lastIndex-- }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess you can't execute in parallel this code, isn't? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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 { | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 😅