-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.go
119 lines (99 loc) · 3.28 KB
/
execute.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
package main
import (
"encoding/json"
"fmt"
"os/exec"
"syscall"
"github.com/allora-network/allora-indexer/types"
"github.com/rs/zerolog/log"
)
func ExecuteCommand(cliApp, node string, parts []string) ([]byte, error) {
if len(parts) == 0 {
return nil, fmt.Errorf("no command parts provided")
}
var completeParts []string
completeParts = append(completeParts, parts...)
completeParts = replacePlaceholders(completeParts, "{node}", node)
completeParts = replacePlaceholders(completeParts, "{cliApp}", cliApp)
log.Info().Strs("command", completeParts).Msg("Executing command")
cmd := exec.Command(completeParts[0], completeParts[1:]...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
output, err := cmd.CombinedOutput()
if err != nil {
log.Error().Err(err).Str("output", string(output)).Msg("Command execution failed")
return nil, err
}
return output, nil
}
func replacePlaceholders(parts []string, placeholder, value string) []string {
var replacedParts []string
for _, part := range parts {
if part == placeholder {
replacedParts = append(replacedParts, value)
} else {
replacedParts = append(replacedParts, part)
}
}
return replacedParts
}
func ExecuteCommandByKey[T any](config ClientConfig, key string, params ...string) (T, error) {
var result T
cmd, ok := config.Commands[key]
if !ok {
return result, fmt.Errorf("command not found")
}
if len(params) > 0 {
cmd.Parts = append(cmd.Parts, params...)
}
log.Debug().Str("commandName", key).Msg("Starting execution")
output, err := ExecuteCommand(config.CliApp, config.Node, cmd.Parts)
if err != nil {
log.Error().Err(err).Msg("Failed to execute command")
return result, err
}
log.Debug().Str("raw output", string(output)).Msg("Command raw output")
err = json.Unmarshal(output, &result)
if err != nil {
log.Error().Err(err).Str("json", string(output)).Msg("Failed to unmarshal JSON")
return result, err
}
return result, nil
}
func DecodeTx(config ClientConfig, params string, blockHeight uint64) (types.Tx, error) {
var result types.Tx
// Determine the appropriate version based on block height and chain id
var alloradPath string
if config.ChainId == chainIdAlloraTestnet1 {
switch {
case blockHeight >= 1977711:
alloradPath = "/usr/local/bin/allorad" // 0.8.0
case blockHeight >= 1857950:
alloradPath = "/usr/local/bin/previous/v0.7.0/allorad" // 0.7.0
case blockHeight >= 1643705:
alloradPath = "/usr/local/bin/previous/v0.6.3/allorad" // 0.6.3
case blockHeight >= 1574267:
alloradPath = "/usr/local/bin/previous/v6/allorad" // 0.6.0
case blockHeight >= 1296200:
alloradPath = "/usr/local/bin/previous/v5/allorad" // 0.5.0
case blockHeight >= 1004550:
alloradPath = "/usr/local/bin/previous/v4/allorad" // 0.4.0
case blockHeight >= 812000:
alloradPath = "/usr/local/bin/previous/v3/allorad" // 0.3.0
default:
alloradPath = "/usr/local/bin/previous/v2/allorad" // 0.2.14
}
} else if config.ChainId == chainIdAlloraMainnet1 {
alloradPath = "/usr/local/bin/allorad"
} else {
return result, fmt.Errorf("chain id not supported")
}
// Update the config to use the selected allorad binary
config.CliApp = alloradPath
result, err := ExecuteCommandByKey[types.Tx](config, "decodeTx", params)
if err == nil {
return result, nil
}
return result, err
}