Skip to content

Commit

Permalink
chore: split func to generate covenant pk
Browse files Browse the repository at this point in the history
  • Loading branch information
RafilxTenfen committed Nov 20, 2024
1 parent 2021de6 commit 17ebc5d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 37 deletions.
32 changes: 29 additions & 3 deletions itest/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"time"

"github.com/babylonlabs-io/btc-staker/itest/containers"
"github.com/babylonlabs-io/btc-staker/itest/testutil"

"github.com/babylonlabs-io/babylon/btcstaking"
"github.com/babylonlabs-io/babylon/crypto/bip322"
btcctypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types"

Expand Down Expand Up @@ -853,7 +855,6 @@ func TestStakeFromPhase1(t *testing.T) {
require.NoError(t, err)

tmBTC := StartManagerBtc(t, ctx, numMatureOutputsInWallet, manager)
// defer tm.Stop(t, cancel)
defer func() {
cancel()
manager.ClearResources()
Expand All @@ -862,8 +863,33 @@ func TestStakeFromPhase1(t *testing.T) {
minStakingTime := uint16(100)
stakerAddr := datagen.GenRandomAccount().GetAddress()
testStakingData := GetTestStakingData(t, tmBTC.WalletPubKey, minStakingTime, 10000, 1, stakerAddr)
fpKey := hex.EncodeToString(schnorr.SerializePubKey(testStakingData.FinalityProviderBtcKeys[0]))

require.NotNil(t, fpKey)
fpPkHex := hex.EncodeToString(schnorr.SerializePubKey(testStakingData.FinalityProviderBtcKeys[0]))
btcStakerPkHex := hex.EncodeToString(schnorr.SerializePubKey(testStakingData.StakerKey))

appCli := testutil.TestApp()
tagHex := datagen.GenRandomHexStr(r, btcstaking.TagLen)

covenants := genCovenants(t, 1)

covenantPkHex := hex.EncodeToString(schnorr.SerializePubKey(covenants[0].PubKey()))

commonFlags := []string{
fmt.Sprintf("--covenant-committee-pks=%s", covenantPkHex),
fmt.Sprintf("--tag=%s", tagHex),
"--covenant-quorum=1", "--network=regtest",
}

fpDepositStakingAmount := 5000000 // 0.05BTC
fpStakingTimeLock := 52560 // 1 year

createTxCmdArgs := []string{
fmt.Sprintf("--staker-pk=%s", btcStakerPkHex),
fmt.Sprintf("--finality-provider-pk=%s", fpPkHex),
fmt.Sprintf("--staking-amount=%d", fpDepositStakingAmount),
fmt.Sprintf("--staking-time=%d", fpStakingTimeLock),
}
res := testutil.AppRunCreatePhase1StakingTx(r, t, appCli, append(createTxCmdArgs, commonFlags...))
require.NotNil(t, res)
// tm.createAndRegisterFinalityProviders(t, testStakingData)
}
65 changes: 31 additions & 34 deletions itest/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ type TestManager struct {
}

type TestManagerBTC struct {
RawAddr string
MinerAddr btcutil.Address
BitcoindHandler *BitcoindTestHandler
Bitcoind *dockertest.Resource
Expand Down Expand Up @@ -267,16 +266,31 @@ func StartManagerBtc(
minerAddressDecoded, err := btcutil.DecodeAddress(br.Address, regtestParams)
require.NoError(t, err)

bitcoindHost := fmt.Sprintf("127.0.0.1:%s", bitcoind.GetPort("18443/tcp"))
rpcBtc := btcRpcTestClient(t, bitcoindHost)

err = rpcBtc.WalletPassphrase(passphrase, 20)
require.NoError(t, err)

info, err := rpcBtc.GetAddressInfo(br.Address)
require.NoError(t, err)

pubKeyHex := *info.PubKey
pubKeyBytes, err := hex.DecodeString(pubKeyHex)
require.NoError(t, err)

walletPubKey, err := btcec.ParsePubKey(pubKeyBytes)
require.NoError(t, err)

return &TestManagerBTC{
RawAddr: br.Address,
MinerAddr: minerAddressDecoded,
BitcoindHandler: bitcoindHandler,
Bitcoind: bitcoind,
WalletName: walletName,
WalletPassphrase: passphrase,
// BitcoindHost: bitcoindHost,
// WalletPubKey: walletPubKey,
// TestRpcBtcClient: rpcBtc,
BitcoindHost: bitcoindHost,
WalletPubKey: walletPubKey,
TestRpcBtcClient: rpcBtc,
}
}

Expand All @@ -291,13 +305,7 @@ func StartManager(
tmBTC := StartManagerBtc(t, ctx, numMatureOutputsInWallet, manager)

quorum := 2
numCovenants := 3
var coventantPrivKeys []*btcec.PrivateKey
for i := 0; i < numCovenants; i++ {
covenantPrivKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
coventantPrivKeys = append(coventantPrivKeys, covenantPrivKey)
}
coventantPrivKeys := genCovenants(t, 3)

var buff bytes.Buffer
err = regtestParams.GenesisBlock.Header.Serialize(&buff)
Expand All @@ -321,11 +329,7 @@ func StartManager(
)
require.NoError(t, err)

rpcBtcHost := fmt.Sprintf("127.0.0.1:%s", tmBTC.Bitcoind.GetPort("18443/tcp"))
cfg, c := defaultStakerConfigAndBtc(t, tmBTC.WalletName, tmBTC.WalletPassphrase, rpcBtcHost)
cfg.BtcNodeBackendConfig.Bitcoind.RPCHost = rpcBtcHost
cfg.WalletRPCConfig.Host = fmt.Sprintf("127.0.0.1:%s", tmBTC.Bitcoind.GetPort("18443/tcp"))

cfg := defaultStakerConfig(t, tmBTC.WalletName, tmBTC.WalletPassphrase, tmBTC.BitcoindHost)
// update port with the dynamically allocated one from docker
cfg.BabylonConfig.RPCAddr = fmt.Sprintf("http://localhost:%s", babylond.GetPort("26657/tcp"))
cfg.BabylonConfig.GRPCAddr = fmt.Sprintf("https://localhost:%s", babylond.GetPort("9090/tcp"))
Expand Down Expand Up @@ -360,20 +364,6 @@ func StartManager(
bl, err := babylonclient.NewBabylonController(cfg.BabylonConfig, &cfg.ActiveNetParams, logger, zapLogger)
require.NoError(t, err)

walletClient := stakerApp.Wallet()

err = walletClient.UnlockWallet(20)
require.NoError(t, err)

info, err := c.GetAddressInfo(tmBTC.RawAddr)
require.NoError(t, err)

pubKeyHex := *info.PubKey
pubKeyBytes, err := hex.DecodeString(pubKeyHex)
require.NoError(t, err)
walletPubKey, err := btcec.ParsePubKey(pubKeyBytes)
require.NoError(t, err)

addressString := fmt.Sprintf("127.0.0.1:%d", testutil.AllocateUniquePort(t))
addrPort := netip.MustParseAddrPort(addressString)
address := net.TCPAddrFromAddrPort(addrPort)
Expand Down Expand Up @@ -401,9 +391,6 @@ func StartManager(
stakerClient, err := dc.NewStakerServiceJSONRPCClient("tcp://" + addressString)
require.NoError(t, err)

tmBTC.TestRpcBtcClient = c
tmBTC.WalletPubKey = walletPubKey
tmBTC.BitcoindHost = rpcBtcHost
return &TestManager{
Config: cfg,
Db: dbbackend,
Expand All @@ -418,6 +405,16 @@ func StartManager(
}
}

func genCovenants(t *testing.T, numCovenants int) []*btcec.PrivateKey {
var coventantPrivKeys []*btcec.PrivateKey
for i := 0; i < numCovenants; i++ {
covenantPrivKey, err := btcec.NewPrivateKey()
require.NoError(t, err)
coventantPrivKeys = append(coventantPrivKeys, covenantPrivKey)
}
return coventantPrivKeys
}

func (tm *TestManager) Stop(t *testing.T, cancelFunc context.CancelFunc) {
cancelFunc()
tm.wg.Wait()
Expand Down

0 comments on commit 17ebc5d

Please sign in to comment.