Skip to content

Commit

Permalink
Test cleanup (#135)
Browse files Browse the repository at this point in the history
* version independent monerod path with localhost-only binding

* fixed lint target and install script

* created 3rd ETH key constant to avoid race condition in tests

* updated run-integration-tests.sh with the same changes made to run-unit-tests.sh

* new design for paritioning out 2 unique keys to test packages

* restored accidentally deleted tests/alice.key

* removed websocket connection leaks from tests

* made file paths unique between tests with better file cleanup

* fix for the websocket connection leak commit

* reverted increase of GenerateBlocks (didn't mean to commit that)

* fixed maker/taker key that I had reversed

* fixed incorrect zero-balance check

* fixed comment on ClaimOrRefund

* added back sample script for installing go in /usr/local/go

* etchclient creation cleanup using t.Cleanup()

* minor cleanup to ganache_test_keys code

* initial dynamic monero-wallet-rpc implementation for tests

* converted monero tests to use dynamic monero-wallet-rpc services

* unit tests all using dynamic monero-wallet-rpc services

* fixed 2 tests that failed after moving to dynamic monero-wallet-rpc services

* fixed low balance issues in TestSwapState_NotifyClaimed

Co-authored-by: noot <[email protected]>
  • Loading branch information
dimalinux and noot authored Jun 29, 2022
1 parent 68f1766 commit adfb0fa
Show file tree
Hide file tree
Showing 46 changed files with 518 additions and 243 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
*.dylib

# Monero dir
monero.tar.bz2
/monero-x86*
/monero.tar.bz2
/monero-*

# bin
swapcli
Expand Down Expand Up @@ -52,4 +52,4 @@ Cargo.lock

*.key
log
*.log
*.log
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
.PHONY: lint test install build build-dleq mock
all: build-dleq install

GOPATH ?= $(shell go env GOPATH)

lint:
./scripts/install_lint.sh
./scripts/install-lint.sh
${GOPATH}/bin/golangci-lint run

test:
Expand Down
5 changes: 3 additions & 2 deletions cmd/daemon/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math/big"
"path"

"github.com/noot/atomic-swap/common"
pcommon "github.com/noot/atomic-swap/protocol"
Expand All @@ -20,7 +21,7 @@ var (
errNoEthereumPrivateKey = errors.New("must provide --ethereum-privkey file for non-development environment")
)

func getOrDeploySwapFactory(address ethcommon.Address, env common.Environment, basepath string, chainID *big.Int,
func getOrDeploySwapFactory(address ethcommon.Address, env common.Environment, basePath string, chainID *big.Int,
privkey *ecdsa.PrivateKey, ec *ethclient.Client) (*swapfactory.SwapFactory, ethcommon.Address, error) {
var (
sf *swapfactory.SwapFactory
Expand All @@ -46,7 +47,7 @@ func getOrDeploySwapFactory(address ethcommon.Address, env common.Environment, b
log.Infof("deployed SwapFactory.sol: address=%s tx hash=%s", address, tx.Hash())

// store the contract address on disk
fp := fmt.Sprintf("%s/contractaddress", basepath)
fp := path.Join(basePath, "contractaddress")
if err = pcommon.WriteContractAddressToFile(fp, address.String()); err != nil {
return nil, ethcommon.Address{}, fmt.Errorf("failed to write contract address to file: %w", err)
}
Expand Down
10 changes: 7 additions & 3 deletions cmd/daemon/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/noot/atomic-swap/common"
"github.com/noot/atomic-swap/tests"

ethcommon "github.com/ethereum/go-ethereum/common"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
Expand All @@ -13,15 +14,18 @@ import (
)

func TestGetOrDeploySwapFactory(t *testing.T) {
pk, err := ethcrypto.HexToECDSA(common.DefaultPrivKeyXMRTaker)
pk, err := ethcrypto.HexToECDSA(tests.GetTakerTestKey(t))
require.NoError(t, err)

ec, err := ethclient.Dial(common.DefaultEthEndpoint)
require.NoError(t, err)
defer ec.Close()

tmpDir := t.TempDir()

_, addr, err := getOrDeploySwapFactory(ethcommon.Address{},
common.Development,
"/tmp",
tmpDir,
big.NewInt(common.GanacheChainID),
pk,
ec,
Expand All @@ -31,7 +35,7 @@ func TestGetOrDeploySwapFactory(t *testing.T) {

_, addr2, err := getOrDeploySwapFactory(addr,
common.Development,
"/tmp",
tmpDir,
big.NewInt(common.GanacheChainID),
pk,
ec,
Expand Down
7 changes: 5 additions & 2 deletions cmd/recover/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"testing"

Expand All @@ -16,6 +17,7 @@ import (
"github.com/noot/atomic-swap/protocol/xmrmaker"
"github.com/noot/atomic-swap/protocol/xmrtaker"
"github.com/noot/atomic-swap/swapfactory"
"github.com/noot/atomic-swap/tests"

"github.com/stretchr/testify/require"
"github.com/urfave/cli"
Expand Down Expand Up @@ -120,7 +122,7 @@ func createInfoFile(t *testing.T, kpA, kpB *mcrypto.PrivateKeyPair, contractAddr

bz, err := json.MarshalIndent(infofile, "", "\t")
require.NoError(t, err)
filepath := os.TempDir() + "/test-infofile.txt"
filepath := path.Join(t.TempDir(), "test-infofile.txt")
err = ioutil.WriteFile(filepath, bz, os.ModePerm)
require.NoError(t, err)
return filepath
Expand All @@ -136,10 +138,11 @@ func TestRecover_sharedSwapSecret(t *testing.T) {

c := newTestContext(t,
"test --xmrtaker with shared swap secret",
[]string{flagXMRTaker, flagInfoFile},
[]string{flagXMRTaker, flagInfoFile, flagMoneroWalletEndpoint},
[]interface{}{
true,
infoFilePath,
tests.CreateWalletRPCService(t),
},
)

Expand Down
16 changes: 6 additions & 10 deletions cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

logging "github.com/ipfs/go-log"
"github.com/urfave/cli"
Expand All @@ -28,19 +29,14 @@ var (

// GetEthereumPrivateKey returns an ethereum private key hex string given the CLI options.
func GetEthereumPrivateKey(c *cli.Context, env common.Environment, devXMRMaker,
useExternal bool) (ethPrivKey string, err error) {
useExternal bool) (ethPrivKeyHex string, err error) {
if c.String(flagEthereumPrivKey) != "" {
ethPrivKeyFile := c.String(flagEthereumPrivKey)
key, err := os.ReadFile(filepath.Clean(ethPrivKeyFile))
if err != nil {
return "", fmt.Errorf("failed to read ethereum-privkey file: %w", err)
}

if key[len(key)-1] == '\n' {
key = key[:len(key)-1]
}

ethPrivKey = string(key)
ethPrivKeyHex = strings.TrimSpace(string(key))
} else {
if env != common.Development || useExternal {
// TODO: allow this to be set via RPC
Expand All @@ -50,13 +46,13 @@ func GetEthereumPrivateKey(c *cli.Context, env common.Environment, devXMRMaker,

log.Warn("no ethereum private key file provided, using ganache deterministic key")
if devXMRMaker {
ethPrivKey = common.DefaultPrivKeyXMRMaker
ethPrivKeyHex = common.DefaultPrivKeyXMRMaker
} else {
ethPrivKey = common.DefaultPrivKeyXMRTaker
ethPrivKeyHex = common.DefaultPrivKeyXMRTaker
}
}

return ethPrivKey, nil
return ethPrivKeyHex, nil
}

// GetEnvironment returns a common.Environment from the CLI options.
Expand Down
4 changes: 2 additions & 2 deletions common/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const (
DefaultMoneroDaemonEndpoint = "http://127.0.0.1:18081/json_rpc"
DefaultEthEndpoint = "ws://localhost:8545"

// DefaultPrivKeyXMRTaker is the private key at index 0 from `ganache-cli -d`
// DefaultPrivKeyXMRTaker is the private key at index 0 from `ganache-cli --deterministic`
DefaultPrivKeyXMRTaker = "4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"

// DefaultPrivKeyXMRMaker is the private key at index 1 from `ganache-cli -d`
// DefaultPrivKeyXMRMaker is the private key at index 1 from `ganache-cli --deterministic`
DefaultPrivKeyXMRMaker = "6cbed15c793ce57650b9877cf6fa156fbef513c4e6134f022a85b1ffdd59b2a1"
)
4 changes: 2 additions & 2 deletions dleq/dleq.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ type FarcasterDLEq struct{}

// Prove generates a new DLEq proof
func (d *FarcasterDLEq) Prove() (*Proof, error) {
t := time.Now().Format("2006-Jan-2-15:04:05")
t := time.Now().Format("2006-01-02-15:04:05.999999999")
path := fmt.Sprintf("%s-%s", defaultProofPath, t)

cmd := exec.Command(dleqGenBinPath, path)
Expand Down Expand Up @@ -119,7 +119,7 @@ func (d *FarcasterDLEq) Prove() (*Proof, error) {

// Verify verifies a DLEq proof
func (d *FarcasterDLEq) Verify(p *Proof) (*VerifyResult, error) {
t := time.Now().Format("2006-Jan-2-15:04:05")
t := time.Now().Format("2006-01-02-15:04:05.999999999")
path := fmt.Sprintf("%s-verify-%s", defaultProofPath, t)

if err := ioutil.WriteFile(path, p.proof, os.ModePerm); err != nil {
Expand Down
16 changes: 11 additions & 5 deletions docs/build.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Building the project

1. Install go [here](https://go.dev/doc/install).
1. Install Golang

For Linux 64-bit:
On Ubuntu, the easiest way to keep up-to-date with the latest stable version of
Go is with snap:
```bash
wget https://go.dev/dl/go1.18.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.18.linux-amd64.tar.gz
sudo snap install go --classic
```
On other systems or in docker, use the directions here: https://go.dev/doc/install.
Summary for X86-64 Linux:
```bash
wget https://go.dev/dl/go1.18.3.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.18.3.linux-amd64.tar.gz
echo "export PATH=$PATH:/usr/local/go/bin" >> .profile
source .profile
```
Expand All @@ -21,4 +27,4 @@ cd atomic-swap
make build
```

This creates the binaries `swapd` and `swapcli`.
This creates the binaries `swapd` and `swapcli`.
22 changes: 14 additions & 8 deletions docs/local.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,29 @@
### Requirements

- go 1.17+ (see [build instructions](./build.md) to download Go.)
- ganache-cli (can be installed with `npm i -g ganache-cli`) I suggest using nvm to install npm: https://github.com/nvm-sh/nvm#installing-and-updating
- ganache (can be installed with `npm install --location=global ganache-cli`)

Note: this program has only been tested on Ubuntu 20.04.
These programs and scripts have only been tested on X86-64 Ubuntu 20.04 and 22.04.
Using nvm is [the suggested way](https://github.com/nvm-sh/nvm#installing-and-updating)
to install npm. If you install npm using a package manager like snap, ensure the install
prefix (`npm config get prefix`) is a directory that you have write access to without sudo.
You can change the directory with the command `npm config set prefix ~/.npm-packages`. See
[this document](https://github.com/sindresorhus/guides/blob/main/npm-global-without-sudo.md)
if you want a more sophisticated setup.

#### Set up development environment

Note: the `scripts/install-monero-linux.sh` script will download the monero binaries needed for you. You can also check out the `scripts/run-unit-tests.sh` script for the commands needed to setup the environment.

Start ganache-cli with determinstic keys:
Start ganache-cli with deterministic keys:
```bash
ganache-cli -d
ganache-cli --deterministic --accounts=20
```

Start monerod for regtest, this binary is in the monero bin directory:
```bash
cd ./monero-x86_64-linux-gnu
./monerod --regtest --fixed-difficulty=1 --rpc-bind-port 18081 --offline
cd ./monero-bin
./monerod --regtest --fixed-difficulty=1 --rpc-bind-ip 127.0.0.1 --rpc-bind-port 18081 --offline
```

Create a wallet for "Bob", who will own XMR later on:
Expand All @@ -31,7 +37,7 @@ You do not need to mine blocks, and you can exit the the wallet-cli once Bob's a

Start monero-wallet-rpc for Bob on port 18083. Make sure `--wallet-dir` corresponds to the directory the wallet from the previous step is in:
```bash
./monero-wallet-rpc --rpc-bind-port 18083 --password "" --disable-rpc-login --wallet-dir .
./monero-wallet-rpc --rpc-bind-ip 127.0.0.1 --rpc-bind-port 18083 --password "" --disable-rpc-login --wallet-dir .
```

Open the wallet:
Expand Down Expand Up @@ -61,7 +67,7 @@ This will deposit some XMR in Bob's account.

Start monero-wallet-rpc for Alice on port 18084 (note that the directory provided to `--wallet-dir` is where Alice's XMR wallet will end up):
```bash
./monero-wallet-rpc --rpc-bind-port 18084 --password "" --disable-rpc-login --wallet-dir .
./monero-wallet-rpc --rpc-bind-ip 127.0.0.1 --rpc-bind-port 18084 --password "" --disable-rpc-login --wallet-dir .
```
#### Build and run

Expand Down
Loading

0 comments on commit adfb0fa

Please sign in to comment.