Skip to content

Commit c0fad9d

Browse files
committed
chore(drand): upgrade drand to drand/v2 and drand/go-clients
1 parent e4d8ec1 commit c0fad9d

File tree

4 files changed

+84
-125
lines changed

4 files changed

+84
-125
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)

go.mod

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ replace github.com/filecoin-project/filecoin-ffi => ./extern/filecoin-ffi // pro
1414

1515
require (
1616
contrib.go.opencensus.io/exporter/prometheus v0.4.2
17-
github.com/BurntSushi/toml v1.3.2
17+
github.com/BurntSushi/toml v1.4.0
1818
github.com/GeertJohan/go.rice v1.0.3
1919
github.com/Gurpartap/async v0.0.0-20180927173644-4f7f499dd9ee
2020
github.com/Kubuxu/imtui v0.0.0-20210401140320-41663d68d0fa
@@ -30,14 +30,13 @@ require (
3030
github.com/detailyang/go-fallocate v0.0.0-20180908115635-432fa640bd2e
3131
github.com/dgraph-io/badger/v2 v2.2007.4
3232
github.com/docker/go-units v0.5.0
33-
github.com/drand/drand v1.5.11
3433
github.com/drand/kyber v1.3.1
3534
github.com/dustin/go-humanize v1.0.1
3635
github.com/elastic/go-elasticsearch/v7 v7.14.0
3736
github.com/elastic/go-sysinfo v1.7.0
3837
github.com/elastic/gosigar v0.14.3
3938
github.com/etclabscore/go-openrpc-reflect v0.0.36
40-
github.com/fatih/color v1.15.0
39+
github.com/fatih/color v1.17.0
4140
github.com/filecoin-project/filecoin-ffi v1.31.0
4241
github.com/filecoin-project/go-address v1.2.0
4342
github.com/filecoin-project/go-amt-ipld/v4 v4.4.0
@@ -135,7 +134,7 @@ require (
135134
github.com/stretchr/testify v1.10.0
136135
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // dependency-check-ignore: unknown
137136
github.com/triplewz/poseidon v0.0.2
138-
github.com/urfave/cli/v2 v2.25.5
137+
github.com/urfave/cli/v2 v2.27.2
139138
github.com/whyrusleeping/bencher v0.0.0-20190829221104-bb6607aa8bba
140139
github.com/whyrusleeping/cbor-gen v0.2.0
141140
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7
@@ -189,11 +188,13 @@ require (
189188
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
190189
github.com/cskr/pubsub v1.0.2 // indirect
191190
github.com/daaku/go.zipexe v1.0.2 // indirect
192-
github.com/davecgh/go-spew v1.1.1 // indirect
191+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
193192
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
194193
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
195194
github.com/dgraph-io/ristretto v0.1.1 // indirect
196195
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
196+
github.com/drand/drand/v2 v2.0.4
197+
github.com/drand/go-clients v0.2.1
197198
github.com/drand/kyber-bls12381 v0.3.1 // indirect
198199
github.com/elastic/go-windows v1.0.0 // indirect
199200
github.com/etclabscore/go-jsonschema-walk v0.0.6 // indirect
@@ -217,7 +218,7 @@ require (
217218
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
218219
github.com/godbus/dbus/v5 v5.1.0 // indirect
219220
github.com/gogo/protobuf v1.3.2 // indirect
220-
github.com/golang/glog v1.2.0 // indirect
221+
github.com/golang/glog v1.2.1 // indirect
221222
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
222223
github.com/golang/protobuf v1.5.4 // indirect
223224
github.com/golang/snappy v0.0.4 // indirect
@@ -307,7 +308,7 @@ require (
307308
github.com/pion/turn/v2 v2.1.6 // indirect
308309
github.com/pion/webrtc/v3 v3.3.4 // indirect
309310
github.com/pkg/errors v0.9.1 // indirect
310-
github.com/pmezard/go-difflib v1.0.0 // indirect
311+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
311312
github.com/prometheus/client_model v0.6.1 // indirect
312313
github.com/prometheus/common v0.60.0 // indirect
313314
github.com/prometheus/procfs v0.15.1 // indirect
@@ -331,11 +332,12 @@ require (
331332
github.com/wlynxg/anet v0.0.5 // indirect
332333
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
333334
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
334-
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
335+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
335336
github.com/zondax/hid v0.9.2 // indirect
336337
github.com/zondax/ledger-go v0.14.3 // indirect
337338
gitlab.com/yawning/secp256k1-voi v0.0.0-20230925100816-f2616030848b // indirect
338339
gitlab.com/yawning/tuplehash v0.0.0-20230713102510-df83abbf9a02 // indirect
340+
go.dedis.ch/fixbuf v1.0.3 // indirect
339341
go.dedis.ch/kyber/v4 v4.0.0-pre2.0.20240924132404-4de33740016e // indirect; dependency-check-ignore: unknown
340342
go.opentelemetry.io/otel/trace v1.28.0 // indirect
341343
go.uber.org/atomic v1.11.0 // indirect
@@ -345,8 +347,9 @@ require (
345347
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
346348
golang.org/x/text v0.21.0 // indirect
347349
gonum.org/v1/gonum v0.15.0 // indirect
348-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240515191416-fc5f0ca64291 // indirect
349-
google.golang.org/grpc v1.64.0 // indirect
350+
google.golang.org/genproto/googleapis/api v0.0.0-20240723171418-e6d459c13d2a // indirect
351+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240723171418-e6d459c13d2a // indirect
352+
google.golang.org/grpc v1.65.0 // indirect
350353
google.golang.org/protobuf v1.35.1 // indirect
351354
gopkg.in/cheggaaa/pb.v1 v1.0.28 // indirect
352355
gopkg.in/yaml.v2 v2.4.0 // indirect

0 commit comments

Comments
 (0)