Skip to content

Commit

Permalink
remote signer grpc server (#215)
Browse files Browse the repository at this point in the history
* remote signer grpc server

* simplify api

* expose ToProto

* Go 1.21

* buf.lock perms

* Remove buf mod update

* direct path

* test updates

* Fix race in leader election test

* simplify

* Add test for horcrux proxy

* bump to go 1.21 in test too
  • Loading branch information
agouin authored Nov 13, 2023
1 parent 0a99656 commit 706fa2f
Show file tree
Hide file tree
Showing 44 changed files with 5,282 additions and 1,863 deletions.
17 changes: 5 additions & 12 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@ jobs:
runs-on: ubuntu-latest
steps:
# Install and setup go
- name: Set up Go 1.19
- name: Set up Go 1.21
uses: actions/setup-go@v4
with:
go-version: '^1.19'
go-version: '^1.21.4'

# checkout horcrux
- name: checkout horcrux
uses: actions/checkout@v3

# make sure proto files are up to date
- name: generate fresh signer proto .go files
run: make signer-proto

# run tests
- name: run horcrux tests
run: make test
Expand All @@ -48,21 +44,18 @@ jobs:
- Test2Of3SignerThreeSentriesUniqueConnection
- TestUpgradeValidatorToHorcrux
- TestSingleSignerTwoSentries
- TestHorcruxProxyGRPC
steps:
# Install and setup go
- name: Set up Go 1.20
- name: Set up Go 1.21
uses: actions/setup-go@v4
with:
go-version: '^1.20.0'
go-version: '^1.21.4'

# checkout horcrux
- name: checkout horcrux
uses: actions/checkout@v3

# make sure proto files are up to date
- name: generate fresh signer proto .go files
run: make signer-proto

# run test matrix
- name: run test
run: cd test && go test -v -timeout 30m -run ^${{ matrix.test }}$ .
30 changes: 17 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,22 @@ build-horcrux-docker:
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
mkfile_dir := $(dir $(mkfile_path))

signer-proto:
docker run \
--rm \
-u $(shell id -u ${USER}):$(shell id -g ${USER}) \
--mount type=bind,source=$(mkfile_dir)/signer/proto,target=/horcrux/signer/proto \
--entrypoint protoc \
namely/protoc-all \
--go_out=/horcrux \
--go_opt=paths=source_relative \
--go-grpc_out=/horcrux \
--go-grpc_opt=paths=source_relative \
--proto_path /horcrux \
$(shell find $(mkfile_dir) -name *.proto -printf "%P\n")
DOCKER := $(shell which docker)
protoVer=0.11.2
protoImageName=ghcr.io/cosmos/proto-builder:$(protoVer)
protoImage=$(DOCKER) run --rm -v $(CURDIR):/workspace --workdir /workspace $(protoImageName)

proto-all: proto-format proto-lint proto-gen

proto-gen:
@echo "Generating Protobuf files"
@$(protoImage) sh ./scripts/protocgen.sh

proto-format:
@$(protoImage) find ./ -name "*.proto" -exec clang-format -i {} \;

proto-lint:
@$(protoImage) buf lint --error-format=json


.PHONY: all lint test race msan tools clean build
6 changes: 5 additions & 1 deletion cmd/horcrux/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
flagGRPCTimeout = "grpc-timeout"
flagOverwrite = "overwrite"
flagBare = "bare"
flagGRPCAddress = "flagGRPCAddress"
)

func configCmd() *cobra.Command {
Expand Down Expand Up @@ -67,7 +68,8 @@ for threshold signer mode, --cosigner flags and --threshold flag are required.
if keyDirFlag != "" {
keyDir = &keyDirFlag
}
debugAddr, _ := cmdFlags.GetString("debug-addr")
debugAddr, _ := cmdFlags.GetString(flagDebugAddr)
grpcAddr, _ := cmdFlags.GetString(flagGRPCAddress)
if signMode == string(signer.SignModeThreshold) {
// Threshold Mode Config
cosignersFlag, _ := cmdFlags.GetStringSlice(flagCosigner)
Expand All @@ -90,6 +92,7 @@ for threshold signer mode, --cosigner flags and --threshold flag are required.
},
ChainNodes: cn,
DebugAddr: debugAddr,
GRPCAddr: grpcAddr,
}

if !bare {
Expand Down Expand Up @@ -158,5 +161,6 @@ for threshold signer mode, --cosigner flags and --threshold flag are required.
false,
"allows initialization without providing any flags. If flags are provided, will not perform final validation",
)
f.StringP(flagGRPCAddress, "g", "", "GRPC address if listener should be enabled")
return cmd
}
2 changes: 2 additions & 0 deletions cmd/horcrux/cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ chainNodes:
- privValAddr: tcp://10.168.0.1:1234
- privValAddr: tcp://10.168.0.2:1234
debugAddr: ""
grpcAddr: ""
`,
},
{
Expand All @@ -62,6 +63,7 @@ chainNodes:
- privValAddr: tcp://10.168.0.1:1234
- privValAddr: tcp://10.168.0.2:1234
debugAddr: ""
grpcAddr: ""
`,
},
{
Expand Down
10 changes: 5 additions & 5 deletions cmd/horcrux/cmd/leader_election.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ horcrux elect 2 # elect specific leader`,
ctx, cancelFunc := context.WithTimeout(context.Background(), 30*time.Second)
defer cancelFunc()

grpcClient := proto.NewCosignerGRPCClient(conn)
grpcClient := proto.NewCosignerClient(conn)
_, err = grpcClient.TransferLeadership(
ctx,
&proto.CosignerGRPCTransferLeadershipRequest{LeaderID: leaderID},
&proto.TransferLeadershipRequest{LeaderID: leaderID},
)
if err != nil {
return err
}

res, err := grpcClient.GetLeader(ctx, &proto.CosignerGRPCGetLeaderRequest{})
res, err := grpcClient.GetLeader(ctx, &proto.GetLeaderRequest{})
if err != nil {
return err
}
Expand Down Expand Up @@ -166,9 +166,9 @@ func getLeaderCmd() *cobra.Command {
ctx, cancelFunc := context.WithTimeout(context.Background(), 30*time.Second)
defer cancelFunc()

grpcClient := proto.NewCosignerGRPCClient(conn)
grpcClient := proto.NewCosignerClient(conn)

res, err := grpcClient.GetLeader(ctx, &proto.CosignerGRPCGetLeaderRequest{})
res, err := grpcClient.GetLeader(ctx, &proto.GetLeaderRequest{})
if err != nil {
return err
}
Expand Down
11 changes: 10 additions & 1 deletion cmd/horcrux/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func startCmd() *cobra.Command {
return fmt.Errorf("this is a legacy config. run `horcrux config migrate` to migrate to the latest format")
}

logger := cometlog.NewTMLogger(cometlog.NewSyncWriter(out)).With("module", "validator")
logger := cometlog.NewTMLogger(cometlog.NewSyncWriter(out))

// create all directories up to the state directory
if err = os.MkdirAll(config.StateDir, 0700); err != nil {
Expand Down Expand Up @@ -61,6 +61,15 @@ func startCmd() *cobra.Command {
panic(fmt.Errorf("unexpected sign mode: %s", config.Config.SignMode))
}

if config.Config.GRPCAddr != "" {
grpcServer := signer.NewRemoteSignerGRPCServer(logger, val, config.Config.GRPCAddr)
services = append(services, grpcServer)

if err := grpcServer.Start(); err != nil {
return fmt.Errorf("failed to start grpc server: %w", err)
}
}

go EnableDebugAndMetrics(cmd.Context(), out)

services, err = signer.StartRemoteSigners(services, logger, val, config.Config.Nodes())
Expand Down
1 change: 1 addition & 0 deletions cmd/horcrux/cmd/testdata/config-migrated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ chainNodes:
- privValAddr: tcp://127.0.0.1:2345
- privValAddr: tcp://127.0.0.1:3456
debugAddr: ""
grpcAddr: ""
2 changes: 1 addition & 1 deletion docker/horcrux/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM --platform=$BUILDPLATFORM golang:1.19-alpine AS build-env
FROM --platform=$BUILDPLATFORM golang:1.21-alpine AS build-env

RUN apk add --update --no-cache curl make git libc-dev bash gcc linux-headers eudev-dev

Expand Down
2 changes: 1 addition & 1 deletion docker/horcrux/native.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.19-alpine AS build-env
FROM golang:1.21-alpine AS build-env

RUN apk add --update --no-cache curl make git libc-dev bash gcc linux-headers eudev-dev

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/strangelove-ventures/horcrux

go 1.19
go 1.21

require (
github.com/Jille/raft-grpc-leader-rpc v1.1.0
Expand All @@ -9,6 +9,7 @@ require (
github.com/armon/go-metrics v0.4.1
github.com/cometbft/cometbft v0.37.2
github.com/cosmos/cosmos-sdk v0.47.3
github.com/cosmos/gogoproto v1.4.10
github.com/ethereum/go-ethereum v1.12.0
github.com/gogo/protobuf v1.3.2
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
Expand All @@ -25,7 +26,6 @@ require (
gitlab.com/unit410/threshold-ed25519 v0.0.0-20220725172740-6ee731f539ac
golang.org/x/sync v0.1.0
google.golang.org/grpc v1.55.0
google.golang.org/protobuf v1.30.0
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -44,7 +44,6 @@ require (
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.2 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gogoproto v1.4.10 // indirect
github.com/cosmos/ledger-cosmos-go v0.12.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
Expand Down Expand Up @@ -107,6 +106,7 @@ require (
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
Loading

0 comments on commit 706fa2f

Please sign in to comment.