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

Use context logger #590

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# vendor/
/dist/
.vscode
.idea

config/config.mainnet.toml
config/config.testnet.toml
3 changes: 2 additions & 1 deletion bridgectrl/bridgectrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ func (bt *BridgeController) GetExitRoot(ctx context.Context, networkID int, dbTx
}

func (bt *BridgeController) AddRollupExitLeaf(ctx context.Context, rollupLeaf etherman.RollupExitLeaf, dbTx pgx.Tx) error {
logger := log.LoggerFromCtx(ctx)
err := bt.rollupsTree.addRollupExitLeaf(ctx, rollupLeaf, dbTx)
if err != nil {
log.Error("error adding rollupleaf. Error: ", err)
logger.Error("error adding rollupleaf. Error: ", err)
return err
}
return nil
Expand Down
15 changes: 9 additions & 6 deletions bridgectrl/merkletree.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func buildIntermediate(leaves [][KeyLen]byte) ([][][]byte, [][32]byte) {
}

func (mt *MerkleTree) updateLeaf(ctx context.Context, depositID uint64, leaves [][KeyLen]byte, dbTx pgx.Tx) error {
logger := log.LoggerFromCtx(ctx)
var (
nodes [][][][]byte
ns [][][]byte
Expand All @@ -206,7 +207,7 @@ func (mt *MerkleTree) updateLeaf(ctx context.Context, depositID uint64, leaves [
if len(ns) != 1 {
return fmt.Errorf("error: more than one root detected: %+v", nodes)
}
log.Debug("Root calculated: ", common.Bytes2Hex(ns[0][0]))
logger.Debug("Root calculated: ", common.Bytes2Hex(ns[0][0]))
err := mt.store.SetRoot(ctx, ns[0][0], depositID, mt.network, dbTx)
if err != nil {
return err
Expand Down Expand Up @@ -260,7 +261,8 @@ func (mt *MerkleTree) getLeaves(ctx context.Context, dbTx pgx.Tx) ([][KeyLen]byt
return result, nil
}

func (mt *MerkleTree) buildMTRoot(leaves [][KeyLen]byte) (common.Hash, error) {
func (mt *MerkleTree) buildMTRoot(ctx context.Context, leaves [][KeyLen]byte) (common.Hash, error) {
logger := log.LoggerFromCtx(ctx)
var (
nodes [][][][]byte
ns [][][]byte
Expand All @@ -279,13 +281,13 @@ func (mt *MerkleTree) buildMTRoot(leaves [][KeyLen]byte) (common.Hash, error) {
if len(ns) != 1 {
return common.Hash{}, fmt.Errorf("error: more than one root detected: %+v", nodes)
}
log.Debug("Root calculated: ", common.Bytes2Hex(ns[0][0]))
logger.Debug("Root calculated: ", common.Bytes2Hex(ns[0][0]))

return common.BytesToHash(ns[0][0]), nil
}

func (mt MerkleTree) storeLeaves(ctx context.Context, leaves [][KeyLen]byte, blockID uint64, dbTx pgx.Tx) error {
root, err := mt.buildMTRoot(leaves)
root, err := mt.buildMTRoot(ctx, leaves)
if err != nil {
return err
}
Expand All @@ -311,9 +313,10 @@ func (mt MerkleTree) storeLeaves(ctx context.Context, leaves [][KeyLen]byte, blo
// }

func (mt MerkleTree) addRollupExitLeaf(ctx context.Context, rollupLeaf etherman.RollupExitLeaf, dbTx pgx.Tx) error {
logger := log.LoggerFromCtx(ctx)
storedRollupLeaves, err := mt.store.GetLatestRollupExitLeaves(ctx, dbTx)
if err != nil {
log.Error("error getting latest rollup exit leaves. Error: ", err)
logger.Error("error getting latest rollup exit leaves. Error: ", err)
return err
}
// If rollupLeaf.RollupId is lower or equal than len(storedRollupLeaves), we can add it in the proper position of the array
Expand Down Expand Up @@ -346,7 +349,7 @@ func (mt MerkleTree) addRollupExitLeaf(ctx context.Context, rollupLeaf etherman.
}
err = mt.storeLeaves(ctx, leaves, rollupLeaf.BlockID, dbTx)
if err != nil {
log.Error("error storing leaves. Error: ", err)
logger.Error("error storing leaves. Error: ", err)
return err
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions bridgectrl/merkletree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,15 @@ func TestBuildMTRootAndStore(t *testing.T) {
}

if len(leaves) != 0 {
root, err := mt.buildMTRoot(leaves)
root, err := mt.buildMTRoot(ctx, leaves)
require.NoError(t, err)
require.Equal(t, testVector.CurrentRoot, root.String())
}

var res [KeyLen]byte
copy(res[:], common.Hex2Bytes(testVector.NewLeaf.CurrentHash[2:]))
leaves = append(leaves, res)
newRoot, err := mt.buildMTRoot(leaves)
newRoot, err := mt.buildMTRoot(ctx, leaves)
require.NoError(t, err)
require.Equal(t, testVector.NewRoot, newRoot.String())

Expand Down Expand Up @@ -452,7 +452,7 @@ func TestPerformanceComputeRoot(t *testing.T) {
log.Debug("End creating leaves: ", time.Now().Unix()-initTime)
initTime = time.Now().Unix()
log.Debug("Init computing root: ", initTime)
_, err = mt.buildMTRoot(leaves)
_, err = mt.buildMTRoot(ctx, leaves)
require.NoError(t, err)
log.Debug("End creating leaves: ", time.Now().Unix()-initTime)
}
Loading