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

Evm bridge #124

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.idea
**/**/yu
**/**/reddio_db
**/**/evm_bridge_db


/test/tmp/
data
Expand All @@ -9,4 +11,6 @@ transfer_test
benchmark_test
uniswap_test
uniswap_benchmark_test
eth_benchmark_data.json
eth_benchmark_data.json
**/.env
**/.sepolia.env
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,6 @@ generate_bindings:

# Clean up generated files
clean_bindings:
rm -f $(ERC20T_GO) $(TOKEN_GO) $(WETH9_GO) $(UNISWAPV2FACTORY_GO) $(UNISWAPV2ROUTER01_GO)
rm -f $(ERC20T_GO) $(TOKEN_GO) $(WETH9_GO) $(UNISWAPV2FACTORY_GO) $(UNISWAPV2ROUTER01_GO)


49 changes: 48 additions & 1 deletion cmd/node/app/app.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package app

import (
"context"
"log"
"net/http"
"time"

"github.com/common-nighthawk/go-figure"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"github.com/yu-org/yu/apps/poa"
yuConfig "github.com/yu-org/yu/config"
"github.com/yu-org/yu/core/kernel"
Expand All @@ -14,6 +19,10 @@ import (
"github.com/reddio-com/reddio/evm"
"github.com/reddio-com/reddio/evm/ethrpc"
"github.com/reddio-com/reddio/parallel"
l2watcherTri "github.com/reddio-com/reddio/watcher"
l1watcher "github.com/reddio-com/reddio/watcher/controller"

"github.com/reddio-com/reddio/watcher/relayer"
)

func Start(evmPath, yuPath, poaPath, configPath string) {
Expand All @@ -35,6 +44,8 @@ func StartUpChain(yuCfg *yuConfig.KernelConf, poaCfg *poa.PoaConfig, evmCfg *evm

ethrpc.StartupEthRPC(chain, evmCfg)

StartupL1Watcher(chain, evmCfg)

chain.Startup()

}
Expand All @@ -43,9 +54,10 @@ func InitReddio(yuCfg *yuConfig.KernelConf, poaCfg *poa.PoaConfig, evmCfg *evm.G
poaTri := poa.NewPoa(poaCfg)
solidityTri := evm.NewSolidity(evmCfg)
parallelTri := parallel.NewParallelEVM()
watcherTri := l2watcherTri.NewWatcher(evmCfg)

chain := startup.InitDefaultKernel(
yuCfg, poaTri, solidityTri, parallelTri,
yuCfg, poaTri, solidityTri, parallelTri, watcherTri,
)
//chain.WithExecuteFn(chain.OrderedExecute)
chain.WithExecuteFn(parallelTri.Execute)
Expand All @@ -57,3 +69,38 @@ func startPromServer() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8080", nil)
}

func StartupL1Watcher(chain *kernel.Kernel, cfg *evm.GethConfig) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
if !cfg.EnableL1Client {
logrus.Info("no client enabled, stop init watcher")
return
}

l1Client, err := ethclient.Dial(cfg.L1ClientAddress)
if err != nil {
log.Fatal("failed to connect to L1 geth", "endpoint", cfg.L1ClientAddress, "err", err)
}

l2Client, err := ethclient.Dial(cfg.L2ClientAddress)
if err != nil {
log.Fatal("failed to connect to L2 geth", "endpoint", cfg.L2ClientAddress, "err", err)
}
l1ToL2Relayer, err := relayer.NewL1ToL2Relayer(ctx, cfg, l2Client, chain)
if err != nil {
logrus.Fatal("init bridge relayer failed: ", err)
}

if cfg.EnableL1Client {
l1Watcher, err := l1watcher.NewL1EventsWatcher(ctx, cfg, l1Client, l1ToL2Relayer)
if err != nil {
logrus.Fatal("init L1 client failed: ", err)
}
err = l1Watcher.Run(ctx)
if err != nil {
logrus.Fatal("l1 client run failed: ", err)
}
}

}
14 changes: 13 additions & 1 deletion conf/evm.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
enable_eth_rpc = true
eth_host = "0.0.0.0"
eth_port = "9092"
eth_port = "9092"


# [Module:Watcher]
enable_l1_client = false
# note: need websocket rpc to listen to the L1 event
## use your own node address
l1_client_address = ""
l2_client_address = "http://localhost:9092"
parentlayer_contract_address = "0xb59b7ea47FC73b6F468BC3187e9C10F3dCDaA75A"
childlayer_contract_address = "0xeC054c6ee2DbbeBC9EbCA50CdBF94A94B02B2E40"
# Used to collect L2->L1 cross-chain messages from L2 blocks. 'n' indicates collecting once every 'n' L2 blocks, collecting cross-chain messages from 'n' L2 blocks at a time.
l2_block_collection_depth = 5
13 changes: 11 additions & 2 deletions evm/cfg.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package evm

import (
"math/big"
"time"

"github.com/BurntSushi/toml"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
Expand All @@ -9,8 +12,6 @@ import (
"github.com/ethereum/go-ethereum/params"
yuConfig "github.com/reddio-com/reddio/evm/config"
"github.com/sirupsen/logrus"
"math/big"
"time"
)

type GethConfig struct {
Expand Down Expand Up @@ -47,6 +48,14 @@ type GethConfig struct {
EnableEthRPC bool `toml:"enable_eth_rpc"`
EthHost string `toml:"eth_host"`
EthPort string `toml:"eth_port"`

// EventsWatcher configs
EnableL1Client bool `toml:"enable_l1_client"`
L1ClientAddress string `toml:"l1_client_address"`
L2ClientAddress string `toml:"l2_client_address"`
ParentLayerContractAddress string `toml:"parentlayer_contract_address"`
ChildLayerContractAddress string `toml:"childlayer_contract_address"`
L2BlockCollectionDepth *big.Int `toml:"l2_block_collection_depth"`
}

func (gc *GethConfig) Copy() *GethConfig {
Expand Down
27 changes: 14 additions & 13 deletions evm/ethrpc/log_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import (
"encoding/json"
"errors"
"fmt"
"math/big"
"slices"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"
"github.com/sirupsen/logrus"
yutypes "github.com/yu-org/yu/core/types"
"math/big"
"slices"
)

var (
Expand Down Expand Up @@ -273,17 +274,17 @@ func (f *LogFilter) checkMatches(ctx context.Context, vLog *types.Log) bool {
}

// TODO: The logic for topic filtering is a bit complex; it will not be implemented for now.
//if len(f.topics) > len(vLog.Topics) {
// return false
//}
//for i, sub := range f.topics {
// if len(sub) == 0 {
// continue // empty rule set == wildcard
// }
// if !slices.Contains(sub, vLog.Topics[i]) {
// return false
// }
//}
// if len(f.topics) > len(vLog.Topics) {
// return false
// }
// for i, sub := range f.topics {
// if len(sub) == 0 {
// continue // empty rule set == wildcard
// }
// if !slices.Contains(sub, vLog.Topics[i]) {
// return false
// }
// }

return true
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ replace github.com/yu-org/yu => ./pkg-yu

require (
github.com/BurntSushi/toml v1.2.1
github.com/HyperService-Consortium/go-hexutil v1.0.1
github.com/cockroachdb/pebble v1.1.2
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/ethereum/go-ethereum v1.14.0
github.com/holiman/uint256 v1.2.4
github.com/joho/godotenv v1.5.1
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.20.2
github.com/rs/cors v1.11.0
Expand All @@ -25,7 +28,6 @@ require (
require (
github.com/ChainSafe/go-schnorrkel v0.0.0-20200626160457-b38283118816 // indirect
github.com/DataDog/zstd v1.5.6-0.20230824185856-869dae002e5e // indirect
github.com/HyperService-Consortium/go-hexutil v1.0.1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/VictoriaMetrics/fastcache v1.12.1 // indirect
github.com/anqiansong/ketty v0.0.0-20211202021934-dbaf2e277891 // indirect
Expand All @@ -42,7 +44,6 @@ require (
github.com/cockroachdb/errors v1.11.3 // indirect
github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/pebble v1.1.2 // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/consensys/bavard v0.1.13 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkr
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI=
github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
Expand Down
40 changes: 40 additions & 0 deletions watcher/abi/backend_abi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package backendabi

import (
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)

var (
IL1ParentBridgeCoreFacetABI *abi.ABI
IL2ChildBridgeCoreFacetABI *abi.ABI

L1DownwardMessageEventSig common.Hash
L2UpwardMessageEventSig common.Hash
)

func init() {
IL1ParentBridgeCoreFacetABI, _ = IL1ParentBridgeCoreFacetMetaData.GetAbi()
L1DownwardMessageEventSig = IL1ParentBridgeCoreFacetABI.Events["DownwardMessage"].ID

IL2ChildBridgeCoreFacetABI, _ = IL2ChildBridgeCoreFacetMetaData.GetAbi()
L2UpwardMessageEventSig = IL2ChildBridgeCoreFacetABI.Events["UpwardMessage"].ID
}

var IL1ParentBridgeCoreFacetMetaData = &bind.MetaData{
ABI: "[{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"uint256\",\"name\":\"sequence\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint32\",\"name\":\"payloadType\",\"type\":\"uint32\"},{\"indexed\":false,\"internalType\":\"bytes\",\"name\":\"payload\",\"type\":\"bytes\"}],\"name\":\"DownwardMessage\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"uint32\",\"name\":\"payloadType\",\"type\":\"uint32\"},{\"internalType\":\"bytes\",\"name\":\"payload\",\"type\":\"bytes\"}],\"name\":\"sendDownwardMessage\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]",
}
var IL2ChildBridgeCoreFacetMetaData = &bind.MetaData{
ABI: "[{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"uint256\",\"name\":\"sequence\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint32\",\"name\":\"payloadType\",\"type\":\"uint32\"},{\"indexed\":false,\"internalType\":\"bytes\",\"name\":\"payload\",\"type\":\"bytes\"}],\"name\":\"UpwardMessage\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"uint32\",\"name\":\"payloadType\",\"type\":\"uint32\"},{\"internalType\":\"bytes\",\"name\":\"payload\",\"type\":\"bytes\"}],\"name\":\"sendUpwardMessage\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]",
}

type ChildBridgeCoreFacetUpwardMessageEvent struct {
Sequence *big.Int
PayloadType uint32
Payload []byte
Raw types.Log // Blockchain specific contextual infos
}
1 change: 1 addition & 0 deletions watcher/contract/ChildBridgeCoreFacet.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"sequence","type":"uint256"},{"indexed":false,"internalType":"uint32","name":"payloadType","type":"uint32"},{"indexed":false,"internalType":"bytes","name":"payload","type":"bytes"}],"name":"UpwardMessage","type":"event"},{"inputs":[{"internalType":"uint32","name":"payloadType","type":"uint32"},{"internalType":"bytes","name":"payload","type":"bytes"}],"name":"sendUpwardMessage","outputs":[],"stateMutability":"nonpayable","type":"function"}]
Loading
Loading