Make GetSectorsForWinningPoSt fast again

This commit is contained in:
Łukasz Magiera 2020-09-21 18:28:32 +02:00
parent a307e4593a
commit 0b5e4a9612
3 changed files with 61 additions and 43 deletions

View File

@ -55,3 +55,20 @@ func AsArray(store Store, root cid.Cid, version network.Version) (Array, error)
} }
return nil, xerrors.Errorf("unknown network version: %d", version) return nil, xerrors.Errorf("unknown network version: %d", version)
} }
type ROnlyArray interface {
Get(idx uint64, v cbor.Unmarshaler) (bool, error)
ForEach(v cbor.Unmarshaler, fn func(idx int64) error) error
}
type ProxyArray struct {
GetFunc func(idx uint64, v cbor.Unmarshaler) (bool, error)
ForEachFunc func(v cbor.Unmarshaler, fn func(idx int64) error) error
}
func (a *ProxyArray) Get(idx uint64, v cbor.Unmarshaler) (bool, error) {
return a.GetFunc(idx, v)
}
func (a *ProxyArray) ForEach(v cbor.Unmarshaler, fn func(idx int64) error) error {
return a.ForEachFunc(v, fn)
}

View File

@ -1,7 +1,6 @@
package miner package miner
import ( import (
"github.com/filecoin-project/go-state-types/dline"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
cbg "github.com/whyrusleeping/cbor-gen" cbg "github.com/whyrusleeping/cbor-gen"
@ -11,6 +10,7 @@ import (
"github.com/filecoin-project/go-bitfield" "github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/cbor" "github.com/filecoin-project/go-state-types/cbor"
"github.com/filecoin-project/go-state-types/dline"
builtin0 "github.com/filecoin-project/specs-actors/actors/builtin" builtin0 "github.com/filecoin-project/specs-actors/actors/builtin"
miner0 "github.com/filecoin-project/specs-actors/actors/builtin/miner" miner0 "github.com/filecoin-project/specs-actors/actors/builtin/miner"
@ -47,7 +47,7 @@ type State interface {
FindSector(abi.SectorNumber) (*SectorLocation, error) FindSector(abi.SectorNumber) (*SectorLocation, error)
GetSectorExpiration(abi.SectorNumber) (*SectorExpiration, error) GetSectorExpiration(abi.SectorNumber) (*SectorExpiration, error)
GetPrecommittedSector(abi.SectorNumber) (*SectorPreCommitOnChainInfo, error) GetPrecommittedSector(abi.SectorNumber) (*SectorPreCommitOnChainInfo, error)
LoadSectorsFromSet(filter *bitfield.BitField, filterOut bool) (adt.Array, error) LoadSectorsFromSet(filter *bitfield.BitField, filterOut bool) (adt.ROnlyArray, error)
IsAllocated(abi.SectorNumber) (bool, error) IsAllocated(abi.SectorNumber) (bool, error)
LoadDeadline(idx uint64) (Deadline, error) LoadDeadline(idx uint64) (Deadline, error)

View File

@ -4,17 +4,19 @@ import (
"bytes" "bytes"
"errors" "errors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/dline"
"github.com/filecoin-project/lotus/chain/actors/adt"
adt0 "github.com/filecoin-project/specs-actors/actors/util/adt"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
cbg "github.com/whyrusleeping/cbor-gen" cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/cbor"
"github.com/filecoin-project/go-state-types/dline"
miner0 "github.com/filecoin-project/specs-actors/actors/builtin/miner" miner0 "github.com/filecoin-project/specs-actors/actors/builtin/miner"
adt0 "github.com/filecoin-project/specs-actors/actors/util/adt"
"github.com/filecoin-project/lotus/chain/actors/adt"
) )
var _ State = (*state0)(nil) var _ State = (*state0)(nil)
@ -161,56 +163,55 @@ func (s *state0) GetPrecommittedSector(num abi.SectorNumber) (*SectorPreCommitOn
return &ret, nil return &ret, nil
} }
func (s *state0) LoadSectorsFromSet(filter *bitfield.BitField, filterOut bool) (adt.Array, error) { func (s *state0) LoadSectorsFromSet(filter *bitfield.BitField, filterOut bool) (adt.ROnlyArray, error) {
a, err := adt0.AsArray(s.store, s.State.Sectors) a, err := adt0.AsArray(s.store, s.State.Sectors)
if err != nil { if err != nil {
return nil, err return nil, err
} }
ret := adt0.MakeEmptyArray(s.store) incl := func(i uint64) (bool, error) {
var v cbg.Deferred
if err := a.ForEach(&v, func(i int64) error {
include := true include := true
if filter != nil { if filter != nil {
set, err := filter.IsSet(uint64(i)) set, err := filter.IsSet(i)
if err != nil { if err != nil {
return xerrors.Errorf("filter check error: %w", err) return false, xerrors.Errorf("filter check error: %w", err)
} }
if set == filterOut { if set == filterOut {
include = false include = false
} }
} }
return include, nil
if include {
var oci miner0.SectorOnChainInfo
if err := oci.UnmarshalCBOR(bytes.NewReader(v.Raw)); err != nil {
return err
}
noci := SectorOnChainInfo{
SectorNumber: oci.SectorNumber,
SealProof: oci.SealProof,
SealedCID: oci.SealedCID,
DealIDs: oci.DealIDs,
Activation: oci.Activation,
Expiration: oci.Expiration,
DealWeight: oci.DealWeight,
VerifiedDealWeight: oci.VerifiedDealWeight,
InitialPledge: oci.InitialPledge,
ExpectedDayReward: oci.ExpectedDayReward,
ExpectedStoragePledge: oci.ExpectedStoragePledge,
}
if err := ret.Set(uint64(i), &noci); err != nil {
return err
}
}
return nil
}); err != nil {
return nil, err
} }
return ret, nil return &adt.ProxyArray{
GetFunc: func(idx uint64, v cbor.Unmarshaler) (bool, error) {
i, err := incl(idx)
if err != nil {
return false, err
}
if !i {
return false, nil
}
// TODO: ActorUpgrade potentially convert
return a.Get(idx, v)
},
ForEachFunc: func(v cbor.Unmarshaler, fn func(int64) error) error {
// TODO: ActorUpgrade potentially convert the output
return a.ForEach(v, func(i int64) error {
include, err := incl(uint64(i))
if err != nil {
return err
}
if !include {
return nil
}
return fn(i)
})
},
}, nil
} }
func (s *state0) LoadPreCommittedSectors() (adt.Map, error) { func (s *state0) LoadPreCommittedSectors() (adt.Map, error) {