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

nimbus-verified-proxy integration #3281

Merged
merged 1 commit into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ statusgo-shared-library: ##@cross-compile Build status-go as shared library for
mkdir -p build/bin/statusgo-lib
go run cmd/library/*.go > build/bin/statusgo-lib/main.go
@echo "Building shared library..."
@echo "Tags: $(BUILD_TAGS)"
$(GOBIN_SHARED_LIB_CFLAGS) $(GOBIN_SHARED_LIB_CGO_LDFLAGS) go build \
-tags '$(BUILD_TAGS)' \
$(BUILD_FLAGS) \
Expand Down Expand Up @@ -389,3 +390,16 @@ migration:
migration-protocol: DEFAULT_PROTOCOL_PATH := protocol/migrations/sqlite
migration-protocol:
touch $(DEFAULT_PROTOCOL_PATH)/$(shell date +%s)_$(D).up.sql

PROXY_WRAPPER_PATH = $(CURDIR)/vendor/github.com/siphiuel/lc-proxy-wrapper
-include $(PROXY_WRAPPER_PATH)/Makefile.vars

#export VERIF_PROXY_OUT_PATH = $(CURDIR)/vendor/github.com/siphiuel/lc-proxy-wrapper
build-verif-proxy:
$(MAKE) -C $(NIMBUS_ETH1_PATH) libverifproxy

build-verif-proxy-wrapper:
$(MAKE) -C $(VERIF_PROXY_OUT_PATH) build-verif-proxy-wrapper

test-verif-proxy-wrapper:
CGO_CFLAGS="$(CGO_CFLAGS)" go test -v github.com/status-im/status-go/rpc -tags gowaku_skip_migrations,nimbus_light_client -run ^TestProxySuite$$ -testify.m TestRun -ldflags $(LDFLAGS)
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.151.15
0.151.16
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,17 @@ require (
github.com/ladydascalie/currency v1.6.0
github.com/meirf/gopart v0.0.0-20180520194036-37e9492a85a8
github.com/schollz/peerdiscovery v1.7.0
github.com/siphiuel/lc-proxy-wrapper v0.0.0-20230516150924-246507cee8c7
github.com/waku-org/go-waku v0.6.0
github.com/yeqown/go-qrcode/v2 v2.2.1
github.com/yeqown/go-qrcode/writer/standard v1.2.1
go.uber.org/multierr v1.8.0
golang.org/x/exp v0.0.0-20230206171751-46f607a40771
golang.org/x/net v0.8.0
)

require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/PuerkitoBio/goquery v1.6.1 // indirect
github.com/RoaringBitmap/roaring v0.9.4 // indirect
github.com/StackExchange/wmi v0.0.0-20210224194228-fe8f1750fd46 // indirect
Expand Down Expand Up @@ -258,7 +261,6 @@ require (
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/dig v1.15.0 // indirect
go.uber.org/fx v1.18.2 // indirect
golang.org/x/exp v0.0.0-20230206171751-46f607a40771 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbt
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/ClickHouse/clickhouse-go v1.4.3/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI=
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
Expand Down Expand Up @@ -1941,6 +1942,8 @@ github.com/shurcooL/sanitized_anchor_name v0.0.0-20170918181015-86672fcb3f95/go.
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/shurcooL/users v0.0.0-20180125191416-49c67e49c537/go.mod h1:QJTqeLYEDaXHZDBsXlPCDqdhQuJkuw4NOtaxYe3xii4=
github.com/shurcooL/webdavfs v0.0.0-20170829043945-18c3829fa133/go.mod h1:hKmq5kWdCj2z2KEozexVbfEZIWiTjhE0+UjmZgPqehw=
github.com/siphiuel/lc-proxy-wrapper v0.0.0-20230516150924-246507cee8c7 h1:l7v5b3ghketKidVR6QjJlsgrrgzRnRRCc0lywjjONjQ=
github.com/siphiuel/lc-proxy-wrapper v0.0.0-20230516150924-246507cee8c7/go.mod h1:Cpir2acMsYVjrwbmRKN+2w1mYbBZdWwmoIyqc7x470U=
github.com/sirupsen/logrus v1.0.4-0.20170822132746-89742aefa4b2/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down
2 changes: 2 additions & 0 deletions params/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const (
// SendTransactionMethodName defines the name for a giving transaction.
SendTransactionMethodName = "eth_sendTransaction"

BalanceMethodName = "eth_getBalance"

// AccountsMethodName defines the name for listing the currently signed accounts.
AccountsMethodName = "eth_accounts"

Expand Down
15 changes: 15 additions & 0 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type Client struct {
walletNotifier func(chainID uint64, message string)
}

// Is initialized in a build-tag-dependent module
var verifProxyInitFn func(c *Client)

// NewClient initializes Client and tries to connect to both,
// upstream and local node.
//
Expand Down Expand Up @@ -92,6 +95,10 @@ func NewClient(client *gethrpc.Client, upstreamChainID uint64, upstream params.U

c.router = newRouter(c.upstreamEnabled)

if verifProxyInitFn != nil {
verifProxyInitFn(&c)
}

return &c, nil
}

Expand Down Expand Up @@ -247,6 +254,14 @@ func (c *Client) RegisterHandler(method string, handler Handler) {
c.handlers[method] = handler
}

// UnregisterHandler removes a previously registered handler.
func (c *Client) UnregisterHandler(method string) {
c.handlersMx.Lock()
defer c.handlersMx.Unlock()

delete(c.handlers, method)
}

// callMethod calls registered RPC handler with given args and pointer to result.
// It handles proper params and result converting
//
Expand Down
71 changes: 71 additions & 0 deletions rpc/verif_proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//go:build nimbus_light_client
// +build nimbus_light_client

package rpc

import (
"context"
"fmt"
)

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/log"
gethrpc "github.com/ethereum/go-ethereum/rpc"
proxy "github.com/siphiuel/lc-proxy-wrapper"
Copy link
Member

Choose a reason for hiding this comment

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

There is a chance that this line will give a lint error as your own repos would be considered 3rd party and should be grouped with other 3rd party packages.

"github.com/status-im/status-go/params"
)

type VerifProxy struct {
config *proxy.Config
client *gethrpc.Client
log log.Logger
}

func init() {
verifProxyInitFn = func(c *Client) {
ctx := context.Background()
var testConfig = proxy.Config{
Eth2Network: "mainnet",
TrustedBlockRoot: "0xc5182cdb750fe088138b0d475683cda26a96befc24de16fb17bcf49d9cadf2f7",
Web3Url: c.upstreamURL,
RpcAddress: "127.0.0.1",
RpcPort: 8545,
LogLevel: "INFO",
}
proxy.StartLightClient(ctx, &testConfig)
verifProxy, err := newVerifProxy(&testConfig, c.log)
if err != nil {
c.RegisterHandler(
params.BalanceMethodName,
func(ctx context.Context, v uint64, params ...interface{}) (interface{}, error) {
addr := params[0].(common.Address)
return verifProxy.GetBalance(ctx, addr)
},
)
}
}
}

func newVerifProxy(cfg *proxy.Config, log log.Logger) (*VerifProxy, error) {
endpoint := "http://" + cfg.RpcAddress + ":" + fmt.Sprint(cfg.RpcPort)
client, err := gethrpc.DialHTTP(endpoint)
if err != nil {
log.Error("Error when creating VerifProxy client", err)
return nil, err
}
proxy := &VerifProxy{cfg, client, log}
return proxy, nil
}

func (p *VerifProxy) GetBalance(ctx context.Context, address common.Address) (interface{}, error) {
var result hexutil.Big
err := p.client.CallContext(ctx, &result, "eth_getBalance", address, "latest")
if err != nil {
p.log.Error("Error when invoking GetBalance", err)
return nil, err
}
return result, nil

}
89 changes: 89 additions & 0 deletions rpc/verif_proxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//go:build nimbus_light_client
// +build nimbus_light_client

package rpc

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"testing"
"time"

"net/http"
"net/http/httptest"
Comment on lines +7 to +16
Copy link
Member

Choose a reason for hiding this comment

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

These are all std lib packages so can be grouped together

)

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
gethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/status-im/status-go/params"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

type ProxySuite struct {
suite.Suite
}

func TestProxySuite(t *testing.T) {
suite.Run(t, new(ProxySuite))
}

func (s *ProxySuite) startRpcClient(infuraURL string) *Client {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `{
"id": 1,
"jsonrpc": "2.0",
"result": "0x234234e22b9ffc2387e18636e0534534a3d0c56b0243567432453264c16e78a2adc"
}`)
}))
defer ts.Close()

gethRPCClient, err := gethrpc.Dial(ts.URL)
require.NoError(s.T(), err)

db, close := setupTestNetworkDB(s.T())
defer close()
c, err := NewClient(gethRPCClient, 1, params.UpstreamRPCConfig{Enabled: true, URL: infuraURL}, []params.Network{}, db)
require.NoError(s.T(), err)

return c
}

func (s *ProxySuite) TestRun() {
infuraURL := "https://mainnet.infura.io/v3/800c641949d64d768a5070a1b0511938"
client := s.startRpcClient(infuraURL)

// Run light client proxy
ctx, cancel := context.WithCancel(context.Background())

signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
fmt.Println("Before range signals")

// Invoke eth_getBalance
var result hexutil.Big
var addr common.Address
addr = common.HexToAddress("0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5")
chainID := uint64(1)

time.Sleep(200 * time.Second)
err := client.CallContext(ctx, &result, chainID, "eth_getBalance", addr, "latest")
require.NoError(s.T(), err)

client.UnregisterHandler("eth_getBalance")
var resultRaw hexutil.Big
err = client.CallContext(ctx, &resultRaw, chainID, "eth_getBalance", addr, "latest")
s.Require().Equal(result, resultRaw)
for range signals {
fmt.Println("Signal caught, exiting")
cancel()
}
fmt.Println("Exiting")

}
2 changes: 2 additions & 0 deletions vendor/github.com/BurntSushi/toml/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/BurntSushi/toml/COPYING

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading