-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
110 lines (85 loc) · 2.64 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
package etherface
import (
"context"
"errors"
"fmt"
"strings"
"github.com/avast/retry-go/v4"
"github.com/syndtr/goleveldb/leveldb"
)
var (
ErrorNoResults = errors.New("no results")
)
const (
EtherfaceDBPath = "/root/node/etherface"
DefaultAttempts = 3
)
// etherface (https://github.com/volsa/etherface) is an Ethereum Signature Database.
// It provides a way to lookup function names based on their hash.
// It is an optional dependency for the decentralized component to improve the output usability.
type Client interface {
Lookup(ctx context.Context, hash string) (string, error)
}
var _ Client = (*etherfaceClient)(nil)
// etherfaceClient is a client for querying the etherface database
type etherfaceClient struct {
db *leveldb.DB
attempts uint
}
// Lookup queries the etherface database for the function name of a given hash
func (h *etherfaceClient) Lookup(ctx context.Context, hash string) (functionName string, err error) {
retryableFunc := func() error {
functionName, err = h.query(ctx, hash)
return err
}
retryIfFunc := func(err error) bool {
return !errors.Is(err, ErrorNoResults)
}
if err := retry.Do(retryableFunc, retry.Attempts(h.attempts), retry.RetryIf(retryIfFunc)); err != nil {
return "", fmt.Errorf("retry attempts: %w", err)
}
return functionName, nil
}
// query queries the etherface database for the function name of a given hash
func (h *etherfaceClient) query(_ context.Context, hash string) (string, error) {
normalizedHash := hash
if strings.HasPrefix(hash, "0x") {
normalizedHash = hash[2:]
}
data, err := h.db.Get([]byte(normalizedHash), nil)
if err != nil {
if errors.Is(err, leveldb.ErrNotFound) {
return "", ErrorNoResults
}
return "", fmt.Errorf("failed to get data from leveldb: %w", err)
}
return extractFunctionName(string(data)), nil
}
// extractFunctionName extracts the function name from a string before the first bracket
func extractFunctionName(str string) string {
index := strings.Index(str, "(")
if index != -1 {
return str[:index]
}
return ""
}
// NewEtherfaceClient creates a new Etherface client based on leveldb
func NewEtherfaceClient() (Client, error) {
database, err := leveldb.OpenFile(EtherfaceDBPath, nil)
if err != nil {
return nil, fmt.Errorf("failed to open leveldb: %w", err)
}
instance := etherfaceClient{
db: database,
attempts: DefaultAttempts,
}
return &instance, nil
}
type ClientOption func(client *etherfaceClient) error
// WithAttempts sets the number of attempts for querying the etherface database
func WithAttempts(attempts uint) ClientOption {
return func(h *etherfaceClient) error {
h.attempts = attempts
return nil
}
}