Skip to content

Commit

Permalink
Add tests for adversarial scenarios (#300)
Browse files Browse the repository at this point in the history
* fix and test cheating scenario (malicious double spend)

* test and fix async vtxo cheating cases

* add replace statement in go.mod

* Update server/internal/core/application/covenantless.go

Co-authored-by: Pietralberto Mazza <[email protected]>
Signed-off-by: Louis Singer <[email protected]>

* Update server/internal/infrastructure/wallet/btc-embedded/psbt.go

Co-authored-by: Pietralberto Mazza <[email protected]>
Signed-off-by: Louis Singer <[email protected]>

* Update server/test/e2e/covenant/e2e_test.go

Co-authored-by: Pietralberto Mazza <[email protected]>
Signed-off-by: Louis Singer <[email protected]>

* Update server/test/e2e/covenantless/e2e_test.go

Co-authored-by: Pietralberto Mazza <[email protected]>
Signed-off-by: Louis Singer <[email protected]>

* Update server/test/e2e/covenantless/e2e_test.go

Co-authored-by: Pietralberto Mazza <[email protected]>
Signed-off-by: Louis Singer <[email protected]>

* remove unused

* [btc-embedded] fix GetNotificationChannel

* [tx-builder] fix redeem transaction fee estimator

* close grpc client in tests

* [application] rework listentoscannerNotification

* [application][covenant] fix getConnectorAmount

* [tx-builder][covenant] get connector amount from wallet

* e2e test sleep time

* [liquid-standalone] ListConnectorUtxos: filter by script client side

* fix Makefile integrationtest

* do not use cache in integration tests

* use VtxoKey as argument of findForfeitTxBitcoin

* wrap adversarial test in t.Run

* increaste test timeout

* CI: setup go 1.23.1

* CI: revert go version

* add replace in server/go.mod

* Update server/internal/core/application/covenant.go

Co-authored-by: Pietralberto Mazza <[email protected]>
Signed-off-by: Louis Singer <[email protected]>

* remove replace

* readd replace statement

* fixes

* go work sync

* fix CI

---------

Signed-off-by: Louis Singer <[email protected]>
Co-authored-by: Pietralberto Mazza <[email protected]>
  • Loading branch information
louisinger and altafan committed Sep 16, 2024
1 parent 4c8c5c0 commit 3782793
Show file tree
Hide file tree
Showing 31 changed files with 710 additions and 276 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ark.artifacts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ jobs:
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.21.0
go-version: 1.23.1

- name: Build binaries
run: make build-all
Expand Down
12 changes: 7 additions & 5 deletions .github/workflows/ark.integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: ci_integration

on:
push:
branches: [master]
branches:
- master
pull_request:
branches:
- master
Expand All @@ -15,11 +16,12 @@ jobs:
run:
working-directory: ./server
steps:
- uses: actions/setup-go@v3
with:
go-version: ">1.17.2"
- uses: actions/checkout@v3
- run: go get -v -t -d ./...
- uses: actions/setup-go@v4
with:
go-version: '>=1.23.1'
- name: Run go work sync
run: go work sync

- name: Run Nigiri
uses: vulpemventures/nigiri-github-action@v1
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ark.release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ jobs:
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.21.0
go-version: 1.23.1

# Build binaries for all architectures
- name: Build binaries
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/ark.unit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ name: ci_unit

on:
push:
branches:
- master
paths:
- 'server/**'
- 'pkg/client-sdk/**'
branches: [master]
pull_request:
branches:
- master
Expand Down Expand Up @@ -56,7 +57,8 @@ jobs:
uses: securego/gosec@master
with:
args: '-severity high -quiet -exclude=G115 ./...'
- run: go get -v -t -d ./...
- name: Run go work sync
run: go work sync
- name: unit testing
run: make test

Expand All @@ -83,4 +85,4 @@ jobs:
args: '-severity high -quiet -exclude=G115 ./...'
- run: go get -v -t -d ./...
- name: unit testing
run: make test
run: make test
1 change: 0 additions & 1 deletion docker-compose.clark.regtest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ services:
- ARK_LOG_LEVEL=5
- ARK_ROUND_LIFETIME=512
- ARK_TX_BUILDER_TYPE=covenantless
- ARK_MIN_RELAY_FEE=200
- ARK_ESPLORA_URL=http://chopsticks:3000
- ARK_BITCOIND_RPC_USER=admin1
- ARK_BITCOIND_RPC_PASS=123
Expand Down
4 changes: 2 additions & 2 deletions pkg/client-sdk/ark_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type ArkClient interface {
Unlock(ctx context.Context, password string) error
Lock(ctx context.Context, password string) error
Balance(ctx context.Context, computeExpiryDetails bool) (*Balance, error)
Receive(ctx context.Context) (string, string, error)
Receive(ctx context.Context) (offchainAddr, boardingAddr string, err error)
SendOnChain(ctx context.Context, receivers []Receiver) (string, error)
SendOffChain(
ctx context.Context, withExpiryCoinselect bool, receivers []Receiver,
Expand All @@ -26,7 +26,7 @@ type ArkClient interface {
) (string, error)
SendAsync(ctx context.Context, withExpiryCoinselect bool, receivers []Receiver) (string, error)
Claim(ctx context.Context) (string, error)
ListVtxos(ctx context.Context) ([]client.Vtxo, []client.Vtxo, error)
ListVtxos(ctx context.Context) (spendable, spent []client.Vtxo, err error)
GetTransactionHistory(ctx context.Context) ([]Transaction, error)
Dump(ctx context.Context) (seed string, err error)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/client-sdk/covenant_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/ark-network/ark/pkg/client-sdk/client"
"github.com/ark-network/ark/pkg/client-sdk/explorer"
"github.com/ark-network/ark/pkg/client-sdk/internal/utils"
"github.com/ark-network/ark/pkg/client-sdk/internal/utils/redemption"
"github.com/ark-network/ark/pkg/client-sdk/redemption"
"github.com/ark-network/ark/pkg/client-sdk/store"
"github.com/ark-network/ark/pkg/client-sdk/wallet"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
Expand Down
24 changes: 23 additions & 1 deletion pkg/client-sdk/covenantless_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/ark-network/ark/pkg/client-sdk/client"
"github.com/ark-network/ark/pkg/client-sdk/explorer"
"github.com/ark-network/ark/pkg/client-sdk/internal/utils"
"github.com/ark-network/ark/pkg/client-sdk/internal/utils/redemption"
"github.com/ark-network/ark/pkg/client-sdk/redemption"
"github.com/ark-network/ark/pkg/client-sdk/store"
"github.com/ark-network/ark/pkg/client-sdk/wallet"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
Expand Down Expand Up @@ -975,6 +975,15 @@ func (a *covenantlessArkClient) handleRoundStream(

var signerSession bitcointree.SignerSession

const (
start = iota
roundSigningStarted
roundSigningNoncesGenerated
roundFinalization
)

step := start

for {
select {
case <-ctx.Done():
Expand All @@ -991,24 +1000,35 @@ func (a *covenantlessArkClient) handleRoundStream(
return "", fmt.Errorf("round failed: %s", event.(client.RoundFailedEvent).Reason)
case client.RoundSigningStartedEvent:
pingStop()
if step != start {
continue
}
log.Info("a round signing started")
signerSession, err = a.handleRoundSigningStarted(
ctx, roundEphemeralKey, event.(client.RoundSigningStartedEvent),
)
if err != nil {
return "", err
}
step++
continue
case client.RoundSigningNoncesGeneratedEvent:
if step != roundSigningStarted {
continue
}
pingStop()
log.Info("round combined nonces generated")
if err := a.handleRoundSigningNoncesGenerated(
ctx, event.(client.RoundSigningNoncesGeneratedEvent), roundEphemeralKey, signerSession,
); err != nil {
return "", err
}
step++
continue
case client.RoundFinalizationEvent:
if step != roundSigningNoncesGenerated {
continue
}
pingStop()
log.Info("a round finalization started")

Expand All @@ -1031,6 +1051,8 @@ func (a *covenantlessArkClient) handleRoundStream(

log.Info("done.")
log.Info("waiting for round finalization...")
step++
continue
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/client-sdk/internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"runtime/debug"
"sort"
"sync"

"github.com/ark-network/ark/common"
"github.com/ark-network/ark/pkg/client-sdk/client"
Expand Down Expand Up @@ -265,8 +266,13 @@ func DecryptAES128(encrypted, password []byte) ([]byte, error) {
return plaintext, nil
}

var lock = &sync.Mutex{}

// deriveKey derives a 32 byte array key from a custom passhprase
func deriveKey(password, salt []byte) ([]byte, []byte, error) {
lock.Lock()
defer lock.Unlock()

if salt == nil {
salt = make([]byte, 32)
if _, err := rand.Read(salt); err != nil {
Expand Down
16 changes: 9 additions & 7 deletions pkg/client-sdk/wallet/singlekey/bitcoin_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,16 @@ func (s *bitcoinWallet) SignTransaction(
return "", fmt.Errorf("signature verification failed")
}

updater.Upsbt.Inputs[i].TaprootScriptSpendSig = []*psbt.TaprootScriptSpendSig{
{
XOnlyPubKey: schnorr.SerializePubKey(pubkey),
LeafHash: hash.CloneBytes(),
Signature: sig.Serialize(),
SigHash: txscript.SigHashDefault,
},
if len(updater.Upsbt.Inputs[i].TaprootScriptSpendSig) == 0 {
updater.Upsbt.Inputs[i].TaprootScriptSpendSig = make([]*psbt.TaprootScriptSpendSig, 0)
}

updater.Upsbt.Inputs[i].TaprootScriptSpendSig = append(updater.Upsbt.Inputs[i].TaprootScriptSpendSig, &psbt.TaprootScriptSpendSig{
XOnlyPubKey: schnorr.SerializePubKey(pubkey),
LeafHash: hash.CloneBytes(),
Signature: sig.Serialize(),
SigHash: txscript.SigHashDefault,
})
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ help:
## intergrationtest: runs integration tests
integrationtest:
@echo "Running integration tests..."
@go test -v -count=1 -race -timeout 200s github.com/ark-network/ark/server/test/e2e/...
@go test -v -count 1 -timeout 300s github.com/ark-network/ark/server/test/e2e/covenant
@go test -v -count 1 -timeout 300s github.com/ark-network/ark/server/test/e2e/covenantless

## lint: lint codebase
lint:
Expand Down
Loading

0 comments on commit 3782793

Please sign in to comment.