-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vitaliy Vlasov
committed
May 17, 2023
1 parent
c5a8b40
commit 3d45661
Showing
8 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//go:build nimbus_light_client | ||
// +build nimbus_light_client | ||
|
||
package rpc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
//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" | ||
|
||
"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") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters