Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Konradstaniec/add remote signer module #33

Merged
merged 8 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## Unreleased

* [#33](https://github.com/babylonlabs-io/covenant-emulator/pull/33) Add remote
signer sub module

## v0.8.0

### Bug fixes
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ build-docker:

test:
go test ./...
cd covenant-signer; 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
cd covenant-signer; make test-e2e

mock-gen:
mkdir -p $(MOCKS_DIR)
Expand Down Expand Up @@ -125,4 +127,4 @@ release:
else
release:
@echo "Error: GITHUB_TOKEN is not defined. Please define it before running 'make release'."
endif
endif
37 changes: 37 additions & 0 deletions covenant-signer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FROM golang:1.22.3-alpine as builder
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let's use go 1.23


# Version to build. Default is the Git HEAD.
ARG VERSION="HEAD"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

version is not being used


# Use muslc for static libs
ARG BUILD_TAGS="muslc"

RUN apk add --no-cache --update openssh git make build-base linux-headers libc-dev \
pkgconfig zeromq-dev musl-dev alpine-sdk libsodium-dev \
libzmq-static libsodium-static gcc

# Build
WORKDIR /go/src/github.com/babylonlabs-io/covenant-emulator/covenant-signer
# Cache dependencies
COPY go.mod go.sum /go/src/github.com/babylonlabs-io/covenant-emulator/covenant-signer/
# Copy the rest of the files
COPY ./ /go/src/github.com/babylonlabs-io/covenant-emulator/covenant-signer/

RUN CGO_LDFLAGS="$CGO_LDFLAGS -lstdc++ -lm -lsodium" \
CGO_ENABLED=1 \
BUILD_TAGS=$BUILD_TAGS \
LINK_STATICALLY=true \
make build

# FINAL IMAGE
FROM alpine:3.16 AS run

RUN addgroup --gid 1138 -S covenant-signer && adduser --uid 1138 -S covenant-signer -G covenant-signer

RUN apk add bash curl jq

COPY --from=builder /go/src/github.com/babylonlabs-io/covenant-emulator/covenant-signer/build/covenant-signer /bin/covenant-signer

WORKDIR /home/covenant-signer
RUN chown -R covenant-signer /home/covenant-signer
USER covenant-signer
41 changes: 41 additions & 0 deletions covenant-signer/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
DOCKER = $(shell which docker)
BUILDDIR ?= $(CURDIR)/build

PACKAGES_E2E=$(shell go list ./... | grep '/itest')

ldflags := $(LDFLAGS)
build_tags := $(BUILD_TAGS)
build_args := $(BUILD_ARGS)

ifeq ($(VERBOSE),true)
build_args += -v
endif

ifeq ($(LINK_STATICALLY),true)
ldflags += -linkmode=external -extldflags "-Wl,-z,muldefs -static" -v
endif

BUILD_TARGETS := build install
BUILD_FLAGS := --tags "$(build_tags)" --ldflags '$(ldflags)'

all: build install

build: BUILD_ARGS := $(build_args) -o $(BUILDDIR)

$(BUILD_TARGETS): go.sum $(BUILDDIR)/
go $@ -mod=readonly $(BUILD_FLAGS) $(BUILD_ARGS) ./...

$(BUILDDIR)/:
mkdir -p $(BUILDDIR)/

build-docker:
$(DOCKER) build --tag babylonlabs-io/covenant-signer -f Dockerfile \
$(shell git rev-parse --show-toplevel)

.PHONY: build build-docker install tests

test:
go test ./...

test-e2e:
go test -mod=readonly -timeout=25m -v $(PACKAGES_E2E) -count=1 --tags=e2e
32 changes: 32 additions & 0 deletions covenant-signer/cmd/dumpDefaultCfgCmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"fmt"

"github.com/babylonlabs-io/covenant-emulator/covenant-signer/config"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(dumpCfgCmd)
}

var dumpCfgCmd = &cobra.Command{
Use: "dump-cfg",
Short: "dumps default confiiguration file",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Short: "dumps default confiiguration file",
Short: "dumps default configuration file",

RunE: func(cmd *cobra.Command, args []string) error {
path, err := cmd.Flags().GetString(configPathKey)
if err != nil {
return err
}

err = config.WriteConfigToFile(path, config.DefaultConfig())

if err != nil {
return err
}

fmt.Printf("Default configuration file dumped to: %s \n", path)
return nil
},
}
50 changes: 50 additions & 0 deletions covenant-signer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cmd

import (
"path/filepath"

"github.com/btcsuite/btcd/btcutil"
"github.com/spf13/cobra"
)

var (
// Used for flags.
configPath string
configPathKey = "config"

globalParamPath string
globalParamKey = "params"

rootCmd = &cobra.Command{
Use: "covenant-signer",
Short: "remote signing serivce to perform covenant duties",
}

// C:\Users\<username>\AppData\Local\tools on Windows
// ~/.tools on Linux
// ~/Library/Application Support/tools on MacOS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why tools?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to signer (it was copy paste mistake 😅 )

dafaultConfigDir = btcutil.AppDataDir("signer", false)
dafaultConfigPath = filepath.Join(dafaultConfigDir, "config.toml")
defaultGlobalParamsPath = filepath.Join(dafaultConfigDir, "global-params.json")
)

// Execute executes the root command.
func Execute() error {
return rootCmd.Execute()
}

func init() {
rootCmd.PersistentFlags().StringVar(
&configPath,
configPathKey,
dafaultConfigPath,
"path to the configuration file",
)

rootCmd.PersistentFlags().StringVar(
&globalParamPath,
globalParamKey,
defaultGlobalParamsPath,
"path to the global params file",
)
}
73 changes: 73 additions & 0 deletions covenant-signer/cmd/signerCmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"

"github.com/babylonlabs-io/covenant-emulator/covenant-signer/config"
"github.com/babylonlabs-io/covenant-emulator/covenant-signer/keystore/cosmos"
m "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"
)

func init() {
rootCmd.AddCommand(runSignerCmd)
}

var runSignerCmd = &cobra.Command{
Use: "start",
Short: "starts the signer service",
RunE: func(cmd *cobra.Command, args []string) error {
configPath, err := cmd.Flags().GetString(configPathKey)
if err != nil {
return err
}
cfg, err := config.GetConfig(configPath)
if err != nil {
return err
}

parsedConfig, err := cfg.Parse()

if err != nil {
return err
}

var prk signerapp.PrivKeyRetriever
if parsedConfig.KeyStoreConfig.KeyStoreType == config.CosmosKeyStore {
kr, err := cosmos.NewCosmosKeyringRetriever(parsedConfig.KeyStoreConfig.CosmosKeyStore)
if err != nil {
return err
}
prk = kr
} else {
return fmt.Errorf("unknown key store type")
}

app := signerapp.NewSignerApp(
prk,
)

metrics := m.NewCovenantSignerMetrics()

srv, err := signerservice.New(
cmd.Context(),
parsedConfig,
app,
metrics,
)

if err != nil {
return err
}

metricsAddress := fmt.Sprintf("%s:%d", cfg.Metrics.Host, cfg.Metrics.Port)

m.Start(metricsAddress, metrics.Registry)

// TODO: Add signal handling and gracefull shutdown
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's start using context instead of chan and signal interrupts (see vigilante or benchmark repo). Can be done in separate PR

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree on that though I would to in separate pr 👍

return srv.Start()
},
}
Loading