-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from informalsystems/mergify/bp/v0.37.x/pr-60
Add abci_info implementation (backport #60)
- Loading branch information
Showing
12 changed files
with
252 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Automated Tests | ||
on: | ||
push: | ||
branches: | ||
- main | ||
- v0.37.x | ||
- v0.34.x | ||
pull_request: | ||
branches: | ||
- main | ||
- v0.37.x | ||
- v0.34.x | ||
jobs: | ||
Automated_Tests: | ||
runs-on: ubuntu-latest | ||
steps: | ||
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it | ||
- uses: actions/checkout@v4 | ||
- name: Make test | ||
run: make test-docker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# import simd from ibc-go | ||
FROM ghcr.io/cosmos/simapp:v0.47 AS simapp-builder | ||
|
||
FROM golang:1.20-alpine as cometmock-builder | ||
|
||
ENV PACKAGES curl make git libc-dev bash gcc linux-headers | ||
RUN apk add --no-cache $PACKAGES | ||
|
||
ENV CGO_ENABLED=0 | ||
ENV GOOS=linux | ||
ENV GOFLAGS="-buildvcs=false" | ||
|
||
# cache gomodules for cometmock | ||
ADD ./go.mod /go.mod | ||
ADD ./go.sum /go.sum | ||
RUN go mod download | ||
|
||
# Add CometMock and install it | ||
ADD . /CometMock | ||
WORKDIR /CometMock | ||
RUN go build -o /usr/local/bin/cometmock ./cometmock | ||
|
||
RUN apk update | ||
RUN apk add --no-cache which iputils procps-ng tmux net-tools htop jq gcompat | ||
|
||
COPY --from=simapp-builder /usr/bin/simd /usr/local/bin/simd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,13 @@ | ||
install: | ||
go install ./cometmock | ||
go install ./cometmock | ||
|
||
test-locally: | ||
go test -timeout 600s ./e2e-tests -test.v | ||
|
||
test-docker: | ||
# Build the Docker image | ||
docker build -f Dockerfile-test -t cometmock-test . | ||
|
||
# Start a container and execute the test command inside | ||
docker rm cometmock-test-instance || true | ||
docker run --name cometmock-test-instance --workdir /CometMock cometmock-test go test -timeout 600s ./e2e-tests -test.v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"os/exec" | ||
"strconv" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func runCommandWithOutput(cmd *exec.Cmd) (string, error) { | ||
var stdout, stderr bytes.Buffer | ||
cmd.Stdout = &stdout | ||
cmd.Stderr = &stderr | ||
|
||
err := cmd.Run() | ||
if err != nil { | ||
return "", fmt.Errorf("error running command: %v\nstdout: %s\nstderr: %s", err, stdout.String(), stderr.String()) | ||
} | ||
|
||
return stdout.String(), nil | ||
} | ||
|
||
// From the output of the AbciInfo command, extract the latest block height. | ||
// The json bytes should look e.g. like this: | ||
// {"jsonrpc":"2.0","id":1,"result":{"response":{"data":"interchain-security-p","last_block_height":"2566","last_block_app_hash":"R4Q3Si7+t7TIidl2oTHcQRDNEz+lP0IDWhU5OI89psg="}}} | ||
func extractHeightFromInfo(jsonBytes []byte) (int, error) { | ||
// Use a generic map to represent the JSON structure | ||
var data map[string]interface{} | ||
|
||
if err := json.Unmarshal(jsonBytes, &data); err != nil { | ||
return -1, fmt.Errorf("Failed to unmarshal JSON %s \n error was %v", string(jsonBytes), err) | ||
} | ||
|
||
// Navigate the map and use type assertions to get the last_block_height | ||
result, ok := data["result"].(map[string]interface{}) | ||
if !ok { | ||
return -1, fmt.Errorf("Failed to navigate abci_info output structure trying to access result: json was %s", string(jsonBytes)) | ||
} | ||
|
||
response, ok := result["response"].(map[string]interface{}) | ||
if !ok { | ||
return -1, fmt.Errorf("Failed to navigate abci_info output structure trying to access response: json was %s", string(jsonBytes)) | ||
} | ||
|
||
lastBlockHeight, ok := response["last_block_height"].(string) | ||
if !ok { | ||
return -1, fmt.Errorf("Failed to navigate abci_info output structure trying to access last_block_height: json was %s", string(jsonBytes)) | ||
} | ||
|
||
return strconv.Atoi(lastBlockHeight) | ||
} | ||
|
||
// Tests happy path functionality for Abci Info. | ||
func TestAbciInfo(t *testing.T) { | ||
// execute the local-testnet-singlechain.sh script | ||
t.Log("Running local-testnet-singlechain.sh") | ||
cmd := exec.Command("./local-testnet-singlechain.sh", "simd") | ||
_, err := runCommandWithOutput(cmd) | ||
if err != nil { | ||
t.Fatalf("Error running local-testnet-singlechain.sh: %v", err) | ||
} | ||
|
||
t.Log("Done starting testnet") | ||
|
||
// wait until we are producing blocks | ||
for { | ||
out, err := exec.Command("bash", "-c", "simd q block --node tcp://127.0.0.1:22331 | jq -r '.block.header.height'").Output() | ||
if err == nil { | ||
t.Log("We are producing blocks: ", string(out)) | ||
break | ||
} | ||
t.Log("Waiting for blocks to be produced, latest output: ", string(out)) | ||
time.Sleep(1 * time.Second) | ||
} | ||
|
||
// call the abci_info command by calling curl on the REST endpoint | ||
// curl -H 'Content-Type: application/json' -H 'Accept:application/json' --data '{"jsonrpc":"2.0","method":"abci_info","id":1}' 127.0.0.1:22331 | ||
args := []string{"bash", "-c", "curl -H 'Content-Type: application/json' -H 'Accept:application/json' --data '{\"jsonrpc\":\"2.0\",\"method\":\"abci_info\",\"id\":1}' 127.0.0.1:22331"} | ||
cmd = exec.Command(args[0], args[1:]...) | ||
out, err := runCommandWithOutput(cmd) | ||
if err != nil { | ||
t.Fatalf("Error running curl\ncommand: %v\noutput: %v\nerror: %v", cmd, string(out), err) | ||
} | ||
|
||
// extract the latest block height from the output | ||
height, err := extractHeightFromInfo([]byte(out)) | ||
if err != nil { | ||
t.Fatalf("Error extracting block height from abci_info output: %v", err) | ||
} | ||
|
||
// wait a bit to make sure the block height has increased | ||
time.Sleep(2 * time.Second) | ||
|
||
// call the abci_info command again | ||
cmd2 := exec.Command(args[0], args[1:]...) | ||
out2, err := runCommandWithOutput(cmd2) | ||
if err != nil { | ||
t.Fatalf("Error running curl\ncommand: %v\noutput: %v\nerror: %v", cmd2, string(out2), err) | ||
} | ||
|
||
// extract the latest block height from the output | ||
height2, err := extractHeightFromInfo([]byte(out2)) | ||
if err != nil { | ||
t.Fatalf("Error extracting block height from abci_info output: %v", err) | ||
} | ||
|
||
// check that the block height has increased | ||
if height2 <= height { | ||
t.Fatalf("Expected block height to increase, but it did not. First height was %v, second height was %v", height, height2) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.