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

fix(drand): add null HistoricalBeaconClient for old beacons #12830

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 23 additions & 18 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,15 +94,14 @@ 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)

}

opts := []dclient.Option{
Expand All @@ -112,7 +113,11 @@ 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 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))
}
}

client, err := dclient.Wrap(clients, opts...)
Expand All @@ -131,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 @@ -165,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 @@ -192,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 @@ -239,10 +244,10 @@ 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)
return nil, xerrors.Errorf("creating drand beacon #%d: %w", i, err)
}
shd = append(shd, beacon.BeaconPoint{Start: dc.Start, Beacon: bc})
}
Expand Down
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
Loading
Loading