Skip to content

Commit 8514197

Browse files
committed
feat: state sync loading and setup
1 parent b44ddd4 commit 8514197

File tree

3 files changed

+144
-6
lines changed

3 files changed

+144
-6
lines changed

models/weaveinit/run_l1_node.go

+82-5
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ func initializeApp(state *RunL1NodeState) tea.Cmd {
739739
url = state.initiadEndpoint
740740
case string(Mainnet), string(Testnet):
741741
var result map[string]interface{}
742-
err = utils.MakeGetRequest(strings.ToLower(state.network), "lcd", "/cosmos/base/tendermint/v1beta1/node_info", nil, &result)
742+
err = utils.MakeGetRequestUsingConfig(strings.ToLower(state.network), "lcd", "/cosmos/base/tendermint/v1beta1/node_info", nil, &result)
743743
if err != nil {
744744
panic(err)
745745
}
@@ -1016,6 +1016,7 @@ func (m *ExistingDataReplaceSelect) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
10161016
m.state.replaceExistingData = false
10171017
return m, tea.Quit
10181018
case ReplaceData:
1019+
// TODO: comet unsafe-reset-all here too
10191020
m.state.replaceExistingData = true
10201021
switch m.state.syncMethod {
10211022
case string(Snapshot):
@@ -1074,7 +1075,6 @@ func (m *SnapshotEndpointInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
10741075
}
10751076

10761077
func (m *SnapshotEndpointInput) View() string {
1077-
// TODO: Correctly render terminal output
10781078
view := m.state.weave.Render() + styles.RenderPrompt(m.GetQuestion(), []string{"snapshot url"}, styles.Question)
10791079
if m.err != nil {
10801080
return view + "\n" + m.TextInput.ViewErr(m.err)
@@ -1086,6 +1086,7 @@ type StateSyncEndpointInput struct {
10861086
utils.TextInput
10871087
state *RunL1NodeState
10881088
question string
1089+
err error
10891090
}
10901091

10911092
func NewStateSyncEndpointInput(state *RunL1NodeState) *StateSyncEndpointInput {
@@ -1109,16 +1110,20 @@ func (m *StateSyncEndpointInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
11091110
if done {
11101111
m.state.stateSyncEndpoint = input.Text
11111112
m.state.weave.PushPreviousResponse(styles.RenderPreviousResponse(styles.DotsSeparator, m.GetQuestion(), []string{"state sync RPC"}, input.Text))
1112-
// TODO: Continue
1113-
return m, tea.Quit
1113+
newLoader := NewStateSyncSetupLoading(m.state)
1114+
return newLoader, newLoader.Init()
11141115
}
11151116
m.TextInput = input
11161117
return m, cmd
11171118
}
11181119

11191120
func (m *StateSyncEndpointInput) View() string {
11201121
// TODO: Correctly render terminal output
1121-
return m.state.weave.Render() + styles.RenderPrompt(m.GetQuestion(), []string{"state sync RPC"}, styles.Question) + m.TextInput.View()
1122+
view := m.state.weave.Render() + styles.RenderPrompt(m.GetQuestion(), []string{"state sync RPC"}, styles.Question)
1123+
if m.err != nil {
1124+
return view + "\n" + m.TextInput.ViewErr(m.err)
1125+
}
1126+
return view + m.TextInput.View()
11221127
}
11231128

11241129
type SnapshotDownloadLoading struct {
@@ -1231,3 +1236,75 @@ func snapshotExtractor() tea.Cmd {
12311236
return utils.EndLoading{}
12321237
}
12331238
}
1239+
1240+
type StateSyncSetupLoading struct {
1241+
utils.Loading
1242+
state *RunL1NodeState
1243+
}
1244+
1245+
func NewStateSyncSetupLoading(state *RunL1NodeState) *StateSyncSetupLoading {
1246+
return &StateSyncSetupLoading{
1247+
Loading: utils.NewLoading("Setting up State Sync...", setupStateSync(state)),
1248+
state: state,
1249+
}
1250+
}
1251+
1252+
func (m *StateSyncSetupLoading) Init() tea.Cmd {
1253+
return m.Loading.Init()
1254+
}
1255+
1256+
func (m *StateSyncSetupLoading) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
1257+
loader, cmd := m.Loading.Update(msg)
1258+
m.Loading = loader
1259+
switch msg := msg.(type) {
1260+
case utils.ErrorLoading:
1261+
m.state.weave.PopPreviousResponse()
1262+
m.state.weave.PopPreviousResponse()
1263+
model := NewStateSyncEndpointInput(m.state)
1264+
model.err = msg.Err
1265+
return model, cmd
1266+
}
1267+
1268+
if m.Loading.Completing {
1269+
m.state.weave.PushPreviousResponse(styles.RenderPreviousResponse(styles.NoSeparator, fmt.Sprintf("Snapshot setup successfully."), []string{}, ""))
1270+
return m, tea.Quit
1271+
}
1272+
return m, cmd
1273+
}
1274+
1275+
func (m *StateSyncSetupLoading) View() string {
1276+
if m.Completing {
1277+
return m.state.weave.Render()
1278+
}
1279+
return m.state.weave.Render() + m.Loading.View()
1280+
}
1281+
1282+
func setupStateSync(state *RunL1NodeState) tea.Cmd {
1283+
return func() tea.Msg {
1284+
userHome, err := os.UserHomeDir()
1285+
if err != nil {
1286+
return utils.ErrorLoading{Err: fmt.Errorf("[error] Failed to get user home: %v", err)}
1287+
}
1288+
1289+
stateSyncInfo, err := utils.GetStateSyncInfo(state.stateSyncEndpoint)
1290+
if err != nil {
1291+
return utils.ErrorLoading{Err: fmt.Errorf("[error] Failed to get state sync info: %v", err)}
1292+
}
1293+
1294+
initiaConfigPath := filepath.Join(userHome, utils.InitiaConfigDirectory)
1295+
if err = utils.UpdateTomlValue(filepath.Join(initiaConfigPath, "config.toml"), "statesync.enable", "true"); err != nil {
1296+
return utils.ErrorLoading{Err: fmt.Errorf("[error] Failed to setup state sync enable: %v", err)}
1297+
}
1298+
if err = utils.UpdateTomlValue(filepath.Join(initiaConfigPath, "config.toml"), "statesync.rpc_servers", fmt.Sprintf("%[1]s,%[1]s", state.stateSyncEndpoint)); err != nil {
1299+
return utils.ErrorLoading{Err: fmt.Errorf("[error] Failed to setup state sync rpc_servers: %v", err)}
1300+
}
1301+
if err = utils.UpdateTomlValue(filepath.Join(initiaConfigPath, "config.toml"), "statesync.trust_height", fmt.Sprintf("%d", stateSyncInfo.TrustHeight)); err != nil {
1302+
return utils.ErrorLoading{Err: fmt.Errorf("[error] Failed to setup state sync trust_height: %v", err)}
1303+
}
1304+
if err = utils.UpdateTomlValue(filepath.Join(initiaConfigPath, "config.toml"), "statesync.trust_hash", fmt.Sprintf("%s", stateSyncInfo.TrustHash)); err != nil {
1305+
return utils.ErrorLoading{Err: fmt.Errorf("[error] Failed to setup state sync trust_hash: %v", err)}
1306+
}
1307+
1308+
return utils.EndLoading{}
1309+
}
1310+
}

utils/http.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@ func GetEndpointURL(network, endpoint string) (string, error) {
2020
return url, nil
2121
}
2222

23-
func MakeGetRequest(network, endpoint, additionalPath string, params map[string]string, result interface{}) error {
23+
func MakeGetRequestUsingConfig(network, endpoint, additionalPath string, params map[string]string, result interface{}) error {
2424
baseURL, err := GetEndpointURL(network, endpoint)
2525
if err != nil {
2626
return err
2727
}
28+
return makeGetRequest(baseURL, additionalPath, params, result)
29+
}
30+
31+
func MakeGetRequestUsingURL(overrideURL, additionalPath string, params map[string]string, result interface{}) error {
32+
return makeGetRequest(overrideURL, additionalPath, params, result)
33+
}
2834

35+
func makeGetRequest(baseURL, additionalPath string, params map[string]string, result interface{}) error {
2936
fullURL := fmt.Sprintf("%s%s", baseURL, additionalPath)
3037

3138
client := &http.Client{

utils/statesync.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
)
7+
8+
type BlockResponse struct {
9+
Result struct {
10+
Block struct {
11+
Header struct {
12+
Height string `json:"height"`
13+
} `json:"header"`
14+
} `json:"block"`
15+
} `json:"result"`
16+
}
17+
18+
type HashResponse struct {
19+
Result struct {
20+
BlockID struct {
21+
Hash string `json:"hash"`
22+
} `json:"block_id"`
23+
} `json:"result"`
24+
}
25+
26+
type StateSyncInfo struct {
27+
TrustHeight int
28+
TrustHash string
29+
}
30+
31+
func GetStateSyncInfo(url string) (*StateSyncInfo, error) {
32+
var latestBlock BlockResponse
33+
err := MakeGetRequestUsingURL(url, "/block", nil, &latestBlock)
34+
if err != nil {
35+
return nil, fmt.Errorf("Error fetching latest block height: %v\n", err)
36+
}
37+
38+
latestHeight, err := strconv.Atoi(latestBlock.Result.Block.Header.Height)
39+
if err != nil {
40+
return nil, fmt.Errorf("Error converting block height to integer: %v\n", err)
41+
}
42+
blockHeight := latestHeight - 2000
43+
44+
var trustHashResp HashResponse
45+
err = MakeGetRequestUsingURL(url, fmt.Sprintf("/block?height=%d", blockHeight), nil, &trustHashResp)
46+
if err != nil {
47+
return nil, fmt.Errorf("Error fetching trust hash: %v\n", err)
48+
}
49+
50+
return &StateSyncInfo{
51+
TrustHeight: blockHeight,
52+
TrustHash: trustHashResp.Result.BlockID.Hash,
53+
}, nil
54+
}

0 commit comments

Comments
 (0)