-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
61 lines (50 loc) · 1.68 KB
/
rpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package rpc
import (
"time"
"github.com/sedraxnet/sedrax/app/appmessage"
"github.com/sedraxnet/sedrax/infrastructure/network/rpcclient"
"github.com/sedraxnet/sedrax/domain/dagconfig"
)
const defaultRPCServer = "localhost"
// Config are configurations common to all tests that need to connect to json-rpc
type Config struct {
RPCServer string `short:"s" long:"rpcserver" description:"RPC server to connect to"`
}
// ValidateRPCConfig makes sure that provided Config is valid or returns an error otherwise
func ValidateRPCConfig(config *Config) error {
if config.RPCServer == "" {
config.RPCServer = defaultRPCServer
}
return nil
}
// Client wraps rpcclient.RPCClient with extra functionality needed for stability-tests
type Client struct {
*rpcclient.RPCClient
OnBlockAdded chan struct{}
}
// ConnectToRPC connects to JSON-RPC server specified in the provided config
func ConnectToRPC(config *Config, dagParams *dagconfig.Params) (*Client, error) {
rpcAddress, err := dagParams.NormalizeRPCServerAddress(config.RPCServer)
if err != nil {
return nil, err
}
rpcClient, err := rpcclient.NewRPCClient(rpcAddress)
if err != nil {
return nil, err
}
rpcClient.SetTimeout(time.Second * 120)
rpcClient.SetOnErrorHandler(func(err error) {
log.Errorf("Error from Client: %+v", err)
})
client := &Client{
RPCClient: rpcClient,
OnBlockAdded: make(chan struct{}),
}
return client, nil
}
// RegisterForBlockAddedNotifications registers for block added notifications pushed by the node
func (c *Client) RegisterForBlockAddedNotifications() error {
return c.RPCClient.RegisterForBlockAddedNotifications(func(_ *appmessage.BlockAddedNotificationMessage) {
c.OnBlockAdded <- struct{}{}
})
}