-
Notifications
You must be signed in to change notification settings - Fork 38
/
api.go
81 lines (69 loc) · 2.05 KB
/
api.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
package indexer
import (
"context"
"fmt"
"github.com/0glabs/0g-storage-client/common/shard"
eth_common "github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
)
// Requires `indexerApi` implements the `Interface` interface.
var _ Interface = (*IndexerApi)(nil)
// IndexerApi indexer service configuration
type IndexerApi struct {
Namespace string
}
// NewIndexerApi creates indexer service configuration
func NewIndexerApi() *IndexerApi {
return &IndexerApi{"indexer"}
}
// GetShardedNodes return storage node list
func (api *IndexerApi) GetShardedNodes(ctx context.Context) (ShardedNodes, error) {
trusted, err := defaultNodeManager.Trusted()
if err != nil {
return ShardedNodes{}, errors.WithMessage(err, "Failed to retrieve trusted nodes")
}
return ShardedNodes{
Trusted: trusted,
Discovered: defaultNodeManager.Discovered(),
}, nil
}
// GetNodeLocations return IP locations of all nodes.
func (api *IndexerApi) GetNodeLocations(ctx context.Context) (map[string]*IPLocation, error) {
result := make(map[string]*IPLocation)
// trusted nodes
for _, v := range defaultNodeManager.TrustedClients() {
ip := parseIP(v.URL())
if loc, ok := defaultIPLocationManager.Get(ip); ok {
result[ip] = loc
}
}
// discovered nodes
for _, v := range defaultNodeManager.Discovered() {
ip := parseIP(v.URL)
if loc, ok := defaultIPLocationManager.Get(ip); ok {
result[ip] = loc
}
}
return result, nil
}
// GetFileLocations return locations info of given file.
func (api *IndexerApi) GetFileLocations(ctx context.Context, root string) (locations []*shard.ShardedNode, err error) {
// find corresponding tx sequence
hash := eth_common.HexToHash(root)
trustedClients := defaultNodeManager.TrustedClients()
var txSeq uint64
found := false
for _, client := range trustedClients {
info, err := client.GetFileInfo(ctx, hash)
if err != nil || info == nil {
continue
}
txSeq = info.Tx.Seq
found = true
break
}
if !found {
return nil, fmt.Errorf("file not found")
}
return defaultFileLocationCache.GetFileLocations(ctx, txSeq)
}