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

Simulation framework for multi-client server testing #358

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
.vscode/

*.wasm
wasm_exec.js
wasm_exec.js

.idea
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use (
./server
./server/pkg/kvdb
./server/pkg/macaroons
./simulation
)

replace github.com/btcsuite/btcd/btcec/v2 => github.com/btcsuite/btcd/btcec/v2 v2.3.3
111 changes: 1 addition & 110 deletions server/test/e2e/covenantless/e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package e2e_test

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"testing"
"time"
Expand Down Expand Up @@ -41,7 +39,7 @@ func TestMain(m *testing.M) {
os.Exit(1)
}

if err := setupAspWallet(); err != nil {
if err := utils.SetupServerWalletCovenantless(0.0); err != nil {
fmt.Println(err)
os.Exit(1)
}
Expand Down Expand Up @@ -398,113 +396,6 @@ func runClarkCommand(arg ...string) (string, error) {
return utils.RunCommand("docker", args...)
}

func setupAspWallet() error {
adminHttpClient := &http.Client{
Timeout: 15 * time.Second,
}

req, err := http.NewRequest("GET", "http://localhost:7070/v1/admin/wallet/seed", nil)
if err != nil {
return fmt.Errorf("failed to prepare generate seed request: %s", err)
}
req.Header.Set("Authorization", "Basic YWRtaW46YWRtaW4=")

seedResp, err := adminHttpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to generate seed: %s", err)
}

var seed struct {
Seed string `json:"seed"`
}

if err := json.NewDecoder(seedResp.Body).Decode(&seed); err != nil {
return fmt.Errorf("failed to parse response: %s", err)
}

reqBody := bytes.NewReader([]byte(fmt.Sprintf(`{"seed": "%s", "password": "%s"}`, seed.Seed, utils.Password)))
req, err = http.NewRequest("POST", "http://localhost:7070/v1/admin/wallet/create", reqBody)
if err != nil {
return fmt.Errorf("failed to prepare wallet create request: %s", err)
}
req.Header.Set("Authorization", "Basic YWRtaW46YWRtaW4=")
req.Header.Set("Content-Type", "application/json")

if _, err := adminHttpClient.Do(req); err != nil {
return fmt.Errorf("failed to create wallet: %s", err)
}

reqBody = bytes.NewReader([]byte(fmt.Sprintf(`{"password": "%s"}`, utils.Password)))
req, err = http.NewRequest("POST", "http://localhost:7070/v1/admin/wallet/unlock", reqBody)
if err != nil {
return fmt.Errorf("failed to prepare wallet unlock request: %s", err)
}
req.Header.Set("Authorization", "Basic YWRtaW46YWRtaW4=")
req.Header.Set("Content-Type", "application/json")

if _, err := adminHttpClient.Do(req); err != nil {
return fmt.Errorf("failed to unlock wallet: %s", err)
}

var status struct {
Initialized bool `json:"initialized"`
Unlocked bool `json:"unlocked"`
Synced bool `json:"synced"`
}
for {
time.Sleep(time.Second)

req, err := http.NewRequest("GET", "http://localhost:7070/v1/admin/wallet/status", nil)
if err != nil {
return fmt.Errorf("failed to prepare status request: %s", err)
}
resp, err := adminHttpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to get status: %s", err)
}
if err := json.NewDecoder(resp.Body).Decode(&status); err != nil {
return fmt.Errorf("failed to parse status response: %s", err)
}
if status.Initialized && status.Unlocked && status.Synced {
break
}
}

var addr struct {
Address string `json:"address"`
}
for addr.Address == "" {
time.Sleep(time.Second)

req, err = http.NewRequest("GET", "http://localhost:7070/v1/admin/wallet/address", nil)
if err != nil {
return fmt.Errorf("failed to prepare new address request: %s", err)
}
req.Header.Set("Authorization", "Basic YWRtaW46YWRtaW4=")

resp, err := adminHttpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to get new address: %s", err)
}

if err := json.NewDecoder(resp.Body).Decode(&addr); err != nil {
return fmt.Errorf("failed to parse response: %s", err)
}
}

const numberOfFaucet = 15 // must cover the liquidity needed for all tests

for i := 0; i < numberOfFaucet; i++ {
_, err = utils.RunCommand("nigiri", "faucet", addr.Address)
if err != nil {
return fmt.Errorf("failed to fund wallet: %s", err)
}
}

time.Sleep(5 * time.Second)
return nil
}

func setupArkSDK(t *testing.T) (arksdk.ArkClient, client.ASPClient) {
appDataStore, err := store.NewStore(store.Config{
ConfigStoreType: types.FileStore,
Expand Down
122 changes: 122 additions & 0 deletions server/test/e2e/test_utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package e2e

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os/exec"
"strings"
"sync"
Expand Down Expand Up @@ -104,3 +107,122 @@ func newCommand(name string, arg ...string) *exec.Cmd {
cmd := exec.Command(name, arg...)
return cmd
}

func SetupServerWalletCovenantless(initFunding float64) error {
adminHttpClient := &http.Client{
Timeout: 15 * time.Second,
}

req, err := http.NewRequest("GET", "http://localhost:7070/v1/admin/wallet/seed", nil)
if err != nil {
return fmt.Errorf("failed to prepare generate seed request: %s", err)
}
req.Header.Set("Authorization", "Basic YWRtaW46YWRtaW4=")

seedResp, err := adminHttpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to generate seed: %s", err)
}

var seed struct {
Seed string `json:"seed"`
}

if err := json.NewDecoder(seedResp.Body).Decode(&seed); err != nil {
return fmt.Errorf("failed to parse response: %s", err)
}

reqBody := bytes.NewReader([]byte(fmt.Sprintf(`{"seed": "%s", "password": "%s"}`, seed.Seed, Password)))
req, err = http.NewRequest("POST", "http://localhost:7070/v1/admin/wallet/create", reqBody)
if err != nil {
return fmt.Errorf("failed to prepare wallet create request: %s", err)
}
req.Header.Set("Authorization", "Basic YWRtaW46YWRtaW4=")
req.Header.Set("Content-Type", "application/json")

if _, err := adminHttpClient.Do(req); err != nil {
return fmt.Errorf("failed to create wallet: %s", err)
}

reqBody = bytes.NewReader([]byte(fmt.Sprintf(`{"password": "%s"}`, Password)))
req, err = http.NewRequest("POST", "http://localhost:7070/v1/admin/wallet/unlock", reqBody)
if err != nil {
return fmt.Errorf("failed to prepare wallet unlock request: %s", err)
}
req.Header.Set("Authorization", "Basic YWRtaW46YWRtaW4=")
req.Header.Set("Content-Type", "application/json")

if _, err := adminHttpClient.Do(req); err != nil {
return fmt.Errorf("failed to unlock wallet: %s", err)
}

var status struct {
Initialized bool `json:"initialized"`
Unlocked bool `json:"unlocked"`
Synced bool `json:"synced"`
}
for {
time.Sleep(time.Second)

req, err := http.NewRequest("GET", "http://localhost:7070/v1/admin/wallet/status", nil)
if err != nil {
return fmt.Errorf("failed to prepare status request: %s", err)
}
resp, err := adminHttpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to get status: %s", err)
}
if err := json.NewDecoder(resp.Body).Decode(&status); err != nil {
return fmt.Errorf("failed to parse status response: %s", err)
}
if status.Initialized && status.Unlocked && status.Synced {
break
}
}

var addr struct {
Address string `json:"address"`
}
for addr.Address == "" {
time.Sleep(time.Second)

req, err = http.NewRequest("GET", "http://localhost:7070/v1/admin/wallet/address", nil)
if err != nil {
return fmt.Errorf("failed to prepare new address request: %s", err)
}
req.Header.Set("Authorization", "Basic YWRtaW46YWRtaW4=")

resp, err := adminHttpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to get new address: %s", err)
}

if err := json.NewDecoder(resp.Body).Decode(&addr); err != nil {
return fmt.Errorf("failed to parse response: %s", err)
}
}

if initFunding == 0.0 {
const numberOfFaucet = 15 // must cover the liquidity needed for all tests
for i := 0; i < numberOfFaucet; i++ {
_, err = RunCommand("nigiri", "faucet", addr.Address)
if err != nil {
return fmt.Errorf("failed to fund wallet: %s", err)
}
}

} else {
chunkSize := initFunding / 5
for i := 0; i < 5; i++ {
amount := fmt.Sprintf("%.8f", chunkSize)
_, err = RunCommand("nigiri", "faucet", addr.Address, amount)
if err != nil {
return fmt.Errorf("failed to fund wallet chunk %d: %s", i+1, err)
}
time.Sleep(1 * time.Second)
}
}

time.Sleep(5 * time.Second)
return nil
}
6 changes: 6 additions & 0 deletions simulation/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.PHONY: run

SIMULATION ?= simulation1.yaml

run:
go run main.go -simulation=$(SIMULATION)
26 changes: 26 additions & 0 deletions simulation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Simulation Framework for Server Testing

This simulation framework is designed to test the **Ark Server** by simulating multiple clients performing
various actions over several rounds. It reads simulation configurations from YAML files, validates them against a predefined schema,
and executes the simulation accordingly.

## Usage

### 1. Start Nigiri

Ensure that Nigiri (a Bitcoin Regtest environment) is running:

```sh
nigiri start
```

#### 2. Configure the Testing Scenario
Create a simulation YAML file based on the [schema.yaml](./schema.yaml). You can refer to the [this](./simulation1.yaml) example for guidance.

#### 3. Run simulation

```sh
make run SIMULATION=your_simulation.yaml
```

Replace your_simulation.yaml with the path to your simulation configuration file.
22 changes: 22 additions & 0 deletions simulation/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module github.com/ark-network/ark/simulation

go 1.23.1

require (
github.com/sirupsen/logrus v1.9.3
github.com/xeipuuv/gojsonschema v1.2.0
sigs.k8s.io/yaml v1.4.0
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
golang.org/x/sys v0.24.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
15 changes: 15 additions & 0 deletions simulation/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
Loading
Loading