Skip to content

Commit

Permalink
Ler2block (#61)
Browse files Browse the repository at this point in the history
* wip

* wip

* WIP

* decoding direct and indeirecr assets and messages works

* connect everything

* fix building contract scripts

* fix building contract scripts

* wip

* WIP

* tree migrated to SQLite

* wip

* wip

* bridgesync working with sqlite

* pass tests

* minor cleaning

* add GetBlockByLER func

* handle err not found

* merge develop

* use memory for sqlite on the tests

* increase timestamp to pass UT

* review

* add callbacks on db tx

* lint

* fix compilation

* fix linter II

* fix linter III

* fix linter

* increase linter TO

* fix UTs and lint

* increase linter TO

* add PR requests
  • Loading branch information
arnaubennassar authored and Stefan-Ethernal committed Sep 17, 2024
1 parent 17b8d54 commit ea8379f
Show file tree
Hide file tree
Showing 40 changed files with 1,442 additions and 1,187 deletions.
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)
}
23 changes: 23 additions & 0 deletions bridgesync/migrations/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package migrations

import (
_ "embed"

"github.com/0xPolygon/cdk/db"
"github.com/0xPolygon/cdk/db/types"
treeMigrations "github.com/0xPolygon/cdk/tree/migrations"
)

//go:embed bridgesync0001.sql
var mig001 string

func RunMigrations(dbPath string) error {
migrations := []types.Migration{
{
ID: "bridgesync0001",
SQL: mig001,
},
}
migrations = append(migrations, treeMigrations.Migrations...)
return db.RunMigrations(dbPath, migrations)
}
Loading

0 comments on commit ea8379f

Please sign in to comment.