Skip to content

Commit

Permalink
all: add user agents and improve build info log (hemilabs#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuasing authored Aug 21, 2024
1 parent b80c7c1 commit e702ce1
Show file tree
Hide file tree
Showing 25 changed files with 327 additions and 45 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ concurrency:
env:
GO_VERSION: "1.22.x"
PNPM_VERSION: "9.4.x"
GO_LDFLAGS: >-
-X 'github.com/hemilabs/heminetwork/version.Brand=Hemi Labs'
-X github.com/hemilabs/heminetwork/version.PreRelease=
jobs:
# Run tests
Expand Down Expand Up @@ -112,7 +115,8 @@ jobs:
GOARCH: "${{ matrix.goarch }}"
CGO_ENABLED: 0 # Disable CGO.
GOGC: off # Disable GC during build, faster but uses more RAM.
run: make GOCACHE="$(go env GOCACHE)" archive
GO_LDFLAGS: "${{ env.GO_LDFLAGS }}"
run: make GOCACHE="$(go env GOCACHE)" GO_LDFLAGS="$GO_LDFLAGS" archive

- name: "Upload artifacts"
uses: actions/upload-artifact@v4
Expand Down Expand Up @@ -182,6 +186,7 @@ jobs:
VERSION=${{ needs.prepare.outputs.version }}
VCS_REF=${{ github.sha }}
BUILD_DATE=${{ steps.prepare.outputs.build_date }}
GO_LDFLAGS=${{ env.GO_LDFLAGS }}
tags: |
hemilabs/${{ matrix.service }}:latest
hemilabs/${{ matrix.service }}:${{ needs.prepare.outputs.tag }}
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ PROJECTPATH = $(abspath $(dir $(realpath $(firstword $(MAKEFILE_LIST)))))
export GOBIN=$(PROJECTPATH)/bin
export GOCACHE=$(PROJECTPATH)/.gocache
export GOPKG=$(PROJECTPATH)/pkg
GO_LDFLAGS=""
DIST=$(PROJECTPATH)/dist

ifeq ($(GOOS),windows)
Expand Down Expand Up @@ -49,7 +50,7 @@ go-deps:
go mod verify

$(cmds):
go build -trimpath -o $(GOBIN)/$@$(BIN_EXT) ./cmd/$@
go build -trimpath -ldflags "$(GO_LDFLAGS)" -o $(GOBIN)/$@$(BIN_EXT) ./cmd/$@

build:
go build ./...
Expand Down Expand Up @@ -105,4 +106,3 @@ sources: dist

checksums: dist
cd $(DIST) && shasum -a 256 * > $(project)_$(version)_checksums.txt

34 changes: 24 additions & 10 deletions api/auth/secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
dcrecdsa "github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa"

"github.com/hemilabs/heminetwork/api/protocol"
"github.com/hemilabs/heminetwork/version"
)

const (
Expand All @@ -25,19 +26,24 @@ const (
CmdSecp256k1HelloChallengeAccepted = "secp256k1-hello-challenge-accepted"
)

// Hello is a client->server command that sends the client ECDSA public key.
// Secp256k1Hello is a client->server command that sends the client Secp256k1
// public key.
type Secp256k1Hello struct {
PublicKey string `json:"publickey"` // Client compressed public key
// UserAgent is the client user agent.
UserAgent string `json:"userAgent,omitempty"`

// PublicKey is the client compressed public key.
PublicKey string `json:"publickey"`
}

// HelloChallenge is a server->client command that challenges the the client to
// sign the hash of the provided message.
// Secp256k1HelloChallenge is a server->client command that challenges the
// client to sign the hash of the provided message.
type Secp256k1HelloChallenge struct {
Message string `json:"message"`
}

// Secp256k1HelloChallengeAccepted returns the signature of the HelloChallenge.Message
// hash.
// Secp256k1HelloChallengeAccepted is a client->server command containing the
// signature of the Secp256k1HelloChallenge.Message hash.
type Secp256k1HelloChallengeAccepted struct {
Signature string `json:"signature"`
}
Expand Down Expand Up @@ -106,7 +112,8 @@ type Secp256k1Auth struct {
privKey *dcrsecpk256k1.PrivateKey // client private key
pubKey *dcrsecpk256k1.PublicKey // client public key

remotePubKey *dcrsecpk256k1.PublicKey // server side remote key (client)
remoteUserAgent string // client user agent
remotePubKey *dcrsecpk256k1.PublicKey // server side remote key (client)
}

func NewSecp256k1AuthClient(privKey *dcrsecpk256k1.PrivateKey) (*Secp256k1Auth, error) {
Expand All @@ -117,7 +124,11 @@ func NewSecp256k1AuthServer() (*Secp256k1Auth, error) {
return &Secp256k1Auth{}, nil
}

func (s Secp256k1Auth) RemotePublicKey() *dcrsecpk256k1.PublicKey {
func (s *Secp256k1Auth) RemoteUserAgent() string {
return s.remoteUserAgent
}

func (s *Secp256k1Auth) RemotePublicKey() *dcrsecpk256k1.PublicKey {
pub := *s.remotePubKey
return &pub
}
Expand All @@ -136,8 +147,11 @@ func (s *Secp256k1Auth) HandshakeClient(ctx context.Context, conn protocol.APICo
defer log.Tracef("HandshakeClient exit")

pubKey := hex.EncodeToString(s.pubKey.SerializeCompressed())
id := "Hello: " + pubKey
err := protocol.Write(ctx, conn, s, id, Secp256k1Hello{PublicKey: pubKey})
id := "Hello:" + pubKey
err := protocol.Write(ctx, conn, s, id, Secp256k1Hello{
UserAgent: version.UserAgent(),
PublicKey: pubKey,
})
if err != nil {
return err
}
Expand Down
15 changes: 15 additions & 0 deletions api/protocol/dial.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2024 Hemi Labs, Inc.
// Use of this source code is governed by the MIT License,
// which can be found in the LICENSE file.

//go:build !js && !wasm

package protocol

import "github.com/coder/websocket"

func newDialOptions(opts ConnOptions) *websocket.DialOptions {
return &websocket.DialOptions{
HTTPHeader: opts.Headers,
}
}
16 changes: 16 additions & 0 deletions api/protocol/dial_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2024 Hemi Labs, Inc.
// Use of this source code is governed by the MIT License,
// which can be found in the LICENSE file.

//go:build js && wasm

package protocol

import "github.com/coder/websocket"

func newDialOptions(_ ConnOptions) *websocket.DialOptions {
// HTTPHeader is not supported in WASM, due to the JavaScript WebSocket API
// not supporting setting HTTP headers for the handshake/initial HTTP
// request.
return nil
}
6 changes: 5 additions & 1 deletion api/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"reflect"
"sync"
Expand Down Expand Up @@ -380,6 +381,9 @@ type ConnOptions struct {

// Authenticator is the connection authenticator.
Authenticator Authenticator

// Headers are the HTTP headers included in the WebSocket handshake request.
Headers http.Header
}

// defaultConnReadLimit is the default connection read limit.
Expand Down Expand Up @@ -430,7 +434,7 @@ func (ac *Conn) Connect(ctx context.Context) error {
// package.
// Note that we cannot have DialOptions on a WASM websocket
log.Tracef("Connect: dialing %v", ac.serverURL)
conn, _, err := websocket.Dial(connectCtx, ac.serverURL, nil)
conn, _, err := websocket.Dial(connectCtx, ac.serverURL, newDialOptions(ac.opts))
if err != nil {
return fmt.Errorf("dial server: %w", err)
}
Expand Down
9 changes: 7 additions & 2 deletions cmd/bfgd/bfgd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (

var (
log = loggo.GetLogger(daemonName)
welcome = fmt.Sprintf("Hemi Bitcoin Finality Governor: v%v", version.String())
welcome string

cfg = bfg.NewDefaultConfig()
cm = config.CfgMap{
Expand Down Expand Up @@ -129,7 +129,7 @@ func _main() error {
}

loggo.ConfigureLoggers(cfg.LogLevel)
log.Infof("%v", welcome)
log.Infof(welcome)

pc := config.PrintableConfig(cm)
for k := range pc {
Expand All @@ -152,6 +152,11 @@ func _main() error {
return nil
}

func init() {
version.Component = "bfgd"
welcome = "Hemi Bitcoin Finality Governor " + version.BuildInfo()
}

func main() {
if len(os.Args) != 1 {
fmt.Fprintf(os.Stderr, "%v\n", welcome)
Expand Down
7 changes: 6 additions & 1 deletion cmd/bssd/bssd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (

var (
log = loggo.GetLogger(daemonName)
welcome = fmt.Sprintf("Hemi Bitcoin Secure Sequencer: v%s", version.String())
welcome string

cfg = bss.NewDefaultConfig()
cm = config.CfgMap{
Expand Down Expand Up @@ -115,6 +115,11 @@ func _main() error {
return nil
}

func init() {
version.Component = "bssd"
welcome = "Hemi Bitcoin Secure Sequencer " + version.BuildInfo()
}

func main() {
if len(os.Args) != 1 {
fmt.Fprintf(os.Stderr, "%v\n", welcome)
Expand Down
2 changes: 2 additions & 0 deletions cmd/btctool/btctool.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/hemilabs/heminetwork/cmd/btctool/blockstream"
"github.com/hemilabs/heminetwork/cmd/btctool/btctool"
"github.com/hemilabs/heminetwork/database/tbcd"
"github.com/hemilabs/heminetwork/version"
)

var log = loggo.GetLogger("bdf")
Expand Down Expand Up @@ -488,6 +489,7 @@ func addressToScript(addr string) (btcutil.Address, error) {
}

func init() {
version.Component = "btctool"
}

func _main() error {
Expand Down
4 changes: 4 additions & 0 deletions cmd/extool/extool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
"github.com/hemilabs/heminetwork/version"
)

func init() {
version.Component = "extool"
}

func main() {
ver := flag.Bool("v", false, "version")
flag.Parse()
Expand Down
5 changes: 4 additions & 1 deletion cmd/hemictl/hemictl.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const (

var (
log = loggo.GetLogger(daemonName)
welcome = fmt.Sprintf("Hemi Network Controller: v%v", version.String())
welcome string

bssURL string
logLevel string
Expand Down Expand Up @@ -785,6 +785,9 @@ var (
)

func init() {
version.Component = "hemictl"
welcome = "Hemi Network Controller " + version.BuildInfo()

// merge all command maps
for k, v := range bssapi.APICommands() {
allCommands[string(k)] = v
Expand Down
7 changes: 6 additions & 1 deletion cmd/keygen/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
secp256k1KeyPair = flag.Bool("secp256k1", false, "Generate a secp256k1 key pair")
jsonFormat = flag.Bool("json", false, "print output as JSON")

welcome = fmt.Sprintf("key generator: v%v", version.String())
welcome string
)

func usage() {
Expand All @@ -34,6 +34,11 @@ func usage() {
flag.PrintDefaults()
}

func init() {
version.Component = "keygen"
welcome = "Key Generator " + version.BuildInfo()
}

func _main() error {
var btcChainParams *btcchaincfg.Params
switch *net {
Expand Down
7 changes: 6 additions & 1 deletion cmd/popmd/popmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (

var (
log = loggo.GetLogger(daemonName)
welcome = fmt.Sprintf("Hemi Proof of Proof miner: v%v", version.String())
welcome string

cfg = popm.NewDefaultConfig()
cm = config.CfgMap{
Expand Down Expand Up @@ -131,6 +131,11 @@ func _main() error {
return nil
}

func init() {
version.Component = "popmd"
welcome = "Hemi Proof-of-Proof Miner " + version.BuildInfo()
}

func main() {
if len(os.Args) != 1 {
fmt.Fprintf(os.Stderr, "%v\n", welcome)
Expand Down
1 change: 0 additions & 1 deletion cmd/tbcd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,3 @@ The `tbcd` daemon runs an RPC server that listens on the address provided by the
The RPC protocol is **WebSocket-based** and **uses a standard request/response model.**

[Read more about the RPC protocol and available commands](../../api/tbcapi/README.md).

7 changes: 6 additions & 1 deletion cmd/tbcd/tbcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (

var (
log = loggo.GetLogger(daemonName)
welcome = fmt.Sprintf("Hemi Tiny Bitcoin Daemon: v%v", version.String())
welcome string

cfg = tbc.NewDefaultConfig()
cm = config.CfgMap{
Expand Down Expand Up @@ -134,6 +134,11 @@ func HandleSignals(ctx context.Context, cancel context.CancelFunc, callback func
os.Exit(2)
}

func init() {
version.Component = "tbcd"
welcome = "Hemi Tiny Bitcoin Daemon " + version.BuildInfo()
}

func _main() error {
// Parse configuration from environment
if err := config.Parse(cm); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion docker/bfgd/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# Build stage
FROM golang:1.22.6-alpine3.20@sha256:1a478681b671001b7f029f94b5016aed984a23ad99c707f6a0ab6563860ae2f3 AS builder

ARG GO_LDFLAGS

# Add ca-certificates, timezone data, make and git
RUN apk --no-cache add --update ca-certificates tzdata make git

Expand All @@ -18,7 +20,7 @@ WORKDIR /build/
COPY . .

RUN make deps
RUN GOOS=$(go env GOOS) GOARCH=$(go env GOARCH) CGO_ENABLED=0 GOGC=off make bfgd
RUN GOOS=$(go env GOOS) GOARCH=$(go env GOARCH) CGO_ENABLED=0 GOGC=off make GO_LDFLAGS="$GO_LDFLAGS" bfgd

# Run stage
FROM scratch
Expand Down
4 changes: 3 additions & 1 deletion docker/bssd/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# Build stage
FROM golang:1.22.6-alpine3.20@sha256:1a478681b671001b7f029f94b5016aed984a23ad99c707f6a0ab6563860ae2f3 AS builder

ARG GO_LDFLAGS

# Add ca-certificates, timezone data, make and git
RUN apk --no-cache add --update ca-certificates tzdata make git

Expand All @@ -18,7 +20,7 @@ WORKDIR /build/
COPY . .

RUN make deps
RUN GOOS=$(go env GOOS) GOARCH=$(go env GOARCH) CGO_ENABLED=0 GOGC=off make bssd
RUN GOOS=$(go env GOOS) GOARCH=$(go env GOARCH) CGO_ENABLED=0 GOGC=off make GO_LDFLAGS="$GO_LDFLAGS" bssd

# Run stage
FROM scratch
Expand Down
Loading

0 comments on commit e702ce1

Please sign in to comment.