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

Set beacon handler for the default beacon process #38

Merged
merged 1 commit into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions common/beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import "os"
// value should not be changed for backward-compatibility reasons
const DefaultBeaconID = "default"

// DefaultChainHash is the value used when chain hash has an empty value on requests
// from clients. This value should not be changed for
// backward-compatibility reasons
const DefaultChainHash = "default"

// MultiBeaconFolder
const MultiBeaconFolder = "multibeacon"

Expand All @@ -14,3 +19,7 @@ const MultiBeaconFolder = "multibeacon"
func GetBeaconIDFromEnv() string {
return os.Getenv("BEACON_ID")
}

func IsDefaultBeaconID(beaconID string) bool {
return beaconID == DefaultBeaconID || beaconID == ""
}
8 changes: 6 additions & 2 deletions core/drand_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"context"
"errors"
"fmt"
"github.com/drand/drand/chain"
"sync"

"github.com/drand/drand/chain"

"github.com/drand/drand/metrics"
"github.com/drand/drand/metrics/pprof"
"github.com/drand/drand/protobuf/drand"
Expand Down Expand Up @@ -141,7 +142,10 @@ func (dd *DrandDaemon) InstantiateBeaconProcess(beaconID string, store key.Store

if !bp.isFreshRun() {
info := chain.NewChainInfo(bp.group)
dd.handler.HandlerDrand.CreateBeaconHandler(&drandProxy{bp}, info.HashString())
bh := dd.handler.HandlerDrand.CreateBeaconHandler(&drandProxy{bp}, info.HashString())
if common.IsDefaultBeaconID(beaconID) {
dd.handler.HandlerDrand.AddDefaultBeaconHandler(bh)
}
}

return bp, nil
Expand Down
7 changes: 6 additions & 1 deletion core/drand_daemon_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package core
import (
"context"
"fmt"

"github.com/drand/drand/chain"
common2 "github.com/drand/drand/common"

"github.com/drand/drand/key"

Expand Down Expand Up @@ -39,7 +41,10 @@ func (dd *DrandDaemon) InitDKG(c context.Context, in *drand.InitDKGPacket) (*dra
group, err := key.GroupFromProto(chainGroup)
if err == nil {
info := chain.NewChainInfo(group)
dd.handler.HandlerDrand.CreateBeaconHandler(&drandProxy{bp}, info.HashString())
bh := dd.handler.HandlerDrand.CreateBeaconHandler(&drandProxy{bp}, info.HashString())
if common2.IsDefaultBeaconID(beaconID) {
dd.handler.HandlerDrand.AddDefaultBeaconHandler(bh)
}
}
}

Expand Down
14 changes: 12 additions & 2 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func New(ctx context.Context, c client.Client, version string, logger log.Logger
return DrandHandler{instrumented, handler}, nil
}

func (h *handler) CreateBeaconHandler(c client.Client, chainHash string) {
func (h *handler) CreateBeaconHandler(c client.Client, chainHash string) *beaconHandler {
h.state.Lock()
defer h.state.Unlock()

Expand All @@ -126,6 +126,16 @@ func (h *handler) CreateBeaconHandler(c client.Client, chainHash string) {

h.beacons[chainHash] = bh
h.log.Infow("Created BeaconHandler", "chainHash", chainHash)

return bh
}

func (h *handler) AddDefaultBeaconHandler(bh *beaconHandler) {
h.state.Lock()
defer h.state.Unlock()

h.beacons[common.DefaultChainHash] = bh
h.log.Infow("Created Default BeaconHandler")
}

func withCommonHeaders(version string, h func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
Expand Down Expand Up @@ -528,7 +538,7 @@ func readRound(r *http.Request) string {
func (h *handler) getBeaconHandler(chainHash []byte) (*beaconHandler, error) {
chainHashStr := fmt.Sprintf("%x", chainHash)
if chainHashStr == "" {
chainHashStr = common.DefaultBeaconID
chainHashStr = common.DefaultChainHash
}

h.state.Lock()
Expand Down