add ci checks for build, test, lint; lint code; change filenames to be go-idiomatic (#12)

This commit is contained in:
noot
2021-10-25 23:20:14 +01:00
committed by GitHub
parent 75160bb642
commit e9057490f9
21 changed files with 1123 additions and 994 deletions

1
.github/CODEOWNERS vendored Normal file
View File

@@ -0,0 +1 @@
@noot

28
.github/workflows/checks.yml vendored Normal file
View File

@@ -0,0 +1,28 @@
on: [pull_request]
name: checks
jobs:
linter-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run go fmt
run: diff -u <(echo -n) <(gofmt -d -s .)
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: 'latest'
args: -v
vet-check:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v2
with:
go-version: '1.15.x'
- uses: actions/checkout@v2
- name: Run go vet
run: go vet ./...

42
.github/workflows/unit-tests.yml vendored Normal file
View File

@@ -0,0 +1,42 @@
on: [pull_request]
name: unit-tests
jobs:
unit-tests:
strategy:
matrix:
go-version: [1.17.x]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
- id: go-cache-paths
run: |
echo "::set-output name=go-build::$(go env GOCACHE)"
echo "::set-output name=go-mod::$(go env GOMODCACHE)"
- uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- uses: actions/checkout@v2
# cache go build cache
- name: Cache go modules
uses: actions/cache@v2
with:
path: ${{ steps.go-cache-paths.outputs.go-build }}
key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.sum') }}
restore-keys: ${{ runner.os }}-go-build
# cache go mod cache
- name: Cache go modules
uses: actions/cache@v2
with:
path: ${{ steps.go-cache-paths.outputs.go-mod }}
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}
restore-keys: ${{ runner.os }}-go-mod
- name: Run build
run: make build
- name: Run unit tests
run: go test -short ./... -timeout=30m

View File

@@ -20,12 +20,13 @@ import (
logging "github.com/ipfs/go-log"
)
var _ Alice = &alice{}
var log = logging.Logger("alice")
const (
keyAlice = "4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"
keyAlice = "4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d" //nolint
)
var (
_ Alice = &alice{}
log = logging.Logger("alice")
)
func reverse(s []byte) []byte {
@@ -72,7 +73,7 @@ type Alice interface {
type alice struct {
ctx context.Context
t0, t1 time.Time
t0, t1 time.Time //nolint
privkeys *monero.PrivateKeyPair
pubkeys *monero.PublicKeyPair

View File

@@ -20,18 +20,12 @@ import (
logging "github.com/ipfs/go-log"
)
var log = logging.Logger("bob")
const defaultDaemonEndpoint = "http://127.0.0.1:18081/json_rpc"
func reverse(s []byte) []byte {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
var _ Bob = &bob{}
var (
_ Bob = &bob{}
log = logging.Logger("bob")
)
// Bob contains the functions that will be called by a user who owns XMR
// and wishes to swap for ETH.
@@ -73,7 +67,7 @@ type Bob interface {
type bob struct {
ctx context.Context
t0, t1 time.Time
t0, t1 time.Time //nolint
privkeys *monero.PrivateKeyPair
pubkeys *monero.PublicKeyPair

View File

@@ -15,7 +15,9 @@ func (n *node) doProtocolAlice() error {
if err := n.host.Start(); err != nil {
return err
}
defer n.host.Stop()
defer func() {
_ = n.host.Stop()
}()
outCh := make(chan *net.MessageInfo)
n.host.SetOutgoingCh(outCh)
@@ -26,6 +28,7 @@ func (n *node) doProtocolAlice() error {
// can move on to just watching the contract
setupDone := make(chan struct{})
var done bool
for {
select {
case <-n.done:
@@ -35,6 +38,10 @@ func (n *node) doProtocolAlice() error {
log.Error("failed to handle message: error=", err)
}
case <-setupDone:
done = true
}
if done {
break
}
}

View File

@@ -16,7 +16,9 @@ func (n *node) doProtocolBob() error {
if err := n.host.Start(); err != nil {
return err
}
defer n.host.Stop()
defer func() {
_ = n.host.Stop()
}()
outCh := make(chan *net.MessageInfo)
n.host.SetOutgoingCh(outCh)
@@ -27,6 +29,7 @@ func (n *node) doProtocolBob() error {
// can move on to just watching the contract
setupDone := make(chan struct{})
var done bool
for {
select {
case <-n.done:
@@ -36,6 +39,10 @@ func (n *node) doProtocolBob() error {
log.Info("failed to handle message: error=%s\n", err)
}
case <-setupDone:
done = true
}
if done {
break
}
}

15
makefile Normal file
View File

@@ -0,0 +1,15 @@
.PHONY: lint test install build
all: install
lint:
./scripts/install_lint.sh
${GOPATH}/bin/golangci-lint run
test:
go test ./...
install:
cd cmd/ && go install && cd ..
build:
./scripts/build.sh

View File

@@ -59,7 +59,8 @@ func TestClient_Transfer(t *testing.T) {
// transfer to account A+B
err = cA.Transfer(kpABPub.Address(), 0, amount)
require.NoError(t, err)
daemon.callGenerateBlocks(aliceAddress.Address, 1)
err = daemon.callGenerateBlocks(aliceAddress.Address, 1)
require.NoError(t, err)
for {
t.Log("checking balance...")
@@ -72,11 +73,13 @@ func TestClient_Transfer(t *testing.T) {
break
}
daemon.callGenerateBlocks(aliceAddress.Address, 1)
err = daemon.callGenerateBlocks(aliceAddress.Address, 1)
require.NoError(t, err)
time.Sleep(time.Second)
}
daemon.callGenerateBlocks(aliceAddress.Address, 16)
err = daemon.callGenerateBlocks(aliceAddress.Address, 16)
require.NoError(t, err)
// generate spend account for A+B
skAKPriv := SumPrivateSpendKeys(kpA.sk, kpB.sk)

View File

@@ -7,13 +7,6 @@ import (
"github.com/stretchr/testify/require"
)
func reverse(s []byte) []byte {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
func TestPrivateKeyPairToAddress(t *testing.T) {
skBytes := "a6e51afb9662bf2173d807ceaf88938d09a82d1ab2cea3eeb1706eeeb8b6ba03"
pskBytes := "57edf916a28c2a0a172565468564ab1c5c217d33ea63436f7604a96aa28ec471"

View File

@@ -24,7 +24,7 @@ type generateFromKeysRequest struct {
type generateFromKeysResponse struct {
Address string `json:"address"`
Info string `json:"info`
Info string `json:"info"`
}
func (c *client) callGenerateFromKeys(sk *PrivateSpendKey, vk *PrivateViewKey, address Address, filename, password string) error {

View File

@@ -64,8 +64,6 @@ type host struct {
}
func NewHost(port uint64, want, keyfile string, bootnodes []string) (*host, error) {
ctx, cancel := context.WithCancel(context.Background())
key, err := loadKey(keyfile)
if err != nil {
fmt.Println("failed to load libp2p key, generating key...", keyfile)
@@ -95,22 +93,21 @@ func NewHost(port uint64, want, keyfile string, bootnodes []string) (*host, erro
}
// create libp2p host instance
h, err := libp2p.New(ctx, opts...)
h, err := libp2p.New(context.Background(), opts...)
if err != nil {
return nil, err
}
inCh := make(chan *MessageInfo)
ctx, cancel := context.WithCancel(context.Background())
return &host{
ctx: ctx,
cancel: cancel,
h: h,
wantMessage: &WantMessage{Want: want},
//mdns: newMDNS(h),
//discovery: discovery,
bootnodes: bns,
inCh: inCh,
bootnodes: bns,
inCh: inCh,
}, nil
}

View File

@@ -1,10 +1,11 @@
#!/bin/bash
$SOLC_BIN --abi ethereum/contracts/SwapOnChain.sol -o ethereum/abi/ --overwrite
$SOLC_BIN --bin ethereum/contracts/SwapOnChain.sol -o ethereum/bin/ --overwrite
abigen --abi ethereum/abi/SwapOnChain.abi --pkg swapOnChain --type SwapOnChain --out swapOnChain.go --bin ethereum/bin/SwapOnChain.bin
mv swapOnChain.go ./swapOnChain-contract
$SOLC_BIN --abi ethereum/contracts/Swap.sol -o ethereum/abi/ --overwrite
$SOLC_BIN --bin ethereum/contracts/Swap.sol -o ethereum/bin/ --overwrite
abigen --abi ethereum/abi/Swap.abi --pkg swap --type Swap --out swap.go --bin ethereum/bin/Swap.bin
mv swap.go ./swap-contract
$SOLC_BIN --abi ethereum/contracts/SwapDLEQ.sol -o ethereum/abi/ --overwrite
$SOLC_BIN --bin ethereum/contracts/SwapDLEQ.sol -o ethereum/bin/ --overwrite
abigen --abi ethereum/abi/SwapDLEQ.abi --pkg swapDLEQ --type SwapDLEQ --out swapDLEQ.go --bin ethereum/bin/SwapDLEQ.bin
mv swapDLEQ.go ./swapDLEQ-contract
abigen --abi ethereum/abi/SwapDLEQ.abi --pkg swapdleq --type SwapDLEQ --out swap_dleq.go --bin ethereum/bin/SwapDLEQ.bin
mv swap_dleq.go ./swap-dleq-contract

12
scripts/install_lint.sh Executable file
View File

@@ -0,0 +1,12 @@
#!/bin/bash
if [[ -z "${GOPATH}" ]]; then
export GOPATH=~/go
fi
if ! command -v golangci-lint &> /dev/null
then
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.41.0
fi
export PATH=$PATH:$(go env GOPATH)/bin

925
swap-contract/swap.go Normal file

File diff suppressed because one or more lines are too long

View File

@@ -1,4 +1,4 @@
package swapOnChain
package swap
import (
"context"
@@ -28,12 +28,12 @@ func reverse(s []byte) []byte {
return s
}
func setBigIntLE(s []byte) *big.Int {
func setBigIntLE(s []byte) *big.Int { //nolint
s = reverse(s)
return big.NewInt(0).SetBytes(s)
}
func TestDeploySwapOnChain(t *testing.T) {
func TestDeploySwap(t *testing.T) {
conn, err := ethclient.Dial("http://127.0.0.1:8545")
require.NoError(t, err)
@@ -43,7 +43,7 @@ func TestDeploySwapOnChain(t *testing.T) {
authAlice, err := bind.NewKeyedTransactorWithChainID(pk_a, big.NewInt(1337)) // ganache chainID
require.NoError(t, err)
address, tx, swapContract, err := DeploySwapOnChain(authAlice, conn, [32]byte{}, [32]byte{})
address, tx, swapContract, err := DeploySwap(authAlice, conn, [32]byte{}, [32]byte{})
require.NoError(t, err)
t.Log(address)
@@ -71,6 +71,7 @@ func TestSwap_Claim(t *testing.T) {
pk_a, err := crypto.HexToECDSA(keyAlice)
require.NoError(t, err)
pk_b, err := crypto.HexToECDSA(keyBob)
require.NoError(t, err)
authAlice, err := bind.NewKeyedTransactorWithChainID(pk_a, big.NewInt(1337)) // ganache chainID
authAlice.Value = big.NewInt(10)
@@ -79,23 +80,29 @@ func TestSwap_Claim(t *testing.T) {
require.NoError(t, err)
aliceBalanceBefore, err := conn.BalanceAt(context.Background(), authAlice.From, nil)
require.NoError(t, err)
fmt.Println("AliceBalanceBefore: ", aliceBalanceBefore)
// check whether Bob had nothing before the Tx
bobBalanceBefore, err := conn.BalanceAt(context.Background(), authBob.From, nil)
fmt.Println("BobBalanceBefore: ", bobBalanceBefore)
require.NoError(t, err)
fmt.Println("BobBalanceBefore: ", bobBalanceBefore)
var pkAliceFixed [32]byte
copy(pkAliceFixed[:], reverse(pubKeyAlice))
var pkBobFixed [32]byte
copy(pkBobFixed[:], reverse(pubKeyBob))
contractAddress, deployTx, swap, err := DeploySwapOnChain(authAlice, conn, pkBobFixed, pkAliceFixed)
fmt.Println("Deploy Tx Gas Cost:", deployTx.Gas())
aliceBalanceAfter, err := conn.BalanceAt(context.Background(), authAlice.From, nil)
fmt.Println("AliceBalanceAfter: ", aliceBalanceAfter)
contractBalance, err := conn.BalanceAt(context.Background(), contractAddress, nil)
require.Equal(t, contractBalance, big.NewInt(10))
contractAddress, deployTx, swap, err := DeploySwap(authAlice, conn, pkBobFixed, pkAliceFixed)
require.NoError(t, err)
fmt.Println("Deploy Tx Gas Cost:", deployTx.Gas())
aliceBalanceAfter, err := conn.BalanceAt(context.Background(), authAlice.From, nil)
require.NoError(t, err)
fmt.Println("AliceBalanceAfter: ", aliceBalanceAfter)
contractBalance, err := conn.BalanceAt(context.Background(), contractAddress, nil)
require.NoError(t, err)
require.Equal(t, contractBalance, big.NewInt(10))
txOpts := &bind.TransactOpts{
From: authAlice.From,
@@ -161,17 +168,19 @@ func TestSwap_Refund_Within_T0(t *testing.T) {
require.NoError(t, err)
authAlice, err := bind.NewKeyedTransactorWithChainID(pk_a, big.NewInt(1337)) // ganache chainID
authAlice.Value = big.NewInt(10)
require.NoError(t, err)
authAlice.Value = big.NewInt(10)
aliceBalanceBefore, err := conn.BalanceAt(context.Background(), authAlice.From, nil)
require.NoError(t, err)
fmt.Println("AliceBalanceBefore: ", aliceBalanceBefore)
var pkAliceFixed [32]byte
copy(pkAliceFixed[:], reverse(pubKeyAlice))
var pkBobFixed [32]byte
copy(pkBobFixed[:], reverse(pubKeyBob))
contractAddress, _, swap, err := DeploySwapOnChain(authAlice, conn, pkBobFixed, pkAliceFixed)
contractAddress, _, swap, err := DeploySwap(authAlice, conn, pkBobFixed, pkAliceFixed)
require.NoError(t, err)
txOpts := &bind.TransactOpts{
From: authAlice.From,
@@ -219,13 +228,15 @@ func TestSwap_Refund_After_T1(t *testing.T) {
require.NoError(t, err)
aliceBalanceBefore, err := conn.BalanceAt(context.Background(), authAlice.From, nil)
require.NoError(t, err)
fmt.Println("AliceBalanceBefore: ", aliceBalanceBefore)
var pkAliceFixed [32]byte
copy(pkAliceFixed[:], reverse(pubKeyAlice))
var pkBobFixed [32]byte
copy(pkBobFixed[:], reverse(pubKeyBob))
contractAddress, _, swap, err := DeploySwapOnChain(authAlice, conn, pkBobFixed, pkAliceFixed)
contractAddress, _, swap, err := DeploySwap(authAlice, conn, pkBobFixed, pkAliceFixed)
require.NoError(t, err)
txOpts := &bind.TransactOpts{
From: authAlice.From,
@@ -244,6 +255,7 @@ func TestSwap_Refund_After_T1(t *testing.T) {
// wait some, then try again
var result int64
rpcClient, err := rpc.Dial("http://127.0.0.1:8545")
require.NoError(t, err)
ret := rpcClient.Call(&result, "evm_increaseTime", 3600*25)
require.NoError(t, ret)

View File

@@ -1,4 +1,4 @@
package swapDLEQ
package swapdleq
import (
"context"
@@ -130,6 +130,7 @@ func TestSwap_Claim(t *testing.T) {
pk_a, err := crypto.HexToECDSA(keyAlice)
require.NoError(t, err)
pk_b, err := crypto.HexToECDSA(keyBob)
require.NoError(t, err)
authAlice, err := bind.NewKeyedTransactorWithChainID(pk_a, big.NewInt(1337)) // ganache chainID
authAlice.Value = big.NewInt(10)
@@ -138,11 +139,13 @@ func TestSwap_Claim(t *testing.T) {
require.NoError(t, err)
aliceBalanceBefore, err := conn.BalanceAt(context.Background(), authAlice.From, nil)
require.NoError(t, err)
fmt.Println("AliceBalanceBefore: ", aliceBalanceBefore)
// check whether Bob had nothing before the Tx
bobBalanceBefore, err := conn.BalanceAt(context.Background(), authBob.From, nil)
fmt.Println("BobBalanceBefore: ", bobBalanceBefore)
require.NoError(t, err)
fmt.Println("BobBalanceBefore: ", bobBalanceBefore)
var pkAliceFixedX [32]byte
// copy(pkAliceFixedX[:], reverse(pubKeyAliceX.Bytes()))
@@ -156,13 +159,18 @@ func TestSwap_Claim(t *testing.T) {
var pkBobFixedY [32]byte
// copy(pkBobFixedY[:], reverse(pubKeyBobY.Bytes()))
copy(pkBobFixedY[:], reverse(secp256k1BobY))
contractAddress, deployTx, swap, err := DeploySwapDLEQ(authAlice, conn, setBigIntLE(pkBobFixedX[:]), setBigIntLE(pkBobFixedY[:]), setBigIntLE(pkAliceFixedX[:]), setBigIntLE(pkAliceFixedY[:]))
fmt.Println("Deploy Tx Gas Cost:", deployTx.Gas())
aliceBalanceAfter, err := conn.BalanceAt(context.Background(), authAlice.From, nil)
fmt.Println("AliceBalanceAfter: ", aliceBalanceAfter)
contractBalance, err := conn.BalanceAt(context.Background(), contractAddress, nil)
require.Equal(t, contractBalance, big.NewInt(10))
require.NoError(t, err)
fmt.Println("Deploy Tx Gas Cost:", deployTx.Gas())
aliceBalanceAfter, err := conn.BalanceAt(context.Background(), authAlice.From, nil)
require.NoError(t, err)
fmt.Println("AliceBalanceAfter: ", aliceBalanceAfter)
contractBalance, err := conn.BalanceAt(context.Background(), contractAddress, nil)
require.NoError(t, err)
require.Equal(t, contractBalance, big.NewInt(10))
txOpts := &bind.TransactOpts{
From: authAlice.From,
@@ -275,7 +283,6 @@ func TestSwap_Refund_Within_T0(t *testing.T) {
require.NoError(t, err)
fmt.Println("AliceParsed: ", secp256k1AliceX, secp256k1AliceY)
// setup
conn, err := ethclient.Dial("ws://127.0.0.1:8545")
require.NoError(t, err)
@@ -284,10 +291,11 @@ func TestSwap_Refund_Within_T0(t *testing.T) {
require.NoError(t, err)
authAlice, err := bind.NewKeyedTransactorWithChainID(pk_a, big.NewInt(1337)) // ganache chainID
authAlice.Value = big.NewInt(10)
require.NoError(t, err)
authAlice.Value = big.NewInt(10)
aliceBalanceBefore, err := conn.BalanceAt(context.Background(), authAlice.From, nil)
require.NoError(t, err)
fmt.Println("AliceBalanceBefore: ", aliceBalanceBefore)
var pkAliceFixedX [32]byte
@@ -303,8 +311,11 @@ func TestSwap_Refund_Within_T0(t *testing.T) {
// copy(pkBobFixedY[:], reverse(pubKeyBobY.Bytes()))
copy(pkBobFixedY[:], reverse(secp256k1BobY))
contractAddress, deployTx, swap, err := DeploySwapDLEQ(authAlice, conn, setBigIntLE(pkBobFixedX[:]), setBigIntLE(pkBobFixedY[:]), setBigIntLE(pkAliceFixedX[:]), setBigIntLE(pkAliceFixedY[:]))
require.NoError(t, err)
fmt.Println("Deploy Tx Gas Cost:", deployTx.Gas())
aliceBalanceAfter, err := conn.BalanceAt(context.Background(), authAlice.From, nil)
require.NoError(t, err)
fmt.Println("AliceBalanceAfter: ", aliceBalanceAfter)
txOpts := &bind.TransactOpts{
@@ -405,10 +416,11 @@ func TestSwap_Refund_After_T1(t *testing.T) {
require.NoError(t, err)
authAlice, err := bind.NewKeyedTransactorWithChainID(pk_a, big.NewInt(1337)) // ganache chainID
authAlice.Value = big.NewInt(10)
require.NoError(t, err)
authAlice.Value = big.NewInt(10)
aliceBalanceBefore, err := conn.BalanceAt(context.Background(), authAlice.From, nil)
require.NoError(t, err)
fmt.Println("AliceBalanceBefore: ", aliceBalanceBefore)
var pkAliceFixedX [32]byte
@@ -424,8 +436,11 @@ func TestSwap_Refund_After_T1(t *testing.T) {
// copy(pkBobFixedY[:], reverse(pubKeyBobY.Bytes()))
copy(pkBobFixedY[:], reverse(secp256k1BobY))
contractAddress, deployTx, swap, err := DeploySwapDLEQ(authAlice, conn, setBigIntLE(pkBobFixedX[:]), setBigIntLE(pkBobFixedY[:]), setBigIntLE(pkAliceFixedX[:]), setBigIntLE(pkAliceFixedY[:]))
require.NoError(t, err)
fmt.Println("Deploy Tx Gas Cost:", deployTx.Gas())
aliceBalanceAfter, err := conn.BalanceAt(context.Background(), authAlice.From, nil)
require.NoError(t, err)
fmt.Println("AliceBalanceAfter: ", aliceBalanceAfter)
txOpts := &bind.TransactOpts{
From: authAlice.From,
@@ -444,6 +459,7 @@ func TestSwap_Refund_After_T1(t *testing.T) {
// wait some, then try again
var result int64
rpcClient, err := rpc.Dial("http://127.0.0.1:8545")
require.NoError(t, err)
ret := rpcClient.Call(&result, "evm_increaseTime", 3600*25)
require.NoError(t, ret)

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long