Draw the rest of the owl

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-09-09 20:37:12 +02:00 committed by Aayush Rajasekaran
parent 2d3f92aeed
commit 64fa6fd9e5
14 changed files with 110 additions and 36 deletions

View File

@ -1,15 +1,26 @@
package build package build
import "github.com/filecoin-project/lotus/node/modules/dtypes" import (
"sort"
var DrandNetwork = DrandIncentinet "github.com/filecoin-project/lotus/node/modules/dtypes"
)
func DrandConfig() dtypes.DrandConfig {
return DrandConfigs[DrandNetwork]
}
type DrandEnum int type DrandEnum int
func DrandConfigSchedule() dtypes.DrandSchedule {
out := dtypes.DrandSchedule{}
for start, config := range DrandSchedule {
out = append(out, dtypes.DrandPoint{Start: start, Config: DrandConfigs[config]})
}
sort.Slice(out, func(i, j int) bool {
return out[i].Start < out[j].Start
})
return out
}
const ( const (
DrandMainnet DrandEnum = iota + 1 DrandMainnet DrandEnum = iota + 1
DrandTestnet DrandTestnet

View File

@ -13,6 +13,11 @@ import (
const UpgradeBreezeHeight = -1 const UpgradeBreezeHeight = -1
const BreezeGasTampingDuration = 0 const BreezeGasTampingDuration = 0
var DrandSchedule = map[abi.ChainEpoch]DrandEnum{
0: DrandIncentinet,
3: DrandMainnet,
}
func init() { func init() {
power.ConsensusMinerMinPower = big.NewInt(2048) power.ConsensusMinerMinPower = big.NewInt(2048)
miner.SupportedProofTypes = map[abi.RegisteredSealProof]struct{}{ miner.SupportedProofTypes = map[abi.RegisteredSealProof]struct{}{
@ -23,7 +28,7 @@ func init() {
BuildType |= Build2k BuildType |= Build2k
} }
const BlockDelaySecs = uint64(4) const BlockDelaySecs = uint64(30)
const PropagationDelaySecs = uint64(1) const PropagationDelaySecs = uint64(1)

View File

@ -12,6 +12,10 @@ import (
"github.com/filecoin-project/specs-actors/actors/builtin/power" "github.com/filecoin-project/specs-actors/actors/builtin/power"
) )
var DrandSchedule = map[abi.ChainEpoch]DrandEnum{
0: DrandIncentinet,
}
const UpgradeBreezeHeight = 41280 const UpgradeBreezeHeight = 41280
const BreezeGasTampingDuration = 120 const BreezeGasTampingDuration = 120

View File

@ -42,15 +42,30 @@ type BeaconPoint struct {
type RandomBeacon interface { type RandomBeacon interface {
Entry(context.Context, uint64) <-chan Response Entry(context.Context, uint64) <-chan Response
VerifyEntry(types.BeaconEntry, types.BeaconEntry) error VerifyEntry(types.BeaconEntry, types.BeaconEntry) error
MaxBeaconRoundForEpoch(abi.ChainEpoch, types.BeaconEntry) uint64 MaxBeaconRoundForEpoch(abi.ChainEpoch) uint64
} }
func ValidateBlockValues(bSchedule Schedule, h *types.BlockHeader, parentEpoch abi.ChainEpoch, func ValidateBlockValues(bSchedule Schedule, h *types.BlockHeader, parentEpoch abi.ChainEpoch,
prevEntry types.BeaconEntry) error { prevEntry types.BeaconEntry) error {
{
parentBeacon := bSchedule.BeaconForEpoch(parentEpoch)
currBeacon := bSchedule.BeaconForEpoch(h.Height)
if parentBeacon != currBeacon {
if len(h.BeaconEntries) != 2 {
return xerrors.Errorf("expected two beacon entries at beacon fork, got %d", len(h.BeaconEntries))
}
err := currBeacon.VerifyEntry(h.BeaconEntries[1], h.BeaconEntries[0])
if err != nil {
return xerrors.Errorf("beacon at fork point invalid: (%v, %v): %w",
h.BeaconEntries[1], h.BeaconEntries[0], err)
}
return nil
}
}
// TODO: fork logic // TODO: fork logic
b := bSchedule.BeaconForEpoch(h.Height) b := bSchedule.BeaconForEpoch(h.Height)
maxRound := b.MaxBeaconRoundForEpoch(h.Height, prevEntry) maxRound := b.MaxBeaconRoundForEpoch(h.Height)
if maxRound == prevEntry.Round { if maxRound == prevEntry.Round {
if len(h.BeaconEntries) != 0 { if len(h.BeaconEntries) != 0 {
return xerrors.Errorf("expected not to have any beacon entries in this block, got %d", len(h.BeaconEntries)) return xerrors.Errorf("expected not to have any beacon entries in this block, got %d", len(h.BeaconEntries))
@ -78,12 +93,34 @@ func ValidateBlockValues(bSchedule Schedule, h *types.BlockHeader, parentEpoch a
} }
func BeaconEntriesForBlock(ctx context.Context, bSchedule Schedule, epoch abi.ChainEpoch, parentEpoch abi.ChainEpoch, prev types.BeaconEntry) ([]types.BeaconEntry, error) { func BeaconEntriesForBlock(ctx context.Context, bSchedule Schedule, epoch abi.ChainEpoch, parentEpoch abi.ChainEpoch, prev types.BeaconEntry) ([]types.BeaconEntry, error) {
// TODO: fork logic {
parentBeacon := bSchedule.BeaconForEpoch(parentEpoch)
currBeacon := bSchedule.BeaconForEpoch(epoch)
if parentBeacon != currBeacon {
// Fork logic
round := currBeacon.MaxBeaconRoundForEpoch(epoch)
out := make([]types.BeaconEntry, 2)
rch := currBeacon.Entry(ctx, round-1)
res := <-rch
if res.Err != nil {
return nil, xerrors.Errorf("getting entry %d returned error: %w", round-1, res.Err)
}
out[0] = res.Entry
rch = currBeacon.Entry(ctx, round)
res = <-rch
if res.Err != nil {
return nil, xerrors.Errorf("getting entry %d returned error: %w", round, res.Err)
}
out[1] = res.Entry
return out, nil
}
}
beacon := bSchedule.BeaconForEpoch(epoch) beacon := bSchedule.BeaconForEpoch(epoch)
start := build.Clock.Now() start := build.Clock.Now()
maxRound := beacon.MaxBeaconRoundForEpoch(epoch, prev) maxRound := beacon.MaxBeaconRoundForEpoch(epoch)
if maxRound == prev.Round { if maxRound == prev.Round {
return nil, nil return nil, nil
} }

View File

@ -187,7 +187,7 @@ func (db *DrandBeacon) VerifyEntry(curr types.BeaconEntry, prev types.BeaconEntr
return err return err
} }
func (db *DrandBeacon) MaxBeaconRoundForEpoch(filEpoch abi.ChainEpoch, prevEntry types.BeaconEntry) uint64 { func (db *DrandBeacon) MaxBeaconRoundForEpoch(filEpoch abi.ChainEpoch) uint64 {
// TODO: sometimes the genesis time for filecoin is zero and this goes negative // TODO: sometimes the genesis time for filecoin is zero and this goes negative
latestTs := ((uint64(filEpoch) * db.filRoundTime) + db.filGenTime) - db.filRoundTime latestTs := ((uint64(filEpoch) * db.filRoundTime) + db.filGenTime) - db.filRoundTime
dround := (latestTs - db.drandGenTime) / uint64(db.interval.Seconds()) dround := (latestTs - db.drandGenTime) / uint64(db.interval.Seconds())

View File

@ -53,11 +53,7 @@ func (mb *mockBeacon) VerifyEntry(from types.BeaconEntry, to types.BeaconEntry)
return nil return nil
} }
func (mb *mockBeacon) IsEntryForEpoch(e types.BeaconEntry, epoch abi.ChainEpoch, nulls int) (bool, error) { func (mb *mockBeacon) MaxBeaconRoundForEpoch(epoch abi.ChainEpoch) uint64 {
return int64(e.Round) <= int64(epoch) && int64(epoch)-int64(nulls) >= int64(e.Round), nil
}
func (mb *mockBeacon) MaxBeaconRoundForEpoch(epoch abi.ChainEpoch, prevEntry types.BeaconEntry) uint64 {
return uint64(epoch) return uint64(epoch)
} }

View File

@ -226,7 +226,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(new(dtypes.DrandSchedule), modules.BuiltinDrandConfig),
Override(HandleIncomingMessagesKey, modules.HandleIncomingMessages), Override(HandleIncomingMessagesKey, modules.HandleIncomingMessages),
@ -272,7 +272,7 @@ func Online() Option {
Override(new(modules.ClientDealFunds), modules.NewClientDealFunds), Override(new(modules.ClientDealFunds), modules.NewClientDealFunds),
Override(new(storagemarket.StorageClient), modules.StorageClient), Override(new(storagemarket.StorageClient), modules.StorageClient),
Override(new(storagemarket.StorageClientNode), storageadapter.NewClientNodeAdapter), Override(new(storagemarket.StorageClientNode), storageadapter.NewClientNodeAdapter),
Override(new(beacon.RandomBeacon), modules.RandomBeacon), Override(new(beacon.Schedule), modules.RandomSchedule),
Override(new(*paychmgr.Store), paychmgr.NewStore), Override(new(*paychmgr.Store), paychmgr.NewStore),
Override(new(*paychmgr.Manager), paychmgr.NewManager), Override(new(*paychmgr.Manager), paychmgr.NewManager),
@ -535,6 +535,6 @@ func Test() Option {
return Options( return Options(
Unset(RunPeerMgrKey), Unset(RunPeerMgrKey),
Unset(new(*peermgr.PeerMgr)), Unset(new(*peermgr.PeerMgr)),
Override(new(beacon.RandomBeacon), testing.RandomBeacon), Override(new(beacon.Schedule), testing.RandomBeacon),
) )
} }

View File

@ -13,12 +13,13 @@ import (
type BeaconAPI struct { type BeaconAPI struct {
fx.In fx.In
Beacon beacon.RandomBeacon Beacon beacon.Schedule
} }
func (a *BeaconAPI) BeaconGetEntry(ctx context.Context, epoch abi.ChainEpoch) (*types.BeaconEntry, error) { func (a *BeaconAPI) BeaconGetEntry(ctx context.Context, epoch abi.ChainEpoch) (*types.BeaconEntry, error) {
rr := a.Beacon.MaxBeaconRoundForEpoch(epoch, types.BeaconEntry{}) b := a.Beacon.BeaconForEpoch(epoch)
e := a.Beacon.Entry(ctx, rr) rr := b.MaxBeaconRoundForEpoch(epoch)
e := b.Entry(ctx, rr)
select { select {
case be, ok := <-e: case be, ok := <-e:

View File

@ -93,9 +93,9 @@ func BuiltinBootstrap() (dtypes.BootstrapPeers, error) {
return build.BuiltinBootstrap() return build.BuiltinBootstrap()
} }
func DrandBootstrap(d dtypes.DrandConfig) (dtypes.DrandBootstrap, error) { func DrandBootstrap(d dtypes.DrandSchedule) (dtypes.DrandBootstrap, error) {
// TODO: retry resolving, don't fail if at least one resolve succeeds // TODO: retry resolving, don't fail if at least one resolve succeeds
addrs, err := addrutil.ParseAddresses(context.TODO(), d.Relays) addrs, err := addrutil.ParseAddresses(context.TODO(), d[0].Config.Relays)
if err != nil { if err != nil {
log.Errorf("reoslving drand relays addresses: %+v", err) log.Errorf("reoslving drand relays addresses: %+v", err)
return nil, nil return nil, nil

View File

@ -1,5 +1,14 @@
package dtypes package dtypes
import "github.com/filecoin-project/go-state-types/abi"
type DrandSchedule []DrandPoint
type DrandPoint struct {
Start abi.ChainEpoch
Config DrandConfig
}
type DrandConfig struct { type DrandConfig struct {
Servers []string Servers []string
Relays []string Relays []string

View File

@ -49,7 +49,7 @@ type GossipIn struct {
Db dtypes.DrandBootstrap Db dtypes.DrandBootstrap
Cfg *config.Pubsub Cfg *config.Pubsub
Sk *dtypes.ScoreKeeper Sk *dtypes.ScoreKeeper
Dr dtypes.DrandConfig Dr dtypes.DrandSchedule
} }
func getDrandTopic(chainInfoJSON string) (string, error) { func getDrandTopic(chainInfoJSON string) (string, error) {
@ -74,7 +74,7 @@ func GossipSub(in GossipIn) (service *pubsub.PubSub, err error) {
} }
isBootstrapNode := in.Cfg.Bootstrapper isBootstrapNode := in.Cfg.Bootstrapper
drandTopic, err := getDrandTopic(in.Dr.ChainInfoJSON) drandTopic, err := getDrandTopic(in.Dr[0].Config.ChainInfoJSON)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -126,19 +126,27 @@ type RandomBeaconParams struct {
PubSub *pubsub.PubSub `optional:"true"` PubSub *pubsub.PubSub `optional:"true"`
Cs *store.ChainStore Cs *store.ChainStore
DrandConfig dtypes.DrandConfig DrandConfig dtypes.DrandSchedule
} }
func BuiltinDrandConfig() dtypes.DrandConfig { func BuiltinDrandConfig() dtypes.DrandSchedule {
return build.DrandConfig() return build.DrandConfigSchedule()
} }
func RandomBeacon(p RandomBeaconParams, _ dtypes.AfterGenesisSet) (beacon.RandomBeacon, error) { func RandomSchedule(p RandomBeaconParams, _ dtypes.AfterGenesisSet) (beacon.Schedule, error) {
gen, err := p.Cs.GetGenesis() gen, err := p.Cs.GetGenesis()
if err != nil { if err != nil {
return nil, err return nil, err
} }
//return beacon.NewMockBeacon(build.BlockDelaySecs * time.Second) shd := beacon.Schedule{}
return drand.NewDrandBeacon(gen.Timestamp, build.BlockDelaySecs, p.PubSub, p.DrandConfig) for _, dc := range p.DrandConfig {
bc, err := drand.NewDrandBeacon(gen.Timestamp, build.BlockDelaySecs, p.PubSub, dc.Config)
if err != nil {
return nil, xerrors.Errorf("creating drand beacon: %w", err)
}
shd = append(shd, beacon.BeaconPoint{Start: dc.Start, Beacon: bc})
}
return shd, nil
} }

View File

@ -7,6 +7,9 @@ import (
"github.com/filecoin-project/lotus/chain/beacon" "github.com/filecoin-project/lotus/chain/beacon"
) )
func RandomBeacon() (beacon.RandomBeacon, error) { func RandomBeacon() (beacon.Schedule, error) {
return beacon.NewMockBeacon(time.Duration(build.BlockDelaySecs) * time.Second), nil return beacon.Schedule{
{Start: 0,
Beacon: beacon.NewMockBeacon(time.Duration(build.BlockDelaySecs) * time.Second),
}}, nil
} }

View File

@ -7,4 +7,4 @@ export TRUST_PARAMS=1
tag=${TAG:-debug} tag=${TAG:-debug}
go run -tags=$tag ./cmd/lotus wallet import ~/.genesis-sectors/pre-seal-t01000.key go run -tags=$tag ./cmd/lotus wallet import ~/.genesis-sectors/pre-seal-t01000.key
go run -tags=$tag ./cmd/lotus-miner init --actor=t01000 --genesis-miner --pre-sealed-sectors=~/.genesis-sectors --pre-sealed-metadata=~/.genesis-sectors/pre-seal-t01000.json go run -tags=$tag ./cmd/lotus-storage-miner init --actor=t01000 --genesis-miner --pre-sealed-sectors=~/.genesis-sectors --pre-sealed-metadata=~/.genesis-sectors/pre-seal-t01000.json