improve DrandConfig dependency injection

This commit is contained in:
Yusef Napora 2020-06-23 15:56:03 -04:00
parent 99d3571772
commit b448de422e
7 changed files with 42 additions and 32 deletions

View File

@ -121,4 +121,11 @@ const VerifSigCacheSize = 32000
const BlockMessageLimit = 512 const BlockMessageLimit = 512
const BlockGasLimit = 100_000_000_000 const BlockGasLimit = 100_000_000_000
var DrandChain = `{"public_key":"922a2e93828ff83345bae533f5172669a26c02dc76d6bf59c80892e12ab1455c229211886f35bb56af6d5bea981024df","period":25,"genesis_time":1590445175,"hash":"138a324aa6540f93d0dad002aa89454b1bec2b6e948682cde6bd4db40f4b7c9b"}` var DrandConfig = dtypes.DrandConfig{
Servers: []string{
"https://pl-eu.testnet.drand.sh",
"https://pl-us.testnet.drand.sh",
"https://pl-sin.testnet.drand.sh",
},
ChainInfoJSON: `{"public_key":"922a2e93828ff83345bae533f5172669a26c02dc76d6bf59c80892e12ab1455c229211886f35bb56af6d5bea981024df","period":25,"genesis_time":1590445175,"hash":"138a324aa6540f93d0dad002aa89454b1bec2b6e948682cde6bd4db40f4b7c9b"}`,
}

View File

@ -19,33 +19,17 @@ import (
logging "github.com/ipfs/go-log" logging "github.com/ipfs/go-log"
pubsub "github.com/libp2p/go-libp2p-pubsub" pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/filecoin-project/lotus/build" "github.com/filecoin-project/specs-actors/actors/abi"
"github.com/filecoin-project/lotus/chain/beacon" "github.com/filecoin-project/lotus/chain/beacon"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/specs-actors/actors/abi"
) )
var log = logging.Logger("drand") var log = logging.Logger("drand")
type DrandConfig struct { type DrandConfig struct {
Servers []string Servers []string
ChainInfo *dchain.Info ChainInfoJSON string
}
var defaultConfig = DrandConfig{
Servers: []string{
"https://pl-eu.testnet.drand.sh",
"https://pl-us.testnet.drand.sh",
"https://pl-sin.testnet.drand.sh",
},
}
func init() {
var err error
defaultConfig.ChainInfo, err = dchain.InfoFromJSON(bytes.NewReader([]byte(build.DrandChain)))
if err != nil {
panic("could not unmarshal chain info: " + err.Error())
}
} }
type drandPeer struct { type drandPeer struct {
@ -77,15 +61,15 @@ type DrandBeacon struct {
localCache map[uint64]types.BeaconEntry localCache map[uint64]types.BeaconEntry
} }
func NewDrandBeacon(genesisTs, interval uint64, ps *pubsub.PubSub, config *DrandConfig) (*DrandBeacon, error) { func NewDrandBeacon(genesisTs, interval uint64, ps *pubsub.PubSub, config DrandConfig) (*DrandBeacon, error) {
if genesisTs == 0 { if genesisTs == 0 {
panic("what are you doing this cant be zero") panic("what are you doing this cant be zero")
} }
if config == nil { drandChain, err := dchain.InfoFromJSON(bytes.NewReader([]byte(config.ChainInfoJSON)))
config = &defaultConfig if err != nil {
return nil, xerrors.Errorf("unable to unmarshal drand chain info: %w", err)
} }
drandChain := config.ChainInfo
dlogger := dlog.NewKitLoggerFrom(kzap.NewZapSugarLogger( dlogger := dlog.NewKitLoggerFrom(kzap.NewZapSugarLogger(
log.SugaredLogger.Desugar(), zapcore.InfoLevel)) log.SugaredLogger.Desugar(), zapcore.InfoLevel))

View File

@ -7,10 +7,13 @@ import (
dchain "github.com/drand/drand/chain" dchain "github.com/drand/drand/chain"
hclient "github.com/drand/drand/client/http" hclient "github.com/drand/drand/client/http"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/filecoin-project/lotus/build"
) )
func TestPrintGroupInfo(t *testing.T) { func TestPrintGroupInfo(t *testing.T) {
c, err := hclient.New(defaultConfig.Servers[0], nil, nil) server := build.DrandConfig.Servers[0]
c, err := hclient.New(server, nil, nil)
assert.NoError(t, err) assert.NoError(t, err)
cg := c.(interface { cg := c.(interface {
FetchChainInfo(groupHash []byte) (*dchain.Info, error) FetchChainInfo(groupHash []byte) (*dchain.Info, error)

View File

@ -218,6 +218,7 @@ func Online() Option {
Override(new(dtypes.BootstrapPeers), modules.BuiltinBootstrap), Override(new(dtypes.BootstrapPeers), modules.BuiltinBootstrap),
Override(new(dtypes.DrandBootstrap), modules.DrandBootstrap), Override(new(dtypes.DrandBootstrap), modules.DrandBootstrap),
Override(new(dtypes.DrandConfig), modules.BuiltinDrandConfig),
Override(HandleIncomingMessagesKey, modules.HandleIncomingMessages), Override(HandleIncomingMessagesKey, modules.HandleIncomingMessages),

View File

@ -0,0 +1,6 @@
package dtypes
type DrandConfig struct {
Servers []string
ChainInfoJSON string
}

View File

@ -44,13 +44,14 @@ type GossipIn struct {
Db dtypes.DrandBootstrap Db dtypes.DrandBootstrap
Cfg *config.Pubsub Cfg *config.Pubsub
Sk *dtypes.ScoreKeeper Sk *dtypes.ScoreKeeper
Dr dtypes.DrandConfig
} }
func getDrandTopic() (string, error) { func getDrandTopic(chainInfoJSON string) (string, error) {
var drandInfo = struct { var drandInfo = struct {
Hash string `json:"hash"` Hash string `json:"hash"`
}{} }{}
err := json.Unmarshal([]byte(build.DrandChain), &drandInfo) err := json.Unmarshal([]byte(chainInfoJSON), &drandInfo)
if err != nil { if err != nil {
return "", xerrors.Errorf("could not unmarshal drand chain info: %w", err) return "", xerrors.Errorf("could not unmarshal drand chain info: %w", err)
} }
@ -68,7 +69,7 @@ func GossipSub(in GossipIn) (service *pubsub.PubSub, err error) {
} }
isBootstrapNode := in.Cfg.Bootstrapper isBootstrapNode := in.Cfg.Bootstrapper
drandTopic, err := getDrandTopic() drandTopic, err := getDrandTopic(in.Dr.ChainInfoJSON)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -108,9 +108,13 @@ func RetrievalResolver(l *discovery.Local) retrievalmarket.PeerResolver {
type RandomBeaconParams struct { type RandomBeaconParams struct {
fx.In fx.In
DrandConfig *drand.DrandConfig `optional:"true"`
PubSub *pubsub.PubSub `optional:"true"` PubSub *pubsub.PubSub `optional:"true"`
Cs *store.ChainStore Cs *store.ChainStore
DrandConfig dtypes.DrandConfig
}
func BuiltinDrandConfig() dtypes.DrandConfig {
return build.DrandConfig
} }
func RandomBeacon(p RandomBeaconParams, _ dtypes.AfterGenesisSet) (beacon.RandomBeacon, error) { func RandomBeacon(p RandomBeaconParams, _ dtypes.AfterGenesisSet) (beacon.RandomBeacon, error) {
@ -120,5 +124,9 @@ func RandomBeacon(p RandomBeaconParams, _ dtypes.AfterGenesisSet) (beacon.Random
} }
//return beacon.NewMockBeacon(build.BlockDelay * time.Second) //return beacon.NewMockBeacon(build.BlockDelay * time.Second)
return drand.NewDrandBeacon(gen.Timestamp, build.BlockDelay, p.PubSub, p.DrandConfig) config := drand.DrandConfig{
Servers: p.DrandConfig.Servers,
ChainInfoJSON: p.DrandConfig.ChainInfoJSON,
}
return drand.NewDrandBeacon(gen.Timestamp, build.BlockDelay, p.PubSub, config)
} }