-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_addchain.go
172 lines (140 loc) · 4.58 KB
/
route_addchain.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package rest
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
v12 "k8s.io/api/core/v1"
v1 "github.com/allinbits/starport-operator/api/v1"
models "github.com/emerishq/demeris-backend-models/cns"
"github.com/emerishq/demeris-backend-models/validation"
"github.com/emerishq/emeris-cns-server/cns/chainwatch"
"github.com/emerishq/emeris-utils/k8s/operator"
)
const AddChainRoute = "/add"
type AddChainRequest struct {
models.Chain
SkipChannelCreation bool `json:"skip_channel_creation"`
NodeConfig *operator.NodeConfiguration `json:"node_config"`
RelayerConfiguration *operator.RelayerConfiguration `json:"relayer_configuration"`
}
// @Summary Add a new chain configuration
// @Description Add a new chain to the CNS DB
// @Router /add [post]
// @Param chain body AddChainRequest true "Chain data to add"
// @Accept json
// @Produce json
// @Success 201
// @Failure 400 "if cannot parse payload, or cannot validate: fees, denoms, relayer config"
// @Failure 500
func (r *router) addChainHandler(ctx *gin.Context) {
newChain := AddChainRequest{}
usr, _ := ctx.Get("user")
r.s.l.Debug(usr)
if err := ctx.ShouldBindJSON(&newChain); err != nil {
e(ctx, http.StatusBadRequest, validation.MissingFieldsErr(err, false))
r.s.l.Error("cannot bind input data to Chain struct", err)
return
}
if err := validateFees(newChain.Chain); err != nil {
e(ctx, http.StatusBadRequest, err)
r.s.l.Error("fee validation failed", err)
return
}
if err := validateDenoms(newChain.Chain); err != nil {
e(ctx, http.StatusBadRequest, err)
r.s.l.Error("fee validation failed", err)
return
}
if newChain.NodeConfig != nil {
newChain.NodeConfig.Namespace = r.s.Config.KubernetesNamespace
newChain.NodeConfig.Name = newChain.ChainName
// we trust that TestnetConfig holds the real chain ID
if newChain.NodeConfig.TestnetConfig != nil &&
*newChain.NodeConfig.TestnetConfig.ChainId != newChain.NodeInfo.ChainID {
newChain.NodeInfo.ChainID = *newChain.NodeConfig.TestnetConfig.ChainId
}
newChain.NodeConfig.TracelistenerDebug = r.s.Config.Debug
node, err := operator.NewNode(*newChain.NodeConfig)
if err != nil {
e(ctx, http.StatusBadRequest, err)
r.s.l.Error("cannot add chain", err)
return
}
switch newChain.NodeConfig.DisableMinFeeConfig {
case true:
node.Spec.Config.Nodes.TraceStoreContainer.ImagePullPolicy = v12.PullNever
default:
minGasPriceVal := newChain.RelayerToken().GasPriceLevels.Low / 2
minGasPricesStr := fmt.Sprintf("%v%s", minGasPriceVal, newChain.RelayerToken().Name)
cfgOverride := v1.ConfigOverride{
App: []v1.TomlConfigField{
{
Key: "minimum-gas-prices",
Value: v1.TomlConfigFieldValue{
String: &minGasPricesStr,
},
},
},
}
node.Spec.Config.Nodes.ConfigOverride = &cfgOverride
}
hasFaucet := false
if node.Spec.Init != nil {
hasFaucet = node.Spec.Init.Faucet != nil
}
if newChain.RelayerConfiguration == nil {
newChain.RelayerConfiguration = &operator.DefaultRelayerConfiguration
}
if err := newChain.RelayerConfiguration.Validate(); err != nil {
e(ctx, http.StatusBadRequest, err)
r.s.l.Errorw("cannot validate relayer configuration", "error", err)
return
}
if err := r.s.rc.AddChain(chainwatch.Chain{
Name: newChain.ChainName,
AddressPrefix: newChain.NodeInfo.Bech32Config.MainPrefix,
HasFaucet: hasFaucet,
SkipChannelCreation: newChain.SkipChannelCreation,
HDPath: newChain.DerivationPath,
RelayerConfiguration: *newChain.RelayerConfiguration,
}); err != nil {
e(ctx, http.StatusInternalServerError, err)
r.s.l.Error("cannot add chain name to cache", err)
return
}
}
if err := r.s.DB.AddChain(newChain.Chain); err != nil {
e(ctx, http.StatusInternalServerError, err)
r.s.l.Error("cannot add chain", err)
return
}
// return 201
ctx.Writer.WriteHeader(http.StatusCreated)
}
func validateFees(c models.Chain) error {
ft := c.FeeTokens()
if len(ft) == 0 {
return fmt.Errorf("no fee token specified")
}
for _, denom := range ft {
if denom.GasPriceLevels.Empty() {
return fmt.Errorf("fee levels for %s are not defined", denom.Name)
}
}
return nil
}
func validateDenoms(c models.Chain) error {
foundRelayerDenom := false
for _, d := range c.Denoms {
if d.RelayerDenom {
if foundRelayerDenom {
return fmt.Errorf("multiple relayer denoms detected")
}
if d.MinimumThreshRelayerBalance == nil {
return fmt.Errorf("relayer denom detected but no relayer minimum threshold balance defined")
}
foundRelayerDenom = true
}
}
return nil
}