Skip to content

Commit

Permalink
adds babylond docker
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazar955 committed Sep 26, 2024
1 parent cef0e86 commit bdaac41
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 56 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ test:
go test ./...

test-e2e:
cd $(TOOLS_DIR); go install -trimpath $(BABYLON_PKG);
go test -mod=readonly -timeout=25m -v $(PACKAGES_E2E) -count=1 --tags=e2e
go test -mod=readonly -timeout=25m -failfast -v $(PACKAGES_E2E) -count=1 --tags=e2e

proto-gen:
@$(call print, "Compiling protos.")
Expand Down
10 changes: 5 additions & 5 deletions itest/bitcoind_node_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package e2etest
import (
"encoding/json"
"fmt"
"github.com/ory/dockertest/v3"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -34,24 +35,22 @@ type BitcoindTestHandler struct {
m *containers.Manager
}

func NewBitcoindHandler(t *testing.T) *BitcoindTestHandler {
m, err := containers.NewManager()
require.NoError(t, err)
func NewBitcoindHandler(t *testing.T, m *containers.Manager) *BitcoindTestHandler {
return &BitcoindTestHandler{
t: t,
m: m,
}
}

func (h *BitcoindTestHandler) Start() {
func (h *BitcoindTestHandler) Start() *dockertest.Resource {
tempPath, err := os.MkdirTemp("", "bitcoind-staker-test-*")
require.NoError(h.t, err)

h.t.Cleanup(func() {
_ = os.RemoveAll(tempPath)
})

_, err = h.m.RunBitcoindResource(tempPath)
bitcoinResource, err := h.m.RunBitcoindResource(h.t, tempPath)
require.NoError(h.t, err)

h.t.Cleanup(func() {
Expand All @@ -64,6 +63,7 @@ func (h *BitcoindTestHandler) Start() {
return err == nil
}, startTimeout, 500*time.Millisecond, "bitcoind did not start")

return bitcoinResource
}

func (h *BitcoindTestHandler) GetBlockCount() (int, error) {
Expand Down
20 changes: 16 additions & 4 deletions itest/containers/config.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
package containers

import (
"github.com/babylonlabs-io/btc-staker/itest/testutil"
"github.com/stretchr/testify/require"
"testing"
)

// ImageConfig contains all images and their respective tags
// needed for running e2e tests.
type ImageConfig struct {
BitcoindRepository string
BitcoindVersion string
BabylonRepository string
BabylonVersion string
}

//nolint:deadcode
const (
dockerBitcoindRepository = "lncm/bitcoind"
dockerBitcoindVersionTag = "v26.0"
dockerBabylondRepository = "babylonlabs/babylond"
)

// NewImageConfig returns ImageConfig needed for running e2e test.
func NewImageConfig() ImageConfig {
config := ImageConfig{
func NewImageConfig(t *testing.T) ImageConfig {
babylondVersion, err := testutil.GetBabylonVersion()
require.NoError(t, err)

return ImageConfig{
BitcoindRepository: dockerBitcoindRepository,
BitcoindVersion: dockerBitcoindVersionTag,
BabylonRepository: dockerBabylondRepository,
BabylonVersion: babylondVersion,
}
return config

}
159 changes: 141 additions & 18 deletions itest/containers/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import (
"bytes"
"context"
"fmt"
bbn "github.com/babylonlabs-io/babylon/types"
"github.com/babylonlabs-io/btc-staker/itest/testutil"
"github.com/btcsuite/btcd/btcec/v2"
"regexp"
"strconv"
"strings"
"testing"
"time"

Expand All @@ -14,7 +19,8 @@ import (
)

const (
bitcoindContainerName = "bitcoind-test"
bitcoindContainerName = "bitcoind"
babylondContainerName = "babylond"
)

var errRegex = regexp.MustCompile(`(E|e)rror`)
Expand All @@ -29,9 +35,9 @@ type Manager struct {

// NewManager creates a new Manager instance and initializes
// all Docker specific utilities. Returns an error if initialization fails.
func NewManager() (docker *Manager, err error) {
func NewManager(t *testing.T) (docker *Manager, err error) {
docker = &Manager{
cfg: NewImageConfig(),
cfg: NewImageConfig(t),
resources: make(map[string]*dockertest.Resource),
}
docker.pool, err = dockertest.NewPool("")
Expand Down Expand Up @@ -122,32 +128,23 @@ func (m *Manager) ExecCmd(t *testing.T, containerName string, command []string)
}

func (m *Manager) RunBitcoindResource(
t *testing.T,
bitcoindCfgPath string,
) (*dockertest.Resource, error) {
bitcoindResource, err := m.pool.RunWithOptions(
&dockertest.RunOptions{
Name: bitcoindContainerName,
Name: fmt.Sprintf("%s-%s", bitcoindContainerName, t.Name()),
Repository: m.cfg.BitcoindRepository,
Tag: m.cfg.BitcoindVersion,
User: "root:root",
Mounts: []string{
fmt.Sprintf("%s/:/data/.bitcoin", bitcoindCfgPath),
},
ExposedPorts: []string{
"8332",
"8333",
"28332",
"28333",
"18443",
"18444",
Labels: map[string]string{
"e2e": "bitcoind",
},
PortBindings: map[docker.Port][]docker.PortBinding{
"8332/tcp": {{HostIP: "", HostPort: "8332"}},
"8333/tcp": {{HostIP: "", HostPort: "8333"}},
"28332/tcp": {{HostIP: "", HostPort: "28332"}},
"28333/tcp": {{HostIP: "", HostPort: "28333"}},
"18443/tcp": {{HostIP: "", HostPort: "18443"}},
"18444/tcp": {{HostIP: "", HostPort: "18444"}},
ExposedPorts: []string{
"18443/tcp",
},
Cmd: []string{
"-regtest",
Expand All @@ -158,15 +155,141 @@ func (m *Manager) RunBitcoindResource(
"-rpcbind=0.0.0.0",
},
},
func(config *docker.HostConfig) {
config.PortBindings = map[docker.Port][]docker.PortBinding{
"18443/tcp": {{HostIP: "", HostPort: strconv.Itoa(testutil.AllocateUniquePort(t))}}, // only expose what we need
}
config.PublishAllPorts = false // because in dockerfile they already expose them
},
noRestart,
)
if err != nil {
return nil, err
}
m.resources[bitcoindContainerName] = bitcoindResource

return bitcoindResource, nil
}

// RunBabylondResource starts a babylond container
func (m *Manager) RunBabylondResource(
t *testing.T,
mounthPath string,
coventantQuorum int,
baseHeaderHex string,
slashingPkScript string,
covenantPk1 *btcec.PublicKey,
covenantPk2 *btcec.PublicKey,
covenantPk3 *btcec.PublicKey,
) (*dockertest.Resource, error) {
covenantPks := []*bbn.BIP340PubKey{
bbn.NewBIP340PubKeyFromBTCPK(covenantPk1),
bbn.NewBIP340PubKeyFromBTCPK(covenantPk2),
bbn.NewBIP340PubKeyFromBTCPK(covenantPk3),
}

var covenantPksStr []string
for _, pk := range covenantPks {
covenantPksStr = append(covenantPksStr, pk.MarshalHex())
}

cmd := []string{
"sh", "-c", fmt.Sprintf(
"babylond testnet --v=1 --output-dir=/home --starting-ip-address=192.168.10.2 "+
"--keyring-backend=test --chain-id=chain-test --btc-finalization-timeout=4 "+
"--btc-confirmation-depth=2 --additional-sender-account --btc-network=regtest "+
"--min-staking-time-blocks=200 --min-staking-amount-sat=10000 "+
"--slashing-pk-script=%s --btc-base-header=%s --covenant-quorum=%d "+
"--covenant-pks=%s && chmod -R 777 /home && "+
"babylond start --home=/home/node0/babylond",
slashingPkScript, baseHeaderHex, coventantQuorum, strings.Join(covenantPksStr, ",")),
}

resource, err := m.pool.RunWithOptions(
&dockertest.RunOptions{
Name: fmt.Sprintf("%s-%s", babylondContainerName, t.Name()),
Repository: m.cfg.BabylonRepository,
Tag: m.cfg.BabylonVersion,
Labels: map[string]string{
"e2e": "babylond",
},
User: "root:root",
Mounts: []string{
fmt.Sprintf("%s/:/home/", mounthPath),
},
ExposedPorts: []string{
"9090/tcp", // only expose what we need
"26657/tcp",
},
Cmd: cmd,
},
func(config *docker.HostConfig) {
config.PortBindings = map[docker.Port][]docker.PortBinding{
"9090/tcp": {{HostIP: "", HostPort: strconv.Itoa(testutil.AllocateUniquePort(t))}},
"26657/tcp": {{HostIP: "", HostPort: strconv.Itoa(testutil.AllocateUniquePort(t))}},
}
},
noRestart,
)
if err != nil {
return nil, err
}

m.resources[babylondContainerName] = resource

return resource, nil
}

// BabylondTxBankSend send transaction to an address from the node address.
func (m *Manager) BabylondTxBankSend(t *testing.T, addr, coins, walletName string) (bytes.Buffer, bytes.Buffer, error) {
flags := []string{
"babylond",
"tx",
"bank",
"send",
walletName,
addr,
coins,
"--keyring-backend=test",
"--home=/home/node0/babylond",
"--log_level=debug",
"--chain-id=chain-test",
"-b=sync", "--yes", "--gas-prices=10ubbn",
}

return m.ExecCmd(t, babylondContainerName, flags)
}

// BabylondTxBankMultiSend send transaction to an addresses from the node address.
func (m *Manager) BabylondTxBankMultiSend(t *testing.T, walletName string, coins string, addresses ...string) (bytes.Buffer, bytes.Buffer, error) {
// babylond tx bank multi-send [from_key_or_address] [to_address_1 to_address_2 ...] [amount] [flags]
switch len(addresses) {
case 0:
return bytes.Buffer{}, bytes.Buffer{}, nil
case 1:
return m.BabylondTxBankSend(t, addresses[0], coins, walletName)
}

flags := []string{
"babylond",
"tx",
"bank",
"multi-send",
walletName,
}
flags = append(flags, addresses...)
flags = append(flags,
coins,
"--keyring-backend=test",
"--home=/home/node0/babylond",
"--log_level=debug",
"--chain-id=chain-test",
"-b=sync", "--yes", "--gas-prices=10ubbn",
)

return m.ExecCmd(t, babylondContainerName, flags)
}

// ClearResources removes all outstanding Docker resources created by the Manager.
func (m *Manager) ClearResources() error {
for _, resource := range m.resources {
Expand Down
Loading

0 comments on commit bdaac41

Please sign in to comment.