Add custom handlers to the private RPC client (#1137)
This commit is contained in:
parent
ecc39735f5
commit
2bc3191fcf
|
@ -290,16 +290,31 @@ func (b *StatusBackend) DiscardSignRequests(ids []string) map[string]error {
|
|||
|
||||
// registerHandlers attaches Status callback handlers to running node
|
||||
func (b *StatusBackend) registerHandlers() error {
|
||||
rpcClient := b.StatusNode().RPCClient()
|
||||
if rpcClient == nil {
|
||||
var clients []*rpc.Client
|
||||
|
||||
if c := b.StatusNode().RPCClient(); c != nil {
|
||||
clients = append(clients, c)
|
||||
} else {
|
||||
return errors.New("RPC client unavailable")
|
||||
}
|
||||
|
||||
rpcClient.RegisterHandler(params.AccountsMethodName, func(context.Context, ...interface{}) (interface{}, error) {
|
||||
return b.AccountManager().Accounts()
|
||||
})
|
||||
if c := b.StatusNode().RPCPrivateClient(); c != nil {
|
||||
clients = append(clients, c)
|
||||
} else {
|
||||
return errors.New("RPC private client unavailable")
|
||||
}
|
||||
|
||||
rpcClient.RegisterHandler(params.SendTransactionMethodName, func(ctx context.Context, rpcParams ...interface{}) (interface{}, error) {
|
||||
for _, client := range clients {
|
||||
client.RegisterHandler(
|
||||
params.AccountsMethodName,
|
||||
func(context.Context, ...interface{}) (interface{}, error) {
|
||||
return b.AccountManager().Accounts()
|
||||
},
|
||||
)
|
||||
|
||||
client.RegisterHandler(
|
||||
params.SendTransactionMethodName,
|
||||
func(ctx context.Context, rpcParams ...interface{}) (interface{}, error) {
|
||||
txArgs, err := transactions.RPCCalltoSendTxArgs(rpcParams...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -311,10 +326,12 @@ func (b *StatusBackend) registerHandlers() error {
|
|||
}
|
||||
|
||||
return hash.Hex(), err
|
||||
})
|
||||
},
|
||||
)
|
||||
|
||||
rpcClient.RegisterHandler(params.PersonalSignMethodName, b.personalAPI.Sign)
|
||||
rpcClient.RegisterHandler(params.PersonalRecoverMethodName, b.personalAPI.Recover)
|
||||
client.RegisterHandler(params.PersonalSignMethodName, b.personalAPI.Sign)
|
||||
client.RegisterHandler(params.PersonalRecoverMethodName, b.personalAPI.Recover)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -45,9 +45,44 @@ func (s *TransactionsTestSuite) TestCallRPCSendTransaction() {
|
|||
|
||||
s.StartTestBackend()
|
||||
defer s.StopTestBackend()
|
||||
|
||||
EnsureNodeSync(s.Backend.StatusNode().EnsureSync)
|
||||
|
||||
s.sendTransactionUsingRPCClient(s.Backend.CallRPC)
|
||||
}
|
||||
|
||||
func (s *TransactionsTestSuite) TestCallUpstreamRPCSendTransaction() {
|
||||
CheckTestSkipForNetworks(s.T(), params.MainNetworkID, params.StatusChainNetworkID)
|
||||
|
||||
addr, err := GetRemoteURL()
|
||||
s.NoError(err)
|
||||
s.StartTestBackend(e2e.WithUpstream(addr))
|
||||
defer s.StopTestBackend()
|
||||
|
||||
s.sendTransactionUsingRPCClient(s.Backend.CallRPC)
|
||||
}
|
||||
|
||||
func (s *TransactionsTestSuite) TestCallPrivateRPCSendTransaction() {
|
||||
CheckTestSkipForNetworks(s.T(), params.MainNetworkID)
|
||||
|
||||
s.StartTestBackend()
|
||||
defer s.StopTestBackend()
|
||||
EnsureNodeSync(s.Backend.StatusNode().EnsureSync)
|
||||
|
||||
s.sendTransactionUsingRPCClient(s.Backend.CallPrivateRPC)
|
||||
}
|
||||
|
||||
func (s *TransactionsTestSuite) TestCallUpstreamPrivateRPCSendTransaction() {
|
||||
CheckTestSkipForNetworks(s.T(), params.MainNetworkID, params.StatusChainNetworkID)
|
||||
|
||||
addr, err := GetRemoteURL()
|
||||
s.NoError(err)
|
||||
s.StartTestBackend(e2e.WithUpstream(addr))
|
||||
defer s.StopTestBackend()
|
||||
|
||||
s.sendTransactionUsingRPCClient(s.Backend.CallPrivateRPC)
|
||||
}
|
||||
|
||||
func (s *TransactionsTestSuite) sendTransactionUsingRPCClient(callRPCFn func(string) string) {
|
||||
err := s.Backend.SelectAccount(TestConfig.Account1.Address, TestConfig.Account1.Password)
|
||||
s.NoError(err)
|
||||
|
||||
|
@ -65,55 +100,6 @@ func (s *TransactionsTestSuite) TestCallRPCSendTransaction() {
|
|||
method := event["method"].(string)
|
||||
s.Equal(params.SendTransactionMethodName, method)
|
||||
|
||||
txID := event["id"].(string)
|
||||
signResult = s.Backend.ApproveSignRequest(txID, TestConfig.Account1.Password)
|
||||
s.NoError(signResult.Error, "cannot complete queued transaction %s", txID)
|
||||
close(transactionCompleted)
|
||||
}
|
||||
})
|
||||
|
||||
result := s.Backend.CallRPC(`{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "eth_sendTransaction",
|
||||
"params": [{
|
||||
"from": "` + TestConfig.Account1.Address + `",
|
||||
"to": "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
|
||||
"value": "0x9184e72a"
|
||||
}]
|
||||
}`)
|
||||
s.NotContains(result, "error")
|
||||
|
||||
select {
|
||||
case <-transactionCompleted:
|
||||
case <-time.After(time.Minute):
|
||||
s.FailNow("sending transaction timed out")
|
||||
}
|
||||
|
||||
s.Equal(`{"jsonrpc":"2.0","id":1,"result":"`+signResult.Response.Hash().Hex()+`"}`, result)
|
||||
}
|
||||
|
||||
func (s *TransactionsTestSuite) TestCallRPCSendTransactionUpstream() {
|
||||
CheckTestSkipForNetworks(s.T(), params.MainNetworkID, params.StatusChainNetworkID)
|
||||
|
||||
addr, err := GetRemoteURL()
|
||||
s.NoError(err)
|
||||
s.StartTestBackend(e2e.WithUpstream(addr))
|
||||
defer s.StopTestBackend()
|
||||
|
||||
err = s.Backend.SelectAccount(TestConfig.Account2.Address, TestConfig.Account2.Password)
|
||||
s.NoError(err)
|
||||
|
||||
transactionCompleted := make(chan struct{})
|
||||
|
||||
var signResult sign.Result
|
||||
signal.SetDefaultNodeNotificationHandler(func(rawSignal string) {
|
||||
var signalEnvelope signal.Envelope
|
||||
err := json.Unmarshal([]byte(rawSignal), &signalEnvelope)
|
||||
s.NoError(err)
|
||||
|
||||
if signalEnvelope.Type == signal.EventSignRequestAdded {
|
||||
event := signalEnvelope.Event.(map[string]interface{})
|
||||
txID := event["id"].(string)
|
||||
|
||||
// Complete with a wrong passphrase.
|
||||
|
@ -128,13 +114,13 @@ func (s *TransactionsTestSuite) TestCallRPCSendTransactionUpstream() {
|
|||
}
|
||||
})
|
||||
|
||||
result := s.Backend.CallRPC(`{
|
||||
result := callRPCFn(`{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "eth_sendTransaction",
|
||||
"params": [{
|
||||
"from": "` + TestConfig.Account2.Address + `",
|
||||
"to": "` + TestConfig.Account1.Address + `",
|
||||
"from": "` + TestConfig.Account1.Address + `",
|
||||
"to": "` + TestConfig.Account2.Address + `",
|
||||
"value": "0x9184e72a"
|
||||
}]
|
||||
}`)
|
||||
|
|
Loading…
Reference in New Issue