-
Notifications
You must be signed in to change notification settings - Fork 249
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.151.15 | ||
0.151.16 |
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" | ||
"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 | ||
|
||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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.