Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into poc_bats
Browse files Browse the repository at this point in the history
  • Loading branch information
vcastellm committed Sep 16, 2024
2 parents 13df74c + ee78b49 commit 0e0172c
Show file tree
Hide file tree
Showing 44 changed files with 2,130 additions and 1,242 deletions.
Binary file added .github/assets/cdk-logo-name.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions .github/assets/cdk-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
619 changes: 619 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ test-seq_sender:

.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

.PHONY: generate-code-from-proto
generate-code-from-proto: ## Generates code from proto files
Expand Down
93 changes: 41 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,70 +1,59 @@
<!-- PROJECT SHIELDS -->
![Build][Build-badge]
[![Coverage][Coverage-badge]][Sonar-url]
[![Vulnerabilities][Vulnerability-badge]][Sonar-url]

# 0xPolygon GitHub Template
This repo serves as a template for new repositories in the 0xPolygon organization.
<div id="top"></div>
<!-- PROJECT LOGO -->
<br />
<div align="center">

<img src="./.github/assets/cdk-logo.svg#gh-light-mode-only" alt="Logo" width="100">
<img src="./.github/assets/cdk-logo.svg#gh-dark-mode-only" alt="Logo" width="100">

# Action Items
The following actions are required once you create a new repository from this repo:
- Update this README. Complete the sections to reflect your project
- Update `CODEOWNERS` file
- Update `sonar-project.properties` by adding your Sonar project key provided by the Security Team
- Update badges to reflect your project (badges are located at the bottom of this README)
- Ensure you are using Issue and PR Templates
## Polygon CDK

## About The Project
**Polygon CDK** (Chain Development Kit) is a modular framework that developers can use to build and deploy Zero Knowledge Proofs enabled Rollups and Validiums.

The CDK allow to build Rollups that are ZK powered, verifying the execution using the zkEVM prover from Polygon, they can be completelly personalizable because its modullar architecture.

### Built With
<!-- PROJECT SHIELDS -->
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=0xPolygon_cdk&metric=alert_status&token=aa6d76993fc213c4153bf65e0d62e4d08207ea7e)](https://sonarcloud.io/summary/new_code?id=0xPolygon_cdk)

[![Node.js 18][Language-badge]][Language-url]
</div>
</div>

<br />

## Getting Started

### Local Development
## Pre-requisites

Setup Kurtosis following this instructions https://github.com/0xPolygon/kurtosis-cdk?tab=readme-ov-file#getting-started

### Local Testing

- You can run locally against kurtosis-cdk environment using: [docs/local_debug.md](docs/local_debug.md)

### Database
## Contributing

### Installation
Contributions are very welcomed, the guidelines are currently not available (WIP)

## Usage
## Support

Use this space to show useful examples of how a project can be used. Additional screenshots, code examples, and demos work well in this space. This is incredibly useful for support personnel when responding to issues related to your application.
Feel free to [open an issue](https://github.com/0xPolygon/cdk/issues/new) if you have any feature request or bug report.<br />

## Contributing

This is the place to document your delivery workflow. For example:

1. Clone the project
2. Create a feature branch beginning with the ticket number (`git checkout -b INC-7689/update-readme`)
3. Commit your changes (`git commit -m 'Update README.me with default template`)
4. Push to the branch (`git push origin INC-7689/update-readme`)
5. Open a Pull Request
6. After review and approval, changes are deployed immediately

## Contact

![Email][Email-badge]
![Slack][Slack-badge]
[![Production URL][Production-badge]][Production-url]
[![Staging URL][Staging-badge]][Staging-url]


<!-- MARKDOWN LINKS AND IMAGES (update/replace as needed for your application) -->
[Build-badge]: https://github.com/0xPolygon/learn-api/actions/workflows/main.yml/badge.svg
[Coverage-badge]: https://sonarqube.polygon.technology/api/project_badges/measure?project=TODO
[Vulnerability-badge]: https://sonarqube.polygon.technology/api/project_badges/measure?project=TODO
[Sonar-url]: https://sonarqube.polygon.technology/dashboard?id=TODO
[Language-badge]: https://img.shields.io/badge/Nodejs-18.0-informational
[Language-url]: https://nodejs.org/en
[Email-badge]: https://img.shields.io/badge/Email-team_email_address-informational?logo=gmail
[Slack-badge]: https://img.shields.io/badge/Slack-team_ChannelName-informational?logo=slack
[Production-badge]: https://img.shields.io/badge/Production_URL-polygon.technology-informational
[Production-url]: https://link.to/prod
[Staging-badge]: https://img.shields.io/badge/Staging_URL-staging.polygon.technology-informational
[Staging-url]: https://link.to/staging
## License

Polygon Chain Development Kit
Copyright (c) 2024 PT Services DMCC

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
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)
);
Loading

0 comments on commit 0e0172c

Please sign in to comment.