Skip to content

Commit

Permalink
fix(drand): add null HistoricalBeaconClient for old beacons
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Jan 20, 2025
1 parent b83bf69 commit d5899e1
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions chain/beacon/drand/drand.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package drand
import (
"bytes"
"context"
"fmt"
"time"

dchain "github.com/drand/drand/chain"
Expand Down Expand Up @@ -100,7 +101,9 @@ func NewDrandBeacon(genesisTs, interval uint64, ps *pubsub.PubSub, config dtypes
}
hc.(DrandHTTPClient).SetUserAgent("drand-client-lotus/" + build.NodeBuildVersion)
clients = append(clients, hc)

}
if len(clients) == 0 {
clients = append(clients, HistoricalBeaconClient{})
}

opts := []dclient.Option{
Expand Down Expand Up @@ -239,13 +242,42 @@ var _ beacon.RandomBeacon = (*DrandBeacon)(nil)

func BeaconScheduleFromDrandSchedule(dcs dtypes.DrandSchedule, genesisTime uint64, ps *pubsub.PubSub) (beacon.Schedule, error) {
shd := beacon.Schedule{}
for _, dc := range dcs {
for i, dc := range dcs {
bc, err := NewDrandBeacon(genesisTime, buildconstants.BlockDelaySecs, ps, dc.Config)
if err != nil {
return nil, xerrors.Errorf("creating drand beacon: %w", err)
fmt.Printf("failed to create drand beacon: %+v\n", dc)
return nil, xerrors.Errorf("%d creating drand beacon: %w", i, err)
}
shd = append(shd, beacon.BeaconPoint{Start: dc.Start, Beacon: bc})
}

return shd, nil
}

var _ dclient.Client = HistoricalBeaconClient{}

// HistoricalBeaconClient is a drand client that doesn't actually do anything. It's used when
// we don't have a drand network to connect to but still need to provide a beacon client.
// We don't expect calls through to the client to be made since we should only be verifying old
// randomness, not fetching it.
type HistoricalBeaconClient struct{}

func (h HistoricalBeaconClient) Get(ctx context.Context, round uint64) (dclient.Result, error) {
return nil, xerrors.Errorf("no historical randomness available")
}

func (h HistoricalBeaconClient) Watch(ctx context.Context) <-chan dclient.Result {
return nil
}

func (h HistoricalBeaconClient) Info(ctx context.Context) (*dchain.Info, error) {
return nil, xerrors.Errorf("no historical randomness available")
}

func (h HistoricalBeaconClient) RoundAt(time.Time) uint64 {
return 0
}

func (h HistoricalBeaconClient) Close() error {
return nil
}

0 comments on commit d5899e1

Please sign in to comment.