-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkava.go
More file actions
194 lines (161 loc) · 5.6 KB
/
Copy pathkava.go
File metadata and controls
194 lines (161 loc) · 5.6 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
Copyright (c) 2026 tazhate <hate@tazhate.ru>
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package adapters
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
chainsv1alpha2 "github.com/tazhate/chainplane/api/v1alpha2"
)
// --------------------------------------------------------------------------
// Constants
// --------------------------------------------------------------------------
const (
// kavaBlockTime is the average Kava block interval used to estimate network tip.
kavaBlockTime = 6.5 // seconds
)
// --------------------------------------------------------------------------
// Type
// --------------------------------------------------------------------------
type kavaAdapter struct {
protocolAdapter
}
// --------------------------------------------------------------------------
// Registration
// --------------------------------------------------------------------------
func init() {
Register(chainsv1alpha2.ChainKava, &kavaAdapter{
protocolAdapter: protocolAdapter{livenessPort: 26657},
})
}
// --------------------------------------------------------------------------
// Interface methods
// --------------------------------------------------------------------------
func (a *kavaAdapter) DefaultImage(client string) string {
return DefaultImageFor(chainsv1alpha2.ChainKava, client)
}
func (a *kavaAdapter) ConfigTemplate(_ chainsv1alpha2.ChainInstanceSpec) (string, string, error) {
return "app.toml", kavaConfig, nil
}
// ContainerArgs passes --home /data so the node reads from the PVC mount.
func (a *kavaAdapter) ContainerArgs(_ chainsv1alpha2.ChainInstanceSpec) []string {
return []string{"start", "--home", "/data"}
}
func (a *kavaAdapter) ContainerPorts(_ chainsv1alpha2.ChainInstanceSpec) []corev1.ContainerPort {
return []corev1.ContainerPort{
{Name: "rpc", ContainerPort: 26657, Protocol: corev1.ProtocolTCP},
{Name: "api", ContainerPort: 1317, Protocol: corev1.ProtocolTCP},
{Name: "p2p", ContainerPort: 26656, Protocol: corev1.ProtocolTCP},
{Name: "metrics", ContainerPort: 26660, Protocol: corev1.ProtocolTCP},
}
}
// StartupProbe gives Kava up to 1h (120x30s) to complete state sync.
func (a *kavaAdapter) StartupProbe(_ chainsv1alpha2.ChainInstanceSpec) *corev1.Probe {
return tcpProbe(26657, 30, 30, 10, 120)
}
// HealthCheck queries CometBFT RPC /status on port 26657.
func (a *kavaAdapter) HealthCheck(ctx context.Context, rpcURL string) (SyncStatus, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rpcURL+"/status", nil)
if err != nil {
return SyncStatus{}, fmt.Errorf("kava health request: %w", err)
}
resp, err := rpcClient.Do(req)
if err != nil {
return SyncStatus{}, fmt.Errorf("kava health: %w", err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(io.LimitReader(resp.Body, maxResponseBytes))
var status cosmosStatusResponse
if err := json.Unmarshal(body, &status); err != nil {
return SyncStatus{}, fmt.Errorf("kava status parse: %w", err)
}
height := parseDecimalInt64(status.Result.SyncInfo.LatestBlockHeight)
syncing := status.Result.SyncInfo.CatchingUp
if height == 0 && syncing {
return syncingPseudo(), nil
}
highestBlock := height
if syncing {
if blockTime, err := time.Parse(time.RFC3339Nano, status.Result.SyncInfo.LatestBlockTime); err == nil {
lag := time.Since(blockTime).Seconds()
if lag > kavaBlockTime {
highestBlock = height + int64(lag/kavaBlockTime)
}
}
if tip := cosmosNetworkTip(ctx, rpcURL); tip > highestBlock {
highestBlock = tip
}
}
var progress float64
if !syncing {
progress = 100.0
} else {
progress = progressFromBlocks(height, highestBlock)
}
var peers int32
if req2, err2 := http.NewRequestWithContext(ctx, http.MethodGet, rpcURL+"/net_info", nil); err2 == nil {
if resp2, err2 := (&http.Client{Timeout: 5 * time.Second}).Do(req2); err2 == nil {
defer resp2.Body.Close()
body2, _ := io.ReadAll(io.LimitReader(resp2.Body, maxResponseBytes))
var netInfo struct {
Result struct {
NPeers string `json:"n_peers"`
} `json:"result"`
}
if json.Unmarshal(body2, &netInfo) == nil {
peers = int32(parseDecimalInt64(netInfo.Result.NPeers))
}
}
}
return SyncStatus{
IsSyncing: syncing,
CurrentBlock: height,
HighestBlock: highestBlock,
Progress: progress,
Peers: peers,
}, nil
}
// --------------------------------------------------------------------------
// Config
// --------------------------------------------------------------------------
const kavaConfig = `# Kava node config override
[api]
enable = true
address = "tcp://0.0.0.0:1317"
enabled-unsafe-cors = true
[grpc]
enable = false
[telemetry]
enabled = true
prometheus-retention-time = 60
`
func (a *kavaAdapter) DefaultResources() ResourceDefaults {
return ResourceDefaults{
CPURequest: resource.MustParse("4"),
MemoryRequest: resource.MustParse("8Gi"),
Storage: resource.MustParse("1Ti"),
}
}
func (a *kavaAdapter) VersionPolicy() ChainVersionPolicy {
return ChainVersionPolicy{
Registry: "docker.io",
Repository: "kava/kava",
TagPattern: `^v\d+\.\d+\.\d+-goleveldb$`,
}
}