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 17 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
28 changes: 22 additions & 6 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 @@ -47,6 +48,7 @@ func NewL1(
waitForNewBlocksPeriod,
retryAfterErrorPeriod,
maxRetryAttemptsAfterError,
false,
)
}

Expand Down Expand Up @@ -77,6 +79,7 @@ func NewL2(
waitForNewBlocksPeriod,
retryAfterErrorPeriod,
maxRetryAttemptsAfterError,
true,
)
}

Expand All @@ -93,8 +96,9 @@ func newBridgeSync(
waitForNewBlocksPeriod time.Duration,
retryAfterErrorPeriod time.Duration,
maxRetryAttemptsAfterError int,
syncFullClaims bool,
) (*BridgeSync, error) {
processor, err := newProcessor(ctx, dbPath, l1OrL2ID)
processor, err := newProcessor(dbPath, l1OrL2ID)
if err != nil {
return nil, err
}
Expand All @@ -115,7 +119,7 @@ func newBridgeSync(
RetryAfterErrorPeriod: retryAfterErrorPeriod,
}

appender, err := buildAppender(ethClient, bridge)
appender, err := buildAppender(ethClient, bridge, syncFullClaims)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -152,14 +156,26 @@ 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)
}

func (s *BridgeSync) GetProof(ctx context.Context, depositCount uint32, localExitRoot common.Hash) ([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
}
194 changes: 194 additions & 0 deletions bridgesync/claimcalldata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package bridgesync

import (
"context"
"math/big"
"os/exec"
"testing"
"time"

"github.com/0xPolygon/cdk/test/contracts/claimmock"
"github.com/0xPolygon/cdk/test/contracts/claimmockcaller"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/require"
)

type testCase struct {
description string
bridgeAddr common.Address
log types.Log
expectedClaim Claim
}

func TestClaimCalldata(t *testing.T) {
testCases := []testCase{}
// Setup Docker L1
ctx := context.Background()
msg, err := exec.Command("bash", "-l", "-c", "docker compose up -d").CombinedOutput()
require.NoError(t, err, string(msg))
time.Sleep(time.Second * 1)
defer func() {
msg, err = exec.Command("bash", "-l", "-c", "docker compose down").CombinedOutput()
require.NoError(t, err, string(msg))
}()
client, err := ethclient.Dial("http://localhost:8545")
require.NoError(t, err)
privateKey, err := crypto.HexToECDSA("ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")
require.NoError(t, err)
auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(0).SetUint64(1337))
require.NoError(t, err)

// Deploy contracts
bridgeAddr, _, bridgeContract, err := claimmock.DeployClaimmock(auth, client)
require.NoError(t, err)
_, _, claimCaller, err := claimmockcaller.DeployClaimmockcaller(auth, client, bridgeAddr)
require.NoError(t, err)

proofLocal := [32][32]byte{}
proofLocalH := [32]common.Hash{}
proofLocal[5] = common.HexToHash("beef")
proofLocalH[5] = common.HexToHash("beef")
proofRollup := [32][32]byte{}
proofRollupH := [32]common.Hash{}
proofRollup[4] = common.HexToHash("a1fa")
proofRollupH[4] = common.HexToHash("a1fa")
expectedClaim := Claim{
GlobalIndex: big.NewInt(420),
OriginNetwork: 69,
OriginAddress: common.HexToAddress("ffaaffaa"),
DestinationAddress: common.HexToAddress("123456789"),
Amount: big.NewInt(3),
MainnetExitRoot: common.HexToHash("5ca1e"),
RollupExitRoot: common.HexToHash("dead"),
ProofLocalExitRoot: proofLocalH,
ProofRollupExitRoot: proofRollupH,
DestinationNetwork: 0,
Metadata: []byte{},
}
auth.GasLimit = 999999 // for some reason gas estimation fails :(

// direct call claim asset
tx, err := bridgeContract.ClaimAsset(
auth,
proofLocal,
proofRollup,
expectedClaim.GlobalIndex,
expectedClaim.MainnetExitRoot,
expectedClaim.RollupExitRoot,
expectedClaim.OriginNetwork,
expectedClaim.OriginAddress,
0,
expectedClaim.DestinationAddress,
expectedClaim.Amount,
nil,
)
require.NoError(t, err)
time.Sleep(1 * time.Second)
r, err := client.TransactionReceipt(ctx, tx.Hash())
expectedClaim.IsMessage = false
testCases = append(testCases, testCase{
description: "direct call to claim asset",
bridgeAddr: bridgeAddr,
log: *r.Logs[0],
expectedClaim: expectedClaim,
})

// indirect call claim asset
tx, err = claimCaller.ClaimAsset(
auth,
proofLocal,
proofRollup,
expectedClaim.GlobalIndex,
expectedClaim.MainnetExitRoot,
expectedClaim.RollupExitRoot,
expectedClaim.OriginNetwork,
expectedClaim.OriginAddress,
0,
expectedClaim.DestinationAddress,
expectedClaim.Amount,
nil,
)
require.NoError(t, err)
time.Sleep(1 * time.Second)
r, err = client.TransactionReceipt(ctx, tx.Hash())
expectedClaim.IsMessage = false
testCases = append(testCases, testCase{
description: "indirect call to claim asset",
bridgeAddr: bridgeAddr,
log: *r.Logs[0],
expectedClaim: expectedClaim,
})

// direct call claim message
tx, err = bridgeContract.ClaimMessage(
auth,
proofLocal,
proofRollup,
expectedClaim.GlobalIndex,
expectedClaim.MainnetExitRoot,
expectedClaim.RollupExitRoot,
expectedClaim.OriginNetwork,
expectedClaim.OriginAddress,
0,
expectedClaim.DestinationAddress,
expectedClaim.Amount,
nil,
)
require.NoError(t, err)
time.Sleep(1 * time.Second)
r, err = client.TransactionReceipt(ctx, tx.Hash())
expectedClaim.IsMessage = true
testCases = append(testCases, testCase{
description: "direct call to claim message",
bridgeAddr: bridgeAddr,
log: *r.Logs[0],
expectedClaim: expectedClaim,
})

// indirect call claim message
tx, err = claimCaller.ClaimMessage(
auth,
proofLocal,
proofRollup,
expectedClaim.GlobalIndex,
expectedClaim.MainnetExitRoot,
expectedClaim.RollupExitRoot,
expectedClaim.OriginNetwork,
expectedClaim.OriginAddress,
0,
expectedClaim.DestinationAddress,
expectedClaim.Amount,
nil,
)
require.NoError(t, err)
time.Sleep(1 * time.Second)
r, err = client.TransactionReceipt(ctx, tx.Hash())
expectedClaim.IsMessage = true
testCases = append(testCases, testCase{
description: "indirect call to claim message",
bridgeAddr: bridgeAddr,
log: *r.Logs[0],
expectedClaim: expectedClaim,
})

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
claimEvent, err := bridgeContract.ParseClaimEvent(tc.log)
require.NoError(t, err)
actualClaim := Claim{
GlobalIndex: claimEvent.GlobalIndex,
OriginNetwork: claimEvent.OriginNetwork,
OriginAddress: claimEvent.OriginAddress,
DestinationAddress: claimEvent.DestinationAddress,
Amount: claimEvent.Amount,
}
err = setClaimCalldata(client, tc.bridgeAddr, tc.log.TxHash, &actualClaim)
require.NoError(t, err)
require.Equal(t, tc.expectedClaim, actualClaim)
})
}
}
28 changes: 28 additions & 0 deletions bridgesync/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
networks:
default:
name: test-claimdata

services:
test-claimdata-l1:
container_name: test-claimdata-l1
image: hermeznetwork/geth-zkevm-contracts:elderberry-fork.9-geth1.13.11
environment:
- DEV_PERIOD
ports:
- "8545:8545"
entrypoint:
- geth
- --http
- --http.addr
- "0.0.0.0"
- "--http.corsdomain"
- "*"
- "--http.vhosts"
- "*"
- --dev
- --dev.period
- "1"
- "--datadir"
- "/geth_data"
- "--http.api"
- "admin,eth,debug,miner,net,txpool,personal,web3"
Loading
Loading