Skip to content

Commit 4f15bdd

Browse files
committed
chore(drand): upgrade drand to drand/v2 and drand/go-clients
1 parent 32aa3dc commit 4f15bdd

File tree

4 files changed

+230
-254
lines changed

4 files changed

+230
-254
lines changed

chain/beacon/drand/drand.go

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import (
55
"context"
66
"time"
77

8-
dchain "github.com/drand/drand/chain"
9-
dclient "github.com/drand/drand/client"
10-
hclient "github.com/drand/drand/client/http"
11-
dcrypto "github.com/drand/drand/crypto"
12-
dlog "github.com/drand/drand/log"
13-
gclient "github.com/drand/drand/lp2p/client"
8+
dcommon "github.com/drand/drand/v2/common"
9+
dchain "github.com/drand/drand/v2/common/chain"
10+
dlog "github.com/drand/drand/v2/common/log"
11+
dcrypto "github.com/drand/drand/v2/crypto"
12+
dclient "github.com/drand/go-clients/client"
13+
hclient "github.com/drand/go-clients/client/http"
14+
gclient "github.com/drand/go-clients/client/lp2p"
15+
drand "github.com/drand/go-clients/drand"
1416
"github.com/drand/kyber"
1517
lru "github.com/hashicorp/golang-lru/v2"
1618
logging "github.com/ipfs/go-log/v2"
@@ -39,7 +41,7 @@ var log = logging.Logger("drand")
3941
// The root trust for the Drand chain is configured from buildconstants.DrandConfigs
4042
type DrandBeacon struct {
4143
isChained bool
42-
client dclient.Client
44+
client drand.Client
4345

4446
pubkey kyber.Point
4547

@@ -92,13 +94,13 @@ func NewDrandBeacon(genesisTs, interval uint64, ps *pubsub.PubSub, config dtypes
9294
return nil, xerrors.Errorf("unable to unmarshal drand chain info: %w", err)
9395
}
9496

95-
var clients []dclient.Client
97+
var clients []drand.Client
9698
for _, url := range config.Servers {
97-
hc, err := hclient.NewWithInfo(url, drandChain, nil)
99+
hc, err := hclient.NewWithInfo(&logger{&log.SugaredLogger}, url, drandChain, nil)
98100
if err != nil {
99101
return nil, xerrors.Errorf("could not create http drand client: %w", err)
100102
}
101-
hc.(DrandHTTPClient).SetUserAgent("drand-client-lotus/" + build.NodeBuildVersion)
103+
hc.SetUserAgent("drand-client-lotus/" + build.NodeBuildVersion)
102104
clients = append(clients, hc)
103105
}
104106

@@ -111,18 +113,10 @@ func NewDrandBeacon(genesisTs, interval uint64, ps *pubsub.PubSub, config dtypes
111113
if ps != nil {
112114
opts = append(opts, gclient.WithPubsub(ps))
113115
} else {
114-
log.Info("drand beacon without pubsub")
115116
if len(clients) == 0 {
116-
// This hack is necessary to convince a drand beacon to start without any clients. For
117-
// historical becaons we need them to be able to verify old entries but we don't need to fetch
118-
// new ones. With pubsub enabled, it acts as a client so drand is happy, but if we don't have
119-
// pubsub then drand will complain about old beacons withotu clients. So we make one that
120-
// it'll think is a valid client and that it won't speed test (hence the need to mark it as
121-
// as "watcher").
122-
historicalClient := &historicalBeaconClient{}
123-
opts = append(opts, dclient.WithWatcher(func(chainInfo *dchain.Info, cache dclient.Cache) (dclient.Watcher, error) {
124-
return historicalClient, nil
125-
}))
117+
// This is necessary to convince a drand beacon to start without any clients. For historical
118+
// beacons we need them to be able to verify old entries but we don't need to fetch new ones.
119+
clients = append(clients, dclient.EmptyClientWithInfo(drandChain))
126120
}
127121
}
128122

@@ -142,7 +136,7 @@ func NewDrandBeacon(genesisTs, interval uint64, ps *pubsub.PubSub, config dtypes
142136
localCache: lc,
143137
}
144138

145-
sch, err := dcrypto.GetSchemeByIDWithDefault(drandChain.Scheme)
139+
sch, err := dcrypto.GetSchemeByID(drandChain.Scheme)
146140
if err != nil {
147141
return nil, err
148142
}
@@ -176,8 +170,8 @@ func (db *DrandBeacon) Entry(ctx context.Context, round uint64) <-chan beacon.Re
176170
if err != nil {
177171
br.Err = xerrors.Errorf("drand failed Get request: %w", err)
178172
} else {
179-
br.Entry.Round = resp.Round()
180-
br.Entry.Data = resp.Signature()
173+
br.Entry.Round = resp.GetRound()
174+
br.Entry.Data = resp.GetSignature()
181175
}
182176
log.Debugw("done fetching randomness", "round", round, "took", build.Clock.Since(start))
183177
out <- br
@@ -203,7 +197,7 @@ func (db *DrandBeacon) VerifyEntry(entry types.BeaconEntry, prevEntrySig []byte)
203197
// return no error if the value is in the cache already
204198
return nil
205199
}
206-
b := &dchain.Beacon{
200+
b := &dcommon.Beacon{
207201
PreviousSig: prevEntrySig,
208202
Round: entry.Round,
209203
Signature: entry.Data,
@@ -253,38 +247,10 @@ func BeaconScheduleFromDrandSchedule(dcs dtypes.DrandSchedule, genesisTime uint6
253247
for i, dc := range dcs {
254248
bc, err := NewDrandBeacon(genesisTime, buildconstants.BlockDelaySecs, ps, dc.Config)
255249
if err != nil {
256-
return nil, xerrors.Errorf("%d creating drand beacon: %w", i, err)
250+
return nil, xerrors.Errorf("creating drand beacon #%d: %w", i, err)
257251
}
258252
shd = append(shd, beacon.BeaconPoint{Start: dc.Start, Beacon: bc})
259253
}
260254

261255
return shd, nil
262256
}
263-
264-
var _ dclient.Client = historicalBeaconClient{}
265-
266-
// historicalBeaconClient is a drand client that doesn't actually do anything. It's used when
267-
// we don't have a drand network to connect to but still need to provide a beacon client.
268-
// We don't expect calls through to the client to be made since we should only be verifying old
269-
// randomness, not fetching it.
270-
type historicalBeaconClient struct{}
271-
272-
func (h historicalBeaconClient) Get(ctx context.Context, round uint64) (dclient.Result, error) {
273-
return nil, xerrors.Errorf("no historical randomness available")
274-
}
275-
276-
func (h historicalBeaconClient) Watch(ctx context.Context) <-chan dclient.Result {
277-
return nil
278-
}
279-
280-
func (h historicalBeaconClient) Info(ctx context.Context) (*dchain.Info, error) {
281-
return nil, xerrors.Errorf("no historical randomness available")
282-
}
283-
284-
func (h historicalBeaconClient) RoundAt(time.Time) uint64 {
285-
return 0
286-
}
287-
288-
func (h historicalBeaconClient) Close() error {
289-
return nil
290-
}

chain/beacon/drand/drand_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"os"
88
"testing"
99

10-
dchain "github.com/drand/drand/chain"
11-
hclient "github.com/drand/drand/client/http"
10+
dchain "github.com/drand/drand/v2/common/chain"
11+
hclient "github.com/drand/go-clients/client/http"
1212
"github.com/stretchr/testify/assert"
1313

1414
"github.com/filecoin-project/go-state-types/network"
@@ -22,13 +22,10 @@ func TestPrintGroupInfo(t *testing.T) {
2222

2323
drandChain, err := dchain.InfoFromJSON(bytes.NewReader([]byte(chainInfo)))
2424
assert.NoError(t, err)
25-
c, err := hclient.NewWithInfo(server, drandChain, nil)
25+
c, err := hclient.NewWithInfo(&logger{&log.SugaredLogger}, server, drandChain, nil)
2626

2727
assert.NoError(t, err)
28-
cg := c.(interface {
29-
FetchChainInfo(ctx context.Context, groupHash []byte) (*dchain.Info, error)
30-
})
31-
chain, err := cg.FetchChainInfo(context.Background(), nil)
28+
chain, err := c.FetchChainInfo(context.Background(), nil)
3229
assert.NoError(t, err)
3330
err = chain.ToJSON(os.Stdout, nil)
3431
assert.NoError(t, err)

0 commit comments

Comments
 (0)