-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.go
144 lines (124 loc) · 3.81 KB
/
client.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// (c) 2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avalanchegoclient
import (
"fmt"
"time"
"github.com/ava-labs/avalanchego-kurtosis/kurtosis/avalanche/libs/constants"
"github.com/ava-labs/avalanchego/api/admin"
"github.com/ava-labs/avalanchego/api/health"
"github.com/ava-labs/avalanchego/api/info"
"github.com/ava-labs/avalanchego/api/ipcs"
"github.com/ava-labs/avalanchego/api/keystore"
"github.com/ava-labs/avalanchego/vms/avm"
"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/ava-labs/coreth/ethclient"
"github.com/ava-labs/coreth/plugin/evm"
"github.com/sirupsen/logrus"
)
// Chain names
const (
XChain = "X"
CChain = "C"
)
// Client is a general client for avalanche
type Client struct {
admin *admin.Client
xChain *avm.Client
health *health.Client
info *info.Client
ipcs *ipcs.Client
keystore *keystore.Client
platform *platformvm.Client
cChain *evm.Client
cChainEth *ethclient.Client
cChaiConcurrentEth *ConcurrentEthClient
ipAddr string
port int
}
// NewClient returns a Client for interacting with the Chain endpoints
func NewClient(ipAddr string, port int, requestTimeout time.Duration) *Client {
uri := fmt.Sprintf("http://%s:%d", ipAddr, port)
cClient, err := ethclient.Dial(fmt.Sprintf("ws://%s:%d/ext/bc/C/ws", ipAddr, port))
if err != nil {
// retry loop on next call
cClient = nil
}
return &Client{
ipAddr: ipAddr,
port: port,
admin: admin.NewClient(uri, requestTimeout),
xChain: avm.NewClient(uri, XChain, requestTimeout),
health: health.NewClient(uri, requestTimeout),
info: info.NewClient(uri, requestTimeout),
ipcs: ipcs.NewClient(uri, requestTimeout),
keystore: keystore.NewClient(uri, requestTimeout),
platform: platformvm.NewClient(uri, requestTimeout),
cChain: evm.NewCChainClient(uri, requestTimeout),
cChainEth: cClient,
cChaiConcurrentEth: NewConcurrentEthClient(cClient),
}
}
// PChainAPI ...
func (c *Client) PChainAPI() *platformvm.Client {
return c.platform
}
// XChainAPI ...
func (c *Client) XChainAPI() *avm.Client {
return c.xChain
}
// CChainAPI ...
func (c *Client) CChainAPI() *evm.Client {
return c.cChain
}
// CChainEthAPI
func (c *Client) CChainEthAPI() *ethclient.Client {
var err error
var cClient *ethclient.Client
if c.cChainEth == nil {
for startTime := time.Now(); time.Since(startTime) < constants.TimeoutDuration; time.Sleep(time.Second) {
cClient, err = ethclient.Dial(fmt.Sprintf("ws://%s:%d/ext/bc/C/ws", c.ipAddr, c.port))
if err == nil {
c.cChainEth = cClient
return c.cChainEth
}
}
logrus.Infof("About to panic, the avalanchegoclient is unable to contact the CChain at : %s because of %s",
fmt.Sprintf("ws://%s:%d/ext/bc/C/ws", c.ipAddr, c.port),
err.Error())
panic(err)
}
return c.cChainEth
}
// CChaiConcurrentEth wraps the ethclient.Client in a concurrency-safe implementation
func (c *Client) CChaiConcurrentEth() *ConcurrentEthClient {
if c.cChainEth == nil || c.cChaiConcurrentEth.client == nil {
c.cChaiConcurrentEth = NewConcurrentEthClient(c.CChainEthAPI())
}
return c.cChaiConcurrentEth
}
// InfoAPI ...
func (c *Client) InfoAPI() *info.Client {
return c.info
}
// HealthAPI ...
func (c *Client) HealthAPI() *health.Client {
return c.health
}
// IpcsAPI ...
func (c *Client) IpcsAPI() *ipcs.Client {
return c.ipcs
}
// KeystoreAPI ...
func (c *Client) KeystoreAPI() *keystore.Client {
return c.keystore
}
// AdminAPI ...
func (c *Client) AdminAPI() *admin.Client {
return c.admin
}
func (c *Client) Reconnect() *Client {
c.cChainEth = nil
c.CChaiConcurrentEth()
return c
}