-
Notifications
You must be signed in to change notification settings - Fork 672
/
client.go
156 lines (139 loc) · 5.23 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
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package indexer
import (
"context"
"fmt"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/formatting"
"github.com/ava-labs/avalanchego/utils/json"
"github.com/ava-labs/avalanchego/utils/rpc"
)
var _ Client = (*client)(nil)
// Client interface for Avalanche Indexer API Endpoint
type Client interface {
// GetContainerRange returns the transactions at index [startIndex], [startIndex+1], ... , [startIndex+n-1]
// If [n] == 0, returns an empty response (i.e. null).
// If [startIndex] > the last accepted index, returns an error (unless the above apply.)
// If we run out of transactions, returns the ones fetched before running out.
GetContainerRange(ctx context.Context, startIndex uint64, numToFetch int, options ...rpc.Option) ([]Container, error)
// Get a container by its index
GetContainerByIndex(ctx context.Context, index uint64, options ...rpc.Option) (Container, error)
// Get the most recently accepted container and its index
GetLastAccepted(context.Context, ...rpc.Option) (Container, uint64, error)
// Returns 1 less than the number of containers accepted on this chain
GetIndex(ctx context.Context, containerID ids.ID, options ...rpc.Option) (uint64, error)
// Returns true if the given container is accepted
IsAccepted(ctx context.Context, containerID ids.ID, options ...rpc.Option) (bool, error)
// Get a container and its index by its ID
GetContainerByID(ctx context.Context, containerID ids.ID, options ...rpc.Option) (Container, uint64, error)
}
// Client implementation for Avalanche Indexer API Endpoint
type client struct {
requester rpc.EndpointRequester
}
// NewClient creates a client that can interact with an index via HTTP API
// calls.
// [uri] is the path to make API calls to.
// For example:
// - http://1.2.3.4:9650/ext/index/C/block
// - http://1.2.3.4:9650/ext/index/X/tx
func NewClient(uri string) Client {
return &client{
requester: rpc.NewEndpointRequester(uri),
}
}
func (c *client) GetContainerRange(ctx context.Context, startIndex uint64, numToFetch int, options ...rpc.Option) ([]Container, error) {
var fcs GetContainerRangeResponse
err := c.requester.SendRequest(ctx, "index.getContainerRange", &GetContainerRangeArgs{
StartIndex: json.Uint64(startIndex),
NumToFetch: json.Uint64(numToFetch),
Encoding: formatting.Hex,
}, &fcs, options...)
if err != nil {
return nil, err
}
response := make([]Container, len(fcs.Containers))
for i, resp := range fcs.Containers {
containerBytes, err := formatting.Decode(resp.Encoding, resp.Bytes)
if err != nil {
return nil, fmt.Errorf("couldn't decode container %s: %w", resp.ID, err)
}
response[i] = Container{
ID: resp.ID,
Timestamp: resp.Timestamp.Unix(),
Bytes: containerBytes,
}
}
return response, nil
}
func (c *client) GetContainerByIndex(ctx context.Context, index uint64, options ...rpc.Option) (Container, error) {
var fc FormattedContainer
err := c.requester.SendRequest(ctx, "index.getContainerByIndex", &GetContainerByIndexArgs{
Index: json.Uint64(index),
Encoding: formatting.Hex,
}, &fc, options...)
if err != nil {
return Container{}, err
}
containerBytes, err := formatting.Decode(fc.Encoding, fc.Bytes)
if err != nil {
return Container{}, fmt.Errorf("couldn't decode container %s: %w", fc.ID, err)
}
return Container{
ID: fc.ID,
Timestamp: fc.Timestamp.Unix(),
Bytes: containerBytes,
}, nil
}
func (c *client) GetLastAccepted(ctx context.Context, options ...rpc.Option) (Container, uint64, error) {
var fc FormattedContainer
err := c.requester.SendRequest(ctx, "index.getLastAccepted", &GetLastAcceptedArgs{
Encoding: formatting.Hex,
}, &fc, options...)
if err != nil {
return Container{}, 0, err
}
containerBytes, err := formatting.Decode(fc.Encoding, fc.Bytes)
if err != nil {
return Container{}, 0, fmt.Errorf("couldn't decode container %s: %w", fc.ID, err)
}
return Container{
ID: fc.ID,
Timestamp: fc.Timestamp.Unix(),
Bytes: containerBytes,
}, uint64(fc.Index), nil
}
func (c *client) GetIndex(ctx context.Context, id ids.ID, options ...rpc.Option) (uint64, error) {
var index GetIndexResponse
err := c.requester.SendRequest(ctx, "index.getIndex", &GetIndexArgs{
ID: id,
}, &index, options...)
return uint64(index.Index), err
}
func (c *client) IsAccepted(ctx context.Context, id ids.ID, options ...rpc.Option) (bool, error) {
var res IsAcceptedResponse
err := c.requester.SendRequest(ctx, "index.isAccepted", &IsAcceptedArgs{
ID: id,
}, &res, options...)
return res.IsAccepted, err
}
func (c *client) GetContainerByID(ctx context.Context, id ids.ID, options ...rpc.Option) (Container, uint64, error) {
var fc FormattedContainer
err := c.requester.SendRequest(ctx, "index.getContainerByID", &GetContainerByIDArgs{
ID: id,
Encoding: formatting.Hex,
}, &fc, options...)
if err != nil {
return Container{}, 0, err
}
containerBytes, err := formatting.Decode(fc.Encoding, fc.Bytes)
if err != nil {
return Container{}, 0, fmt.Errorf("couldn't decode container %s: %w", fc.ID, err)
}
return Container{
ID: fc.ID,
Timestamp: fc.Timestamp.Unix(),
Bytes: containerBytes,
}, uint64(fc.Index), nil
}