Skip to content

Commit

Permalink
docs: update readme
Browse files Browse the repository at this point in the history
Signed-off-by: Jose Ramon Mañes <jose@celestia.org>
  • Loading branch information
tty47 committed Aug 11, 2023
1 parent 873745d commit dab817d
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"sync"

"github.com/jrmanes/torch/config"
Expand Down Expand Up @@ -280,47 +281,55 @@ func BulkTrustedPeers(pods config.MutualPeer) {
// GenesisHash
func GenesisHash(pods config.MutualPeersConfig) (string, string) {
consensusNode := pods.MutualPeers[0].ConsensusNode
c := exec.Command("wget", "-q", "-O", "-", fmt.Sprintf("http://%s:26657/block?height=1", consensusNode))
url := fmt.Sprintf("http://%s:26657/block?height=1", consensusNode)

// Create a buffer to capture the command's output
var outputBuffer bytes.Buffer
c.Stdout = &outputBuffer
response, err := http.Get(url)
if err != nil {
log.Error("Error making GET request:", err)
return "", ""
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
log.Error("Non-OK response:", response.Status)
return "", ""
}

// Run the command
err := c.Run()
bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Error("Error:", err)
log.Error("Error reading response body:", err)
return "", ""
}

// Convert the output buffer to a string
outputString := outputBuffer.String()
bodyString := string(bodyBytes)
fmt.Println("Response Body:")
fmt.Println(bodyString)

// Parse the JSON response into a generic map
var response map[string]interface{}
err = json.Unmarshal([]byte(outputString), &response)
var jsonResponse map[string]interface{}
err = json.Unmarshal([]byte(bodyString), &jsonResponse)
if err != nil {
log.Error("Error parsing JSON:", err)
return "", ""
}

// Access and print the .block_id.hash field
blockIDHash, ok := response["result"].(map[string]interface{})["block_id"].(map[string]interface{})["hash"].(string)
blockIDHash, ok := jsonResponse["result"].(map[string]interface{})["block_id"].(map[string]interface{})["hash"].(string)
if !ok {
log.Error("Unable to access .block_id.hash")
return "", ""
}

// Access and print the .block.header.time field
blockTime, ok := response["result"].(map[string]interface{})["block"].(map[string]interface{})["header"].(map[string]interface{})["time"].(string)
blockTime, ok := jsonResponse["result"].(map[string]interface{})["block"].(map[string]interface{})["header"].(map[string]interface{})["time"].(string)
if !ok {
log.Error("Unable to access .block.header.time")
return "", ""
}

log.Info("Block ID Hash: ", blockIDHash)
log.Info("Block Time: ", blockTime)
log.Info("Full output: ", outputString)
log.Info("Full output: ", bodyString)

return blockIDHash, blockTime
}
Expand Down

0 comments on commit dab817d

Please sign in to comment.