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 32 commits
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ install-linter: ## Installs the linter

.PHONY: lint
lint: ## Runs the linter
export "GOROOT=$$(go env GOROOT)" && $$(go env GOPATH)/bin/golangci-lint run
export "GOROOT=$$(go env GOROOT)" && $$(go env GOPATH)/bin/golangci-lint run --timeout 5m

$(VENV_PYTHON):
rm -rf $(VENV)
Expand Down
23 changes: 18 additions & 5 deletions bridgesync/bridgesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/0xPolygon/cdk/etherman"
"github.com/0xPolygon/cdk/sync"
tree "github.com/0xPolygon/cdk/tree/types"
"github.com/ethereum/go-ethereum/common"
)

Expand Down Expand Up @@ -98,7 +99,7 @@ func newBridgeSync(
maxRetryAttemptsAfterError int,
syncFullClaims bool,
) (*BridgeSync, error) {
processor, err := newProcessor(ctx, dbPath, l1OrL2ID)
processor, err := newProcessor(dbPath, l1OrL2ID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -159,12 +160,16 @@ func (s *BridgeSync) GetLastProcessedBlock(ctx context.Context) (uint64, error)
return s.processor.GetLastProcessedBlock(ctx)
}

func (s *BridgeSync) GetBridgeIndexByRoot(ctx context.Context, root common.Hash) (uint32, error) {
return s.processor.exitTree.GetIndexByRoot(ctx, root)
func (s *BridgeSync) GetBridgeRootByHash(ctx context.Context, root common.Hash) (tree.Root, error) {
return s.processor.exitTree.GetRootByHash(ctx, root)
}

func (s *BridgeSync) GetClaimsAndBridges(ctx context.Context, fromBlock, toBlock uint64) ([]Event, error) {
return s.processor.GetClaimsAndBridges(ctx, fromBlock, toBlock)
func (s *BridgeSync) GetClaims(ctx context.Context, fromBlock, toBlock uint64) ([]Claim, error) {
return s.processor.GetClaims(ctx, fromBlock, toBlock)
}

func (s *BridgeSync) GetBridges(ctx context.Context, fromBlock, toBlock uint64) ([]Bridge, error) {
return s.processor.GetBridges(ctx, fromBlock, toBlock)
}

// GetProof retrieves the Merkle proof for the given deposit count and exit root.
Expand All @@ -173,3 +178,11 @@ func (s *BridgeSync) GetProof(
) ([32]common.Hash, error) {
return s.processor.exitTree.GetProof(ctx, depositCount, localExitRoot)
}

func (p *processor) GetBlockByLER(ctx context.Context, ler common.Hash) (uint64, error) {
root, err := p.exitTree.GetRootByHash(ctx, ler)
if err != nil {
return 0, err
}
return root.BlockNum, nil
}
2 changes: 2 additions & 0 deletions bridgesync/claimcalldata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type testCase struct {
func TestClaimCalldata(t *testing.T) {
testCases := []testCase{}
// Setup Docker L1
log.Debug("starting docker")
ctx := context.Background()
msg, err := exec.Command("bash", "-l", "-c", "docker compose up -d").CombinedOutput()
require.NoError(t, err, string(msg))
Expand All @@ -36,6 +37,7 @@ func TestClaimCalldata(t *testing.T) {
msg, err = exec.Command("bash", "-l", "-c", "docker compose down").CombinedOutput()
require.NoError(t, err, string(msg))
}()
log.Debug("docker started")
client, err := ethclient.Dial("http://localhost:8545")
require.NoError(t, err)
privateKey, err := crypto.HexToECDSA("ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")
Expand Down
8 changes: 7 additions & 1 deletion bridgesync/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/0xPolygon/cdk-contracts-tooling/contracts/etrog/polygonzkevmbridgev2"
rpcTypes "github.com/0xPolygon/cdk-rpc/types"
"github.com/0xPolygon/cdk/sync"
"github.com/0xPolygon/cdk/tree"
tree "github.com/0xPolygon/cdk/tree/types"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand Down Expand Up @@ -60,6 +60,8 @@ func buildAppender(client EthClienter, bridge common.Address, syncFullClaims boo
)
}
b.Events = append(b.Events, Event{Bridge: &Bridge{
BlockNum: b.Num,
BlockPos: uint64(l.Index),
LeafType: bridge.LeafType,
OriginNetwork: bridge.OriginNetwork,
OriginAddress: bridge.OriginAddress,
Expand All @@ -82,6 +84,8 @@ func buildAppender(client EthClienter, bridge common.Address, syncFullClaims boo
)
}
claim := &Claim{
BlockNum: b.Num,
BlockPos: uint64(l.Index),
GlobalIndex: claimEvent.GlobalIndex,
OriginNetwork: claimEvent.OriginNetwork,
OriginAddress: claimEvent.OriginAddress,
Expand All @@ -106,6 +110,8 @@ func buildAppender(client EthClienter, bridge common.Address, syncFullClaims boo
)
}
claim := &Claim{
BlockNum: b.Num,
BlockPos: uint64(l.Index),
GlobalIndex: big.NewInt(int64(claimEvent.Index)),
OriginNetwork: claimEvent.OriginNetwork,
OriginAddress: claimEvent.OriginAddress,
Expand Down
13 changes: 4 additions & 9 deletions bridgesync/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"math/big"
"path"
"testing"
"time"

Expand Down Expand Up @@ -47,7 +48,7 @@ func newSimulatedClient(t *testing.T, auth *bind.TransactOpts) (

func TestBridgeEventE2E(t *testing.T) {
ctx := context.Background()
dbPathSyncer := t.TempDir()
dbPathSyncer := path.Join(t.TempDir(), "file::memory:?cache=shared")
dbPathReorg := t.TempDir()
privateKey, err := crypto.GenerateKey()
require.NoError(t, err)
Expand All @@ -70,6 +71,7 @@ func TestBridgeEventE2E(t *testing.T) {

for i := 0; i < 100; i++ {
bridge := bridgesync.Bridge{
BlockNum: uint64(2 + i),
Amount: big.NewInt(0),
DepositCount: uint32(i),
DestinationNetwork: 3,
Expand Down Expand Up @@ -116,15 +118,8 @@ func TestBridgeEventE2E(t *testing.T) {
// Get bridges
lastBlock, err := client.Client().BlockNumber(ctx)
require.NoError(t, err)
events, err := syncer.GetClaimsAndBridges(ctx, 0, lastBlock)
actualBridges, err := syncer.GetBridges(ctx, 0, lastBlock)
require.NoError(t, err)
actualBridges := []bridgesync.Bridge{}

for _, event := range events {
if event.Bridge != nil {
actualBridges = append(actualBridges, *event.Bridge)
}
}

// Assert bridges
require.Equal(t, expectedBridges, actualBridges)
Expand Down
42 changes: 42 additions & 0 deletions bridgesync/migrations/bridgesync0001.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- +migrate Down
DROP TABLE IF EXISTS block;
DROP TABLE IF EXISTS claim;
DROP TABLE IF EXISTS bridge;

-- +migrate Up
CREATE TABLE block (
num BIGINT PRIMARY KEY
);

CREATE TABLE bridge (
block_num INTEGER NOT NULL REFERENCES block(num) ON DELETE CASCADE,
block_pos INTEGER NOT NULL,
leaf_type INTEGER NOT NULL,
origin_network INTEGER NOT NULL,
origin_address VARCHAR NOT NULL,
destination_network INTEGER NOT NULL,
destination_address VARCHAR NOT NULL,
amount DECIMAL(78, 0) NOT NULL,
metadata BLOB,
deposit_count INTEGER NOT NULL,
PRIMARY KEY (block_num, block_pos)
);

CREATE TABLE claim (
block_num INTEGER NOT NULL REFERENCES block(num) ON DELETE CASCADE,
block_pos INTEGER NOT NULL,
global_index DECIMAL(78, 0) NOT NULL,
origin_network INTEGER NOT NULL,
origin_address VARCHAR NOT NULL,
destination_address VARCHAR NOT NULL,
amount DECIMAL(78, 0) NOT NULL,
proof_local_exit_root VARCHAR,
proof_rollup_exit_root VARCHAR,
mainnet_exit_root VARCHAR,
rollup_exit_root VARCHAR,
global_exit_root VARCHAR,
destination_network INTEGER NOT NULL,
metadata BLOB,
is_message BOOLEAN,
PRIMARY KEY (block_num, block_pos)
);
61 changes: 61 additions & 0 deletions bridgesync/migrations/bridgesync0001_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package migrations

import (
"context"
"path"
"testing"

"github.com/0xPolygon/cdk/db"
"github.com/stretchr/testify/require"
)

func Test001(t *testing.T) {
dbPath := path.Join(t.TempDir(), "file::memory:?cache=shared")

err := RunMigrations(dbPath)
require.NoError(t, err)
db, err := db.NewSQLiteDB(dbPath)
require.NoError(t, err)

ctx := context.Background()
tx, err := db.BeginTx(ctx, nil)
require.NoError(t, err)

_, err = tx.Exec(`
INSERT INTO block (num) VALUES (1);

INSERT INTO bridge (
block_num,
block_pos,
leaf_type,
origin_network,
origin_address,
destination_network,
destination_address,
amount,
metadata,
deposit_count
) VALUES (1, 0, 0, 0, '0x0000', 0, '0x0000', 0, NULL, 0);

INSERT INTO claim (
block_num,
block_pos,
global_index,
origin_network,
origin_address,
destination_address,
amount,
proof_local_exit_root,
proof_rollup_exit_root,
mainnet_exit_root,
rollup_exit_root,
global_exit_root,
destination_network,
metadata,
is_message
) VALUES (1, 0, 0, 0, '0x0000', '0x0000', 0, '0x000,0x000', '0x000,0x000', '0x000', '0x000', '0x0', 0, NULL, FALSE);
`)
require.NoError(t, err)
err = tx.Commit()
require.NoError(t, err)
}
33 changes: 33 additions & 0 deletions bridgesync/migrations/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package migrations
Copy link
Contributor

Choose a reason for hiding this comment

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

This file and ./l1infotreesync/migrations/migrations.go looks pretty much the same.. is not possible to move this to db package?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMO it's syncer has it's own migrations, it would be odd IMO to have a "centralized" package that imports the migrations files of each syncer, and has functions to run those specific for each syncer


import (
_ "embed"
"strings"

"github.com/0xPolygon/cdk/db"
treeMigrations "github.com/0xPolygon/cdk/tree/migrations"
migrate "github.com/rubenv/sql-migrate"
)

const upDownSeparator = "-- +migrate Up"

//go:embed bridgesync0001.sql
var mig001 string
var mig001splitted = strings.Split(mig001, upDownSeparator)

var bridgeMigrations = &migrate.MemoryMigrationSource{
Migrations: []*migrate.Migration{
{
Id: "bridgesync001",
Up: []string{mig001splitted[1]},
Down: []string{mig001splitted[0]},
},
},
}

func RunMigrations(dbPath string) error {
return db.RunMigrations(dbPath, &migrate.MemoryMigrationSource{Migrations: append(
bridgeMigrations.Migrations,
treeMigrations.Migrations.Migrations...,
)})
}
Loading
Loading