Skip to content

Commit

Permalink
Merge branch 'main' into rp/fix-event-signer
Browse files Browse the repository at this point in the history
  • Loading branch information
rootulp authored Nov 1, 2023
2 parents 1a070e9 + c34a93d commit 1641907
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 8 deletions.
16 changes: 14 additions & 2 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ on:
schedule:
# runs every day at 6am UTC
- cron: "0 6 * * *"
workflow_dispatch:

env:
GO_VERSION: '1.21.1'

jobs:
e2e-tests:
test-e2e:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -23,6 +24,17 @@ jobs:
run: |
mkdir -p $HOME/.kube
echo "${KUBECONFIG_FILE}" > $HOME/.kube/config
- name: Run e2e tests
run: make test-e2e

test-fuzz:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}

- name: Run fuzz tests
run: make test-fuzz
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ test-short:

## test-e2e: Run end to end tests via knuu.
test-e2e:
@version=$(git rev-parse --short HEAD)
@echo "--> Running e2e tests on version: $version"
@KNUU_NAMESPACE=test E2E_VERSION=$version E2E=true go test ./test/e2e/... -timeout 30m
@echo "--> Running e2e tests on version: $(shell git rev-parse --short HEAD)"
@KNUU_NAMESPACE=test E2E_VERSION=$(shell git rev-parse --short HEAD) E2E=true go test ./test/e2e/... -timeout 10m -v
.PHONY: test-e2e

## test-race: Run tests in race mode.
Expand All @@ -142,6 +141,10 @@ test-coverage:
@export VERSION=$(VERSION); bash -x scripts/test_cover.sh
.PHONY: test-coverage

test-fuzz:
bash -x scripts/test_fuzz.sh
.PHONY: test-fuzz

## txsim-install: Install the tx simulator.
txsim-install:
@echo "--> Installing tx simulator"
Expand Down
6 changes: 4 additions & 2 deletions cmd/celestia-appd/cmd/addrbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ func addrbookCommand() *cobra.Command {
}
address, err := p2p.NewNetAddressString(line)
if err != nil {
return err
fmt.Printf("Error parsing %s: %s\n", line, err)
continue
}
err = book.AddAddress(address, address)
if err != nil {
return err
fmt.Printf("Error adding %s: %s\n", address, err)
continue
}
}

Expand Down
128 changes: 128 additions & 0 deletions cmd/celestia-appd/cmd/download-genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package cmd

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"net/http"
"os"

"github.com/cosmos/cosmos-sdk/server"
"github.com/spf13/cobra"
)

var chainIDToSha256 = map[string]string{
"celestia": "9727aac9bbfb021ce7fc695a92f901986421283a891b89e0af97bc9fad187793",
"mocha-4": "0846b99099271b240b638a94e17a6301423b5e4047f6558df543d6e91db7e575",
"arabica-10": "fad0a187669f7a2c11bb07f9dc27140d66d2448b7193e186312713856f28e3e1",
}

func downloadGenesisCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "download-genesis [chain-id]",
Short: "Download genesis file from https://github.com/celestiaorg/networks",
Long: "Download genesis file from https://github.com/celestiaorg/networks.\n" +
"The first argument should be a known chain-id. Ex. celestia, mocha-4, or arabica-10.\n" +
"If no argument is provided, defaults to celestia.\n",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
chainID := getChainIDOrDefault(args)
if !isKnownChainID(chainID) {
return fmt.Errorf("unknown chain-id: %s. Must be: celestia, mocha-4, or arabica-10", chainID)
}
outputFile := server.GetServerContextFromCmd(cmd).Config.GenesisFile()
fmt.Printf("Downloading genesis file for %s to %s\n", chainID, outputFile)

url := fmt.Sprintf("https://raw.githubusercontent.com/celestiaorg/networks/master/%s/genesis.json", chainID)
if err := downloadFile(outputFile, url); err != nil {
return fmt.Errorf("error downloading / persisting the genesis file: %s", err)
}
fmt.Printf("Downloaded genesis file for %s to %s\n", chainID, outputFile)

// Compute SHA-256 hash of the downloaded file
hash, err := computeSha256(outputFile)
if err != nil {
return fmt.Errorf("error computing sha256 hash: %s", err)
}

// Compare computed hash against known hash
knownHash, ok := chainIDToSha256[chainID]
if !ok {
return fmt.Errorf("unknown chain-id: %s", chainID)
}

if hash != knownHash {
return fmt.Errorf("sha256 hash mismatch: got %s, expected %s", hash, knownHash)
}

fmt.Printf("SHA-256 hash verified for %s\n", chainID)
return nil
},
}

return cmd
}

// getChainIDOrDefault returns the chainID from the command line arguments. If
// none is provided, defaults to celestia (mainnet).
func getChainIDOrDefault(args []string) string {
if len(args) == 1 {
return args[0]
}
return "celestia"
}

// isKnownChainID returns true if the chainID is known.
func isKnownChainID(chainID string) bool {
knownChainIDs := []string{
"arabica-10", // testnet
"mocha-4", // testnet
"celestia", // mainnet
}
return contains(knownChainIDs, chainID)
}

// contains checks if a string is present in a slice.
func contains(slice []string, s string) bool {
for _, v := range slice {
if v == s {
return true
}
}
return false
}

// downloadFile will download a URL to a local file.
func downloadFile(filepath string, url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()

_, err = io.Copy(out, resp.Body)
return err
}

// computeSha256 computes the SHA-256 hash of a file.
func computeSha256(filepath string) (string, error) {
f, err := os.Open(filepath)
if err != nil {
return "", err
}
defer f.Close()

hasher := sha256.New()
if _, err := io.Copy(hasher, f); err != nil {
return "", err
}

return hex.EncodeToString(hasher.Sum(nil)), nil
}
1 change: 1 addition & 0 deletions cmd/celestia-appd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig encoding.Config) {
config.Cmd(),
commands.CompactGoLevelDBCmd,
addrbookCommand(),
downloadGenesisCommand(),
)

server.AddCommands(rootCmd, app.DefaultNodeHome, NewAppServer, createAppAndExport, addModuleInitFlags)
Expand Down
10 changes: 10 additions & 0 deletions scripts/test_fuzz.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -e

echo "Running fuzz tests..."
# manually specify the tests to fuzz since go toolchain doesn't support
# fuzzing multiple packages with multiple fuzz tests
go test -fuzz=FuzzNewInfoByte -fuzztime 1m ./pkg/shares
go test -fuzz=FuzzValidSequenceLen -fuzztime 1m ./pkg/shares
go test -fuzz=FuzzSquare -fuzztime 5m ./pkg/square
go test -fuzz=FuzzPFBGasEstimation -fuzztime 3m ./x/blob/types
2 changes: 1 addition & 1 deletion test/e2e/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

const seed = 42

var latestVersion = "v1.0.0-rc12"
var latestVersion = "latest"

// This test runs a simple testnet with 4 validators. It submits both MsgPayForBlobs
// and MsgSends over 30 seconds and then asserts that at least 10 transactions were
Expand Down
62 changes: 62 additions & 0 deletions test/ledger/ledger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ledger

import (
"bytes"
"os"
"os/exec"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLedgerSupport(t *testing.T) {
if testing.Short() {
t.Skip("skipping ledger support test in short mode.")
}

type testCase struct {
name string
ledger bool
want string
}

testCases := []testCase{
{
name: "ledger support enabled",
ledger: true,
want: "Error: failed to generate ledger key: failed to retrieve device: ledger nano S: LedgerHID device (idx 0) not found. Ledger LOCKED OR Other Program/Web Browser may have control of device.\n",
},
{
name: "ledger support disabled",
ledger: false,
want: "Error: failed to generate ledger key: failed to retrieve device: ledger nano S: support for ledger devices is not available in this executable\n",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var cmd *exec.Cmd
if tc.ledger {
// Generate the binary with Ledger support
cmd = exec.Command("go", "build", "-tags", "ledger", "-o", "celestia-appd", "../../cmd/celestia-appd")
} else {
// Generate the binary without Ledger support
cmd = exec.Command("go", "build", "-o", "celestia-appd", "../../cmd/celestia-appd")
}
err := cmd.Run()
require.NoError(t, err)

// Clean up the binary
defer os.Remove("celestia-appd")

// Run the binary
cmd = exec.Command("./celestia-appd", "keys", "add", "test-key-name", "--ledger")
var out bytes.Buffer
cmd.Stderr = &out
err = cmd.Run()
require.Error(t, err)
assert.Equal(t, tc.want, out.String())
})
}
}

0 comments on commit 1641907

Please sign in to comment.