-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
legacy.go
55 lines (44 loc) · 1.31 KB
/
legacy.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
package starknet
import (
"encoding/json"
"sort"
"github.com/pkg/errors"
"github.com/smartcontractkit/sqlx"
starknetdb "github.com/smartcontractkit/chainlink-starknet/relayer/pkg/chainlink/db"
"github.com/smartcontractkit/chainlink/core/logger"
)
type SetupConfig interface {
StarkNetNodes() string
LogSQL() bool
}
// SetupNodes is a hack/shim method to allow node operators to specify multiple nodes via ENV.
// See: https://app.shortcut.com/chainlinklabs/epic/33587/overhaul-config?cf_workflow=500000005&ct_workflow=all
func SetupNodes(db *sqlx.DB, cfg SetupConfig, lggr logger.Logger) (err error) {
str := cfg.StarkNetNodes()
if str == "" {
return nil
}
var nodes []starknetdb.Node
if err = json.Unmarshal([]byte(str), &nodes); err != nil {
return errors.Wrapf(err, "invalid STARKNET_NODES json: %q", str)
}
// Sorting gives a consistent insert ordering
sort.Slice(nodes, func(i, j int) bool {
return nodes[i].Name < nodes[j].Name
})
lggr.Info("STARKNET_NODES was set; clobbering starknet_nodes table")
orm := NewORM(db, lggr, cfg)
return orm.SetupNodes(nodes, uniqueIDs(nodes))
}
func uniqueIDs(ns []starknetdb.Node) (ids []string) {
m := map[string]struct{}{}
for _, n := range ns {
id := n.ChainID
if _, ok := m[id]; ok {
continue
}
ids = append(ids, id)
m[id] = struct{}{}
}
return
}