-
Notifications
You must be signed in to change notification settings - Fork 249
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 16, 2023
1 parent
c5a8b40
commit 40a6268
Showing
8 changed files
with
226 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,70 @@ | ||
// +build nimbus_light_client | ||
|
||
package rpc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
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 | ||
} | ||
|
||
func init() { | ||
verifProxyInitFn = func(c *Client) { | ||
ctx := context.Background() | ||
var testConfig = proxy.Config{ | ||
Eth2Network: "mainnet", | ||
//TrustedBlockRoot: "0x6fd2f9fdc616a0755f3f88ac58e4a7871788c3128261a18bf9645eee7042eb53", | ||
TrustedBlockRoot: "0xc5182cdb750fe088138b0d475683cda26a96befc24de16fb17bcf49d9cadf2f7", | ||
//Web3Url: Web3UrlType{"HttpUrl", "https://mainnet.infura.io/v3/800c641949d64d768a5070a1b0511938"}, | ||
Web3Url: c.upstreamURL, | ||
RpcAddress: "127.0.0.1", | ||
RpcPort: 8545, | ||
LogLevel: "INFO", | ||
//Eth2Network: "prater", | ||
//TrustedBlockRoot: "0x017e4563ebf7fed67cff819c63d8da397b4ed0452a3bbd7cae13476abc5020e4", | ||
} | ||
proxy.StartLightClient(ctx, &testConfig) | ||
verifProxy := NewVerifProxy(&testConfig) | ||
c.RegisterHandler( | ||
params.BalanceMethodName, | ||
func(ctx context.Context, v uint64, params ...interface{}) (interface{}, error) { | ||
fmt.Println("### inside registered handler") | ||
addr := params[0].(common.Address) | ||
return verifProxy.GetBalance(ctx, addr) | ||
}, | ||
) | ||
} | ||
} | ||
|
||
func NewVerifProxy(cfg *proxy.Config) *VerifProxy { | ||
endpoint := "http://" + cfg.RpcAddress + ":" + fmt.Sprint(cfg.RpcPort) | ||
fmt.Println("### NewVerifProxy: ", endpoint) | ||
client, err := gethrpc.DialHTTP(endpoint) | ||
if err != nil { | ||
fmt.Println("Error when creating VerifProxy client", err) | ||
} | ||
proxy := &VerifProxy{cfg, client} | ||
proxy.client = client | ||
return proxy | ||
} | ||
|
||
func (p *VerifProxy) GetBalance(ctx context.Context, address common.Address) (interface{}, error) { | ||
fmt.Println("### IN GetBalance, address ", address) | ||
var result hexutil.Big | ||
err := p.client.CallContext(ctx, &result, "eth_getBalance", address, "latest") | ||
if err != nil { | ||
fmt.Println("### IN GetBalance, error ", 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,109 @@ | ||
// +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 startNode() { | ||
// // Start node | ||
// utils.Init() | ||
|
||
// backend := api.NewGethStatusBackend() | ||
// config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID) | ||
// require.NoError(s.T(), err) | ||
// require.NoError(s.T(), backend.AccountManager().InitKeystore(config.KeyStoreDir)) | ||
// err = backend.StartNode(config) | ||
// require.NoError(s.T(), err) | ||
|
||
// defer func() { | ||
// require.NoError(s.T(), backend.StopNode()) | ||
// }() | ||
// } | ||
|
||
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") | ||
// time.Sleep(8 * time.Second) | ||
// cancel() | ||
|
||
// Invoke eth_getBalance | ||
// '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x407d73d8a49eeb85d32cf465507dd71d507100c1", "latest"],"id":1}' | ||
fmt.Println("### upstream URL: ", client.upstreamURL) | ||
fmt.Println("### upstream chainID: ", client.UpstreamChainID) | ||
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) | ||
fmt.Println("### result: ", result.String()) | ||
|
||
client.UnregisterHandler("eth_getBalance") | ||
var resultRaw hexutil.Big | ||
err = client.CallContext(ctx, &resultRaw, chainID, "eth_getBalance", addr, "latest") | ||
fmt.Println("### resultRaw: ", resultRaw.String()) | ||
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