Skip to content

Commit

Permalink
chore(drand): upgrade drand to drand/v2 and drand/go-clients
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Feb 4, 2025
1 parent e4d8ec1 commit c0fad9d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 125 deletions.
74 changes: 20 additions & 54 deletions chain/beacon/drand/drand.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"context"
"time"

dchain "github.com/drand/drand/chain"
dclient "github.com/drand/drand/client"
hclient "github.com/drand/drand/client/http"
dcrypto "github.com/drand/drand/crypto"
dlog "github.com/drand/drand/log"
gclient "github.com/drand/drand/lp2p/client"
dcommon "github.com/drand/drand/v2/common"
dchain "github.com/drand/drand/v2/common/chain"
dlog "github.com/drand/drand/v2/common/log"
dcrypto "github.com/drand/drand/v2/crypto"
dclient "github.com/drand/go-clients/client"
hclient "github.com/drand/go-clients/client/http"
gclient "github.com/drand/go-clients/client/lp2p"
drand "github.com/drand/go-clients/drand"
"github.com/drand/kyber"
lru "github.com/hashicorp/golang-lru/v2"
logging "github.com/ipfs/go-log/v2"
Expand Down Expand Up @@ -39,7 +41,7 @@ var log = logging.Logger("drand")
// The root trust for the Drand chain is configured from buildconstants.DrandConfigs
type DrandBeacon struct {
isChained bool
client dclient.Client
client drand.Client

pubkey kyber.Point

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

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

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

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

sch, err := dcrypto.GetSchemeByIDWithDefault(drandChain.Scheme)
sch, err := dcrypto.GetSchemeByID(drandChain.Scheme)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -176,8 +170,8 @@ func (db *DrandBeacon) Entry(ctx context.Context, round uint64) <-chan beacon.Re
if err != nil {
br.Err = xerrors.Errorf("drand failed Get request: %w", err)
} else {
br.Entry.Round = resp.Round()
br.Entry.Data = resp.Signature()
br.Entry.Round = resp.GetRound()
br.Entry.Data = resp.GetSignature()
}
log.Debugw("done fetching randomness", "round", round, "took", build.Clock.Since(start))
out <- br
Expand All @@ -203,7 +197,7 @@ func (db *DrandBeacon) VerifyEntry(entry types.BeaconEntry, prevEntrySig []byte)
// return no error if the value is in the cache already
return nil
}
b := &dchain.Beacon{
b := &dcommon.Beacon{
PreviousSig: prevEntrySig,
Round: entry.Round,
Signature: entry.Data,
Expand Down Expand Up @@ -253,38 +247,10 @@ func BeaconScheduleFromDrandSchedule(dcs dtypes.DrandSchedule, genesisTime uint6
for i, dc := range dcs {
bc, err := NewDrandBeacon(genesisTime, buildconstants.BlockDelaySecs, ps, dc.Config)
if err != nil {
return nil, xerrors.Errorf("%d creating drand beacon: %w", i, err)
return nil, xerrors.Errorf("creating drand beacon #%d: %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
}
11 changes: 4 additions & 7 deletions chain/beacon/drand/drand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"os"
"testing"

dchain "github.com/drand/drand/chain"
hclient "github.com/drand/drand/client/http"
dchain "github.com/drand/drand/v2/common/chain"
hclient "github.com/drand/go-clients/client/http"
"github.com/stretchr/testify/assert"

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

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

assert.NoError(t, err)
cg := c.(interface {
FetchChainInfo(ctx context.Context, groupHash []byte) (*dchain.Info, error)
})
chain, err := cg.FetchChainInfo(context.Background(), nil)
chain, err := c.FetchChainInfo(context.Background(), nil)
assert.NoError(t, err)
err = chain.ToJSON(os.Stdout, nil)
assert.NoError(t, err)
Expand Down
23 changes: 13 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ replace github.com/filecoin-project/filecoin-ffi => ./extern/filecoin-ffi // pro

require (
contrib.go.opencensus.io/exporter/prometheus v0.4.2
github.com/BurntSushi/toml v1.3.2
github.com/BurntSushi/toml v1.4.0
github.com/GeertJohan/go.rice v1.0.3
github.com/Gurpartap/async v0.0.0-20180927173644-4f7f499dd9ee
github.com/Kubuxu/imtui v0.0.0-20210401140320-41663d68d0fa
Expand All @@ -30,14 +30,13 @@ require (
github.com/detailyang/go-fallocate v0.0.0-20180908115635-432fa640bd2e
github.com/dgraph-io/badger/v2 v2.2007.4
github.com/docker/go-units v0.5.0
github.com/drand/drand v1.5.11
github.com/drand/kyber v1.3.1
github.com/dustin/go-humanize v1.0.1
github.com/elastic/go-elasticsearch/v7 v7.14.0
github.com/elastic/go-sysinfo v1.7.0
github.com/elastic/gosigar v0.14.3
github.com/etclabscore/go-openrpc-reflect v0.0.36
github.com/fatih/color v1.15.0
github.com/fatih/color v1.17.0
github.com/filecoin-project/filecoin-ffi v1.31.0
github.com/filecoin-project/go-address v1.2.0
github.com/filecoin-project/go-amt-ipld/v4 v4.4.0
Expand Down Expand Up @@ -135,7 +134,7 @@ require (
github.com/stretchr/testify v1.10.0
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // dependency-check-ignore: unknown
github.com/triplewz/poseidon v0.0.2
github.com/urfave/cli/v2 v2.25.5
github.com/urfave/cli/v2 v2.27.2
github.com/whyrusleeping/bencher v0.0.0-20190829221104-bb6607aa8bba
github.com/whyrusleeping/cbor-gen v0.2.0
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7
Expand Down Expand Up @@ -189,11 +188,13 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/cskr/pubsub v1.0.2 // indirect
github.com/daaku/go.zipexe v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/drand/drand/v2 v2.0.4
github.com/drand/go-clients v0.2.1
github.com/drand/kyber-bls12381 v0.3.1 // indirect
github.com/elastic/go-windows v1.0.0 // indirect
github.com/etclabscore/go-jsonschema-walk v0.0.6 // indirect
Expand All @@ -217,7 +218,7 @@ require (
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.2.0 // indirect
github.com/golang/glog v1.2.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.4 // indirect
Expand Down Expand Up @@ -307,7 +308,7 @@ require (
github.com/pion/turn/v2 v2.1.6 // indirect
github.com/pion/webrtc/v3 v3.3.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.60.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
Expand All @@ -331,11 +332,12 @@ require (
github.com/wlynxg/anet v0.0.5 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
github.com/zondax/hid v0.9.2 // indirect
github.com/zondax/ledger-go v0.14.3 // indirect
gitlab.com/yawning/secp256k1-voi v0.0.0-20230925100816-f2616030848b // indirect
gitlab.com/yawning/tuplehash v0.0.0-20230713102510-df83abbf9a02 // indirect
go.dedis.ch/fixbuf v1.0.3 // indirect
go.dedis.ch/kyber/v4 v4.0.0-pre2.0.20240924132404-4de33740016e // indirect; dependency-check-ignore: unknown
go.opentelemetry.io/otel/trace v1.28.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
Expand All @@ -345,8 +347,9 @@ require (
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
golang.org/x/text v0.21.0 // indirect
gonum.org/v1/gonum v0.15.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240515191416-fc5f0ca64291 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240723171418-e6d459c13d2a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240723171418-e6d459c13d2a // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.35.1 // indirect
gopkg.in/cheggaaa/pb.v1 v1.0.28 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit c0fad9d

Please sign in to comment.