Skip to content

Commit

Permalink
fixup v1 actors for new methods
Browse files Browse the repository at this point in the history
Also, correctly handle multiple ADT versions.
  • Loading branch information
Stebalien committed Sep 23, 2020
1 parent f38a7a7 commit 62ae8ef
Show file tree
Hide file tree
Showing 15 changed files with 237 additions and 173 deletions.
10 changes: 10 additions & 0 deletions chain/actors/adt/adt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"github.com/filecoin-project/go-state-types/cbor"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/lotus/chain/actors/builtin"

adt0 "github.com/filecoin-project/specs-actors/actors/util/adt"
adt1 "github.com/filecoin-project/specs-actors/v2/actors/util/adt"
)

type Map interface {
Expand All @@ -25,6 +27,8 @@ func AsMap(store Store, root cid.Cid, version builtin.Version) (Map, error) {
switch version {
case builtin.Version0:
return adt0.AsMap(store, root)
case builtin.Version1:
return adt1.AsMap(store, root)
}
return nil, xerrors.Errorf("unknown network version: %d", version)
}
Expand All @@ -33,6 +37,8 @@ func NewMap(store Store, version builtin.Version) (Map, error) {
switch version {
case builtin.Version0:
return adt0.MakeEmptyMap(store), nil
case builtin.Version1:
return adt1.MakeEmptyMap(store), nil
}
return nil, xerrors.Errorf("unknown network version: %d", version)
}
Expand All @@ -52,6 +58,8 @@ func AsArray(store Store, root cid.Cid, version network.Version) (Array, error)
switch builtin.VersionForNetwork(version) {
case builtin.Version0:
return adt0.AsArray(store, root)
case builtin.Version1:
return adt1.AsArray(store, root)
}
return nil, xerrors.Errorf("unknown network version: %d", version)
}
Expand All @@ -60,6 +68,8 @@ func NewArray(store Store, version builtin.Version) (Array, error) {
switch version {
case builtin.Version0:
return adt0.MakeEmptyArray(store), nil
case builtin.Version1:
return adt1.MakeEmptyArray(store), nil
}
return nil, xerrors.Errorf("unknown network version: %d", version)
}
1 change: 1 addition & 0 deletions chain/actors/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Version int

const (
Version0 = iota
Version1
)

// Converts a network version into a specs-actors version.
Expand Down
3 changes: 1 addition & 2 deletions chain/actors/builtin/init/v0.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"

init_ "github.com/filecoin-project/specs-actors/actors/builtin/init"

"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/node/modules/dtypes"

init_ "github.com/filecoin-project/specs-actors/actors/builtin/init"
adt0 "github.com/filecoin-project/specs-actors/actors/util/adt"
)

Expand Down
19 changes: 19 additions & 0 deletions chain/actors/builtin/init/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"

"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/node/modules/dtypes"
Expand Down Expand Up @@ -45,3 +46,21 @@ func (s *state1) ForEachActor(cb func(id abi.ActorID, address address.Address) e
func (s *state1) NetworkName() (dtypes.NetworkName, error) {
return dtypes.NetworkName(s.State.NetworkName), nil
}

func (s *state1) Remove(addrs ...address.Address) (err error) {
m, err := adt1.AsMap(s.store, s.State.AddressMap)
if err != nil {
return err
}
for _, addr := range addrs {
if err = m.Delete(abi.AddrKey(addr)); err != nil {
return xerrors.Errorf("failed to delete entry for address: %s; err: %w", addr, err)
}
}
amr, err := m.Root()
if err != nil {
return xerrors.Errorf("failed to get address map root: %w", err)
}
s.State.AddressMap = amr
return nil
}
25 changes: 16 additions & 9 deletions chain/actors/builtin/market/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ func (s *state1) TotalLocked() (abi.TokenAmount, error) {
return fml, nil
}

func (s *state1) BalancesChanged(otherState State) bool {
func (s *state1) BalancesChanged(otherState State) (bool, error) {
otherState1, ok := otherState.(*state1)
if !ok {
// there's no way to compare different versions of the state, so let's
// just say that means the state of balances has changed
return true
return true, nil
}
return !s.State.EscrowTable.Equals(otherState1.State.EscrowTable) || !s.State.LockedTable.Equals(otherState1.State.LockedTable)
return !s.State.EscrowTable.Equals(otherState1.State.EscrowTable) || !s.State.LockedTable.Equals(otherState1.State.LockedTable), nil
}

func (s *state1) StatesChanged(otherState State) bool {
func (s *state1) StatesChanged(otherState State) (bool, error) {
otherState1, ok := otherState.(*state1)
if !ok {
// there's no way to compare different versions of the state, so let's
// just say that means the state of balances has changed
return true
return true, nil
}
return !s.State.States.Equals(otherState1.State.States)
return !s.State.States.Equals(otherState1.State.States), nil
}

func (s *state1) States() (DealStates, error) {
Expand All @@ -53,14 +53,14 @@ func (s *state1) States() (DealStates, error) {
return &dealStates1{stateArray}, nil
}

func (s *state1) ProposalsChanged(otherState State) bool {
func (s *state1) ProposalsChanged(otherState State) (bool, error) {
otherState1, ok := otherState.(*state1)
if !ok {
// there's no way to compare different versions of the state, so let's
// just say that means the state of balances has changed
return true
return true, nil
}
return !s.State.Proposals.Equals(otherState1.State.Proposals)
return !s.State.Proposals.Equals(otherState1.State.Proposals), nil
}

func (s *state1) Proposals() (DealProposals, error) {
Expand Down Expand Up @@ -127,6 +127,13 @@ func (s *dealStates1) Get(dealID abi.DealID) (*DealState, bool, error) {
return &deal, true, nil
}

func (s *dealStates1) ForEach(cb func(dealID abi.DealID, ds DealState) error) error {
var ds1 market.DealState
return s.Array.ForEach(&ds1, func(idx int64) error {
return cb(abi.DealID(idx), fromV1DealState(ds1))
})
}

func (s *dealStates1) decode(val *cbg.Deferred) (*DealState, error) {
var ds1 market.DealState
if err := ds1.UnmarshalCBOR(bytes.NewReader(val.Raw)); err != nil {
Expand Down
107 changes: 48 additions & 59 deletions chain/actors/builtin/miner/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
adt1 "github.com/filecoin-project/specs-actors/v2/actors/util/adt"
"github.com/libp2p/go-libp2p-core/peer"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"

miner1 "github.com/filecoin-project/specs-actors/v2/actors/builtin/miner"
)
Expand Down Expand Up @@ -79,6 +78,21 @@ func (s *state1) FindSector(num abi.SectorNumber) (*SectorLocation, error) {
}, nil
}

func (s *state1) NumLiveSectors() (uint64, error) {
dls, err := s.State.LoadDeadlines(s.store)
if err != nil {
return 0, err
}
var total uint64
if err := dls.ForEach(s.store, func(dlIdx uint64, dl *miner1.Deadline) error {
total += dl.LiveSectors
return nil
}); err != nil {
return 0, err
}
return total, nil
}

// GetSectorExpiration returns the effective expiration of the given sector.
//
// If the sector isn't found or has already been terminated, this method returns
Expand Down Expand Up @@ -162,60 +176,37 @@ func (s *state1) GetPrecommittedSector(num abi.SectorNumber) (*SectorPreCommitOn
return &ret, nil
}

func (s *state1) LoadSectorsFromSet(filter *bitfield.BitField, filterOut bool) (adt.Array, error) {
a, err := adt1.AsArray(s.store, s.State.Sectors)
func (s *state1) LoadSectors(snos *bitfield.BitField) ([]*SectorOnChainInfo, error) {
sectors, err := miner1.LoadSectors(s.store, s.State.Sectors)
if err != nil {
return nil, err
}

ret := adt1.MakeEmptyArray(s.store)
var v cbg.Deferred
if err := a.ForEach(&v, func(i int64) error {
include := true
if filter != nil {
set, err := filter.IsSet(uint64(i))
if err != nil {
return xerrors.Errorf("filter check error: %w", err)
}
if set == filterOut {
include = false
}
// If no sector numbers are specified, load all.
if snos == nil {
infos := make([]*SectorOnChainInfo, 0, sectors.Length())
var info1 miner1.SectorOnChainInfo
if err := sectors.ForEach(&info1, func(_ int64) error {
info := fromV1SectorOnChainInfo(info1)
infos = append(infos, &info)
return nil
}); err != nil {
return nil, err
}
return infos, nil
}

if include {
var oci miner1.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 {
// Otherwise, load selected.
infos1, err := sectors.Load(*snos)
if err != nil {
return nil, err
}

return ret, nil
}

func (s *state1) LoadPreCommittedSectors() (adt.Map, error) {
return adt1.AsMap(s.store, s.State.PreCommittedSectors)
infos := make([]*SectorOnChainInfo, len(infos1))
for i, info1 := range infos1 {
info := fromV1SectorOnChainInfo(*info1)
infos[i] = &info
}
return infos, nil
}

func (s *state1) IsAllocated(num abi.SectorNumber) (bool, error) {
Expand Down Expand Up @@ -253,14 +244,14 @@ func (s *state1) NumDeadlines() (uint64, error) {
return miner1.WPoStPeriodDeadlines, nil
}

func (s *state1) DeadlinesChanged(other State) bool {
func (s *state1) DeadlinesChanged(other State) (bool, error) {
other1, ok := other.(*state1)
if !ok {
// treat an upgrade as a change, always
return true
return true, nil
}

return s.State.Deadlines.Equals(other1.Deadlines)
return s.State.Deadlines.Equals(other1.Deadlines), nil
}

func (s *state1) Info() (MinerInfo, error) {
Expand Down Expand Up @@ -297,8 +288,8 @@ func (s *state1) Info() (MinerInfo, error) {
return mi, nil
}

func (s *state1) DeadlineInfo(epoch abi.ChainEpoch) *dline.Info {
return s.State.DeadlineInfo(epoch)
func (s *state1) DeadlineInfo(epoch abi.ChainEpoch) (*dline.Info, error) {
return s.State.DeadlineInfo(epoch), nil
}

func (s *state1) sectors() (adt.Array, error) {
Expand All @@ -312,8 +303,7 @@ func (s *state1) decodeSectorOnChainInfo(val *cbg.Deferred) (SectorOnChainInfo,
return SectorOnChainInfo{}, err
}

ret := fromV1SectorOnChainInfo(si)
return ret, nil
return fromV1SectorOnChainInfo(si), nil
}

func (s *state1) precommits() (adt.Map, error) {
Expand All @@ -327,8 +317,7 @@ func (s *state1) decodeSectorPreCommitOnChainInfo(val *cbg.Deferred) (SectorPreC
return SectorPreCommitOnChainInfo{}, err
}

ret := fromV1SectorPreCommitOnChainInfo(sp)
return ret, nil
return fromV1SectorPreCommitOnChainInfo(sp), nil
}

func (d *deadline1) LoadPartition(idx uint64) (Partition, error) {
Expand All @@ -350,14 +339,14 @@ func (d *deadline1) ForEachPartition(cb func(uint64, Partition) error) error {
})
}

func (d *deadline1) PartitionsChanged(other Deadline) bool {
func (d *deadline1) PartitionsChanged(other Deadline) (bool, error) {
other1, ok := other.(*deadline1)
if !ok {
// treat an upgrade as a change, always
return true
return true, nil
}

return d.Deadline.Partitions.Equals(other1.Deadline.Partitions)
return d.Deadline.Partitions.Equals(other1.Deadline.Partitions), nil
}

func (d *deadline1) PostSubmissions() (bitfield.BitField, error) {
Expand Down
Loading

0 comments on commit 62ae8ef

Please sign in to comment.