Skip to content

Commit

Permalink
pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
KonradStaniec committed Nov 20, 2024
1 parent 3d4c63c commit ba5b366
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 184 deletions.
2 changes: 1 addition & 1 deletion covenant-signer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22.3-alpine as builder
FROM golang:1.23.1-alpine as builder

# Version to build. Default is the Git HEAD.
ARG VERSION="HEAD"
Expand Down
2 changes: 0 additions & 2 deletions covenant-signer/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ func (cfg *Config) Parse() (*ParsedConfig, error) {
}

serverConfig, err := cfg.Server.Parse()

if err != nil {
return nil, err
}

metricsConfig, err := cfg.Metrics.Parse()

if err != nil {
return nil, err
}
Expand Down
4 changes: 1 addition & 3 deletions covenant-signer/go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/babylonlabs-io/covenant-emulator/covenant-signer

go 1.22.3

toolchain go1.22.4
go 1.23.1

require (
github.com/btcsuite/btcd v0.24.2
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion covenant-signer/itest/containers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type ImageConfig struct {
//nolint:deadcode
const (
dockerBitcoindRepository = "lncm/bitcoind"
dockerBitcoindVersionTag = "v26.0"
dockerBitcoindVersionTag = "v27.0"
)

// NewImageConfig returns ImageConfig needed for running e2e test.
Expand Down
12 changes: 6 additions & 6 deletions covenant-signer/itest/containers/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ func (m *Manager) RunBitcoindResource(
fmt.Sprintf("%s/:/data/.bitcoin", bitcoindCfgPath),
},
ExposedPorts: []string{
"8332",
"8333",
"28332",
"28333",
"18443",
"18444",
"8332/tcp",
"8333/tcp",
"28332/tcp",
"28333/tcp",
"18443/tcp",
"18444/tcp",
},
PortBindings: map[docker.Port][]docker.PortBinding{
"8332/tcp": {{HostIP: "", HostPort: "8332"}},
Expand Down
171 changes: 0 additions & 171 deletions covenant-signer/itest/e2e_test.go → covenant-signer/itest/e2etest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,131 +22,15 @@ import (
"github.com/babylonlabs-io/babylon/testutil/datagen"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/stretchr/testify/require"

"github.com/babylonlabs-io/covenant-emulator/covenant-signer/config"
"github.com/babylonlabs-io/covenant-emulator/covenant-signer/itest/containers"
"github.com/babylonlabs-io/covenant-emulator/covenant-signer/keystore/cosmos"
"github.com/babylonlabs-io/covenant-emulator/covenant-signer/observability/metrics"
"github.com/babylonlabs-io/covenant-emulator/covenant-signer/signerapp"
"github.com/babylonlabs-io/covenant-emulator/covenant-signer/signerservice"
"github.com/babylonlabs-io/covenant-emulator/covenant-signer/signerservice/types"
)

var (
netParams = &chaincfg.RegressionNetParams
eventuallyPollInterval = 100 * time.Millisecond
eventuallyTimeout = 10 * time.Second
)

type TestManager struct {
t *testing.T
bitcoindHandler *BitcoindTestHandler
walletPass string
covenantPrivKey *btcec.PrivateKey
signerConfig *config.Config
app *signerapp.SignerApp
server *signerservice.SigningServer
}

type stakingData struct {
stakingAmount btcutil.Amount
stakingTime uint16
stakingFeeRate btcutil.Amount
}

func defaultStakingData() *stakingData {
return &stakingData{
stakingAmount: btcutil.Amount(100000),
stakingTime: 10000,
stakingFeeRate: btcutil.Amount(5000), // feeRatePerKb
}
}

func StartManager(
t *testing.T,
numMatureOutputsInWallet uint32) *TestManager {
m, err := containers.NewManager()
require.NoError(t, err)
t.Cleanup(func() {
_ = m.ClearResources()
})

h := NewBitcoindHandler(t, m)
h.Start()

// Give some time to launch and bitcoind
time.Sleep(2 * time.Second)

passphrase := "pass"
_ = h.CreateWallet("test-wallet", passphrase)
// only outputs which are 100 deep are mature
_ = h.GenerateBlocks(int(numMatureOutputsInWallet) + 100)

appConfig := config.DefaultConfig()

appConfig.KeyStore.KeyStoreType = "cosmos"
appConfig.KeyStore.CosmosKeyStore.ChainID = "test-chain"
appConfig.KeyStore.CosmosKeyStore.Passphrase = passphrase
appConfig.KeyStore.CosmosKeyStore.KeyName = "test-key"
appConfig.KeyStore.CosmosKeyStore.KeyDirectory = ""
appConfig.KeyStore.CosmosKeyStore.KeyringBackend = "memory"

retriever, err := cosmos.NewCosmosKeyringRetriever(appConfig.KeyStore.CosmosKeyStore)
require.NoError(t, err)

keyInfo, err := retriever.Kr.CreateChainKey(
appConfig.KeyStore.CosmosKeyStore.Passphrase,
"",
)
require.NoError(t, err)

app := signerapp.NewSignerApp(
retriever,
)

met := metrics.NewCovenantSignerMetrics()
parsedConfig, err := appConfig.Parse()
require.NoError(t, err)

server, err := signerservice.New(
context.Background(),
parsedConfig,
app,
met,
)

require.NoError(t, err)

go func() {
_ = server.Start()
}()

// Give some time to launch server
time.Sleep(3 * time.Second)

t.Cleanup(func() {
_ = server.Stop(context.TODO())
})

return &TestManager{
t: t,
bitcoindHandler: h,
walletPass: passphrase,
covenantPrivKey: keyInfo.PrivateKey,
signerConfig: appConfig,
app: app,
server: server,
}
}

func (tm *TestManager) SigningServerUrl() string {
return fmt.Sprintf("http://%s:%d", tm.signerConfig.Server.Host, tm.signerConfig.Server.Port)
}

func buildDataToSign(t *testing.T, covnenantPublicKey *btcec.PublicKey) signerapp.ParsedSigningRequest {
stakerPrivKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
Expand Down Expand Up @@ -264,61 +148,6 @@ func TestSigningTransactions(t *testing.T) {
require.NoError(t, err)
}

func (tm *TestManager) verifyResponse(resp *signerapp.ParsedSigningResponse, req *signerapp.ParsedSigningRequest) error {

slashAdaptorSig, err := asig.NewAdaptorSignatureFromBytes(resp.SlashAdaptorSigs[0])

if err != nil {
return err
}

err = btcstaking.EncVerifyTransactionSigWithOutput(
req.SlashingTx,
req.StakingTx.TxOut[req.StakingOutputIdx],
req.SlashingScript,
tm.covenantPrivKey.PubKey(),
req.FpEncKeys[0],
slashAdaptorSig,
)

if err != nil {
return fmt.Errorf("failed to verify slash adaptor signature for slashing tx: %w", err)
}

slashUnbondingAdaptorSig, err := asig.NewAdaptorSignatureFromBytes(resp.SlashUnbondingAdaptorSigs[0])

if err != nil {
return err
}

err = btcstaking.EncVerifyTransactionSigWithOutput(
req.SlashUnbondingTx,
req.UnbondingTx.TxOut[0],
req.UnbondingSlashingScript,
tm.covenantPrivKey.PubKey(),
req.FpEncKeys[0],
slashUnbondingAdaptorSig,
)

if err != nil {
return fmt.Errorf("failed to verify slash unbonding adaptor signature for slash unbonding tx: %w", err)
}

err = btcstaking.VerifyTransactionSigWithOutput(
req.UnbondingTx,
req.StakingTx.TxOut[req.StakingOutputIdx],
req.UnbondingScript,
tm.covenantPrivKey.PubKey(),
resp.UnbondingSig.Serialize(),
)

if err != nil {
return fmt.Errorf("failed to verify unbonding signature for unbonding tx: %w", err)
}

return nil
}

func TestRejectToLargeRequest(t *testing.T) {
tm := StartManager(t, 100)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
Expand Down
Loading

0 comments on commit ba5b366

Please sign in to comment.