Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ubuntu systemctl support & Fix relayer l2 flow #10

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cosmosutils/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,15 @@ func getBinaryURL(version string) string {
}

func GetInitiaBinaryPath(version string) string {
if strings.Contains(version, "@") {
parts := strings.Split(version, "@")
if len(parts) == 2 {
version = parts[1]
} else {
panic(fmt.Sprintf("invalid version format: %s", version))
}
}
traviolus marked this conversation as resolved.
Show resolved Hide resolved

userHome, err := os.UserHomeDir()
if err != nil {
panic(fmt.Sprintf("failed to get user home directory: %v", err))
Expand Down
45 changes: 45 additions & 0 deletions cosmosutils/cli_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cosmosutils

import (
"encoding/json"
"fmt"
"os/exec"

"github.com/initia-labs/weave/client"
)

type InitiadBankBalancesQueryResponse struct {
Balances Coins `json:"balances"`
}

type InitiadQuerier struct {
binaryPath string
}

func NewInitiadQuerier(rest string) *InitiadQuerier {
httpClient := client.NewHTTPClient()
nodeVersion, url := MustGetInitiaBinaryUrlFromLcd(httpClient, rest)
binaryPath := GetInitiaBinaryPath(nodeVersion)
MustInstallInitiaBinary(nodeVersion, url, binaryPath)

return &InitiadQuerier{
binaryPath: binaryPath,
}
}
traviolus marked this conversation as resolved.
Show resolved Hide resolved

func (iq *InitiadQuerier) QueryBankBalances(address, rpc string) (*Coins, error) {
cmd := exec.Command(iq.binaryPath, "query", "bank", "balances", address, "--node", rpc, "--output", "json")

outputBytes, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("failed to query bank balances for %s: %v, output: %s", address, err, string(outputBytes))
}

var queryResponse InitiadBankBalancesQueryResponse
err = json.Unmarshal(outputBytes, &queryResponse)
if err != nil {
panic(fmt.Sprintf("failed to unmarshal JSON: %v", err))
}
traviolus marked this conversation as resolved.
Show resolved Hide resolved

return &queryResponse.Balances, nil
}
16 changes: 5 additions & 11 deletions models/relayer/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,20 +695,14 @@ func waitFetchingBalancesLoading(ctx context.Context) tea.Cmd {
if err != nil {
panic(fmt.Errorf("cannot fetch balance for l1: %v", err))
}
if l1Balances.IsZero() {
state.l1NeedsFunding = true
}
state.l1NeedsFunding = l1Balances.IsZero()

l2ChainId := MustGetL2ChainId(ctx)
l2Registry := registry.MustGetL2Registry(registry.InitiaL1Testnet, l2ChainId)
l2Rest := l2Registry.MustGetActiveLcd()
l2Balances, err := cosmosutils.QueryBankBalances(l2Rest, state.l2RelayerAddress)
querier := cosmosutils.NewInitiadQuerier(l1Rest)
l2Balances, err := querier.QueryBankBalances(state.l2RelayerAddress, MustGetL2ActiveRpc(ctx))
if err != nil {
panic(fmt.Errorf("cannot fetch balance for l2: %v", err))
}
if l2Balances.IsZero() {
state.l2NeedsFunding = true
}
state.l2NeedsFunding = l2Balances.IsZero()

return ui.EndLoading{
Ctx: weavecontext.SetCurrentState(ctx, state),
Expand Down Expand Up @@ -971,7 +965,7 @@ func broadcastDefaultPresetFromGasStation(ctx context.Context) tea.Cmd {
res, err = cliTx.BroadcastMsgSend(
gasStationMnemonic,
state.l2RelayerAddress,
fmt.Sprintf("%s%s", state.l2FundingAmount, "l2/4b66eb60bf9f503ea97fe4dc96d5c604c1dca14ee988e21510ac4b087bf72671"),
fmt.Sprintf("%s%s", state.l2FundingAmount, MustGetL2GasDenom(ctx)),
MustGetL2GasPrices(ctx),
MustGetL2ActiveRpc(ctx),
MustGetL2ChainId(ctx),
Expand Down
14 changes: 11 additions & 3 deletions service/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/initia-labs/weave/common"
"github.com/initia-labs/weave/cosmosutils"
)

type Systemd struct {
Expand Down Expand Up @@ -39,13 +40,20 @@ func (j *Systemd) Create(binaryVersion, appHome string) error {
return fmt.Errorf("failed to get user home directory: %v", err)
}

weaveDataPath := filepath.Join(userHome, common.WeaveDataDirectory)
binaryName := j.commandName.MustGetBinaryName()
binaryPath := filepath.Join(weaveDataPath, binaryVersion, strings.ReplaceAll(binaryVersion, "@", "_"))
var binaryPath string
switch j.commandName {
case Initia:
binaryPath = filepath.Dir(cosmosutils.GetInitiaBinaryPath(binaryVersion))
case Minitia:
binaryPath = filepath.Join(userHome, common.WeaveDataDirectory, binaryVersion, strings.ReplaceAll(binaryVersion, "@", "_"))
default:
binaryPath = filepath.Join(userHome, common.WeaveDataDirectory)
}
traviolus marked this conversation as resolved.
Show resolved Hide resolved

cmd := exec.Command("sudo", "tee", fmt.Sprintf("/etc/systemd/system/%s", j.GetServiceName()))
template := LinuxTemplateMap[j.commandName]
cmd.Stdin = strings.NewReader(fmt.Sprintf(string(template), binaryName, currentUser.Username, binaryPath, j.GetServiceName(), appHome))
cmd.Stdin = strings.NewReader(fmt.Sprintf(string(template), binaryName, currentUser.Username, binaryPath, string(j.commandName), appHome))
if err = cmd.Run(); err != nil {
return fmt.Errorf("failed to create service: %v", err)
}
Expand Down
Loading